tcp_yeah.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * YeAH TCP
  5. *
  6. * For further details look at:
  7. * https://web.archive.org/web/20080316215752/http://wil.cs.caltech.edu/pfldnet2007/paper/YeAH_TCP.pdf
  8. *
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/inet_diag.h>
  14. #include <net/tcp.h>
  15. #include "tcp_vegas.h"
  16. #define TCP_YEAH_ALPHA 80 /* number of packets queued at the bottleneck */
  17. #define TCP_YEAH_GAMMA 1 /* fraction of queue to be removed per rtt */
  18. #define TCP_YEAH_DELTA 3 /* log minimum fraction of cwnd to be removed on loss */
  19. #define TCP_YEAH_EPSILON 1 /* log maximum fraction to be removed on early decongestion */
  20. #define TCP_YEAH_PHY 8 /* maximum delta from base */
  21. #define TCP_YEAH_RHO 16 /* minimum number of consecutive rtt to consider competition on loss */
  22. #define TCP_YEAH_ZETA 50 /* minimum number of state switches to reset reno_count */
  23. #define TCP_SCALABLE_AI_CNT 100U
  24. /* YeAH variables */
  25. struct yeah {
  26. struct vegas vegas; /* must be first */
  27. /* YeAH */
  28. u32 lastQ;
  29. u32 doing_reno_now;
  30. u32 reno_count;
  31. u32 fast_count;
  32. };
  33. static void tcp_yeah_init(struct sock *sk)
  34. {
  35. struct tcp_sock *tp = tcp_sk(sk);
  36. struct yeah *yeah = inet_csk_ca(sk);
  37. tcp_vegas_init(sk);
  38. yeah->doing_reno_now = 0;
  39. yeah->lastQ = 0;
  40. yeah->reno_count = 2;
  41. /* Ensure the MD arithmetic works. This is somewhat pedantic,
  42. * since I don't think we will see a cwnd this large. :) */
  43. tp->snd_cwnd_clamp = min_t(u32, tp->snd_cwnd_clamp, 0xffffffff/128);
  44. }
  45. static void tcp_yeah_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  46. {
  47. struct tcp_sock *tp = tcp_sk(sk);
  48. struct yeah *yeah = inet_csk_ca(sk);
  49. if (!tcp_is_cwnd_limited(sk))
  50. return;
  51. if (tcp_in_slow_start(tp)) {
  52. acked = tcp_slow_start(tp, acked);
  53. if (!acked)
  54. goto do_vegas;
  55. }
  56. if (!yeah->doing_reno_now) {
  57. /* Scalable */
  58. tcp_cong_avoid_ai(tp, min(tcp_snd_cwnd(tp), TCP_SCALABLE_AI_CNT),
  59. acked);
  60. } else {
  61. /* Reno */
  62. tcp_cong_avoid_ai(tp, tcp_snd_cwnd(tp), acked);
  63. }
  64. /* The key players are v_vegas.beg_snd_una and v_beg_snd_nxt.
  65. *
  66. * These are so named because they represent the approximate values
  67. * of snd_una and snd_nxt at the beginning of the current RTT. More
  68. * precisely, they represent the amount of data sent during the RTT.
  69. * At the end of the RTT, when we receive an ACK for v_beg_snd_nxt,
  70. * we will calculate that (v_beg_snd_nxt - v_vegas.beg_snd_una) outstanding
  71. * bytes of data have been ACKed during the course of the RTT, giving
  72. * an "actual" rate of:
  73. *
  74. * (v_beg_snd_nxt - v_vegas.beg_snd_una) / (rtt duration)
  75. *
  76. * Unfortunately, v_vegas.beg_snd_una is not exactly equal to snd_una,
  77. * because delayed ACKs can cover more than one segment, so they
  78. * don't line up yeahly with the boundaries of RTTs.
  79. *
  80. * Another unfortunate fact of life is that delayed ACKs delay the
  81. * advance of the left edge of our send window, so that the number
  82. * of bytes we send in an RTT is often less than our cwnd will allow.
  83. * So we keep track of our cwnd separately, in v_beg_snd_cwnd.
  84. */
  85. do_vegas:
  86. if (after(ack, yeah->vegas.beg_snd_nxt)) {
  87. /* We do the Vegas calculations only if we got enough RTT
  88. * samples that we can be reasonably sure that we got
  89. * at least one RTT sample that wasn't from a delayed ACK.
  90. * If we only had 2 samples total,
  91. * then that means we're getting only 1 ACK per RTT, which
  92. * means they're almost certainly delayed ACKs.
  93. * If we have 3 samples, we should be OK.
  94. */
  95. if (yeah->vegas.cntRTT > 2) {
  96. u32 rtt, queue;
  97. u64 bw;
  98. /* We have enough RTT samples, so, using the Vegas
  99. * algorithm, we determine if we should increase or
  100. * decrease cwnd, and by how much.
  101. */
  102. /* Pluck out the RTT we are using for the Vegas
  103. * calculations. This is the min RTT seen during the
  104. * last RTT. Taking the min filters out the effects
  105. * of delayed ACKs, at the cost of noticing congestion
  106. * a bit later.
  107. */
  108. rtt = yeah->vegas.minRTT;
  109. /* Compute excess number of packets above bandwidth
  110. * Avoid doing full 64 bit divide.
  111. */
  112. bw = tcp_snd_cwnd(tp);
  113. bw *= rtt - yeah->vegas.baseRTT;
  114. do_div(bw, rtt);
  115. queue = bw;
  116. if (queue > TCP_YEAH_ALPHA ||
  117. rtt - yeah->vegas.baseRTT > (yeah->vegas.baseRTT / TCP_YEAH_PHY)) {
  118. if (queue > TCP_YEAH_ALPHA &&
  119. tcp_snd_cwnd(tp) > yeah->reno_count) {
  120. u32 reduction = min(queue / TCP_YEAH_GAMMA ,
  121. tcp_snd_cwnd(tp) >> TCP_YEAH_EPSILON);
  122. tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) - reduction);
  123. tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp),
  124. yeah->reno_count));
  125. tp->snd_ssthresh = tcp_snd_cwnd(tp);
  126. }
  127. if (yeah->reno_count <= 2)
  128. yeah->reno_count = max(tcp_snd_cwnd(tp)>>1, 2U);
  129. else
  130. yeah->reno_count++;
  131. yeah->doing_reno_now = min(yeah->doing_reno_now + 1,
  132. 0xffffffU);
  133. } else {
  134. yeah->fast_count++;
  135. if (yeah->fast_count > TCP_YEAH_ZETA) {
  136. yeah->reno_count = 2;
  137. yeah->fast_count = 0;
  138. }
  139. yeah->doing_reno_now = 0;
  140. }
  141. yeah->lastQ = queue;
  142. }
  143. /* Save the extent of the current window so we can use this
  144. * at the end of the next RTT.
  145. */
  146. yeah->vegas.beg_snd_una = yeah->vegas.beg_snd_nxt;
  147. yeah->vegas.beg_snd_nxt = tp->snd_nxt;
  148. yeah->vegas.beg_snd_cwnd = tcp_snd_cwnd(tp);
  149. /* Wipe the slate clean for the next RTT. */
  150. yeah->vegas.cntRTT = 0;
  151. yeah->vegas.minRTT = 0x7fffffff;
  152. }
  153. }
  154. static u32 tcp_yeah_ssthresh(struct sock *sk)
  155. {
  156. const struct tcp_sock *tp = tcp_sk(sk);
  157. struct yeah *yeah = inet_csk_ca(sk);
  158. u32 reduction;
  159. if (yeah->doing_reno_now < TCP_YEAH_RHO) {
  160. reduction = yeah->lastQ;
  161. reduction = min(reduction, max(tcp_snd_cwnd(tp)>>1, 2U));
  162. reduction = max(reduction, tcp_snd_cwnd(tp) >> TCP_YEAH_DELTA);
  163. } else
  164. reduction = max(tcp_snd_cwnd(tp)>>1, 2U);
  165. yeah->fast_count = 0;
  166. yeah->reno_count = max(yeah->reno_count>>1, 2U);
  167. return max_t(int, tcp_snd_cwnd(tp) - reduction, 2);
  168. }
  169. static struct tcp_congestion_ops tcp_yeah __read_mostly = {
  170. .init = tcp_yeah_init,
  171. .ssthresh = tcp_yeah_ssthresh,
  172. .undo_cwnd = tcp_reno_undo_cwnd,
  173. .cong_avoid = tcp_yeah_cong_avoid,
  174. .set_state = tcp_vegas_state,
  175. .cwnd_event = tcp_vegas_cwnd_event,
  176. .get_info = tcp_vegas_get_info,
  177. .pkts_acked = tcp_vegas_pkts_acked,
  178. .owner = THIS_MODULE,
  179. .name = "yeah",
  180. };
  181. static int __init tcp_yeah_register(void)
  182. {
  183. BUILD_BUG_ON(sizeof(struct yeah) > ICSK_CA_PRIV_SIZE);
  184. tcp_register_congestion_control(&tcp_yeah);
  185. return 0;
  186. }
  187. static void __exit tcp_yeah_unregister(void)
  188. {
  189. tcp_unregister_congestion_control(&tcp_yeah);
  190. }
  191. module_init(tcp_yeah_register);
  192. module_exit(tcp_yeah_unregister);
  193. MODULE_AUTHOR("Angelo P. Castellani");
  194. MODULE_LICENSE("GPL");
  195. MODULE_DESCRIPTION("YeAH TCP");