tcp_westwood.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TCP Westwood+: end-to-end bandwidth estimation for TCP
  4. *
  5. * Angelo Dell'Aera: author of the first version of TCP Westwood+ in Linux 2.4
  6. *
  7. * Support at http://c3lab.poliba.it/index.php/Westwood
  8. * Main references in literature:
  9. *
  10. * - Mascolo S, Casetti, M. Gerla et al.
  11. * "TCP Westwood: bandwidth estimation for TCP" Proc. ACM Mobicom 2001
  12. *
  13. * - A. Grieco, s. Mascolo
  14. * "Performance evaluation of New Reno, Vegas, Westwood+ TCP" ACM Computer
  15. * Comm. Review, 2004
  16. *
  17. * - A. Dell'Aera, L. Grieco, S. Mascolo.
  18. * "Linux 2.4 Implementation of Westwood+ TCP with Rate-Halving :
  19. * A Performance Evaluation Over the Internet" (ICC 2004), Paris, June 2004
  20. *
  21. * Westwood+ employs end-to-end bandwidth measurement to set cwnd and
  22. * ssthresh after packet loss. The probing phase is as the original Reno.
  23. */
  24. #include <linux/mm.h>
  25. #include <linux/module.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/inet_diag.h>
  28. #include <net/tcp.h>
  29. /* TCP Westwood structure */
  30. struct westwood {
  31. u32 bw_ns_est; /* first bandwidth estimation..not too smoothed 8) */
  32. u32 bw_est; /* bandwidth estimate */
  33. u32 rtt_win_sx; /* here starts a new evaluation... */
  34. u32 bk;
  35. u32 snd_una; /* used for evaluating the number of acked bytes */
  36. u32 cumul_ack;
  37. u32 accounted;
  38. u32 rtt;
  39. u32 rtt_min; /* minimum observed RTT */
  40. u8 first_ack; /* flag which infers that this is the first ack */
  41. u8 reset_rtt_min; /* Reset RTT min to next RTT sample*/
  42. };
  43. /* TCP Westwood functions and constants */
  44. #define TCP_WESTWOOD_RTT_MIN (HZ/20) /* 50ms */
  45. #define TCP_WESTWOOD_INIT_RTT (20*HZ) /* maybe too conservative?! */
  46. /*
  47. * @tcp_westwood_create
  48. * This function initializes fields used in TCP Westwood+,
  49. * it is called after the initial SYN, so the sequence numbers
  50. * are correct but new passive connections we have no
  51. * information about RTTmin at this time so we simply set it to
  52. * TCP_WESTWOOD_INIT_RTT. This value was chosen to be too conservative
  53. * since in this way we're sure it will be updated in a consistent
  54. * way as soon as possible. It will reasonably happen within the first
  55. * RTT period of the connection lifetime.
  56. */
  57. static void tcp_westwood_init(struct sock *sk)
  58. {
  59. struct westwood *w = inet_csk_ca(sk);
  60. w->bk = 0;
  61. w->bw_ns_est = 0;
  62. w->bw_est = 0;
  63. w->accounted = 0;
  64. w->cumul_ack = 0;
  65. w->reset_rtt_min = 1;
  66. w->rtt_min = w->rtt = TCP_WESTWOOD_INIT_RTT;
  67. w->rtt_win_sx = tcp_jiffies32;
  68. w->snd_una = tcp_sk(sk)->snd_una;
  69. w->first_ack = 1;
  70. }
  71. /*
  72. * @westwood_do_filter
  73. * Low-pass filter. Implemented using constant coefficients.
  74. */
  75. static inline u32 westwood_do_filter(u32 a, u32 b)
  76. {
  77. return ((7 * a) + b) >> 3;
  78. }
  79. static void westwood_filter(struct westwood *w, u32 delta)
  80. {
  81. /* If the filter is empty fill it with the first sample of bandwidth */
  82. if (w->bw_ns_est == 0 && w->bw_est == 0) {
  83. w->bw_ns_est = w->bk / delta;
  84. w->bw_est = w->bw_ns_est;
  85. } else {
  86. w->bw_ns_est = westwood_do_filter(w->bw_ns_est, w->bk / delta);
  87. w->bw_est = westwood_do_filter(w->bw_est, w->bw_ns_est);
  88. }
  89. }
  90. /*
  91. * @westwood_pkts_acked
  92. * Called after processing group of packets.
  93. * but all westwood needs is the last sample of srtt.
  94. */
  95. static void tcp_westwood_pkts_acked(struct sock *sk,
  96. const struct ack_sample *sample)
  97. {
  98. struct westwood *w = inet_csk_ca(sk);
  99. if (sample->rtt_us > 0)
  100. w->rtt = usecs_to_jiffies(sample->rtt_us);
  101. }
  102. /*
  103. * @westwood_update_window
  104. * It updates RTT evaluation window if it is the right moment to do
  105. * it. If so it calls filter for evaluating bandwidth.
  106. */
  107. static void westwood_update_window(struct sock *sk)
  108. {
  109. struct westwood *w = inet_csk_ca(sk);
  110. s32 delta = tcp_jiffies32 - w->rtt_win_sx;
  111. /* Initialize w->snd_una with the first acked sequence number in order
  112. * to fix mismatch between tp->snd_una and w->snd_una for the first
  113. * bandwidth sample
  114. */
  115. if (w->first_ack) {
  116. w->snd_una = tcp_sk(sk)->snd_una;
  117. w->first_ack = 0;
  118. }
  119. /*
  120. * See if a RTT-window has passed.
  121. * Be careful since if RTT is less than
  122. * 50ms we don't filter but we continue 'building the sample'.
  123. * This minimum limit was chosen since an estimation on small
  124. * time intervals is better to avoid...
  125. * Obviously on a LAN we reasonably will always have
  126. * right_bound = left_bound + WESTWOOD_RTT_MIN
  127. */
  128. if (w->rtt && delta > max_t(u32, w->rtt, TCP_WESTWOOD_RTT_MIN)) {
  129. westwood_filter(w, delta);
  130. w->bk = 0;
  131. w->rtt_win_sx = tcp_jiffies32;
  132. }
  133. }
  134. static inline void update_rtt_min(struct westwood *w)
  135. {
  136. if (w->reset_rtt_min) {
  137. w->rtt_min = w->rtt;
  138. w->reset_rtt_min = 0;
  139. } else
  140. w->rtt_min = min(w->rtt, w->rtt_min);
  141. }
  142. /*
  143. * @westwood_fast_bw
  144. * It is called when we are in fast path. In particular it is called when
  145. * header prediction is successful. In such case in fact update is
  146. * straight forward and doesn't need any particular care.
  147. */
  148. static inline void westwood_fast_bw(struct sock *sk)
  149. {
  150. const struct tcp_sock *tp = tcp_sk(sk);
  151. struct westwood *w = inet_csk_ca(sk);
  152. westwood_update_window(sk);
  153. w->bk += tp->snd_una - w->snd_una;
  154. w->snd_una = tp->snd_una;
  155. update_rtt_min(w);
  156. }
  157. /*
  158. * @westwood_acked_count
  159. * This function evaluates cumul_ack for evaluating bk in case of
  160. * delayed or partial acks.
  161. */
  162. static inline u32 westwood_acked_count(struct sock *sk)
  163. {
  164. const struct tcp_sock *tp = tcp_sk(sk);
  165. struct westwood *w = inet_csk_ca(sk);
  166. w->cumul_ack = tp->snd_una - w->snd_una;
  167. /* If cumul_ack is 0 this is a dupack since it's not moving
  168. * tp->snd_una.
  169. */
  170. if (!w->cumul_ack) {
  171. w->accounted += tp->mss_cache;
  172. w->cumul_ack = tp->mss_cache;
  173. }
  174. if (w->cumul_ack > tp->mss_cache) {
  175. /* Partial or delayed ack */
  176. if (w->accounted >= w->cumul_ack) {
  177. w->accounted -= w->cumul_ack;
  178. w->cumul_ack = tp->mss_cache;
  179. } else {
  180. w->cumul_ack -= w->accounted;
  181. w->accounted = 0;
  182. }
  183. }
  184. w->snd_una = tp->snd_una;
  185. return w->cumul_ack;
  186. }
  187. /*
  188. * TCP Westwood
  189. * Here limit is evaluated as Bw estimation*RTTmin (for obtaining it
  190. * in packets we use mss_cache). Rttmin is guaranteed to be >= 2
  191. * so avoids ever returning 0.
  192. */
  193. static u32 tcp_westwood_bw_rttmin(const struct sock *sk)
  194. {
  195. const struct tcp_sock *tp = tcp_sk(sk);
  196. const struct westwood *w = inet_csk_ca(sk);
  197. return max_t(u32, (w->bw_est * w->rtt_min) / tp->mss_cache, 2);
  198. }
  199. static void tcp_westwood_ack(struct sock *sk, u32 ack_flags)
  200. {
  201. if (ack_flags & CA_ACK_SLOWPATH) {
  202. struct westwood *w = inet_csk_ca(sk);
  203. westwood_update_window(sk);
  204. w->bk += westwood_acked_count(sk);
  205. update_rtt_min(w);
  206. return;
  207. }
  208. westwood_fast_bw(sk);
  209. }
  210. static void tcp_westwood_event(struct sock *sk, enum tcp_ca_event event)
  211. {
  212. struct tcp_sock *tp = tcp_sk(sk);
  213. struct westwood *w = inet_csk_ca(sk);
  214. switch (event) {
  215. case CA_EVENT_COMPLETE_CWR:
  216. tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
  217. tcp_snd_cwnd_set(tp, tp->snd_ssthresh);
  218. break;
  219. case CA_EVENT_LOSS:
  220. tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
  221. /* Update RTT_min when next ack arrives */
  222. w->reset_rtt_min = 1;
  223. break;
  224. default:
  225. /* don't care */
  226. break;
  227. }
  228. }
  229. /* Extract info for Tcp socket info provided via netlink. */
  230. static size_t tcp_westwood_info(struct sock *sk, u32 ext, int *attr,
  231. union tcp_cc_info *info)
  232. {
  233. const struct westwood *ca = inet_csk_ca(sk);
  234. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  235. info->vegas.tcpv_enabled = 1;
  236. info->vegas.tcpv_rttcnt = 0;
  237. info->vegas.tcpv_rtt = jiffies_to_usecs(ca->rtt);
  238. info->vegas.tcpv_minrtt = jiffies_to_usecs(ca->rtt_min);
  239. *attr = INET_DIAG_VEGASINFO;
  240. return sizeof(struct tcpvegas_info);
  241. }
  242. return 0;
  243. }
  244. static struct tcp_congestion_ops tcp_westwood __read_mostly = {
  245. .init = tcp_westwood_init,
  246. .ssthresh = tcp_reno_ssthresh,
  247. .cong_avoid = tcp_reno_cong_avoid,
  248. .undo_cwnd = tcp_reno_undo_cwnd,
  249. .cwnd_event = tcp_westwood_event,
  250. .in_ack_event = tcp_westwood_ack,
  251. .get_info = tcp_westwood_info,
  252. .pkts_acked = tcp_westwood_pkts_acked,
  253. .owner = THIS_MODULE,
  254. .name = "westwood"
  255. };
  256. static int __init tcp_westwood_register(void)
  257. {
  258. BUILD_BUG_ON(sizeof(struct westwood) > ICSK_CA_PRIV_SIZE);
  259. return tcp_register_congestion_control(&tcp_westwood);
  260. }
  261. static void __exit tcp_westwood_unregister(void)
  262. {
  263. tcp_unregister_congestion_control(&tcp_westwood);
  264. }
  265. module_init(tcp_westwood_register);
  266. module_exit(tcp_westwood_unregister);
  267. MODULE_AUTHOR("Stephen Hemminger, Angelo Dell'Aera");
  268. MODULE_LICENSE("GPL");
  269. MODULE_DESCRIPTION("TCP Westwood+");