tcp_veno.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.ntu.edu.sg/home5/ZHOU0022/papers/CPFu03a.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_rtt_calc(struct sock *sk, u32 usrtt)
  61. {
  62. struct veno *veno = inet_csk_ca(sk);
  63. u32 vrtt = usrtt + 1; /* Never allow zero rtt or basertt */
  64. /* Filter to find propagation delay: */
  65. if (vrtt < veno->basertt)
  66. veno->basertt = vrtt;
  67. /* Find the min rtt during the last rtt to find
  68. * the current prop. delay + queuing delay:
  69. */
  70. veno->minrtt = min(veno->minrtt, vrtt);
  71. veno->cntrtt++;
  72. }
  73. static void tcp_veno_state(struct sock *sk, u8 ca_state)
  74. {
  75. if (ca_state == TCP_CA_Open)
  76. veno_enable(sk);
  77. else
  78. veno_disable(sk);
  79. }
  80. /*
  81. * If the connection is idle and we are restarting,
  82. * then we don't want to do any Veno calculations
  83. * until we get fresh rtt samples. So when we
  84. * restart, we reset our Veno state to a clean
  85. * state. After we get acks for this flight of
  86. * packets, _then_ we can make Veno calculations
  87. * again.
  88. */
  89. static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  90. {
  91. if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
  92. tcp_veno_init(sk);
  93. }
  94. static void tcp_veno_cong_avoid(struct sock *sk, u32 ack,
  95. u32 seq_rtt, u32 in_flight, int flag)
  96. {
  97. struct tcp_sock *tp = tcp_sk(sk);
  98. struct veno *veno = inet_csk_ca(sk);
  99. if (!veno->doing_veno_now)
  100. return tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
  101. /* limited by applications */
  102. if (!tcp_is_cwnd_limited(sk, in_flight))
  103. return;
  104. /* We do the Veno calculations only if we got enough rtt samples */
  105. if (veno->cntrtt <= 2) {
  106. /* We don't have enough rtt samples to do the Veno
  107. * calculation, so we'll behave like Reno.
  108. */
  109. tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
  110. } else {
  111. u32 rtt, target_cwnd;
  112. /* We have enough rtt samples, so, using the Veno
  113. * algorithm, we determine the state of the network.
  114. */
  115. rtt = veno->minrtt;
  116. target_cwnd = ((tp->snd_cwnd * veno->basertt)
  117. << V_PARAM_SHIFT) / rtt;
  118. veno->diff = (tp->snd_cwnd << V_PARAM_SHIFT) - target_cwnd;
  119. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  120. /* Slow start. */
  121. tcp_slow_start(tp);
  122. } else {
  123. /* Congestion avoidance. */
  124. if (veno->diff < beta) {
  125. /* In the "non-congestive state", increase cwnd
  126. * every rtt.
  127. */
  128. if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  129. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  130. tp->snd_cwnd++;
  131. tp->snd_cwnd_cnt = 0;
  132. } else
  133. tp->snd_cwnd_cnt++;
  134. } else {
  135. /* In the "congestive state", increase cwnd
  136. * every other rtt.
  137. */
  138. if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  139. if (veno->inc
  140. && tp->snd_cwnd <
  141. tp->snd_cwnd_clamp) {
  142. tp->snd_cwnd++;
  143. veno->inc = 0;
  144. } else
  145. veno->inc = 1;
  146. tp->snd_cwnd_cnt = 0;
  147. } else
  148. tp->snd_cwnd_cnt++;
  149. }
  150. }
  151. if (tp->snd_cwnd < 2)
  152. tp->snd_cwnd = 2;
  153. else if (tp->snd_cwnd > tp->snd_cwnd_clamp)
  154. tp->snd_cwnd = tp->snd_cwnd_clamp;
  155. }
  156. /* Wipe the slate clean for the next rtt. */
  157. /* veno->cntrtt = 0; */
  158. veno->minrtt = 0x7fffffff;
  159. }
  160. /* Veno MD phase */
  161. static u32 tcp_veno_ssthresh(struct sock *sk)
  162. {
  163. const struct tcp_sock *tp = tcp_sk(sk);
  164. struct veno *veno = inet_csk_ca(sk);
  165. if (veno->diff < beta)
  166. /* in "non-congestive state", cut cwnd by 1/5 */
  167. return max(tp->snd_cwnd * 4 / 5, 2U);
  168. else
  169. /* in "congestive state", cut cwnd by 1/2 */
  170. return max(tp->snd_cwnd >> 1U, 2U);
  171. }
  172. static struct tcp_congestion_ops tcp_veno = {
  173. .init = tcp_veno_init,
  174. .ssthresh = tcp_veno_ssthresh,
  175. .cong_avoid = tcp_veno_cong_avoid,
  176. .rtt_sample = tcp_veno_rtt_calc,
  177. .set_state = tcp_veno_state,
  178. .cwnd_event = tcp_veno_cwnd_event,
  179. .owner = THIS_MODULE,
  180. .name = "veno",
  181. };
  182. static int __init tcp_veno_register(void)
  183. {
  184. BUILD_BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE);
  185. tcp_register_congestion_control(&tcp_veno);
  186. return 0;
  187. }
  188. static void __exit tcp_veno_unregister(void)
  189. {
  190. tcp_unregister_congestion_control(&tcp_veno);
  191. }
  192. module_init(tcp_veno_register);
  193. module_exit(tcp_veno_unregister);
  194. MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu");
  195. MODULE_LICENSE("GPL");
  196. MODULE_DESCRIPTION("TCP Veno");