tcp_veno.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TCP Veno congestion control
  4. *
  5. * This is based on the congestion detection/avoidance scheme described in
  6. * C. P. Fu, S. C. Liew.
  7. * "TCP Veno: TCP Enhancement for Transmission over Wireless Access Networks."
  8. * IEEE Journal on Selected Areas in Communication,
  9. * Feb. 2003.
  10. * See https://www.ie.cuhk.edu.hk/fileadmin/staff_upload/soung/Journal/J3.pdf
  11. */
  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_pkts_acked(struct sock *sk,
  62. const struct ack_sample *sample)
  63. {
  64. struct veno *veno = inet_csk_ca(sk);
  65. u32 vrtt;
  66. if (sample->rtt_us < 0)
  67. return;
  68. /* Never allow zero rtt or baseRTT */
  69. vrtt = sample->rtt_us + 1;
  70. /* Filter to find propagation delay: */
  71. if (vrtt < veno->basertt)
  72. veno->basertt = vrtt;
  73. /* Find the min rtt during the last rtt to find
  74. * the current prop. delay + queuing delay:
  75. */
  76. veno->minrtt = min(veno->minrtt, vrtt);
  77. veno->cntrtt++;
  78. }
  79. static void tcp_veno_state(struct sock *sk, u8 ca_state)
  80. {
  81. if (ca_state == TCP_CA_Open)
  82. veno_enable(sk);
  83. else
  84. veno_disable(sk);
  85. }
  86. /*
  87. * If the connection is idle and we are restarting,
  88. * then we don't want to do any Veno calculations
  89. * until we get fresh rtt samples. So when we
  90. * restart, we reset our Veno state to a clean
  91. * state. After we get acks for this flight of
  92. * packets, _then_ we can make Veno calculations
  93. * again.
  94. */
  95. static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
  96. {
  97. if (event == CA_EVENT_CWND_RESTART || event == CA_EVENT_TX_START)
  98. tcp_veno_init(sk);
  99. }
  100. static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  101. {
  102. struct tcp_sock *tp = tcp_sk(sk);
  103. struct veno *veno = inet_csk_ca(sk);
  104. if (!veno->doing_veno_now) {
  105. tcp_reno_cong_avoid(sk, ack, acked);
  106. return;
  107. }
  108. /* limited by applications */
  109. if (!tcp_is_cwnd_limited(sk))
  110. return;
  111. /* We do the Veno calculations only if we got enough rtt samples */
  112. if (veno->cntrtt <= 2) {
  113. /* We don't have enough rtt samples to do the Veno
  114. * calculation, so we'll behave like Reno.
  115. */
  116. tcp_reno_cong_avoid(sk, ack, acked);
  117. } else {
  118. u64 target_cwnd;
  119. u32 rtt;
  120. /* We have enough rtt samples, so, using the Veno
  121. * algorithm, we determine the state of the network.
  122. */
  123. rtt = veno->minrtt;
  124. target_cwnd = (u64)tcp_snd_cwnd(tp) * veno->basertt;
  125. target_cwnd <<= V_PARAM_SHIFT;
  126. do_div(target_cwnd, rtt);
  127. veno->diff = (tcp_snd_cwnd(tp) << V_PARAM_SHIFT) - target_cwnd;
  128. if (tcp_in_slow_start(tp)) {
  129. /* Slow start. */
  130. acked = tcp_slow_start(tp, acked);
  131. if (!acked)
  132. goto done;
  133. }
  134. /* Congestion avoidance. */
  135. if (veno->diff < beta) {
  136. /* In the "non-congestive state", increase cwnd
  137. * every rtt.
  138. */
  139. tcp_cong_avoid_ai(tp, tcp_snd_cwnd(tp), acked);
  140. } else {
  141. /* In the "congestive state", increase cwnd
  142. * every other rtt.
  143. */
  144. if (tp->snd_cwnd_cnt >= tcp_snd_cwnd(tp)) {
  145. if (veno->inc &&
  146. tcp_snd_cwnd(tp) < tp->snd_cwnd_clamp) {
  147. tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
  148. veno->inc = 0;
  149. } else
  150. veno->inc = 1;
  151. tp->snd_cwnd_cnt = 0;
  152. } else
  153. tp->snd_cwnd_cnt += acked;
  154. }
  155. done:
  156. if (tcp_snd_cwnd(tp) < 2)
  157. tcp_snd_cwnd_set(tp, 2);
  158. else if (tcp_snd_cwnd(tp) > tp->snd_cwnd_clamp)
  159. tcp_snd_cwnd_set(tp, tp->snd_cwnd_clamp);
  160. }
  161. /* Wipe the slate clean for the next rtt. */
  162. /* veno->cntrtt = 0; */
  163. veno->minrtt = 0x7fffffff;
  164. }
  165. /* Veno MD phase */
  166. static u32 tcp_veno_ssthresh(struct sock *sk)
  167. {
  168. const struct tcp_sock *tp = tcp_sk(sk);
  169. struct veno *veno = inet_csk_ca(sk);
  170. if (veno->diff < beta)
  171. /* in "non-congestive state", cut cwnd by 1/5 */
  172. return max(tcp_snd_cwnd(tp) * 4 / 5, 2U);
  173. else
  174. /* in "congestive state", cut cwnd by 1/2 */
  175. return max(tcp_snd_cwnd(tp) >> 1U, 2U);
  176. }
  177. static struct tcp_congestion_ops tcp_veno __read_mostly = {
  178. .init = tcp_veno_init,
  179. .ssthresh = tcp_veno_ssthresh,
  180. .undo_cwnd = tcp_reno_undo_cwnd,
  181. .cong_avoid = tcp_veno_cong_avoid,
  182. .pkts_acked = tcp_veno_pkts_acked,
  183. .set_state = tcp_veno_state,
  184. .cwnd_event = tcp_veno_cwnd_event,
  185. .owner = THIS_MODULE,
  186. .name = "veno",
  187. };
  188. static int __init tcp_veno_register(void)
  189. {
  190. BUILD_BUG_ON(sizeof(struct veno) > ICSK_CA_PRIV_SIZE);
  191. tcp_register_congestion_control(&tcp_veno);
  192. return 0;
  193. }
  194. static void __exit tcp_veno_unregister(void)
  195. {
  196. tcp_unregister_congestion_control(&tcp_veno);
  197. }
  198. module_init(tcp_veno_register);
  199. module_exit(tcp_veno_unregister);
  200. MODULE_AUTHOR("Bin Zhou, Cheng Peng Fu");
  201. MODULE_LICENSE("GPL");
  202. MODULE_DESCRIPTION("TCP Veno");