tcp_vegas.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TCP Vegas congestion control
  4. *
  5. * This is based on the congestion detection/avoidance scheme described in
  6. * Lawrence S. Brakmo and Larry L. Peterson.
  7. * "TCP Vegas: End to end congestion avoidance on a global internet."
  8. * IEEE Journal on Selected Areas in Communication, 13(8):1465--1480,
  9. * October 1995. Available from:
  10. * ftp://ftp.cs.arizona.edu/xkernel/Papers/jsac.ps
  11. *
  12. * See http://www.cs.arizona.edu/xkernel/ for their implementation.
  13. * The main aspects that distinguish this implementation from the
  14. * Arizona Vegas implementation are:
  15. * o We do not change the loss detection or recovery mechanisms of
  16. * Linux in any way. Linux already recovers from losses quite well,
  17. * using fine-grained timers, NewReno, and FACK.
  18. * o To avoid the performance penalty imposed by increasing cwnd
  19. * only every-other RTT during slow start, we increase during
  20. * every RTT during slow start, just like Reno.
  21. * o Largely to allow continuous cwnd growth during slow start,
  22. * we use the rate at which ACKs come back as the "actual"
  23. * rate, rather than the rate at which data is sent.
  24. * o To speed convergence to the right rate, we set the cwnd
  25. * to achieve the right ("actual") rate when we exit slow start.
  26. * o To filter out the noise caused by delayed ACKs, we use the
  27. * minimum RTT sample observed during the last RTT to calculate
  28. * the actual rate.
  29. * o When the sender re-starts from idle, it waits until it has
  30. * received ACKs for an entire flight of new data before making
  31. * a cwnd adjustment decision. The original Vegas implementation
  32. * assumed senders never went idle.
  33. */
  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. #include "tcp_vegas.h"
  40. static int alpha = 2;
  41. static int beta = 4;
  42. static int gamma = 1;
  43. module_param(alpha, int, 0644);
  44. MODULE_PARM_DESC(alpha, "lower bound of packets in network");
  45. module_param(beta, int, 0644);
  46. MODULE_PARM_DESC(beta, "upper bound of packets in network");
  47. module_param(gamma, int, 0644);
  48. MODULE_PARM_DESC(gamma, "limit on increase (scale by 2)");
  49. /* There are several situations when we must "re-start" Vegas:
  50. *
  51. * o when a connection is established
  52. * o after an RTO
  53. * o after fast recovery
  54. * o when we send a packet and there is no outstanding
  55. * unacknowledged data (restarting an idle connection)
  56. *
  57. * In these circumstances we cannot do a Vegas calculation at the
  58. * end of the first RTT, because any calculation we do is using
  59. * stale info -- both the saved cwnd and congestion feedback are
  60. * stale.
  61. *
  62. * Instead we must wait until the completion of an RTT during
  63. * which we actually receive ACKs.
  64. */
  65. static void vegas_enable(struct sock *sk)
  66. {
  67. const struct tcp_sock *tp = tcp_sk(sk);
  68. struct vegas *vegas = inet_csk_ca(sk);
  69. /* Begin taking Vegas samples next time we send something. */
  70. vegas->doing_vegas_now = 1;
  71. /* Set the beginning of the next send window. */
  72. vegas->beg_snd_nxt = tp->snd_nxt;
  73. vegas->cntRTT = 0;
  74. vegas->minRTT = 0x7fffffff;
  75. }
  76. /* Stop taking Vegas samples for now. */
  77. static inline void vegas_disable(struct sock *sk)
  78. {
  79. struct vegas *vegas = inet_csk_ca(sk);
  80. vegas->doing_vegas_now = 0;
  81. }
  82. void tcp_vegas_init(struct sock *sk)
  83. {
  84. struct vegas *vegas = inet_csk_ca(sk);
  85. vegas->baseRTT = 0x7fffffff;
  86. vegas_enable(sk);
  87. }
  88. EXPORT_SYMBOL_GPL(tcp_vegas_init);
  89. /* Do RTT sampling needed for Vegas.
  90. * Basically we:
  91. * o min-filter RTT samples from within an RTT to get the current
  92. * propagation delay + queuing delay (we are min-filtering to try to
  93. * avoid the effects of delayed ACKs)
  94. * o min-filter RTT samples from a much longer window (forever for now)
  95. * to find the propagation delay (baseRTT)
  96. */
  97. void tcp_vegas_pkts_acked(struct sock *sk, const struct ack_sample *sample)
  98. {
  99. struct vegas *vegas = inet_csk_ca(sk);
  100. u32 vrtt;
  101. if (sample->rtt_us < 0)
  102. return;
  103. /* Never allow zero rtt or baseRTT */
  104. vrtt = sample->rtt_us + 1;
  105. /* Filter to find propagation delay: */
  106. if (vrtt < vegas->baseRTT)
  107. vegas->baseRTT = vrtt;
  108. /* Find the min RTT during the last RTT to find
  109. * the current prop. delay + queuing delay:
  110. */
  111. vegas->minRTT = min(vegas->minRTT, vrtt);
  112. vegas->cntRTT++;
  113. }
  114. EXPORT_SYMBOL_GPL(tcp_vegas_pkts_acked);
  115. void tcp_vegas_state(struct sock *sk, u8 ca_state)
  116. {
  117. if (ca_state == TCP_CA_Open)
  118. vegas_enable(sk);
  119. else
  120. vegas_disable(sk);
  121. }
  122. EXPORT_SYMBOL_GPL(tcp_vegas_state);
  123. /*
  124. * If the connection is idle and we are restarting,
  125. * then we don't want to do any Vegas calculations
  126. * until we get fresh RTT samples. So when we
  127. * restart, we reset our Vegas state to a clean
  128. * slate. After we get acks for this flight of
  129. * packets, _then_ we can make Vegas calculations
  130. * again.
  131. */
  132. void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  133. {
  134. if (event == CA_EVENT_CWND_RESTART ||
  135. event == CA_EVENT_TX_START)
  136. tcp_vegas_init(sk);
  137. }
  138. EXPORT_SYMBOL_GPL(tcp_vegas_cwnd_event);
  139. static inline u32 tcp_vegas_ssthresh(struct tcp_sock *tp)
  140. {
  141. return min(tp->snd_ssthresh, tcp_snd_cwnd(tp));
  142. }
  143. static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  144. {
  145. struct tcp_sock *tp = tcp_sk(sk);
  146. struct vegas *vegas = inet_csk_ca(sk);
  147. if (!vegas->doing_vegas_now) {
  148. tcp_reno_cong_avoid(sk, ack, acked);
  149. return;
  150. }
  151. if (after(ack, vegas->beg_snd_nxt)) {
  152. /* Do the Vegas once-per-RTT cwnd adjustment. */
  153. /* Save the extent of the current window so we can use this
  154. * at the end of the next RTT.
  155. */
  156. vegas->beg_snd_nxt = tp->snd_nxt;
  157. /* We do the Vegas calculations only if we got enough RTT
  158. * samples that we can be reasonably sure that we got
  159. * at least one RTT sample that wasn't from a delayed ACK.
  160. * If we only had 2 samples total,
  161. * then that means we're getting only 1 ACK per RTT, which
  162. * means they're almost certainly delayed ACKs.
  163. * If we have 3 samples, we should be OK.
  164. */
  165. if (vegas->cntRTT <= 2) {
  166. /* We don't have enough RTT samples to do the Vegas
  167. * calculation, so we'll behave like Reno.
  168. */
  169. tcp_reno_cong_avoid(sk, ack, acked);
  170. } else {
  171. u32 rtt, diff;
  172. u64 target_cwnd;
  173. /* We have enough RTT samples, so, using the Vegas
  174. * algorithm, we determine if we should increase or
  175. * decrease cwnd, and by how much.
  176. */
  177. /* Pluck out the RTT we are using for the Vegas
  178. * calculations. This is the min RTT seen during the
  179. * last RTT. Taking the min filters out the effects
  180. * of delayed ACKs, at the cost of noticing congestion
  181. * a bit later.
  182. */
  183. rtt = vegas->minRTT;
  184. /* Calculate the cwnd we should have, if we weren't
  185. * going too fast.
  186. *
  187. * This is:
  188. * (actual rate in segments) * baseRTT
  189. */
  190. target_cwnd = (u64)tcp_snd_cwnd(tp) * vegas->baseRTT;
  191. do_div(target_cwnd, rtt);
  192. /* Calculate the difference between the window we had,
  193. * and the window we would like to have. This quantity
  194. * is the "Diff" from the Arizona Vegas papers.
  195. */
  196. diff = tcp_snd_cwnd(tp) * (rtt-vegas->baseRTT) / vegas->baseRTT;
  197. if (diff > gamma && tcp_in_slow_start(tp)) {
  198. /* Going too fast. Time to slow down
  199. * and switch to congestion avoidance.
  200. */
  201. /* Set cwnd to match the actual rate
  202. * exactly:
  203. * cwnd = (actual rate) * baseRTT
  204. * Then we add 1 because the integer
  205. * truncation robs us of full link
  206. * utilization.
  207. */
  208. tcp_snd_cwnd_set(tp, min(tcp_snd_cwnd(tp),
  209. (u32)target_cwnd + 1));
  210. tp->snd_ssthresh = tcp_vegas_ssthresh(tp);
  211. } else if (tcp_in_slow_start(tp)) {
  212. /* Slow start. */
  213. tcp_slow_start(tp, acked);
  214. } else {
  215. /* Congestion avoidance. */
  216. /* Figure out where we would like cwnd
  217. * to be.
  218. */
  219. if (diff > beta) {
  220. /* The old window was too fast, so
  221. * we slow down.
  222. */
  223. tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) - 1);
  224. tp->snd_ssthresh
  225. = tcp_vegas_ssthresh(tp);
  226. } else if (diff < alpha) {
  227. /* We don't have enough extra packets
  228. * in the network, so speed up.
  229. */
  230. tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
  231. } else {
  232. /* Sending just as fast as we
  233. * should be.
  234. */
  235. }
  236. }
  237. if (tcp_snd_cwnd(tp) < 2)
  238. tcp_snd_cwnd_set(tp, 2);
  239. else if (tcp_snd_cwnd(tp) > tp->snd_cwnd_clamp)
  240. tcp_snd_cwnd_set(tp, tp->snd_cwnd_clamp);
  241. tp->snd_ssthresh = tcp_current_ssthresh(sk);
  242. }
  243. /* Wipe the slate clean for the next RTT. */
  244. vegas->cntRTT = 0;
  245. vegas->minRTT = 0x7fffffff;
  246. }
  247. /* Use normal slow start */
  248. else if (tcp_in_slow_start(tp))
  249. tcp_slow_start(tp, acked);
  250. }
  251. /* Extract info for Tcp socket info provided via netlink. */
  252. size_t tcp_vegas_get_info(struct sock *sk, u32 ext, int *attr,
  253. union tcp_cc_info *info)
  254. {
  255. const struct vegas *ca = inet_csk_ca(sk);
  256. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  257. info->vegas.tcpv_enabled = ca->doing_vegas_now;
  258. info->vegas.tcpv_rttcnt = ca->cntRTT;
  259. info->vegas.tcpv_rtt = ca->baseRTT;
  260. info->vegas.tcpv_minrtt = ca->minRTT;
  261. *attr = INET_DIAG_VEGASINFO;
  262. return sizeof(struct tcpvegas_info);
  263. }
  264. return 0;
  265. }
  266. EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
  267. static struct tcp_congestion_ops tcp_vegas __read_mostly = {
  268. .init = tcp_vegas_init,
  269. .ssthresh = tcp_reno_ssthresh,
  270. .undo_cwnd = tcp_reno_undo_cwnd,
  271. .cong_avoid = tcp_vegas_cong_avoid,
  272. .pkts_acked = tcp_vegas_pkts_acked,
  273. .set_state = tcp_vegas_state,
  274. .cwnd_event = tcp_vegas_cwnd_event,
  275. .get_info = tcp_vegas_get_info,
  276. .owner = THIS_MODULE,
  277. .name = "vegas",
  278. };
  279. static int __init tcp_vegas_register(void)
  280. {
  281. BUILD_BUG_ON(sizeof(struct vegas) > ICSK_CA_PRIV_SIZE);
  282. tcp_register_congestion_control(&tcp_vegas);
  283. return 0;
  284. }
  285. static void __exit tcp_vegas_unregister(void)
  286. {
  287. tcp_unregister_congestion_control(&tcp_vegas);
  288. }
  289. module_init(tcp_vegas_register);
  290. module_exit(tcp_vegas_unregister);
  291. MODULE_AUTHOR("Stephen Hemminger");
  292. MODULE_LICENSE("GPL");
  293. MODULE_DESCRIPTION("TCP Vegas");