tcp_yeah.c 7.3 KB

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