tcp_westwood.c 8.2 KB

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