tcp_yeah.c 6.9 KB

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