xref: /linux/net/ipv4/tcp_highspeed.c (revision 74975d40b16fd4bad24a2e2630dc7957d8cba013)
1a628d29bSJohn Heffner /*
2a628d29bSJohn Heffner  * Sally Floyd's High Speed TCP (RFC 3649) congestion control
3a628d29bSJohn Heffner  *
4a628d29bSJohn Heffner  * See http://www.icir.org/floyd/hstcp.html
5a628d29bSJohn Heffner  *
6a628d29bSJohn Heffner  * John Heffner <jheffner@psc.edu>
7a628d29bSJohn Heffner  */
8a628d29bSJohn Heffner 
9a628d29bSJohn Heffner #include <linux/module.h>
10a628d29bSJohn Heffner #include <net/tcp.h>
11a628d29bSJohn Heffner 
12a628d29bSJohn Heffner 
13a628d29bSJohn Heffner /* From AIMD tables from RFC 3649 appendix B,
14a628d29bSJohn Heffner  * with fixed-point MD scaled <<8.
15a628d29bSJohn Heffner  */
16a628d29bSJohn Heffner static const struct hstcp_aimd_val {
17a628d29bSJohn Heffner         unsigned int cwnd;
18a628d29bSJohn Heffner         unsigned int md;
19a628d29bSJohn Heffner } hstcp_aimd_vals[] = {
20a628d29bSJohn Heffner  {     38,  128, /*  0.50 */ },
21a628d29bSJohn Heffner  {    118,  112, /*  0.44 */ },
22a628d29bSJohn Heffner  {    221,  104, /*  0.41 */ },
23a628d29bSJohn Heffner  {    347,   98, /*  0.38 */ },
24a628d29bSJohn Heffner  {    495,   93, /*  0.37 */ },
25a628d29bSJohn Heffner  {    663,   89, /*  0.35 */ },
26a628d29bSJohn Heffner  {    851,   86, /*  0.34 */ },
27a628d29bSJohn Heffner  {   1058,   83, /*  0.33 */ },
28a628d29bSJohn Heffner  {   1284,   81, /*  0.32 */ },
29a628d29bSJohn Heffner  {   1529,   78, /*  0.31 */ },
30a628d29bSJohn Heffner  {   1793,   76, /*  0.30 */ },
31a628d29bSJohn Heffner  {   2076,   74, /*  0.29 */ },
32a628d29bSJohn Heffner  {   2378,   72, /*  0.28 */ },
33a628d29bSJohn Heffner  {   2699,   71, /*  0.28 */ },
34a628d29bSJohn Heffner  {   3039,   69, /*  0.27 */ },
35a628d29bSJohn Heffner  {   3399,   68, /*  0.27 */ },
36a628d29bSJohn Heffner  {   3778,   66, /*  0.26 */ },
37a628d29bSJohn Heffner  {   4177,   65, /*  0.26 */ },
38a628d29bSJohn Heffner  {   4596,   64, /*  0.25 */ },
39a628d29bSJohn Heffner  {   5036,   62, /*  0.25 */ },
40a628d29bSJohn Heffner  {   5497,   61, /*  0.24 */ },
41a628d29bSJohn Heffner  {   5979,   60, /*  0.24 */ },
42a628d29bSJohn Heffner  {   6483,   59, /*  0.23 */ },
43a628d29bSJohn Heffner  {   7009,   58, /*  0.23 */ },
44a628d29bSJohn Heffner  {   7558,   57, /*  0.22 */ },
45a628d29bSJohn Heffner  {   8130,   56, /*  0.22 */ },
46a628d29bSJohn Heffner  {   8726,   55, /*  0.22 */ },
47a628d29bSJohn Heffner  {   9346,   54, /*  0.21 */ },
48a628d29bSJohn Heffner  {   9991,   53, /*  0.21 */ },
49a628d29bSJohn Heffner  {  10661,   52, /*  0.21 */ },
50a628d29bSJohn Heffner  {  11358,   52, /*  0.20 */ },
51a628d29bSJohn Heffner  {  12082,   51, /*  0.20 */ },
52a628d29bSJohn Heffner  {  12834,   50, /*  0.20 */ },
53a628d29bSJohn Heffner  {  13614,   49, /*  0.19 */ },
54a628d29bSJohn Heffner  {  14424,   48, /*  0.19 */ },
55a628d29bSJohn Heffner  {  15265,   48, /*  0.19 */ },
56a628d29bSJohn Heffner  {  16137,   47, /*  0.19 */ },
57a628d29bSJohn Heffner  {  17042,   46, /*  0.18 */ },
58a628d29bSJohn Heffner  {  17981,   45, /*  0.18 */ },
59a628d29bSJohn Heffner  {  18955,   45, /*  0.18 */ },
60a628d29bSJohn Heffner  {  19965,   44, /*  0.17 */ },
61a628d29bSJohn Heffner  {  21013,   43, /*  0.17 */ },
62a628d29bSJohn Heffner  {  22101,   43, /*  0.17 */ },
63a628d29bSJohn Heffner  {  23230,   42, /*  0.17 */ },
64a628d29bSJohn Heffner  {  24402,   41, /*  0.16 */ },
65a628d29bSJohn Heffner  {  25618,   41, /*  0.16 */ },
66a628d29bSJohn Heffner  {  26881,   40, /*  0.16 */ },
67a628d29bSJohn Heffner  {  28193,   39, /*  0.16 */ },
68a628d29bSJohn Heffner  {  29557,   39, /*  0.15 */ },
69a628d29bSJohn Heffner  {  30975,   38, /*  0.15 */ },
70a628d29bSJohn Heffner  {  32450,   38, /*  0.15 */ },
71a628d29bSJohn Heffner  {  33986,   37, /*  0.15 */ },
72a628d29bSJohn Heffner  {  35586,   36, /*  0.14 */ },
73a628d29bSJohn Heffner  {  37253,   36, /*  0.14 */ },
74a628d29bSJohn Heffner  {  38992,   35, /*  0.14 */ },
75a628d29bSJohn Heffner  {  40808,   35, /*  0.14 */ },
76a628d29bSJohn Heffner  {  42707,   34, /*  0.13 */ },
77a628d29bSJohn Heffner  {  44694,   33, /*  0.13 */ },
78a628d29bSJohn Heffner  {  46776,   33, /*  0.13 */ },
79a628d29bSJohn Heffner  {  48961,   32, /*  0.13 */ },
80a628d29bSJohn Heffner  {  51258,   32, /*  0.13 */ },
81a628d29bSJohn Heffner  {  53677,   31, /*  0.12 */ },
82a628d29bSJohn Heffner  {  56230,   30, /*  0.12 */ },
83a628d29bSJohn Heffner  {  58932,   30, /*  0.12 */ },
84a628d29bSJohn Heffner  {  61799,   29, /*  0.12 */ },
85a628d29bSJohn Heffner  {  64851,   28, /*  0.11 */ },
86a628d29bSJohn Heffner  {  68113,   28, /*  0.11 */ },
87a628d29bSJohn Heffner  {  71617,   27, /*  0.11 */ },
88a628d29bSJohn Heffner  {  75401,   26, /*  0.10 */ },
89a628d29bSJohn Heffner  {  79517,   26, /*  0.10 */ },
90a628d29bSJohn Heffner  {  84035,   25, /*  0.10 */ },
91a628d29bSJohn Heffner  {  89053,   24, /*  0.10 */ },
92a628d29bSJohn Heffner };
93a628d29bSJohn Heffner 
94a628d29bSJohn Heffner #define HSTCP_AIMD_MAX	ARRAY_SIZE(hstcp_aimd_vals)
95a628d29bSJohn Heffner 
96a628d29bSJohn Heffner struct hstcp {
97a628d29bSJohn Heffner 	u32	ai;
98a628d29bSJohn Heffner };
99a628d29bSJohn Heffner 
100738980ffSStephen Hemminger static int max_ssthresh = 100;
101738980ffSStephen Hemminger module_param(max_ssthresh, int, 0644);
102738980ffSStephen Hemminger MODULE_PARM_DESC(max_ssthresh, "limited slow start threshold (RFC3742)");
103738980ffSStephen Hemminger 
1046687e988SArnaldo Carvalho de Melo static void hstcp_init(struct sock *sk)
105a628d29bSJohn Heffner {
1066687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
1076687e988SArnaldo Carvalho de Melo 	struct hstcp *ca = inet_csk_ca(sk);
108a628d29bSJohn Heffner 
109a628d29bSJohn Heffner 	ca->ai = 0;
110a628d29bSJohn Heffner 
111a628d29bSJohn Heffner 	/* Ensure the MD arithmetic works.  This is somewhat pedantic,
112a628d29bSJohn Heffner 	 * since I don't think we will see a cwnd this large. :) */
113a628d29bSJohn Heffner 	tp->snd_cwnd_clamp = min_t(u32, tp->snd_cwnd_clamp, 0xffffffff/128);
114a628d29bSJohn Heffner }
115a628d29bSJohn Heffner 
1166687e988SArnaldo Carvalho de Melo static void hstcp_cong_avoid(struct sock *sk, u32 adk, u32 rtt,
117bd6af700SStephen Hemminger 			     u32 in_flight, int data_acked)
118a628d29bSJohn Heffner {
1196687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
1206687e988SArnaldo Carvalho de Melo 	struct hstcp *ca = inet_csk_ca(sk);
121a628d29bSJohn Heffner 
122f4805edeSStephen Hemminger 	if (!tcp_is_cwnd_limited(sk, in_flight))
123a628d29bSJohn Heffner 		return;
124a628d29bSJohn Heffner 
125738980ffSStephen Hemminger 	if (tp->snd_cwnd <= tp->snd_ssthresh) {
126738980ffSStephen Hemminger 		/* RFC3742: limited slow start
127738980ffSStephen Hemminger 		 * the window is increased by 1/K MSS for each arriving ACK,
128738980ffSStephen Hemminger 		 * for K = int(cwnd/(0.5 max_ssthresh))
129738980ffSStephen Hemminger 		 */
130738980ffSStephen Hemminger 		if (max_ssthresh > 0 && tp->snd_cwnd > max_ssthresh) {
131738980ffSStephen Hemminger 			u32 k = max(tp->snd_cwnd / (max_ssthresh >> 1), 1U);
132738980ffSStephen Hemminger 			if (++tp->snd_cwnd_cnt >= k) {
133738980ffSStephen Hemminger 				if (tp->snd_cwnd < tp->snd_cwnd_clamp)
134738980ffSStephen Hemminger 					tp->snd_cwnd++;
135738980ffSStephen Hemminger 				tp->snd_cwnd_cnt = 0;
136738980ffSStephen Hemminger 			}
137738980ffSStephen Hemminger 		} else {
138738980ffSStephen Hemminger 			if (tp->snd_cwnd < tp->snd_cwnd_clamp)
139738980ffSStephen Hemminger 				tp->snd_cwnd++;
140738980ffSStephen Hemminger 		}
141738980ffSStephen Hemminger 	} else {
1426150c22eSXiaoliang (David) Wei 		/* Update AIMD parameters.
1436150c22eSXiaoliang (David) Wei 		 *
1446150c22eSXiaoliang (David) Wei 		 * We want to guarantee that:
1456150c22eSXiaoliang (David) Wei 		 *     hstcp_aimd_vals[ca->ai-1].cwnd <
1466150c22eSXiaoliang (David) Wei 		 *     snd_cwnd <=
1476150c22eSXiaoliang (David) Wei 		 *     hstcp_aimd_vals[ca->ai].cwnd
1486150c22eSXiaoliang (David) Wei 		 */
149a628d29bSJohn Heffner 		if (tp->snd_cwnd > hstcp_aimd_vals[ca->ai].cwnd) {
150a628d29bSJohn Heffner 			while (tp->snd_cwnd > hstcp_aimd_vals[ca->ai].cwnd &&
1514a1ff6e2SPatrick McHardy 			       ca->ai < HSTCP_AIMD_MAX - 1)
152a628d29bSJohn Heffner 				ca->ai++;
1536150c22eSXiaoliang (David) Wei 		} else if (ca->ai && tp->snd_cwnd <= hstcp_aimd_vals[ca->ai-1].cwnd) {
1546150c22eSXiaoliang (David) Wei 			while (ca->ai && tp->snd_cwnd <= hstcp_aimd_vals[ca->ai-1].cwnd)
155a628d29bSJohn Heffner 				ca->ai--;
156a628d29bSJohn Heffner 		}
157a628d29bSJohn Heffner 
158a628d29bSJohn Heffner 		/* Do additive increase */
159a628d29bSJohn Heffner 		if (tp->snd_cwnd < tp->snd_cwnd_clamp) {
160fb80a6e1SStephen Hemminger 			/* cwnd = cwnd + a(w) / cwnd */
161fb80a6e1SStephen Hemminger 			tp->snd_cwnd_cnt += ca->ai + 1;
162a628d29bSJohn Heffner 			if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
163a628d29bSJohn Heffner 				tp->snd_cwnd_cnt -= tp->snd_cwnd;
1645528e568SJohn Heffner 				tp->snd_cwnd++;
165a628d29bSJohn Heffner 			}
166a628d29bSJohn Heffner 		}
167a628d29bSJohn Heffner 	}
168a628d29bSJohn Heffner }
169a628d29bSJohn Heffner 
1706687e988SArnaldo Carvalho de Melo static u32 hstcp_ssthresh(struct sock *sk)
171a628d29bSJohn Heffner {
1726687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
1736687e988SArnaldo Carvalho de Melo 	const struct hstcp *ca = inet_csk_ca(sk);
174a628d29bSJohn Heffner 
175a628d29bSJohn Heffner 	/* Do multiplicative decrease */
176a628d29bSJohn Heffner 	return max(tp->snd_cwnd - ((tp->snd_cwnd * hstcp_aimd_vals[ca->ai].md) >> 8), 2U);
177a628d29bSJohn Heffner }
178a628d29bSJohn Heffner 
179a628d29bSJohn Heffner 
180a628d29bSJohn Heffner static struct tcp_congestion_ops tcp_highspeed = {
181a628d29bSJohn Heffner 	.init		= hstcp_init,
182a628d29bSJohn Heffner 	.ssthresh	= hstcp_ssthresh,
183a628d29bSJohn Heffner 	.cong_avoid	= hstcp_cong_avoid,
184a628d29bSJohn Heffner 	.min_cwnd	= tcp_reno_min_cwnd,
185a628d29bSJohn Heffner 
186a628d29bSJohn Heffner 	.owner		= THIS_MODULE,
187a628d29bSJohn Heffner 	.name		= "highspeed"
188a628d29bSJohn Heffner };
189a628d29bSJohn Heffner 
190a628d29bSJohn Heffner static int __init hstcp_register(void)
191a628d29bSJohn Heffner {
192*74975d40SAlexey Dobriyan 	BUILD_BUG_ON(sizeof(struct hstcp) > ICSK_CA_PRIV_SIZE);
193a628d29bSJohn Heffner 	return tcp_register_congestion_control(&tcp_highspeed);
194a628d29bSJohn Heffner }
195a628d29bSJohn Heffner 
196a628d29bSJohn Heffner static void __exit hstcp_unregister(void)
197a628d29bSJohn Heffner {
198a628d29bSJohn Heffner 	tcp_unregister_congestion_control(&tcp_highspeed);
199a628d29bSJohn Heffner }
200a628d29bSJohn Heffner 
201a628d29bSJohn Heffner module_init(hstcp_register);
202a628d29bSJohn Heffner module_exit(hstcp_unregister);
203a628d29bSJohn Heffner 
204a628d29bSJohn Heffner MODULE_AUTHOR("John Heffner");
205a628d29bSJohn Heffner MODULE_LICENSE("GPL");
206a628d29bSJohn Heffner MODULE_DESCRIPTION("High Speed TCP");
207