tcp_yeah.c 7.0 KB

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