109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a7868ea6SBaruch Even /*
3a7868ea6SBaruch Even * H-TCP congestion control. The algorithm is detailed in:
4a7868ea6SBaruch Even * R.N.Shorten, D.J.Leith:
5a7868ea6SBaruch Even * "H-TCP: TCP for high-speed and long-distance networks"
6a7868ea6SBaruch Even * Proc. PFLDnet, Argonne, 2004.
77a6498ebSAlexander A. Klimov * https://www.hamilton.ie/net/htcp3.pdf
8a7868ea6SBaruch Even */
9a7868ea6SBaruch Even
10a7868ea6SBaruch Even #include <linux/mm.h>
11a7868ea6SBaruch Even #include <linux/module.h>
12a7868ea6SBaruch Even #include <net/tcp.h>
13a7868ea6SBaruch Even
14a7868ea6SBaruch Even #define ALPHA_BASE (1<<7) /* 1.0 with shift << 7 */
15a7868ea6SBaruch Even #define BETA_MIN (1<<6) /* 0.5 with shift << 7 */
16a7868ea6SBaruch Even #define BETA_MAX 102 /* 0.8 with shift << 7 */
17a7868ea6SBaruch Even
189121c777SStephen Hemminger static int use_rtt_scaling __read_mostly = 1;
19a7868ea6SBaruch Even module_param(use_rtt_scaling, int, 0644);
20a7868ea6SBaruch Even MODULE_PARM_DESC(use_rtt_scaling, "turn on/off RTT scaling");
21a7868ea6SBaruch Even
229121c777SStephen Hemminger static int use_bandwidth_switch __read_mostly = 1;
23a7868ea6SBaruch Even module_param(use_bandwidth_switch, int, 0644);
24a7868ea6SBaruch Even MODULE_PARM_DESC(use_bandwidth_switch, "turn on/off bandwidth switcher");
25a7868ea6SBaruch Even
26a7868ea6SBaruch Even struct htcp {
272a272f98SGavin McCullagh u32 alpha; /* Fixed point arith, << 7 */
28a7868ea6SBaruch Even u8 beta; /* Fixed point arith, << 7 */
299121c777SStephen Hemminger u8 modeswitch; /* Delay modeswitch
309121c777SStephen Hemminger until we had at least one congestion event */
310bc6d90bSBaruch Even u16 pkts_acked;
320bc6d90bSBaruch Even u32 packetcount;
33a7868ea6SBaruch Even u32 minRTT;
34a7868ea6SBaruch Even u32 maxRTT;
352404043aSDavid S. Miller u32 last_cong; /* Time since last congestion event end */
362404043aSDavid S. Miller u32 undo_last_cong;
37a7868ea6SBaruch Even
38a7868ea6SBaruch Even u32 undo_maxRTT;
39a7868ea6SBaruch Even u32 undo_old_maxB;
40a7868ea6SBaruch Even
41a7868ea6SBaruch Even /* Bandwidth estimation */
42a7868ea6SBaruch Even u32 minB;
43a7868ea6SBaruch Even u32 maxB;
44a7868ea6SBaruch Even u32 old_maxB;
45a7868ea6SBaruch Even u32 Bi;
46a7868ea6SBaruch Even u32 lasttime;
47a7868ea6SBaruch Even };
48a7868ea6SBaruch Even
htcp_cong_time(const struct htcp * ca)499121c777SStephen Hemminger static inline u32 htcp_cong_time(const struct htcp *ca)
5050bf3e22SBaruch Even {
5150bf3e22SBaruch Even return jiffies - ca->last_cong;
5250bf3e22SBaruch Even }
5350bf3e22SBaruch Even
htcp_ccount(const struct htcp * ca)549121c777SStephen Hemminger static inline u32 htcp_ccount(const struct htcp *ca)
5550bf3e22SBaruch Even {
5650bf3e22SBaruch Even return htcp_cong_time(ca) / ca->minRTT;
5750bf3e22SBaruch Even }
5850bf3e22SBaruch Even
htcp_reset(struct htcp * ca)59a7868ea6SBaruch Even static inline void htcp_reset(struct htcp *ca)
60a7868ea6SBaruch Even {
6150bf3e22SBaruch Even ca->undo_last_cong = ca->last_cong;
62a7868ea6SBaruch Even ca->undo_maxRTT = ca->maxRTT;
63a7868ea6SBaruch Even ca->undo_old_maxB = ca->old_maxB;
64a7868ea6SBaruch Even
6550bf3e22SBaruch Even ca->last_cong = jiffies;
66a7868ea6SBaruch Even }
67a7868ea6SBaruch Even
htcp_cwnd_undo(struct sock * sk)686687e988SArnaldo Carvalho de Melo static u32 htcp_cwnd_undo(struct sock *sk)
69a7868ea6SBaruch Even {
706687e988SArnaldo Carvalho de Melo struct htcp *ca = inet_csk_ca(sk);
719121c777SStephen Hemminger
728f65b535SDoug Leith if (ca->undo_last_cong) {
7350bf3e22SBaruch Even ca->last_cong = ca->undo_last_cong;
74a7868ea6SBaruch Even ca->maxRTT = ca->undo_maxRTT;
75a7868ea6SBaruch Even ca->old_maxB = ca->undo_old_maxB;
768f65b535SDoug Leith ca->undo_last_cong = 0;
778f65b535SDoug Leith }
789121c777SStephen Hemminger
794faf7839SYuchung Cheng return tcp_reno_undo_cwnd(sk);
80a7868ea6SBaruch Even }
81a7868ea6SBaruch Even
measure_rtt(struct sock * sk,u32 srtt)82113bbbd8SStephen Hemminger static inline void measure_rtt(struct sock *sk, u32 srtt)
83a7868ea6SBaruch Even {
846687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk);
856687e988SArnaldo Carvalho de Melo struct htcp *ca = inet_csk_ca(sk);
86a7868ea6SBaruch Even
87a7868ea6SBaruch Even /* keep track of minimum RTT seen so far, minRTT is zero at first */
88a7868ea6SBaruch Even if (ca->minRTT > srtt || !ca->minRTT)
89a7868ea6SBaruch Even ca->minRTT = srtt;
90a7868ea6SBaruch Even
91a7868ea6SBaruch Even /* max RTT */
92f34d1955SStephen Hemminger if (icsk->icsk_ca_state == TCP_CA_Open) {
93a7868ea6SBaruch Even if (ca->maxRTT < ca->minRTT)
94a7868ea6SBaruch Even ca->maxRTT = ca->minRTT;
959d4fb27dSJoe Perches if (ca->maxRTT < srtt &&
969d4fb27dSJoe Perches srtt <= ca->maxRTT + msecs_to_jiffies(20))
97a7868ea6SBaruch Even ca->maxRTT = srtt;
98a7868ea6SBaruch Even }
99a7868ea6SBaruch Even }
100a7868ea6SBaruch Even
measure_achieved_throughput(struct sock * sk,const struct ack_sample * sample)101688d1945Sstephen hemminger static void measure_achieved_throughput(struct sock *sk,
102756ee172SLawrence Brakmo const struct ack_sample *sample)
103a7868ea6SBaruch Even {
1046687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk);
1056687e988SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk);
1066687e988SArnaldo Carvalho de Melo struct htcp *ca = inet_csk_ca(sk);
107ac9517fcSEric Dumazet u32 now = tcp_jiffies32;
108a7868ea6SBaruch Even
1090bc6d90bSBaruch Even if (icsk->icsk_ca_state == TCP_CA_Open)
110756ee172SLawrence Brakmo ca->pkts_acked = sample->pkts_acked;
1110bc6d90bSBaruch Even
112756ee172SLawrence Brakmo if (sample->rtt_us > 0)
113756ee172SLawrence Brakmo measure_rtt(sk, usecs_to_jiffies(sample->rtt_us));
114113bbbd8SStephen Hemminger
1150bc6d90bSBaruch Even if (!use_bandwidth_switch)
1160bc6d90bSBaruch Even return;
1170bc6d90bSBaruch Even
118a7868ea6SBaruch Even /* achieved throughput calculations */
119571a5dd8SIlpo Järvinen if (!((1 << icsk->icsk_ca_state) & (TCPF_CA_Open | TCPF_CA_Disorder))) {
120a7868ea6SBaruch Even ca->packetcount = 0;
121a7868ea6SBaruch Even ca->lasttime = now;
122a7868ea6SBaruch Even return;
123a7868ea6SBaruch Even }
124a7868ea6SBaruch Even
125756ee172SLawrence Brakmo ca->packetcount += sample->pkts_acked;
126a7868ea6SBaruch Even
12740570375SEric Dumazet if (ca->packetcount >= tcp_snd_cwnd(tp) - (ca->alpha >> 7 ? : 1) &&
1289d4fb27dSJoe Perches now - ca->lasttime >= ca->minRTT &&
1299d4fb27dSJoe Perches ca->minRTT > 0) {
130a7868ea6SBaruch Even __u32 cur_Bi = ca->packetcount * HZ / (now - ca->lasttime);
1319121c777SStephen Hemminger
13250bf3e22SBaruch Even if (htcp_ccount(ca) <= 3) {
133a7868ea6SBaruch Even /* just after backoff */
134a7868ea6SBaruch Even ca->minB = ca->maxB = ca->Bi = cur_Bi;
135a7868ea6SBaruch Even } else {
136a7868ea6SBaruch Even ca->Bi = (3 * ca->Bi + cur_Bi) / 4;
137a7868ea6SBaruch Even if (ca->Bi > ca->maxB)
138a7868ea6SBaruch Even ca->maxB = ca->Bi;
139a7868ea6SBaruch Even if (ca->minB > ca->maxB)
140a7868ea6SBaruch Even ca->minB = ca->maxB;
141a7868ea6SBaruch Even }
142a7868ea6SBaruch Even ca->packetcount = 0;
143a7868ea6SBaruch Even ca->lasttime = now;
144a7868ea6SBaruch Even }
145a7868ea6SBaruch Even }
146a7868ea6SBaruch Even
htcp_beta_update(struct htcp * ca,u32 minRTT,u32 maxRTT)147a7868ea6SBaruch Even static inline void htcp_beta_update(struct htcp *ca, u32 minRTT, u32 maxRTT)
148a7868ea6SBaruch Even {
149a7868ea6SBaruch Even if (use_bandwidth_switch) {
150a7868ea6SBaruch Even u32 maxB = ca->maxB;
151a7868ea6SBaruch Even u32 old_maxB = ca->old_maxB;
152a7868ea6SBaruch Even
153688d1945Sstephen hemminger ca->old_maxB = ca->maxB;
154a7868ea6SBaruch Even if (!between(5 * maxB, 4 * old_maxB, 6 * old_maxB)) {
155a7868ea6SBaruch Even ca->beta = BETA_MIN;
156a7868ea6SBaruch Even ca->modeswitch = 0;
157a7868ea6SBaruch Even return;
158a7868ea6SBaruch Even }
159a7868ea6SBaruch Even }
160a7868ea6SBaruch Even
161c33ad6e4SBaruch Even if (ca->modeswitch && minRTT > msecs_to_jiffies(10) && maxRTT) {
162a7868ea6SBaruch Even ca->beta = (minRTT << 7) / maxRTT;
163a7868ea6SBaruch Even if (ca->beta < BETA_MIN)
164a7868ea6SBaruch Even ca->beta = BETA_MIN;
165a7868ea6SBaruch Even else if (ca->beta > BETA_MAX)
166a7868ea6SBaruch Even ca->beta = BETA_MAX;
167a7868ea6SBaruch Even } else {
168a7868ea6SBaruch Even ca->beta = BETA_MIN;
169a7868ea6SBaruch Even ca->modeswitch = 1;
170a7868ea6SBaruch Even }
171a7868ea6SBaruch Even }
172a7868ea6SBaruch Even
htcp_alpha_update(struct htcp * ca)173a7868ea6SBaruch Even static inline void htcp_alpha_update(struct htcp *ca)
174a7868ea6SBaruch Even {
175a7868ea6SBaruch Even u32 minRTT = ca->minRTT;
176a7868ea6SBaruch Even u32 factor = 1;
17750bf3e22SBaruch Even u32 diff = htcp_cong_time(ca);
178a7868ea6SBaruch Even
179a7868ea6SBaruch Even if (diff > HZ) {
180a7868ea6SBaruch Even diff -= HZ;
181a7868ea6SBaruch Even factor = 1 + (10 * diff + ((diff / 2) * (diff / 2) / HZ)) / HZ;
182a7868ea6SBaruch Even }
183a7868ea6SBaruch Even
184a7868ea6SBaruch Even if (use_rtt_scaling && minRTT) {
185a7868ea6SBaruch Even u32 scale = (HZ << 3) / (10 * minRTT);
1869121c777SStephen Hemminger
1879121c777SStephen Hemminger /* clamping ratio to interval [0.5,10]<<3 */
188*871cdea0SChristophe JAILLET scale = clamp(scale, 1U << 2, 10U << 3);
189a7868ea6SBaruch Even factor = (factor << 3) / scale;
190a7868ea6SBaruch Even if (!factor)
191a7868ea6SBaruch Even factor = 1;
192a7868ea6SBaruch Even }
193a7868ea6SBaruch Even
194a7868ea6SBaruch Even ca->alpha = 2 * factor * ((1 << 7) - ca->beta);
195a7868ea6SBaruch Even if (!ca->alpha)
196a7868ea6SBaruch Even ca->alpha = ALPHA_BASE;
197a7868ea6SBaruch Even }
198a7868ea6SBaruch Even
1999121c777SStephen Hemminger /*
2009121c777SStephen Hemminger * After we have the rtt data to calculate beta, we'd still prefer to wait one
201a7868ea6SBaruch Even * rtt before we adjust our beta to ensure we are working from a consistent
202a7868ea6SBaruch Even * data.
203a7868ea6SBaruch Even *
204a7868ea6SBaruch Even * This function should be called when we hit a congestion event since only at
205a7868ea6SBaruch Even * that point do we really have a real sense of maxRTT (the queues en route
206a7868ea6SBaruch Even * were getting just too full now).
207a7868ea6SBaruch Even */
htcp_param_update(struct sock * sk)2086687e988SArnaldo Carvalho de Melo static void htcp_param_update(struct sock *sk)
209a7868ea6SBaruch Even {
2106687e988SArnaldo Carvalho de Melo struct htcp *ca = inet_csk_ca(sk);
211a7868ea6SBaruch Even u32 minRTT = ca->minRTT;
212a7868ea6SBaruch Even u32 maxRTT = ca->maxRTT;
213a7868ea6SBaruch Even
214a7868ea6SBaruch Even htcp_beta_update(ca, minRTT, maxRTT);
215a7868ea6SBaruch Even htcp_alpha_update(ca);
216a7868ea6SBaruch Even
2179121c777SStephen Hemminger /* add slowly fading memory for maxRTT to accommodate routing changes */
218a7868ea6SBaruch Even if (minRTT > 0 && maxRTT > minRTT)
219a7868ea6SBaruch Even ca->maxRTT = minRTT + ((maxRTT - minRTT) * 95) / 100;
220a7868ea6SBaruch Even }
221a7868ea6SBaruch Even
htcp_recalc_ssthresh(struct sock * sk)2226687e988SArnaldo Carvalho de Melo static u32 htcp_recalc_ssthresh(struct sock *sk)
223a7868ea6SBaruch Even {
2246687e988SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk);
2256687e988SArnaldo Carvalho de Melo const struct htcp *ca = inet_csk_ca(sk);
2269121c777SStephen Hemminger
2276687e988SArnaldo Carvalho de Melo htcp_param_update(sk);
22840570375SEric Dumazet return max((tcp_snd_cwnd(tp) * ca->beta) >> 7, 2U);
229a7868ea6SBaruch Even }
230a7868ea6SBaruch Even
htcp_cong_avoid(struct sock * sk,u32 ack,u32 acked)23124901551SEric Dumazet static void htcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
232a7868ea6SBaruch Even {
2336687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk);
2346687e988SArnaldo Carvalho de Melo struct htcp *ca = inet_csk_ca(sk);
235a7868ea6SBaruch Even
23624901551SEric Dumazet if (!tcp_is_cwnd_limited(sk))
237a7868ea6SBaruch Even return;
238a7868ea6SBaruch Even
239071d5080SYuchung Cheng if (tcp_in_slow_start(tp))
2409f9843a7SYuchung Cheng tcp_slow_start(tp, acked);
2417faffa1cSStephen Hemminger else {
242a7868ea6SBaruch Even /* In dangerous area, increase slowly.
243a7868ea6SBaruch Even * In theory this is tp->snd_cwnd += alpha / tp->snd_cwnd
244a7868ea6SBaruch Even */
24540570375SEric Dumazet if ((tp->snd_cwnd_cnt * ca->alpha)>>7 >= tcp_snd_cwnd(tp)) {
24640570375SEric Dumazet if (tcp_snd_cwnd(tp) < tp->snd_cwnd_clamp)
24740570375SEric Dumazet tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
248a7868ea6SBaruch Even tp->snd_cwnd_cnt = 0;
24950bf3e22SBaruch Even htcp_alpha_update(ca);
2500bc6d90bSBaruch Even } else
2510bc6d90bSBaruch Even tp->snd_cwnd_cnt += ca->pkts_acked;
2520bc6d90bSBaruch Even
2530bc6d90bSBaruch Even ca->pkts_acked = 1;
254a7868ea6SBaruch Even }
255a7868ea6SBaruch Even }
256a7868ea6SBaruch Even
htcp_init(struct sock * sk)2576687e988SArnaldo Carvalho de Melo static void htcp_init(struct sock *sk)
258a7868ea6SBaruch Even {
2596687e988SArnaldo Carvalho de Melo struct htcp *ca = inet_csk_ca(sk);
260a7868ea6SBaruch Even
261a7868ea6SBaruch Even memset(ca, 0, sizeof(struct htcp));
262a7868ea6SBaruch Even ca->alpha = ALPHA_BASE;
263a7868ea6SBaruch Even ca->beta = BETA_MIN;
2640bc6d90bSBaruch Even ca->pkts_acked = 1;
26550bf3e22SBaruch Even ca->last_cong = jiffies;
266a7868ea6SBaruch Even }
267a7868ea6SBaruch Even
htcp_state(struct sock * sk,u8 new_state)2686687e988SArnaldo Carvalho de Melo static void htcp_state(struct sock *sk, u8 new_state)
269a7868ea6SBaruch Even {
270a7868ea6SBaruch Even switch (new_state) {
27150bf3e22SBaruch Even case TCP_CA_Open:
27250bf3e22SBaruch Even {
27350bf3e22SBaruch Even struct htcp *ca = inet_csk_ca(sk);
274688d1945Sstephen hemminger
2758f65b535SDoug Leith if (ca->undo_last_cong) {
27650bf3e22SBaruch Even ca->last_cong = jiffies;
2778f65b535SDoug Leith ca->undo_last_cong = 0;
2788f65b535SDoug Leith }
27950bf3e22SBaruch Even }
28050bf3e22SBaruch Even break;
281a7868ea6SBaruch Even case TCP_CA_CWR:
282a7868ea6SBaruch Even case TCP_CA_Recovery:
283a7868ea6SBaruch Even case TCP_CA_Loss:
2846687e988SArnaldo Carvalho de Melo htcp_reset(inet_csk_ca(sk));
285a7868ea6SBaruch Even break;
286a7868ea6SBaruch Even }
287a7868ea6SBaruch Even }
288a7868ea6SBaruch Even
289a252bebeSStephen Hemminger static struct tcp_congestion_ops htcp __read_mostly = {
290a7868ea6SBaruch Even .init = htcp_init,
291a7868ea6SBaruch Even .ssthresh = htcp_recalc_ssthresh,
292a7868ea6SBaruch Even .cong_avoid = htcp_cong_avoid,
293a7868ea6SBaruch Even .set_state = htcp_state,
294a7868ea6SBaruch Even .undo_cwnd = htcp_cwnd_undo,
295a7868ea6SBaruch Even .pkts_acked = measure_achieved_throughput,
296a7868ea6SBaruch Even .owner = THIS_MODULE,
297a7868ea6SBaruch Even .name = "htcp",
298a7868ea6SBaruch Even };
299a7868ea6SBaruch Even
htcp_register(void)300a7868ea6SBaruch Even static int __init htcp_register(void)
301a7868ea6SBaruch Even {
30274975d40SAlexey Dobriyan BUILD_BUG_ON(sizeof(struct htcp) > ICSK_CA_PRIV_SIZE);
303a7868ea6SBaruch Even BUILD_BUG_ON(BETA_MIN >= BETA_MAX);
304a7868ea6SBaruch Even return tcp_register_congestion_control(&htcp);
305a7868ea6SBaruch Even }
306a7868ea6SBaruch Even
htcp_unregister(void)307a7868ea6SBaruch Even static void __exit htcp_unregister(void)
308a7868ea6SBaruch Even {
309a7868ea6SBaruch Even tcp_unregister_congestion_control(&htcp);
310a7868ea6SBaruch Even }
311a7868ea6SBaruch Even
312a7868ea6SBaruch Even module_init(htcp_register);
313a7868ea6SBaruch Even module_exit(htcp_unregister);
314a7868ea6SBaruch Even
315a7868ea6SBaruch Even MODULE_AUTHOR("Baruch Even");
316a7868ea6SBaruch Even MODULE_LICENSE("GPL");
317a7868ea6SBaruch Even MODULE_DESCRIPTION("H-TCP");
318