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 CUBIC: Binary Increase Congestion control for TCP v2.3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Home page:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *      http://netsrv.csc.ncsu.edu/twiki/bin/view/Main/BIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * This is from the implementation of CUBIC TCP in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Sangtae Ha, Injong Rhee and Lisong Xu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *  "CUBIC: A New TCP-Friendly High-Speed TCP Variant"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *  in ACM SIGOPS Operating System Review, July 2008.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Available from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *  http://netsrv.csc.ncsu.edu/export/cubic_a_new_tcp_2008.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * CUBIC integrates a new slow start algorithm, called HyStart.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * The details of HyStart are presented in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *  Sangtae Ha and Injong Rhee,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *  "Taming the Elephants: New TCP Slow Start", NCSU TechReport 2008.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * Available from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *  http://netsrv.csc.ncsu.edu/export/hystart_techreport_2008.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * All testing results are available from:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * http://netsrv.csc.ncsu.edu/wiki/index.php/TCP_Testing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * Unless CUBIC is enabled and congestion window is large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * this behaves the same as the original Reno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/math64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define BICTCP_BETA_SCALE    1024	/* Scale factor beta calculation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 					 * max_cwnd = snd_cwnd * beta
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define	BICTCP_HZ		10	/* BIC HZ 2^10 = 1024 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) /* Two methods of hybrid slow start */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define HYSTART_ACK_TRAIN	0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define HYSTART_DELAY		0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) /* Number of delay samples for detecting the increase of delay */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define HYSTART_MIN_SAMPLES	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define HYSTART_DELAY_MIN	(4000U)	/* 4 ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #define HYSTART_DELAY_MAX	(16000U)	/* 16 ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define HYSTART_DELAY_THRESH(x)	clamp(x, HYSTART_DELAY_MIN, HYSTART_DELAY_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int fast_convergence __read_mostly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static int beta __read_mostly = 717;	/* = 717/1024 (BICTCP_BETA_SCALE) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static int initial_ssthresh __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static int bic_scale __read_mostly = 41;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static int tcp_friendliness __read_mostly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static int hystart __read_mostly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static int hystart_detect __read_mostly = HYSTART_ACK_TRAIN | HYSTART_DELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static int hystart_low_window __read_mostly = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static int hystart_ack_delta_us __read_mostly = 2000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static u32 cube_rtt_scale __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static u32 beta_scale __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static u64 cube_factor __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) /* Note parameters that are used for precomputing scale factors are read-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) module_param(fast_convergence, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) module_param(beta, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) MODULE_PARM_DESC(beta, "beta for multiplicative increase");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) module_param(initial_ssthresh, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) module_param(bic_scale, int, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) MODULE_PARM_DESC(bic_scale, "scale (scaled by 1024) value for bic function (bic_scale/1024)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) module_param(tcp_friendliness, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) module_param(hystart, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) MODULE_PARM_DESC(hystart, "turn on/off hybrid slow start algorithm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) module_param(hystart_detect, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) MODULE_PARM_DESC(hystart_detect, "hybrid slow start detection mechanisms"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		 " 1: packet-train 2: delay 3: both packet-train and delay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) module_param(hystart_low_window, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) MODULE_PARM_DESC(hystart_low_window, "lower bound cwnd for hybrid slow start");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) module_param(hystart_ack_delta_us, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) MODULE_PARM_DESC(hystart_ack_delta_us, "spacing between ack's indicating train (usecs)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) /* BIC TCP Parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) struct bictcp {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	u32	cnt;		/* increase cwnd by 1 after ACKs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	u32	last_max_cwnd;	/* last maximum snd_cwnd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	u32	last_cwnd;	/* the last snd_cwnd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	u32	last_time;	/* time when updated last_cwnd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	u32	bic_origin_point;/* origin point of bic function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	u32	bic_K;		/* time to origin point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 				   from the beginning of the current epoch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	u32	delay_min;	/* min delay (usec) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	u32	epoch_start;	/* beginning of an epoch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	u32	ack_cnt;	/* number of acks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	u32	tcp_cwnd;	/* estimated tcp cwnd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	u16	unused;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	u8	sample_cnt;	/* number of samples to decide curr_rtt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	u8	found;		/* the exit point is found? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	u32	round_start;	/* beginning of each round */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	u32	end_seq;	/* end_seq of the round */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	u32	last_ack;	/* last time when the ACK spacing is close */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	u32	curr_rtt;	/* the minimum rtt of current round */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static inline void bictcp_reset(struct bictcp *ca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	ca->cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	ca->last_max_cwnd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	ca->last_cwnd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	ca->last_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	ca->bic_origin_point = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	ca->bic_K = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	ca->delay_min = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	ca->epoch_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	ca->ack_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	ca->tcp_cwnd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	ca->found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static inline u32 bictcp_clock_us(const struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return tcp_sk(sk)->tcp_mstamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static inline void bictcp_hystart_reset(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct bictcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	ca->round_start = ca->last_ack = bictcp_clock_us(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	ca->end_seq = tp->snd_nxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ca->curr_rtt = ~0U;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	ca->sample_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static void bictcp_init(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	struct bictcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	bictcp_reset(ca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (hystart)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		bictcp_hystart_reset(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (!hystart && initial_ssthresh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static void bictcp_cwnd_event(struct sock *sk, enum tcp_ca_event event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (event == CA_EVENT_TX_START) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		struct bictcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		u32 now = tcp_jiffies32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		s32 delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		delta = now - tcp_sk(sk)->lsndtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		/* We were application limited (idle) for a while.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		 * Shift epoch_start to keep cwnd growth to cubic curve.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		if (ca->epoch_start && delta > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			ca->epoch_start += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			if (after(ca->epoch_start, now))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				ca->epoch_start = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* calculate the cubic root of x using a table lookup followed by one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  * Newton-Raphson iteration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * Avg err ~= 0.195%
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static u32 cubic_root(u64 a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	u32 x, b, shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	 * cbrt(x) MSB values for x MSB values in [0..63].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * Precomputed then refined by hand - Willy Tarreau
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 * For x in [0..63],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	 *   v = cbrt(x << 18) - 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	 *   cbrt(x) = (v[x] + 10) >> 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	static const u8 v[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		/* 0x00 */    0,   54,   54,   54,  118,  118,  118,  118,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		/* 0x08 */  123,  129,  134,  138,  143,  147,  151,  156,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		/* 0x10 */  157,  161,  164,  168,  170,  173,  176,  179,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		/* 0x18 */  181,  185,  187,  190,  192,  194,  197,  199,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		/* 0x20 */  200,  202,  204,  206,  209,  211,  213,  215,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		/* 0x28 */  217,  219,  221,  222,  224,  225,  227,  229,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		/* 0x30 */  231,  232,  234,  236,  237,  239,  240,  242,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		/* 0x38 */  244,  245,  246,  248,  250,  251,  252,  254,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	b = fls64(a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (b < 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		/* a in [0..63] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return ((u32)v[(u32)a] + 35) >> 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	b = ((b * 84) >> 8) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	shift = (a >> (b * 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 * Newton-Raphson iteration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 *                         2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * x    = ( 2 * x  +  a / x  ) / 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 *  k+1          k         k
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	x = (2 * x + (u32)div64_u64(a, (u64)x * (u64)(x - 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	x = ((x * 341) >> 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * Compute congestion window to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static inline void bictcp_update(struct bictcp *ca, u32 cwnd, u32 acked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	u32 delta, bic_target, max_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	u64 offs, t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	ca->ack_cnt += acked;	/* count the number of ACKed packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (ca->last_cwnd == cwnd &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	    (s32)(tcp_jiffies32 - ca->last_time) <= HZ / 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	/* The CUBIC function can update ca->cnt at most once per jiffy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	 * On all cwnd reduction events, ca->epoch_start is set to 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 * which will force a recalculation of ca->cnt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (ca->epoch_start && tcp_jiffies32 == ca->last_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		goto tcp_friendliness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	ca->last_cwnd = cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	ca->last_time = tcp_jiffies32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (ca->epoch_start == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		ca->epoch_start = tcp_jiffies32;	/* record beginning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		ca->ack_cnt = acked;			/* start counting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		ca->tcp_cwnd = cwnd;			/* syn with cubic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		if (ca->last_max_cwnd <= cwnd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			ca->bic_K = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			ca->bic_origin_point = cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			/* Compute new K based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			 * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			ca->bic_K = cubic_root(cube_factor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 					       * (ca->last_max_cwnd - cwnd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			ca->bic_origin_point = ca->last_max_cwnd;
^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) 	/* cubic function - calc*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	/* calculate c * time^3 / rtt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 *  while considering overflow in calculation of time^3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 * (so time^3 is done by using 64 bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 * and without the support of division of 64bit numbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 * (so all divisions are done by using 32 bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	 *  also NOTE the unit of those veriables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	 *	  time  = (t - K) / 2^bictcp_HZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 *	  c = bic_scale >> 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 * rtt  = (srtt >> 3) / HZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	 * !!! The following code does not have overflow problems,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	 * if the cwnd < 1 million packets !!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	t = (s32)(tcp_jiffies32 - ca->epoch_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	t += usecs_to_jiffies(ca->delay_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	/* change the unit from HZ to bictcp_HZ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	t <<= BICTCP_HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	do_div(t, HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	if (t < ca->bic_K)		/* t - K */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		offs = ca->bic_K - t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		offs = t - ca->bic_K;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	/* c/rtt * (t-K)^3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	delta = (cube_rtt_scale * offs * offs * offs) >> (10+3*BICTCP_HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (t < ca->bic_K)                            /* below origin*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		bic_target = ca->bic_origin_point - delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	else                                          /* above origin*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		bic_target = ca->bic_origin_point + delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	/* cubic function - calc bictcp_cnt*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (bic_target > cwnd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		ca->cnt = cwnd / (bic_target - cwnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		ca->cnt = 100 * cwnd;              /* very small increment*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	 * The initial growth of cubic function may be too conservative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	 * when the available bandwidth is still unknown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	if (ca->last_max_cwnd == 0 && ca->cnt > 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		ca->cnt = 20;	/* increase cwnd 5% per RTT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) tcp_friendliness:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	/* TCP Friendly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (tcp_friendliness) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		u32 scale = beta_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		delta = (cwnd * scale) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		while (ca->ack_cnt > delta) {		/* update tcp cwnd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			ca->ack_cnt -= delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			ca->tcp_cwnd++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		if (ca->tcp_cwnd > cwnd) {	/* if bic is slower than tcp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			delta = ca->tcp_cwnd - cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			max_cnt = cwnd / delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			if (ca->cnt > max_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 				ca->cnt = max_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	/* The maximum rate of cwnd increase CUBIC allows is 1 packet per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	 * 2 packets ACKed, meaning cwnd grows at 1.5x per RTT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	ca->cnt = max(ca->cnt, 2U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	struct bictcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (!tcp_is_cwnd_limited(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (tcp_in_slow_start(tp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		acked = tcp_slow_start(tp, acked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		if (!acked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	bictcp_update(ca, tp->snd_cwnd, acked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	tcp_cong_avoid_ai(tp, ca->cnt, acked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static u32 bictcp_recalc_ssthresh(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	const struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	struct bictcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	ca->epoch_start = 0;	/* end of epoch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	/* Wmax and fast convergence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			/ (2 * BICTCP_BETA_SCALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		ca->last_max_cwnd = tp->snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static void bictcp_state(struct sock *sk, u8 new_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (new_state == TCP_CA_Loss) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		bictcp_reset(inet_csk_ca(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		bictcp_hystart_reset(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /* Account for TSO/GRO delays.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  * Otherwise short RTT flows could get too small ssthresh, since during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  * slow start we begin with small TSO packets and ca->delay_min would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  * not account for long aggregation delay when TSO packets get bigger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)  * Ideally even with a very small RTT we would like to have at least one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)  * TSO packet being sent and received by GRO, and another one in qdisc layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)  * We apply another 100% factor because @rate is doubled at this point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  * We cap the cushion to 1ms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static u32 hystart_ack_delay(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	unsigned long rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	rate = READ_ONCE(sk->sk_pacing_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (!rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	return min_t(u64, USEC_PER_MSEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		     div64_ul((u64)GSO_MAX_SIZE * 4 * USEC_PER_SEC, rate));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static void hystart_update(struct sock *sk, u32 delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	struct bictcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	u32 threshold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (after(tp->snd_una, ca->end_seq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		bictcp_hystart_reset(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (hystart_detect & HYSTART_ACK_TRAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		u32 now = bictcp_clock_us(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		/* first detection parameter - ack-train detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		if ((s32)(now - ca->last_ack) <= hystart_ack_delta_us) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			ca->last_ack = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			threshold = ca->delay_min + hystart_ack_delay(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			/* Hystart ack train triggers if we get ack past
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			 * ca->delay_min/2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			 * Pacing might have delayed packets up to RTT/2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			 * during slow start.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			if (sk->sk_pacing_status == SK_PACING_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 				threshold >>= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			if ((s32)(now - ca->round_start) > threshold) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 				ca->found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 				pr_debug("hystart_ack_train (%u > %u) delay_min %u (+ ack_delay %u) cwnd %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 					 now - ca->round_start, threshold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 					 ca->delay_min, hystart_ack_delay(sk), tp->snd_cwnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 				NET_INC_STATS(sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 					      LINUX_MIB_TCPHYSTARTTRAINDETECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 				NET_ADD_STATS(sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 					      LINUX_MIB_TCPHYSTARTTRAINCWND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 					      tp->snd_cwnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 				tp->snd_ssthresh = tp->snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (hystart_detect & HYSTART_DELAY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		/* obtain the minimum delay of more than sampling packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		if (ca->curr_rtt > delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			ca->curr_rtt = delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		if (ca->sample_cnt < HYSTART_MIN_SAMPLES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			ca->sample_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			if (ca->curr_rtt > ca->delay_min +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			    HYSTART_DELAY_THRESH(ca->delay_min >> 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 				ca->found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 				NET_INC_STATS(sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 					      LINUX_MIB_TCPHYSTARTDELAYDETECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 				NET_ADD_STATS(sock_net(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 					      LINUX_MIB_TCPHYSTARTDELAYCWND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 					      tp->snd_cwnd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 				tp->snd_ssthresh = tp->snd_cwnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static void bictcp_acked(struct sock *sk, const struct ack_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	const struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	struct bictcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	u32 delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	/* Some calls are for duplicates without timetamps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	if (sample->rtt_us < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	/* Discard delay samples right after fast recovery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	if (ca->epoch_start && (s32)(tcp_jiffies32 - ca->epoch_start) < HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	delay = sample->rtt_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	if (delay == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		delay = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	/* first time call or link delay decreases */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	if (ca->delay_min == 0 || ca->delay_min > delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		ca->delay_min = delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	/* hystart triggers when cwnd is larger than some threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (!ca->found && tcp_in_slow_start(tp) && hystart &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	    tp->snd_cwnd >= hystart_low_window)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		hystart_update(sk, delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static struct tcp_congestion_ops cubictcp __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	.init		= bictcp_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	.ssthresh	= bictcp_recalc_ssthresh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	.cong_avoid	= bictcp_cong_avoid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	.set_state	= bictcp_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	.undo_cwnd	= tcp_reno_undo_cwnd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	.cwnd_event	= bictcp_cwnd_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	.pkts_acked     = bictcp_acked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	.name		= "cubic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) static int __init cubictcp_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	/* Precompute a bunch of the scaling factors that are used per-packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	 * based on SRTT of 100ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	beta_scale = 8*(BICTCP_BETA_SCALE+beta) / 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		/ (BICTCP_BETA_SCALE - beta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	cube_rtt_scale = (bic_scale * 10);	/* 1024*c/rtt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	/* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	 *  so K = cubic_root( (wmax-cwnd)*rtt/c )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	 * the unit of K is bictcp_HZ=2^10, not HZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	 *  c = bic_scale >> 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	 *  rtt = 100ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	 * the following code has been designed and tested for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	 * cwnd < 1 million packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	 * RTT < 100 seconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	 * HZ < 1,000,00  (corresponding to 10 nano-second)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	/* 1/c * 2^2*bictcp_HZ * srtt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	cube_factor = 1ull << (10+3*BICTCP_HZ); /* 2^40 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	/* divide by bic_scale and by constant Srtt (100ms) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	do_div(cube_factor, bic_scale * 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	return tcp_register_congestion_control(&cubictcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) static void __exit cubictcp_unregister(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	tcp_unregister_congestion_control(&cubictcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) module_init(cubictcp_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) module_exit(cubictcp_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) MODULE_AUTHOR("Sangtae Ha, Stephen Hemminger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) MODULE_DESCRIPTION("CUBIC TCP");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) MODULE_VERSION("2.3");