tcp_vegas.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/config.h>
  34. #include <linux/mm.h>
  35. #include <linux/module.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/tcp_diag.h>
  38. #include <net/tcp.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 = 1<<V_PARAM_SHIFT;
  44. static int beta = 3<<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. /* Vegas variables */
  53. struct vegas {
  54. u32 beg_snd_nxt; /* right edge during last RTT */
  55. u32 beg_snd_una; /* left edge during last RTT */
  56. u32 beg_snd_cwnd; /* saves the size of the cwnd */
  57. u8 doing_vegas_now;/* if true, do vegas for this RTT */
  58. u16 cntRTT; /* # of RTTs measured within last RTT */
  59. u32 minRTT; /* min of RTTs measured within last RTT (in usec) */
  60. u32 baseRTT; /* the min of all Vegas RTT measurements seen (in usec) */
  61. };
  62. /* There are several situations when we must "re-start" Vegas:
  63. *
  64. * o when a connection is established
  65. * o after an RTO
  66. * o after fast recovery
  67. * o when we send a packet and there is no outstanding
  68. * unacknowledged data (restarting an idle connection)
  69. *
  70. * In these circumstances we cannot do a Vegas calculation at the
  71. * end of the first RTT, because any calculation we do is using
  72. * stale info -- both the saved cwnd and congestion feedback are
  73. * stale.
  74. *
  75. * Instead we must wait until the completion of an RTT during
  76. * which we actually receive ACKs.
  77. */
  78. static inline void vegas_enable(struct tcp_sock *tp)
  79. {
  80. struct vegas *vegas = tcp_ca(tp);
  81. /* Begin taking Vegas samples next time we send something. */
  82. vegas->doing_vegas_now = 1;
  83. /* Set the beginning of the next send window. */
  84. vegas->beg_snd_nxt = tp->snd_nxt;
  85. vegas->cntRTT = 0;
  86. vegas->minRTT = 0x7fffffff;
  87. }
  88. /* Stop taking Vegas samples for now. */
  89. static inline void vegas_disable(struct tcp_sock *tp)
  90. {
  91. struct vegas *vegas = tcp_ca(tp);
  92. vegas->doing_vegas_now = 0;
  93. }
  94. static void tcp_vegas_init(struct tcp_sock *tp)
  95. {
  96. struct vegas *vegas = tcp_ca(tp);
  97. vegas->baseRTT = 0x7fffffff;
  98. vegas_enable(tp);
  99. }
  100. /* Do RTT sampling needed for Vegas.
  101. * Basically we:
  102. * o min-filter RTT samples from within an RTT to get the current
  103. * propagation delay + queuing delay (we are min-filtering to try to
  104. * avoid the effects of delayed ACKs)
  105. * o min-filter RTT samples from a much longer window (forever for now)
  106. * to find the propagation delay (baseRTT)
  107. */
  108. static void tcp_vegas_rtt_calc(struct tcp_sock *tp, u32 usrtt)
  109. {
  110. struct vegas *vegas = tcp_ca(tp);
  111. u32 vrtt = usrtt + 1; /* Never allow zero rtt or baseRTT */
  112. /* Filter to find propagation delay: */
  113. if (vrtt < vegas->baseRTT)
  114. vegas->baseRTT = vrtt;
  115. /* Find the min RTT during the last RTT to find
  116. * the current prop. delay + queuing delay:
  117. */
  118. vegas->minRTT = min(vegas->minRTT, vrtt);
  119. vegas->cntRTT++;
  120. }
  121. static void tcp_vegas_state(struct tcp_sock *tp, u8 ca_state)
  122. {
  123. if (ca_state == TCP_CA_Open)
  124. vegas_enable(tp);
  125. else
  126. vegas_disable(tp);
  127. }
  128. /*
  129. * If the connection is idle and we are restarting,
  130. * then we don't want to do any Vegas calculations
  131. * until we get fresh RTT samples. So when we
  132. * restart, we reset our Vegas state to a clean
  133. * slate. After we get acks for this flight of
  134. * packets, _then_ we can make Vegas calculations
  135. * again.
  136. */
  137. static void tcp_vegas_cwnd_event(struct tcp_sock *tp, enum tcp_ca_event event)
  138. {
  139. if (event == CA_EVENT_CWND_RESTART ||
  140. event == CA_EVENT_TX_START)
  141. tcp_vegas_init(tp);
  142. }
  143. static void tcp_vegas_cong_avoid(struct tcp_sock *tp, u32 ack,
  144. u32 seq_rtt, u32 in_flight, int flag)
  145. {
  146. struct vegas *vegas = tcp_ca(tp);
  147. if (!vegas->doing_vegas_now)
  148. return tcp_reno_cong_avoid(tp, ack, seq_rtt, 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. /* Take into account the current RTT sample too, to
  189. * decrease the impact of delayed acks. This double counts
  190. * this sample since we count it for the next window as well,
  191. * but that's not too awful, since we're taking the min,
  192. * rather than averaging.
  193. */
  194. tcp_vegas_rtt_calc(tp, seq_rtt*1000);
  195. /* We do the Vegas calculations only if we got enough RTT
  196. * samples that we can be reasonably sure that we got
  197. * at least one RTT sample that wasn't from a delayed ACK.
  198. * If we only had 2 samples total,
  199. * then that means we're getting only 1 ACK per RTT, which
  200. * means they're almost certainly delayed ACKs.
  201. * If we have 3 samples, we should be OK.
  202. */
  203. if (vegas->cntRTT <= 2) {
  204. /* We don't have enough RTT samples to do the Vegas
  205. * calculation, so we'll behave like Reno.
  206. */
  207. if (tp->snd_cwnd > tp->snd_ssthresh)
  208. tp->snd_cwnd++;
  209. } else {
  210. u32 rtt, target_cwnd, diff;
  211. /* We have enough RTT samples, so, using the Vegas
  212. * algorithm, we determine if we should increase or
  213. * decrease cwnd, and by how much.
  214. */
  215. /* Pluck out the RTT we are using for the Vegas
  216. * calculations. This is the min RTT seen during the
  217. * last RTT. Taking the min filters out the effects
  218. * of delayed ACKs, at the cost of noticing congestion
  219. * a bit later.
  220. */
  221. rtt = vegas->minRTT;
  222. /* Calculate the cwnd we should have, if we weren't
  223. * going too fast.
  224. *
  225. * This is:
  226. * (actual rate in segments) * baseRTT
  227. * We keep it as a fixed point number with
  228. * V_PARAM_SHIFT bits to the right of the binary point.
  229. */
  230. target_cwnd = ((old_wnd * vegas->baseRTT)
  231. << V_PARAM_SHIFT) / rtt;
  232. /* Calculate the difference between the window we had,
  233. * and the window we would like to have. This quantity
  234. * is the "Diff" from the Arizona Vegas papers.
  235. *
  236. * Again, this is a fixed point number with
  237. * V_PARAM_SHIFT bits to the right of the binary
  238. * point.
  239. */
  240. diff = (old_wnd << V_PARAM_SHIFT) - target_cwnd;
  241. if (tp->snd_cwnd < tp->snd_ssthresh) {
  242. /* Slow start. */
  243. if (diff > gamma) {
  244. /* Going too fast. Time to slow down
  245. * and switch to congestion avoidance.
  246. */
  247. tp->snd_ssthresh = 2;
  248. /* Set cwnd to match the actual rate
  249. * exactly:
  250. * cwnd = (actual rate) * baseRTT
  251. * Then we add 1 because the integer
  252. * truncation robs us of full link
  253. * utilization.
  254. */
  255. tp->snd_cwnd = min(tp->snd_cwnd,
  256. (target_cwnd >>
  257. V_PARAM_SHIFT)+1);
  258. }
  259. } else {
  260. /* Congestion avoidance. */
  261. u32 next_snd_cwnd;
  262. /* Figure out where we would like cwnd
  263. * to be.
  264. */
  265. if (diff > beta) {
  266. /* The old window was too fast, so
  267. * we slow down.
  268. */
  269. next_snd_cwnd = old_snd_cwnd - 1;
  270. } else if (diff < alpha) {
  271. /* We don't have enough extra packets
  272. * in the network, so speed up.
  273. */
  274. next_snd_cwnd = old_snd_cwnd + 1;
  275. } else {
  276. /* Sending just as fast as we
  277. * should be.
  278. */
  279. next_snd_cwnd = old_snd_cwnd;
  280. }
  281. /* Adjust cwnd upward or downward, toward the
  282. * desired value.
  283. */
  284. if (next_snd_cwnd > tp->snd_cwnd)
  285. tp->snd_cwnd++;
  286. else if (next_snd_cwnd < tp->snd_cwnd)
  287. tp->snd_cwnd--;
  288. }
  289. }
  290. /* Wipe the slate clean for the next RTT. */
  291. vegas->cntRTT = 0;
  292. vegas->minRTT = 0x7fffffff;
  293. }
  294. /* The following code is executed for every ack we receive,
  295. * except for conditions checked in should_advance_cwnd()
  296. * before the call to tcp_cong_avoid(). Mainly this means that
  297. * we only execute this code if the ack actually acked some
  298. * data.
  299. */
  300. /* If we are in slow start, increase our cwnd in response to this ACK.
  301. * (If we are not in slow start then we are in congestion avoidance,
  302. * and adjust our congestion window only once per RTT. See the code
  303. * above.)
  304. */
  305. if (tp->snd_cwnd <= tp->snd_ssthresh)
  306. tp->snd_cwnd++;
  307. /* to keep cwnd from growing without bound */
  308. tp->snd_cwnd = min_t(u32, tp->snd_cwnd, tp->snd_cwnd_clamp);
  309. /* Make sure that we are never so timid as to reduce our cwnd below
  310. * 2 MSS.
  311. *
  312. * Going below 2 MSS would risk huge delayed ACKs from our receiver.
  313. */
  314. tp->snd_cwnd = max(tp->snd_cwnd, 2U);
  315. }
  316. /* Extract info for Tcp socket info provided via netlink. */
  317. static void tcp_vegas_get_info(struct tcp_sock *tp, u32 ext,
  318. struct sk_buff *skb)
  319. {
  320. const struct vegas *ca = tcp_ca(tp);
  321. if (ext & (1<<(TCPDIAG_VEGASINFO-1))) {
  322. struct tcpvegas_info *info;
  323. info = RTA_DATA(__RTA_PUT(skb, TCPDIAG_VEGASINFO,
  324. sizeof(*info)));
  325. info->tcpv_enabled = ca->doing_vegas_now;
  326. info->tcpv_rttcnt = ca->cntRTT;
  327. info->tcpv_rtt = ca->baseRTT;
  328. info->tcpv_minrtt = ca->minRTT;
  329. rtattr_failure: ;
  330. }
  331. }
  332. static struct tcp_congestion_ops tcp_vegas = {
  333. .init = tcp_vegas_init,
  334. .ssthresh = tcp_reno_ssthresh,
  335. .cong_avoid = tcp_vegas_cong_avoid,
  336. .min_cwnd = tcp_reno_min_cwnd,
  337. .rtt_sample = tcp_vegas_rtt_calc,
  338. .set_state = tcp_vegas_state,
  339. .cwnd_event = tcp_vegas_cwnd_event,
  340. .get_info = tcp_vegas_get_info,
  341. .owner = THIS_MODULE,
  342. .name = "vegas",
  343. };
  344. static int __init tcp_vegas_register(void)
  345. {
  346. BUG_ON(sizeof(struct vegas) > TCP_CA_PRIV_SIZE);
  347. tcp_register_congestion_control(&tcp_vegas);
  348. return 0;
  349. }
  350. static void __exit tcp_vegas_unregister(void)
  351. {
  352. tcp_unregister_congestion_control(&tcp_vegas);
  353. }
  354. module_init(tcp_vegas_register);
  355. module_exit(tcp_vegas_unregister);
  356. MODULE_AUTHOR("Stephen Hemminger");
  357. MODULE_LICENSE("GPL");
  358. MODULE_DESCRIPTION("TCP Vegas");