tcp_veno.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * TCP Veno congestion control
  3. *
  4. * This is based on the congestion detection/avoidance scheme described in
  5. * C. P. Fu, S. C. Liew.
  6. * "TCP Veno: TCP Enhancement for Transmission over Wireless Access Networks."
  7. * IEEE Journal on Selected Areas in Communication,
  8. * Feb. 2003.
  9. * See http://www.ie.cuhk.edu.hk/fileadmin/staff_upload/soung/Journal/J3.pdf
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/module.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/inet_diag.h>
  15. #include <net/tcp.h>
  16. /* Default values of the Veno variables, in fixed-point representation
  17. * with V_PARAM_SHIFT bits to the right of the binary point.
  18. */
  19. #define V_PARAM_SHIFT 1
  20. static const int beta = 3 << V_PARAM_SHIFT;
  21. /* Veno variables */
  22. struct veno {
  23. u8 doing_veno_now; /* if true, do veno for this rtt */
  24. u16 cntrtt; /* # of rtts measured within last rtt */
  25. u32 minrtt; /* min of rtts measured within last rtt (in usec) */
  26. u32 basertt; /* the min of all Veno rtt measurements seen (in usec) */
  27. u32 inc; /* decide whether to increase cwnd */
  28. u32 diff; /* calculate the diff rate */
  29. };
  30. /* There are several situations when we must "re-start" Veno:
  31. *
  32. * o when a connection is established
  33. * o after an RTO
  34. * o after fast recovery
  35. * o when we send a packet and there is no outstanding
  36. * unacknowledged data (restarting an idle connection)
  37. *
  38. */
  39. static inline void veno_enable(struct sock *sk)
  40. {
  41. struct veno *veno = inet_csk_ca(sk);
  42. /* turn on Veno */
  43. veno->doing_veno_now = 1;
  44. veno->minrtt = 0x7fffffff;
  45. }
  46. static inline void veno_disable(struct sock *sk)
  47. {
  48. struct veno *veno = inet_csk_ca(sk);
  49. /* turn off Veno */
  50. veno->doing_veno_now = 0;
  51. }
  52. static void tcp_veno_init(struct sock *sk)
  53. {
  54. struct veno *veno = inet_csk_ca(sk);
  55. veno->basertt = 0x7fffffff;
  56. veno->inc = 1;
  57. veno_enable(sk);
  58. }
  59. /* Do rtt sampling needed for Veno. */
  60. static void tcp_veno_pkts_acked(struct sock *sk, u32 cnt, s32 rtt_us)
  61. {
  62. struct veno *veno = inet_csk_ca(sk);
  63. u32 vrtt;
  64. if (rtt_us < 0)
  65. return;
  66. /* Never allow zero rtt or baseRTT */
  67. vrtt = rtt_us + 1;
  68. /* Filter to find propagation delay: */
  69. if (vrtt < veno->basertt)
  70. veno->basertt = vrtt;
  71. /* Find the min rtt during the last rtt to find
  72. * the current prop. delay + queuing delay:
  73. */
  74. veno->minrtt = min(veno->minrtt, vrtt);
  75. veno->cntrtt++;
  76. }
  77. static void tcp_veno_state(struct sock *sk, u8 ca_state)
  78. {
  79. if (ca_state == TCP_CA_Open)
  80. veno_enable(sk);
  81. else
  82. veno_disable(sk);
  83. }
  84. /*
  85. * If the connection is idle and we are restarting,
  86. * then we don't want to do any Veno calculations
  87. * until we get fresh rtt samples. So when we
  88. * restart, we reset our Veno state to a clean
  89. * state. After we get acks for this flight of
  90. * packets, _then_ we can make Veno calculations
  91. * again.
  92. */
  93. static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  94. {
  95. if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
  96. tcp_veno_init(sk);
  97. }
  98. static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
  99. {
  100. struct tcp_sock *tp = tcp_sk(sk);
  101. struct veno *veno = inet_csk_ca(sk);
  102. if (!veno->doing_veno_now) {
  103. tcp_reno_cong_avoid(sk, ack, in_flight);
  104. return;
  105. }
  106. /* limited by applications */
  107. if (!tcp_is_cwnd_limited(sk, in_flight))
  108. return;
  109. /* We do the Veno calculations only if we got enough rtt samples */
  110. if (veno->cntrtt <= 2) {
  111. /* We don't have enough rtt samples to do the Veno
  112. * calculation, so we'll behave like Reno.
  113. */
  114. tcp_reno_cong_avoid(sk, ack, in_flight);
  115. } else {
  116. u64 target_cwnd;
  117. u32 rtt;
  118. /* We have enough rtt samples, so, using the Veno
  119. * algorithm, we determine the state of the network.
  120. */
  121. rtt = veno->minrtt;
  122. target_cwnd = (tp->snd_cwnd * veno->basertt);
  123. target_cwnd <<= V_PARAM_SHIFT;
  124. do_div(target_cwnd, rtt);
  125. veno->diff = (tp->snd_cwnd << V_PARAM_SHIFT) - target_cwnd;
  126. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  127. /* Slow start. */
  128. tcp_slow_start(tp);
  129. } else {
  130. /* Congestion avoidance. */
  131. if (veno->diff < beta) {
  132. /* In the "non-congestive state", increase cwnd
  133. * every rtt.
  134. */
  135. tcp_cong_avoid_ai(tp, tp->snd_cwnd);
  136. } else {
  137. /* In the "congestive state", increase cwnd
  138. * every other rtt.
  139. */
  140. if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  141. if (veno->inc &&
  142. tp->snd_cwnd < tp->snd_cwnd_clamp) {
  143. tp->snd_cwnd++;
  144. veno->inc = 0;
  145. } else
  146. veno->inc = 1;
  147. tp->snd_cwnd_cnt = 0;
  148. } else
  149. tp->snd_cwnd_cnt++;
  150. }
  151. }
  152. if (tp->snd_cwnd < 2)
  153. tp->snd_cwnd = 2;
  154. else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
  155. tp->snd_cwnd = tp->snd_cwnd_clamp;
  156. }
  157. /* Wipe the slate clean for the next rtt. */
  158. /* veno->cntrtt = 0; */
  159. veno->minrtt = 0x7fffffff;
  160. }
  161. /* Veno MD phase */
  162. static u32 tcp_veno_ssthresh(struct sock *sk)
  163. {
  164. const struct tcp_sock *tp = tcp_sk(sk);
  165. struct veno *veno = inet_csk_ca(sk);
  166. if (veno->diff < beta)
  167. /* in "non-congestive state", cut cwnd by 1/5 */
  168. return max(tp->snd_cwnd * 4 / 5, 2U);
  169. else
  170. /* in "congestive state", cut cwnd by 1/2 */
  171. return max(tp->snd_cwnd >> 1U, 2U);
  172. }
  173. static struct tcp_congestion_ops tcp_veno __read_mostly = {
  174. .flags = TCP_CONG_RTT_STAMP,
  175. .init = tcp_veno_init,
  176. .ssthresh = tcp_veno_ssthresh,
  177. .cong_avoid = tcp_veno_cong_avoid,
  178. .pkts_acked = tcp_veno_pkts_acked,
  179. .set_state = tcp_veno_state,
  180. .cwnd_event = tcp_veno_cwnd_event,
  181. .owner = THIS_MODULE,
  182. .name = "veno",
  183. };
  184. static int __init tcp_veno_register(void)
  185. {
  186. BUILD_BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE);
  187. tcp_register_congestion_control(&tcp_veno);
  188. return 0;
  189. }
  190. static void __exit tcp_veno_unregister(void)
  191. {
  192. tcp_unregister_congestion_control(&tcp_veno);
  193. }
  194. module_init(tcp_veno_register);
  195. module_exit(tcp_veno_unregister);
  196. MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu");
  197. MODULE_LICENSE("GPL");
  198. MODULE_DESCRIPTION("TCP Veno");