187270762SStephen Hemminger /* 287270762SStephen Hemminger * TCP Westwood+ 387270762SStephen Hemminger * 487270762SStephen Hemminger * Angelo Dell'Aera: TCP Westwood+ support 587270762SStephen Hemminger */ 687270762SStephen Hemminger 787270762SStephen Hemminger #include <linux/config.h> 887270762SStephen Hemminger #include <linux/mm.h> 987270762SStephen Hemminger #include <linux/module.h> 1087270762SStephen Hemminger #include <linux/skbuff.h> 11*a8c2190eSArnaldo Carvalho de Melo #include <linux/inet_diag.h> 1287270762SStephen Hemminger #include <net/tcp.h> 1387270762SStephen Hemminger 1487270762SStephen Hemminger /* TCP Westwood structure */ 1587270762SStephen Hemminger struct westwood { 1687270762SStephen Hemminger u32 bw_ns_est; /* first bandwidth estimation..not too smoothed 8) */ 1787270762SStephen Hemminger u32 bw_est; /* bandwidth estimate */ 1887270762SStephen Hemminger u32 rtt_win_sx; /* here starts a new evaluation... */ 1987270762SStephen Hemminger u32 bk; 2087270762SStephen Hemminger u32 snd_una; /* used for evaluating the number of acked bytes */ 2187270762SStephen Hemminger u32 cumul_ack; 2287270762SStephen Hemminger u32 accounted; 2387270762SStephen Hemminger u32 rtt; 2487270762SStephen Hemminger u32 rtt_min; /* minimum observed RTT */ 2587270762SStephen Hemminger }; 2687270762SStephen Hemminger 2787270762SStephen Hemminger 2887270762SStephen Hemminger /* TCP Westwood functions and constants */ 2987270762SStephen Hemminger #define TCP_WESTWOOD_RTT_MIN (HZ/20) /* 50ms */ 3087270762SStephen Hemminger #define TCP_WESTWOOD_INIT_RTT (20*HZ) /* maybe too conservative?! */ 3187270762SStephen Hemminger 3287270762SStephen Hemminger /* 3387270762SStephen Hemminger * @tcp_westwood_create 3487270762SStephen Hemminger * This function initializes fields used in TCP Westwood+, 3587270762SStephen Hemminger * it is called after the initial SYN, so the sequence numbers 3687270762SStephen Hemminger * are correct but new passive connections we have no 3787270762SStephen Hemminger * information about RTTmin at this time so we simply set it to 3887270762SStephen Hemminger * TCP_WESTWOOD_INIT_RTT. This value was chosen to be too conservative 3987270762SStephen Hemminger * since in this way we're sure it will be updated in a consistent 4087270762SStephen Hemminger * way as soon as possible. It will reasonably happen within the first 4187270762SStephen Hemminger * RTT period of the connection lifetime. 4287270762SStephen Hemminger */ 436687e988SArnaldo Carvalho de Melo static void tcp_westwood_init(struct sock *sk) 4487270762SStephen Hemminger { 456687e988SArnaldo Carvalho de Melo struct westwood *w = inet_csk_ca(sk); 4687270762SStephen Hemminger 4787270762SStephen Hemminger w->bk = 0; 4887270762SStephen Hemminger w->bw_ns_est = 0; 4987270762SStephen Hemminger w->bw_est = 0; 5087270762SStephen Hemminger w->accounted = 0; 5187270762SStephen Hemminger w->cumul_ack = 0; 5287270762SStephen Hemminger w->rtt_min = w->rtt = TCP_WESTWOOD_INIT_RTT; 5387270762SStephen Hemminger w->rtt_win_sx = tcp_time_stamp; 546687e988SArnaldo Carvalho de Melo w->snd_una = tcp_sk(sk)->snd_una; 5587270762SStephen Hemminger } 5687270762SStephen Hemminger 5787270762SStephen Hemminger /* 5887270762SStephen Hemminger * @westwood_do_filter 5987270762SStephen Hemminger * Low-pass filter. Implemented using constant coefficients. 6087270762SStephen Hemminger */ 6187270762SStephen Hemminger static inline u32 westwood_do_filter(u32 a, u32 b) 6287270762SStephen Hemminger { 6387270762SStephen Hemminger return (((7 * a) + b) >> 3); 6487270762SStephen Hemminger } 6587270762SStephen Hemminger 6687270762SStephen Hemminger static inline void westwood_filter(struct westwood *w, u32 delta) 6787270762SStephen Hemminger { 6887270762SStephen Hemminger w->bw_ns_est = westwood_do_filter(w->bw_ns_est, w->bk / delta); 6987270762SStephen Hemminger w->bw_est = westwood_do_filter(w->bw_est, w->bw_ns_est); 7087270762SStephen Hemminger } 7187270762SStephen Hemminger 7287270762SStephen Hemminger /* 7387270762SStephen Hemminger * @westwood_pkts_acked 7487270762SStephen Hemminger * Called after processing group of packets. 7587270762SStephen Hemminger * but all westwood needs is the last sample of srtt. 7687270762SStephen Hemminger */ 776687e988SArnaldo Carvalho de Melo static void tcp_westwood_pkts_acked(struct sock *sk, u32 cnt) 7887270762SStephen Hemminger { 796687e988SArnaldo Carvalho de Melo struct westwood *w = inet_csk_ca(sk); 8087270762SStephen Hemminger if (cnt > 0) 816687e988SArnaldo Carvalho de Melo w->rtt = tcp_sk(sk)->srtt >> 3; 8287270762SStephen Hemminger } 8387270762SStephen Hemminger 8487270762SStephen Hemminger /* 8587270762SStephen Hemminger * @westwood_update_window 8687270762SStephen Hemminger * It updates RTT evaluation window if it is the right moment to do 8787270762SStephen Hemminger * it. If so it calls filter for evaluating bandwidth. 8887270762SStephen Hemminger */ 896687e988SArnaldo Carvalho de Melo static void westwood_update_window(struct sock *sk) 9087270762SStephen Hemminger { 916687e988SArnaldo Carvalho de Melo struct westwood *w = inet_csk_ca(sk); 9287270762SStephen Hemminger s32 delta = tcp_time_stamp - w->rtt_win_sx; 9387270762SStephen Hemminger 9487270762SStephen Hemminger /* 9587270762SStephen Hemminger * See if a RTT-window has passed. 9687270762SStephen Hemminger * Be careful since if RTT is less than 9787270762SStephen Hemminger * 50ms we don't filter but we continue 'building the sample'. 9887270762SStephen Hemminger * This minimum limit was chosen since an estimation on small 9987270762SStephen Hemminger * time intervals is better to avoid... 10087270762SStephen Hemminger * Obviously on a LAN we reasonably will always have 10187270762SStephen Hemminger * right_bound = left_bound + WESTWOOD_RTT_MIN 10287270762SStephen Hemminger */ 10387270762SStephen Hemminger if (w->rtt && delta > max_t(u32, w->rtt, TCP_WESTWOOD_RTT_MIN)) { 10487270762SStephen Hemminger westwood_filter(w, delta); 10587270762SStephen Hemminger 10687270762SStephen Hemminger w->bk = 0; 10787270762SStephen Hemminger w->rtt_win_sx = tcp_time_stamp; 10887270762SStephen Hemminger } 10987270762SStephen Hemminger } 11087270762SStephen Hemminger 11187270762SStephen Hemminger /* 11287270762SStephen Hemminger * @westwood_fast_bw 11387270762SStephen Hemminger * It is called when we are in fast path. In particular it is called when 11487270762SStephen Hemminger * header prediction is successful. In such case in fact update is 11587270762SStephen Hemminger * straight forward and doesn't need any particular care. 11687270762SStephen Hemminger */ 1176687e988SArnaldo Carvalho de Melo static inline void westwood_fast_bw(struct sock *sk) 11887270762SStephen Hemminger { 1196687e988SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 1206687e988SArnaldo Carvalho de Melo struct westwood *w = inet_csk_ca(sk); 12187270762SStephen Hemminger 1226687e988SArnaldo Carvalho de Melo westwood_update_window(sk); 12387270762SStephen Hemminger 12487270762SStephen Hemminger w->bk += tp->snd_una - w->snd_una; 12587270762SStephen Hemminger w->snd_una = tp->snd_una; 12687270762SStephen Hemminger w->rtt_min = min(w->rtt, w->rtt_min); 12787270762SStephen Hemminger } 12887270762SStephen Hemminger 12987270762SStephen Hemminger /* 13087270762SStephen Hemminger * @westwood_acked_count 13187270762SStephen Hemminger * This function evaluates cumul_ack for evaluating bk in case of 13287270762SStephen Hemminger * delayed or partial acks. 13387270762SStephen Hemminger */ 1346687e988SArnaldo Carvalho de Melo static inline u32 westwood_acked_count(struct sock *sk) 13587270762SStephen Hemminger { 1366687e988SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 1376687e988SArnaldo Carvalho de Melo struct westwood *w = inet_csk_ca(sk); 13887270762SStephen Hemminger 13987270762SStephen Hemminger w->cumul_ack = tp->snd_una - w->snd_una; 14087270762SStephen Hemminger 14187270762SStephen Hemminger /* If cumul_ack is 0 this is a dupack since it's not moving 14287270762SStephen Hemminger * tp->snd_una. 14387270762SStephen Hemminger */ 14487270762SStephen Hemminger if (!w->cumul_ack) { 14587270762SStephen Hemminger w->accounted += tp->mss_cache; 14687270762SStephen Hemminger w->cumul_ack = tp->mss_cache; 14787270762SStephen Hemminger } 14887270762SStephen Hemminger 14987270762SStephen Hemminger if (w->cumul_ack > tp->mss_cache) { 15087270762SStephen Hemminger /* Partial or delayed ack */ 15187270762SStephen Hemminger if (w->accounted >= w->cumul_ack) { 15287270762SStephen Hemminger w->accounted -= w->cumul_ack; 15387270762SStephen Hemminger w->cumul_ack = tp->mss_cache; 15487270762SStephen Hemminger } else { 15587270762SStephen Hemminger w->cumul_ack -= w->accounted; 15687270762SStephen Hemminger w->accounted = 0; 15787270762SStephen Hemminger } 15887270762SStephen Hemminger } 15987270762SStephen Hemminger 16087270762SStephen Hemminger w->snd_una = tp->snd_una; 16187270762SStephen Hemminger 16287270762SStephen Hemminger return w->cumul_ack; 16387270762SStephen Hemminger } 16487270762SStephen Hemminger 1656687e988SArnaldo Carvalho de Melo static inline u32 westwood_bw_rttmin(const struct sock *sk) 16687270762SStephen Hemminger { 1676687e988SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 1686687e988SArnaldo Carvalho de Melo const struct westwood *w = inet_csk_ca(sk); 16987270762SStephen Hemminger return max_t(u32, (w->bw_est * w->rtt_min) / tp->mss_cache, 2); 17087270762SStephen Hemminger } 17187270762SStephen Hemminger 17287270762SStephen Hemminger /* 17387270762SStephen Hemminger * TCP Westwood 17487270762SStephen Hemminger * Here limit is evaluated as Bw estimation*RTTmin (for obtaining it 17587270762SStephen Hemminger * in packets we use mss_cache). Rttmin is guaranteed to be >= 2 17687270762SStephen Hemminger * so avoids ever returning 0. 17787270762SStephen Hemminger */ 1786687e988SArnaldo Carvalho de Melo static u32 tcp_westwood_cwnd_min(struct sock *sk) 17987270762SStephen Hemminger { 1806687e988SArnaldo Carvalho de Melo return westwood_bw_rttmin(sk); 18187270762SStephen Hemminger } 18287270762SStephen Hemminger 1836687e988SArnaldo Carvalho de Melo static void tcp_westwood_event(struct sock *sk, enum tcp_ca_event event) 18487270762SStephen Hemminger { 1856687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 1866687e988SArnaldo Carvalho de Melo struct westwood *w = inet_csk_ca(sk); 18787270762SStephen Hemminger 18887270762SStephen Hemminger switch(event) { 18987270762SStephen Hemminger case CA_EVENT_FAST_ACK: 1906687e988SArnaldo Carvalho de Melo westwood_fast_bw(sk); 19187270762SStephen Hemminger break; 19287270762SStephen Hemminger 19387270762SStephen Hemminger case CA_EVENT_COMPLETE_CWR: 1946687e988SArnaldo Carvalho de Melo tp->snd_cwnd = tp->snd_ssthresh = westwood_bw_rttmin(sk); 19587270762SStephen Hemminger break; 19687270762SStephen Hemminger 19787270762SStephen Hemminger case CA_EVENT_FRTO: 1986687e988SArnaldo Carvalho de Melo tp->snd_ssthresh = westwood_bw_rttmin(sk); 19987270762SStephen Hemminger break; 20087270762SStephen Hemminger 20187270762SStephen Hemminger case CA_EVENT_SLOW_ACK: 2026687e988SArnaldo Carvalho de Melo westwood_update_window(sk); 2036687e988SArnaldo Carvalho de Melo w->bk += westwood_acked_count(sk); 20487270762SStephen Hemminger w->rtt_min = min(w->rtt, w->rtt_min); 20587270762SStephen Hemminger break; 20687270762SStephen Hemminger 20787270762SStephen Hemminger default: 20887270762SStephen Hemminger /* don't care */ 20987270762SStephen Hemminger break; 21087270762SStephen Hemminger } 21187270762SStephen Hemminger } 21287270762SStephen Hemminger 21387270762SStephen Hemminger 21487270762SStephen Hemminger /* Extract info for Tcp socket info provided via netlink. */ 2156687e988SArnaldo Carvalho de Melo static void tcp_westwood_info(struct sock *sk, u32 ext, 21687270762SStephen Hemminger struct sk_buff *skb) 21787270762SStephen Hemminger { 2186687e988SArnaldo Carvalho de Melo const struct westwood *ca = inet_csk_ca(sk); 21973c1f4a0SArnaldo Carvalho de Melo if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) { 22087270762SStephen Hemminger struct rtattr *rta; 22187270762SStephen Hemminger struct tcpvegas_info *info; 22287270762SStephen Hemminger 22373c1f4a0SArnaldo Carvalho de Melo rta = __RTA_PUT(skb, INET_DIAG_VEGASINFO, sizeof(*info)); 22487270762SStephen Hemminger info = RTA_DATA(rta); 22587270762SStephen Hemminger info->tcpv_enabled = 1; 22687270762SStephen Hemminger info->tcpv_rttcnt = 0; 22787270762SStephen Hemminger info->tcpv_rtt = jiffies_to_usecs(ca->rtt); 22887270762SStephen Hemminger info->tcpv_minrtt = jiffies_to_usecs(ca->rtt_min); 22987270762SStephen Hemminger rtattr_failure: ; 23087270762SStephen Hemminger } 23187270762SStephen Hemminger } 23287270762SStephen Hemminger 23387270762SStephen Hemminger 23487270762SStephen Hemminger static struct tcp_congestion_ops tcp_westwood = { 23587270762SStephen Hemminger .init = tcp_westwood_init, 23687270762SStephen Hemminger .ssthresh = tcp_reno_ssthresh, 23787270762SStephen Hemminger .cong_avoid = tcp_reno_cong_avoid, 23887270762SStephen Hemminger .min_cwnd = tcp_westwood_cwnd_min, 23987270762SStephen Hemminger .cwnd_event = tcp_westwood_event, 24087270762SStephen Hemminger .get_info = tcp_westwood_info, 24187270762SStephen Hemminger .pkts_acked = tcp_westwood_pkts_acked, 24287270762SStephen Hemminger 24387270762SStephen Hemminger .owner = THIS_MODULE, 24487270762SStephen Hemminger .name = "westwood" 24587270762SStephen Hemminger }; 24687270762SStephen Hemminger 24787270762SStephen Hemminger static int __init tcp_westwood_register(void) 24887270762SStephen Hemminger { 2496687e988SArnaldo Carvalho de Melo BUG_ON(sizeof(struct westwood) > ICSK_CA_PRIV_SIZE); 25087270762SStephen Hemminger return tcp_register_congestion_control(&tcp_westwood); 25187270762SStephen Hemminger } 25287270762SStephen Hemminger 25387270762SStephen Hemminger static void __exit tcp_westwood_unregister(void) 25487270762SStephen Hemminger { 25587270762SStephen Hemminger tcp_unregister_congestion_control(&tcp_westwood); 25687270762SStephen Hemminger } 25787270762SStephen Hemminger 25887270762SStephen Hemminger module_init(tcp_westwood_register); 25987270762SStephen Hemminger module_exit(tcp_westwood_unregister); 26087270762SStephen Hemminger 26187270762SStephen Hemminger MODULE_AUTHOR("Stephen Hemminger, Angelo Dell'Aera"); 26287270762SStephen Hemminger MODULE_LICENSE("GPL"); 26387270762SStephen Hemminger MODULE_DESCRIPTION("TCP Westwood+"); 264