tcp_vegas.c 12 KB

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