tcp_veno.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/config.h>
  12. #include <linux/mm.h>
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/inet_diag.h>
  16. #include <net/tcp.h>
  17. /* Default values of the Veno variables, in fixed-point representation
  18. * with V_PARAM_SHIFT bits to the right of the binary point.
  19. */
  20. #define V_PARAM_SHIFT 1
  21. static const int beta = 3 << V_PARAM_SHIFT;
  22. /* Veno variables */
  23. struct veno {
  24. u8 doing_veno_now; /* if true, do veno for this rtt */
  25. u16 cntrtt; /* # of rtts measured within last rtt */
  26. u32 minrtt; /* min of rtts measured within last rtt (in usec) */
  27. u32 basertt; /* the min of all Veno rtt measurements seen (in usec) */
  28. u32 inc; /* decide whether to increase cwnd */
  29. u32 diff; /* calculate the diff rate */
  30. };
  31. /* There are several situations when we must "re-start" Veno:
  32. *
  33. * o when a connection is established
  34. * o after an RTO
  35. * o after fast recovery
  36. * o when we send a packet and there is no outstanding
  37. * unacknowledged data (restarting an idle connection)
  38. *
  39. */
  40. static inline void veno_enable(struct sock *sk)
  41. {
  42. struct veno *veno = inet_csk_ca(sk);
  43. /* turn on Veno */
  44. veno->doing_veno_now = 1;
  45. veno->minrtt = 0x7fffffff;
  46. }
  47. static inline void veno_disable(struct sock *sk)
  48. {
  49. struct veno *veno = inet_csk_ca(sk);
  50. /* turn off Veno */
  51. veno->doing_veno_now = 0;
  52. }
  53. static void tcp_veno_init(struct sock *sk)
  54. {
  55. struct veno *veno = inet_csk_ca(sk);
  56. veno->basertt = 0x7fffffff;
  57. veno->inc = 1;
  58. veno_enable(sk);
  59. }
  60. /* Do rtt sampling needed for Veno. */
  61. static void tcp_veno_rtt_calc(struct sock *sk, u32 usrtt)
  62. {
  63. struct veno *veno = inet_csk_ca(sk);
  64. u32 vrtt = usrtt + 1; /* Never allow zero rtt or basertt */
  65. /* Filter to find propagation delay: */
  66. if (vrtt < veno->basertt)
  67. veno->basertt = vrtt;
  68. /* Find the min rtt during the last rtt to find
  69. * the current prop. delay + queuing delay:
  70. */
  71. veno->minrtt = min(veno->minrtt, vrtt);
  72. veno->cntrtt++;
  73. }
  74. static void tcp_veno_state(struct sock *sk, u8 ca_state)
  75. {
  76. if (ca_state == TCP_CA_Open)
  77. veno_enable(sk);
  78. else
  79. veno_disable(sk);
  80. }
  81. /*
  82. * If the connection is idle and we are restarting,
  83. * then we don't want to do any Veno calculations
  84. * until we get fresh rtt samples. So when we
  85. * restart, we reset our Veno state to a clean
  86. * state. After we get acks for this flight of
  87. * packets, _then_ we can make Veno calculations
  88. * again.
  89. */
  90. static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  91. {
  92. if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
  93. tcp_veno_init(sk);
  94. }
  95. static void tcp_veno_cong_avoid(struct sock *sk, u32 ack,
  96. u32 seq_rtt, u32 in_flight, int flag)
  97. {
  98. struct tcp_sock *tp = tcp_sk(sk);
  99. struct veno *veno = inet_csk_ca(sk);
  100. if (!veno->doing_veno_now)
  101. return tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
  102. /* limited by applications */
  103. if (!tcp_is_cwnd_limited(sk, in_flight))
  104. return;
  105. /* We do the Veno calculations only if we got enough rtt samples */
  106. if (veno->cntrtt <= 2) {
  107. /* We don't have enough rtt samples to do the Veno
  108. * calculation, so we'll behave like Reno.
  109. */
  110. tcp_reno_cong_avoid(sk, ack, seq_rtt, in_flight, flag);
  111. } else {
  112. u32 rtt, target_cwnd;
  113. /* We have enough rtt samples, so, using the Veno
  114. * algorithm, we determine the state of the network.
  115. */
  116. rtt = veno->minrtt;
  117. target_cwnd = ((tp->snd_cwnd * veno->basertt)
  118. << V_PARAM_SHIFT) / rtt;
  119. veno->diff = (tp->snd_cwnd << V_PARAM_SHIFT) - target_cwnd;
  120. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  121. /* Slow start. */
  122. tcp_slow_start(tp);
  123. } else {
  124. /* Congestion avoidance. */
  125. if (veno->diff < beta) {
  126. /* In the "non-congestive state", increase cwnd
  127. * every rtt.
  128. */
  129. if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  130. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  131. tp->snd_cwnd++;
  132. tp->snd_cwnd_cnt = 0;
  133. } else
  134. tp->snd_cwnd_cnt++;
  135. } else {
  136. /* In the "congestive state", increase cwnd
  137. * every other rtt.
  138. */
  139. if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  140. if (veno->inc
  141. && tp->snd_cwnd <
  142. 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 = {
  174. .init = tcp_veno_init,
  175. .ssthresh = tcp_veno_ssthresh,
  176. .cong_avoid = tcp_veno_cong_avoid,
  177. .rtt_sample = tcp_veno_rtt_calc,
  178. .set_state = tcp_veno_state,
  179. .cwnd_event = tcp_veno_cwnd_event,
  180. .owner = THIS_MODULE,
  181. .name = "veno",
  182. };
  183. static int __init tcp_veno_register(void)
  184. {
  185. BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE);
  186. tcp_register_congestion_control(&tcp_veno);
  187. return 0;
  188. }
  189. static void __exit tcp_veno_unregister(void)
  190. {
  191. tcp_unregister_congestion_control(&tcp_veno);
  192. }
  193. module_init(tcp_veno_register);
  194. module_exit(tcp_veno_unregister);
  195. MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu");
  196. MODULE_LICENSE("GPL");
  197. MODULE_DESCRIPTION("TCP Veno");