tcp_vegas.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/inet_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 sock *sk)
  79. {
  80. const struct tcp_sock *tp = tcp_sk(sk);
  81. struct vegas *vegas = inet_csk_ca(sk);
  82. /* Begin taking Vegas samples next time we send something. */
  83. vegas->doing_vegas_now = 1;
  84. /* Set the beginning of the next send window. */
  85. vegas->beg_snd_nxt = tp->snd_nxt;
  86. vegas->cntRTT = 0;
  87. vegas->minRTT = 0x7fffffff;
  88. }
  89. /* Stop taking Vegas samples for now. */
  90. static inline void vegas_disable(struct sock *sk)
  91. {
  92. struct vegas *vegas = inet_csk_ca(sk);
  93. vegas->doing_vegas_now = 0;
  94. }
  95. static void tcp_vegas_init(struct sock *sk)
  96. {
  97. struct vegas *vegas = inet_csk_ca(sk);
  98. vegas->baseRTT = 0x7fffffff;
  99. vegas_enable(sk);
  100. }
  101. /* Do RTT sampling needed for Vegas.
  102. * Basically we:
  103. * o min-filter RTT samples from within an RTT to get the current
  104. * propagation delay + queuing delay (we are min-filtering to try to
  105. * avoid the effects of delayed ACKs)
  106. * o min-filter RTT samples from a much longer window (forever for now)
  107. * to find the propagation delay (baseRTT)
  108. */
  109. static void tcp_vegas_rtt_calc(struct sock *sk, u32 usrtt)
  110. {
  111. struct vegas *vegas = inet_csk_ca(sk);
  112. u32 vrtt = usrtt + 1; /* Never allow zero rtt or baseRTT */
  113. /* Filter to find propagation delay: */
  114. if (vrtt < vegas->baseRTT)
  115. vegas->baseRTT = vrtt;
  116. /* Find the min RTT during the last RTT to find
  117. * the current prop. delay + queuing delay:
  118. */
  119. vegas->minRTT = min(vegas->minRTT, vrtt);
  120. vegas->cntRTT++;
  121. }
  122. static void tcp_vegas_state(struct sock *sk, u8 ca_state)
  123. {
  124. if (ca_state == TCP_CA_Open)
  125. vegas_enable(sk);
  126. else
  127. vegas_disable(sk);
  128. }
  129. /*
  130. * If the connection is idle and we are restarting,
  131. * then we don't want to do any Vegas calculations
  132. * until we get fresh RTT samples. So when we
  133. * restart, we reset our Vegas state to a clean
  134. * slate. After we get acks for this flight of
  135. * packets, _then_ we can make Vegas calculations
  136. * again.
  137. */
  138. static void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  139. {
  140. if (event == CA_EVENT_CWND_RESTART ||
  141. event == CA_EVENT_TX_START)
  142. tcp_vegas_init(sk);
  143. }
  144. static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack,
  145. u32 seq_rtt, u32 in_flight, int flag)
  146. {
  147. struct tcp_sock *tp = tcp_sk(sk);
  148. struct vegas *vegas = inet_csk_ca(sk);
  149. if (!vegas->doing_vegas_now)
  150. return tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
  151. /* The key players are v_beg_snd_una and v_beg_snd_nxt.
  152. *
  153. * These are so named because they represent the approximate values
  154. * of snd_una and snd_nxt at the beginning of the current RTT. More
  155. * precisely, they represent the amount of data sent during the RTT.
  156. * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt,
  157. * we will calculate that (v_beg_snd_nxt - v_beg_snd_una) outstanding
  158. * bytes of data have been ACKed during the course of the RTT, giving
  159. * an "actual" rate of:
  160. *
  161. * (v_beg_snd_nxt - v_beg_snd_una) / (rtt duration)
  162. *
  163. * Unfortunately, v_beg_snd_una is not exactly equal to snd_una,
  164. * because delayed ACKs can cover more than one segment, so they
  165. * don't line up nicely with the boundaries of RTTs.
  166. *
  167. * Another unfortunate fact of life is that delayed ACKs delay the
  168. * advance of the left edge of our send window, so that the number
  169. * of bytes we send in an RTT is often less than our cwnd will allow.
  170. * So we keep track of our cwnd separately, in v_beg_snd_cwnd.
  171. */
  172. if (after(ack, vegas->beg_snd_nxt)) {
  173. /* Do the Vegas once-per-RTT cwnd adjustment. */
  174. u32 old_wnd, old_snd_cwnd;
  175. /* Here old_wnd is essentially the window of data that was
  176. * sent during the previous RTT, and has all
  177. * been acknowledged in the course of the RTT that ended
  178. * with the ACK we just received. Likewise, old_snd_cwnd
  179. * is the cwnd during the previous RTT.
  180. */
  181. old_wnd = (vegas->beg_snd_nxt - vegas->beg_snd_una) /
  182. tp->mss_cache;
  183. old_snd_cwnd = vegas->beg_snd_cwnd;
  184. /* Save the extent of the current window so we can use this
  185. * at the end of the next RTT.
  186. */
  187. vegas->beg_snd_una = vegas->beg_snd_nxt;
  188. vegas->beg_snd_nxt = tp->snd_nxt;
  189. vegas->beg_snd_cwnd = tp->snd_cwnd;
  190. /* Take into account the current RTT sample too, to
  191. * decrease the impact of delayed acks. This double counts
  192. * this sample since we count it for the next window as well,
  193. * but that's not too awful, since we're taking the min,
  194. * rather than averaging.
  195. */
  196. tcp_vegas_rtt_calc(sk, seq_rtt * 1000);
  197. /* We do the Vegas calculations only if we got enough RTT
  198. * samples that we can be reasonably sure that we got
  199. * at least one RTT sample that wasn't from a delayed ACK.
  200. * If we only had 2 samples total,
  201. * then that means we're getting only 1 ACK per RTT, which
  202. * means they're almost certainly delayed ACKs.
  203. * If we have 3 samples, we should be OK.
  204. */
  205. if (vegas->cntRTT <= 2) {
  206. /* We don't have enough RTT samples to do the Vegas
  207. * calculation, so we'll behave like Reno.
  208. */
  209. if (tp->snd_cwnd > tp->snd_ssthresh)
  210. tp->snd_cwnd++;
  211. } else {
  212. u32 rtt, target_cwnd, diff;
  213. /* We have enough RTT samples, so, using the Vegas
  214. * algorithm, we determine if we should increase or
  215. * decrease cwnd, and by how much.
  216. */
  217. /* Pluck out the RTT we are using for the Vegas
  218. * calculations. This is the min RTT seen during the
  219. * last RTT. Taking the min filters out the effects
  220. * of delayed ACKs, at the cost of noticing congestion
  221. * a bit later.
  222. */
  223. rtt = vegas->minRTT;
  224. /* Calculate the cwnd we should have, if we weren't
  225. * going too fast.
  226. *
  227. * This is:
  228. * (actual rate in segments) * baseRTT
  229. * We keep it as a fixed point number with
  230. * V_PARAM_SHIFT bits to the right of the binary point.
  231. */
  232. target_cwnd = ((old_wnd * vegas->baseRTT)
  233. << V_PARAM_SHIFT) / rtt;
  234. /* Calculate the difference between the window we had,
  235. * and the window we would like to have. This quantity
  236. * is the "Diff" from the Arizona Vegas papers.
  237. *
  238. * Again, this is a fixed point number with
  239. * V_PARAM_SHIFT bits to the right of the binary
  240. * point.
  241. */
  242. diff = (old_wnd << V_PARAM_SHIFT) - target_cwnd;
  243. if (tp->snd_cwnd < tp->snd_ssthresh) {
  244. /* Slow start. */
  245. if (diff > gamma) {
  246. /* Going too fast. Time to slow down
  247. * and switch to congestion avoidance.
  248. */
  249. tp->snd_ssthresh = 2;
  250. /* Set cwnd to match the actual rate
  251. * exactly:
  252. * cwnd = (actual rate) * baseRTT
  253. * Then we add 1 because the integer
  254. * truncation robs us of full link
  255. * utilization.
  256. */
  257. tp->snd_cwnd = min(tp->snd_cwnd,
  258. (target_cwnd >>
  259. V_PARAM_SHIFT)+1);
  260. }
  261. } else {
  262. /* Congestion avoidance. */
  263. u32 next_snd_cwnd;
  264. /* Figure out where we would like cwnd
  265. * to be.
  266. */
  267. if (diff > beta) {
  268. /* The old window was too fast, so
  269. * we slow down.
  270. */
  271. next_snd_cwnd = old_snd_cwnd - 1;
  272. } else if (diff < alpha) {
  273. /* We don't have enough extra packets
  274. * in the network, so speed up.
  275. */
  276. next_snd_cwnd = old_snd_cwnd + 1;
  277. } else {
  278. /* Sending just as fast as we
  279. * should be.
  280. */
  281. next_snd_cwnd = old_snd_cwnd;
  282. }
  283. /* Adjust cwnd upward or downward, toward the
  284. * desired value.
  285. */
  286. if (next_snd_cwnd > tp->snd_cwnd)
  287. tp->snd_cwnd++;
  288. else if (next_snd_cwnd < tp->snd_cwnd)
  289. tp->snd_cwnd--;
  290. }
  291. }
  292. /* Wipe the slate clean for the next RTT. */
  293. vegas->cntRTT = 0;
  294. vegas->minRTT = 0x7fffffff;
  295. }
  296. /* The following code is executed for every ack we receive,
  297. * except for conditions checked in should_advance_cwnd()
  298. * before the call to tcp_cong_avoid(). Mainly this means that
  299. * we only execute this code if the ack actually acked some
  300. * data.
  301. */
  302. /* If we are in slow start, increase our cwnd in response to this ACK.
  303. * (If we are not in slow start then we are in congestion avoidance,
  304. * and adjust our congestion window only once per RTT. See the code
  305. * above.)
  306. */
  307. if (tp->snd_cwnd <= tp->snd_ssthresh)
  308. tp->snd_cwnd++;
  309. /* to keep cwnd from growing without bound */
  310. tp->snd_cwnd = min_t(u32, tp->snd_cwnd, tp->snd_cwnd_clamp);
  311. /* Make sure that we are never so timid as to reduce our cwnd below
  312. * 2 MSS.
  313. *
  314. * Going below 2 MSS would risk huge delayed ACKs from our receiver.
  315. */
  316. tp->snd_cwnd = max(tp->snd_cwnd, 2U);
  317. }
  318. /* Extract info for Tcp socket info provided via netlink. */
  319. static void tcp_vegas_get_info(struct sock *sk, u32 ext,
  320. struct sk_buff *skb)
  321. {
  322. const struct vegas *ca = inet_csk_ca(sk);
  323. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  324. struct tcpvegas_info *info;
  325. info = RTA_DATA(__RTA_PUT(skb, INET_DIAG_VEGASINFO,
  326. sizeof(*info)));
  327. info->tcpv_enabled = ca->doing_vegas_now;
  328. info->tcpv_rttcnt = ca->cntRTT;
  329. info->tcpv_rtt = ca->baseRTT;
  330. info->tcpv_minrtt = ca->minRTT;
  331. rtattr_failure: ;
  332. }
  333. }
  334. static struct tcp_congestion_ops tcp_vegas = {
  335. .init = tcp_vegas_init,
  336. .ssthresh = tcp_reno_ssthresh,
  337. .cong_avoid = tcp_vegas_cong_avoid,
  338. .min_cwnd = tcp_reno_min_cwnd,
  339. .rtt_sample = tcp_vegas_rtt_calc,
  340. .set_state = tcp_vegas_state,
  341. .cwnd_event = tcp_vegas_cwnd_event,
  342. .get_info = tcp_vegas_get_info,
  343. .owner = THIS_MODULE,
  344. .name = "vegas",
  345. };
  346. static int __init tcp_vegas_register(void)
  347. {
  348. BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
  349. tcp_register_congestion_control(&tcp_vegas);
  350. return 0;
  351. }
  352. static void __exit tcp_vegas_unregister(void)
  353. {
  354. tcp_unregister_congestion_control(&tcp_vegas);
  355. }
  356. module_init(tcp_vegas_register);
  357. module_exit(tcp_vegas_unregister);
  358. MODULE_AUTHOR("Stephen Hemminger");
  359. MODULE_LICENSE("GPL");
  360. MODULE_DESCRIPTION("TCP Vegas");