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)  * H-TCP congestion control. The algorithm is detailed in:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * R.N.Shorten, D.J.Leith:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *   "H-TCP: TCP for high-speed and long-distance networks"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *   Proc. PFLDnet, Argonne, 2004.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * https://www.hamilton.ie/net/htcp3.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #define ALPHA_BASE	(1<<7)	/* 1.0 with shift << 7 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define BETA_MIN	(1<<6)	/* 0.5 with shift << 7 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define BETA_MAX	102	/* 0.8 with shift << 7 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static int use_rtt_scaling __read_mostly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) module_param(use_rtt_scaling, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) MODULE_PARM_DESC(use_rtt_scaling, "turn on/off RTT scaling");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static int use_bandwidth_switch __read_mostly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) module_param(use_bandwidth_switch, int, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) MODULE_PARM_DESC(use_bandwidth_switch, "turn on/off bandwidth switcher");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct htcp {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	u32	alpha;		/* Fixed point arith, << 7 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	u8	beta;           /* Fixed point arith, << 7 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	u8	modeswitch;	/* Delay modeswitch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 				   until we had at least one congestion event */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	u16	pkts_acked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	u32	packetcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	u32	minRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	u32	maxRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	u32	last_cong;	/* Time since last congestion event end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	u32	undo_last_cong;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	u32	undo_maxRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	u32	undo_old_maxB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	/* Bandwidth estimation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	u32	minB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	u32	maxB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	u32	old_maxB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	u32	Bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	u32	lasttime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static inline u32 htcp_cong_time(const struct htcp *ca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	return jiffies - ca->last_cong;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static inline u32 htcp_ccount(const struct htcp *ca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	return htcp_cong_time(ca) / ca->minRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static inline void htcp_reset(struct htcp *ca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	ca->undo_last_cong = ca->last_cong;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	ca->undo_maxRTT = ca->maxRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	ca->undo_old_maxB = ca->old_maxB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	ca->last_cong = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static u32 htcp_cwnd_undo(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	struct htcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (ca->undo_last_cong) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		ca->last_cong = ca->undo_last_cong;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		ca->maxRTT = ca->undo_maxRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		ca->old_maxB = ca->undo_old_maxB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		ca->undo_last_cong = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return tcp_reno_undo_cwnd(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static inline void measure_rtt(struct sock *sk, u32 srtt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	const struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct htcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	/* keep track of minimum RTT seen so far, minRTT is zero at first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (ca->minRTT > srtt || !ca->minRTT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		ca->minRTT = srtt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	/* max RTT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (icsk->icsk_ca_state == TCP_CA_Open) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		if (ca->maxRTT < ca->minRTT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			ca->maxRTT = ca->minRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		if (ca->maxRTT < srtt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		    srtt <= ca->maxRTT + msecs_to_jiffies(20))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			ca->maxRTT = srtt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void measure_achieved_throughput(struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 					const struct ack_sample *sample)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	const struct inet_connection_sock *icsk = inet_csk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	const struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct htcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	u32 now = tcp_jiffies32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (icsk->icsk_ca_state == TCP_CA_Open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		ca->pkts_acked = sample->pkts_acked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (sample->rtt_us > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		measure_rtt(sk, usecs_to_jiffies(sample->rtt_us));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (!use_bandwidth_switch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	/* achieved throughput calculations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (!((1 << icsk->icsk_ca_state) & (TCPF_CA_Open | TCPF_CA_Disorder))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		ca->packetcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		ca->lasttime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return;
^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) 	ca->packetcount += sample->pkts_acked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (ca->packetcount >= tp->snd_cwnd - (ca->alpha >> 7 ? : 1) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	    now - ca->lasttime >= ca->minRTT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	    ca->minRTT > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		__u32 cur_Bi = ca->packetcount * HZ / (now - ca->lasttime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		if (htcp_ccount(ca) <= 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			/* just after backoff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			ca->minB = ca->maxB = ca->Bi = cur_Bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			ca->Bi = (3 * ca->Bi + cur_Bi) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			if (ca->Bi > ca->maxB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 				ca->maxB = ca->Bi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			if (ca->minB > ca->maxB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 				ca->minB = ca->maxB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		ca->packetcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		ca->lasttime = now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^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) static inline void htcp_beta_update(struct htcp *ca, u32 minRTT, u32 maxRTT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (use_bandwidth_switch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		u32 maxB = ca->maxB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		u32 old_maxB = ca->old_maxB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		ca->old_maxB = ca->maxB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (!between(5 * maxB, 4 * old_maxB, 6 * old_maxB)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			ca->beta = BETA_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			ca->modeswitch = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (ca->modeswitch && minRTT > msecs_to_jiffies(10) && maxRTT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		ca->beta = (minRTT << 7) / maxRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		if (ca->beta < BETA_MIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			ca->beta = BETA_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		else if (ca->beta > BETA_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			ca->beta = BETA_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		ca->beta = BETA_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		ca->modeswitch = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static inline void htcp_alpha_update(struct htcp *ca)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	u32 minRTT = ca->minRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	u32 factor = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	u32 diff = htcp_cong_time(ca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (diff > HZ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		diff -= HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		factor = 1 + (10 * diff + ((diff / 2) * (diff / 2) / HZ)) / HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (use_rtt_scaling && minRTT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		u32 scale = (HZ << 3) / (10 * minRTT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		/* clamping ratio to interval [0.5,10]<<3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		scale = min(max(scale, 1U << 2), 10U << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		factor = (factor << 3) / scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		if (!factor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			factor = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	ca->alpha = 2 * factor * ((1 << 7) - ca->beta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (!ca->alpha)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		ca->alpha = ALPHA_BASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  * After we have the rtt data to calculate beta, we'd still prefer to wait one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  * rtt before we adjust our beta to ensure we are working from a consistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  * data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  * This function should be called when we hit a congestion event since only at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * that point do we really have a real sense of maxRTT (the queues en route
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  * were getting just too full now).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static void htcp_param_update(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	struct htcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	u32 minRTT = ca->minRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	u32 maxRTT = ca->maxRTT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	htcp_beta_update(ca, minRTT, maxRTT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	htcp_alpha_update(ca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	/* add slowly fading memory for maxRTT to accommodate routing changes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (minRTT > 0 && maxRTT > minRTT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		ca->maxRTT = minRTT + ((maxRTT - minRTT) * 95) / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static u32 htcp_recalc_ssthresh(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	const struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	const struct htcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	htcp_param_update(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	return max((tp->snd_cwnd * ca->beta) >> 7, 2U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static void htcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct tcp_sock *tp = tcp_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct htcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (!tcp_is_cwnd_limited(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (tcp_in_slow_start(tp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		tcp_slow_start(tp, acked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		/* In dangerous area, increase slowly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		 * In theory this is tp->snd_cwnd += alpha / tp->snd_cwnd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		if ((tp->snd_cwnd_cnt * ca->alpha)>>7 >= tp->snd_cwnd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			if (tp->snd_cwnd < tp->snd_cwnd_clamp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				tp->snd_cwnd++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			tp->snd_cwnd_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			htcp_alpha_update(ca);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			tp->snd_cwnd_cnt += ca->pkts_acked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		ca->pkts_acked = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static void htcp_init(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	struct htcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	memset(ca, 0, sizeof(struct htcp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	ca->alpha = ALPHA_BASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	ca->beta = BETA_MIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	ca->pkts_acked = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	ca->last_cong = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static void htcp_state(struct sock *sk, u8 new_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	switch (new_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	case TCP_CA_Open:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			struct htcp *ca = inet_csk_ca(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			if (ca->undo_last_cong) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 				ca->last_cong = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				ca->undo_last_cong = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	case TCP_CA_CWR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	case TCP_CA_Recovery:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	case TCP_CA_Loss:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		htcp_reset(inet_csk_ca(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static struct tcp_congestion_ops htcp __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	.init		= htcp_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	.ssthresh	= htcp_recalc_ssthresh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	.cong_avoid	= htcp_cong_avoid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	.set_state	= htcp_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	.undo_cwnd	= htcp_cwnd_undo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	.pkts_acked	= measure_achieved_throughput,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	.name		= "htcp",
^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) static int __init htcp_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	BUILD_BUG_ON(sizeof(struct htcp) > ICSK_CA_PRIV_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	BUILD_BUG_ON(BETA_MIN >= BETA_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	return tcp_register_congestion_control(&htcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) static void __exit htcp_unregister(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	tcp_unregister_congestion_control(&htcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) module_init(htcp_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) module_exit(htcp_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) MODULE_AUTHOR("Baruch Even");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) MODULE_DESCRIPTION("H-TCP");