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/config.h>
  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_time_stamp;
  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, u32 cnt)
  96. {
  97. struct westwood *w = inet_csk_ca(sk);
  98. if (cnt > 0)
  99. w->rtt = tcp_sk(sk)->srtt >> 3;
  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_event(struct sock *sk, enum tcp_ca_event event)
  199. {
  200. struct tcp_sock *tp = tcp_sk(sk);
  201. struct westwood *w = inet_csk_ca(sk);
  202. switch(event) {
  203. case CA_EVENT_FAST_ACK:
  204. westwood_fast_bw(sk);
  205. break;
  206. case CA_EVENT_COMPLETE_CWR:
  207. tp->snd_cwnd = tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
  208. break;
  209. case CA_EVENT_FRTO:
  210. tp->snd_ssthresh = tcp_westwood_bw_rttmin(sk);
  211. /* Update RTT_min when next ack arrives */
  212. w->reset_rtt_min = 1;
  213. break;
  214. case CA_EVENT_SLOW_ACK:
  215. westwood_update_window(sk);
  216. w->bk += westwood_acked_count(sk);
  217. update_rtt_min(w);
  218. break;
  219. default:
  220. /* don't care */
  221. break;
  222. }
  223. }
  224. /* Extract info for Tcp socket info provided via netlink. */
  225. static void tcp_westwood_info(struct sock *sk, u32 ext,
  226. struct sk_buff *skb)
  227. {
  228. const struct westwood *ca = inet_csk_ca(sk);
  229. if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  230. struct rtattr *rta;
  231. struct tcpvegas_info *info;
  232. rta = __RTA_PUT(skb, INET_DIAG_VEGASINFO, sizeof(*info));
  233. info = RTA_DATA(rta);
  234. info->tcpv_enabled = 1;
  235. info->tcpv_rttcnt = 0;
  236. info->tcpv_rtt = jiffies_to_usecs(ca->rtt);
  237. info->tcpv_minrtt = jiffies_to_usecs(ca->rtt_min);
  238. rtattr_failure: ;
  239. }
  240. }
  241. static struct tcp_congestion_ops tcp_westwood = {
  242. .init = tcp_westwood_init,
  243. .ssthresh = tcp_reno_ssthresh,
  244. .cong_avoid = tcp_reno_cong_avoid,
  245. .min_cwnd = tcp_westwood_bw_rttmin,
  246. .cwnd_event = tcp_westwood_event,
  247. .get_info = tcp_westwood_info,
  248. .pkts_acked = tcp_westwood_pkts_acked,
  249. .owner = THIS_MODULE,
  250. .name = "westwood"
  251. };
  252. static int __init tcp_westwood_register(void)
  253. {
  254. BUG_ON(sizeof(struct westwood) > ICSK_CA_PRIV_SIZE);
  255. return tcp_register_congestion_control(&tcp_westwood);
  256. }
  257. static void __exit tcp_westwood_unregister(void)
  258. {
  259. tcp_unregister_congestion_control(&tcp_westwood);
  260. }
  261. module_init(tcp_westwood_register);
  262. module_exit(tcp_westwood_unregister);
  263. MODULE_AUTHOR("Stephen Hemminger, Angelo Dell'Aera");
  264. MODULE_LICENSE("GPL");
  265. MODULE_DESCRIPTION("TCP Westwood+");