Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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