xref: /openbmc/linux/net/ipv4/tcp_dctcp.c (revision 724ba675)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* DataCenter TCP (DCTCP) congestion control.
3  *
4  * http://simula.stanford.edu/~alizade/Site/DCTCP.html
5  *
6  * This is an implementation of DCTCP over Reno, an enhancement to the
7  * TCP congestion control algorithm designed for data centers. DCTCP
8  * leverages Explicit Congestion Notification (ECN) in the network to
9  * provide multi-bit feedback to the end hosts. DCTCP's goal is to meet
10  * the following three data center transport requirements:
11  *
12  *  - High burst tolerance (incast due to partition/aggregate)
13  *  - Low latency (short flows, queries)
14  *  - High throughput (continuous data updates, large file transfers)
15  *    with commodity shallow buffered switches
16  *
17  * The algorithm is described in detail in the following two papers:
18  *
19  * 1) Mohammad Alizadeh, Albert Greenberg, David A. Maltz, Jitendra Padhye,
20  *    Parveen Patel, Balaji Prabhakar, Sudipta Sengupta, and Murari Sridharan:
21  *      "Data Center TCP (DCTCP)", Data Center Networks session
22  *      Proc. ACM SIGCOMM, New Delhi, 2010.
23  *   http://simula.stanford.edu/~alizade/Site/DCTCP_files/dctcp-final.pdf
24  *
25  * 2) Mohammad Alizadeh, Adel Javanmard, and Balaji Prabhakar:
26  *      "Analysis of DCTCP: Stability, Convergence, and Fairness"
27  *      Proc. ACM SIGMETRICS, San Jose, 2011.
28  *   http://simula.stanford.edu/~alizade/Site/DCTCP_files/dctcp_analysis-full.pdf
29  *
30  * Initial prototype from Abdul Kabbani, Masato Yasuda and Mohammad Alizadeh.
31  *
32  * Authors:
33  *
34  *	Daniel Borkmann <dborkman@redhat.com>
35  *	Florian Westphal <fw@strlen.de>
36  *	Glenn Judd <glenn.judd@morganstanley.com>
37  */
38 
39 #include <linux/btf.h>
40 #include <linux/btf_ids.h>
41 #include <linux/module.h>
42 #include <linux/mm.h>
43 #include <net/tcp.h>
44 #include <linux/inet_diag.h>
45 #include "tcp_dctcp.h"
46 
47 #define DCTCP_MAX_ALPHA	1024U
48 
49 struct dctcp {
50 	u32 old_delivered;
51 	u32 old_delivered_ce;
52 	u32 prior_rcv_nxt;
53 	u32 dctcp_alpha;
54 	u32 next_seq;
55 	u32 ce_state;
56 	u32 loss_cwnd;
57 	struct tcp_plb_state plb;
58 };
59 
60 static unsigned int dctcp_shift_g __read_mostly = 4; /* g = 1/2^4 */
61 module_param(dctcp_shift_g, uint, 0644);
62 MODULE_PARM_DESC(dctcp_shift_g, "parameter g for updating dctcp_alpha");
63 
64 static unsigned int dctcp_alpha_on_init __read_mostly = DCTCP_MAX_ALPHA;
65 module_param(dctcp_alpha_on_init, uint, 0644);
66 MODULE_PARM_DESC(dctcp_alpha_on_init, "parameter for initial alpha value");
67 
68 static struct tcp_congestion_ops dctcp_reno;
69 
70 static void dctcp_reset(const struct tcp_sock *tp, struct dctcp *ca)
71 {
72 	ca->next_seq = tp->snd_nxt;
73 
74 	ca->old_delivered = tp->delivered;
75 	ca->old_delivered_ce = tp->delivered_ce;
76 }
77 
78 __bpf_kfunc static void dctcp_init(struct sock *sk)
79 {
80 	const struct tcp_sock *tp = tcp_sk(sk);
81 
82 	if ((tp->ecn_flags & TCP_ECN_OK) ||
83 	    (sk->sk_state == TCP_LISTEN ||
84 	     sk->sk_state == TCP_CLOSE)) {
85 		struct dctcp *ca = inet_csk_ca(sk);
86 
87 		ca->prior_rcv_nxt = tp->rcv_nxt;
88 
89 		ca->dctcp_alpha = min(dctcp_alpha_on_init, DCTCP_MAX_ALPHA);
90 
91 		ca->loss_cwnd = 0;
92 		ca->ce_state = 0;
93 
94 		dctcp_reset(tp, ca);
95 		tcp_plb_init(sk, &ca->plb);
96 
97 		return;
98 	}
99 
100 	/* No ECN support? Fall back to Reno. Also need to clear
101 	 * ECT from sk since it is set during 3WHS for DCTCP.
102 	 */
103 	inet_csk(sk)->icsk_ca_ops = &dctcp_reno;
104 	INET_ECN_dontxmit(sk);
105 }
106 
107 __bpf_kfunc static u32 dctcp_ssthresh(struct sock *sk)
108 {
109 	struct dctcp *ca = inet_csk_ca(sk);
110 	struct tcp_sock *tp = tcp_sk(sk);
111 
112 	ca->loss_cwnd = tcp_snd_cwnd(tp);
113 	return max(tcp_snd_cwnd(tp) - ((tcp_snd_cwnd(tp) * ca->dctcp_alpha) >> 11U), 2U);
114 }
115 
116 __bpf_kfunc static void dctcp_update_alpha(struct sock *sk, u32 flags)
117 {
118 	const struct tcp_sock *tp = tcp_sk(sk);
119 	struct dctcp *ca = inet_csk_ca(sk);
120 
121 	/* Expired RTT */
122 	if (!before(tp->snd_una, ca->next_seq)) {
123 		u32 delivered = tp->delivered - ca->old_delivered;
124 		u32 delivered_ce = tp->delivered_ce - ca->old_delivered_ce;
125 		u32 alpha = ca->dctcp_alpha;
126 		u32 ce_ratio = 0;
127 
128 		if (delivered > 0) {
129 			/* dctcp_alpha keeps EWMA of fraction of ECN marked
130 			 * packets. Because of EWMA smoothing, PLB reaction can
131 			 * be slow so we use ce_ratio which is an instantaneous
132 			 * measure of congestion. ce_ratio is the fraction of
133 			 * ECN marked packets in the previous RTT.
134 			 */
135 			if (delivered_ce > 0)
136 				ce_ratio = (delivered_ce << TCP_PLB_SCALE) / delivered;
137 			tcp_plb_update_state(sk, &ca->plb, (int)ce_ratio);
138 			tcp_plb_check_rehash(sk, &ca->plb);
139 		}
140 
141 		/* alpha = (1 - g) * alpha + g * F */
142 
143 		alpha -= min_not_zero(alpha, alpha >> dctcp_shift_g);
144 		if (delivered_ce) {
145 
146 			/* If dctcp_shift_g == 1, a 32bit value would overflow
147 			 * after 8 M packets.
148 			 */
149 			delivered_ce <<= (10 - dctcp_shift_g);
150 			delivered_ce /= max(1U, delivered);
151 
152 			alpha = min(alpha + delivered_ce, DCTCP_MAX_ALPHA);
153 		}
154 		/* dctcp_alpha can be read from dctcp_get_info() without
155 		 * synchro, so we ask compiler to not use dctcp_alpha
156 		 * as a temporary variable in prior operations.
157 		 */
158 		WRITE_ONCE(ca->dctcp_alpha, alpha);
159 		dctcp_reset(tp, ca);
160 	}
161 }
162 
163 static void dctcp_react_to_loss(struct sock *sk)
164 {
165 	struct dctcp *ca = inet_csk_ca(sk);
166 	struct tcp_sock *tp = tcp_sk(sk);
167 
168 	ca->loss_cwnd = tcp_snd_cwnd(tp);
169 	tp->snd_ssthresh = max(tcp_snd_cwnd(tp) >> 1U, 2U);
170 }
171 
172 __bpf_kfunc static void dctcp_state(struct sock *sk, u8 new_state)
173 {
174 	if (new_state == TCP_CA_Recovery &&
175 	    new_state != inet_csk(sk)->icsk_ca_state)
176 		dctcp_react_to_loss(sk);
177 	/* We handle RTO in dctcp_cwnd_event to ensure that we perform only
178 	 * one loss-adjustment per RTT.
179 	 */
180 }
181 
182 __bpf_kfunc static void dctcp_cwnd_event(struct sock *sk, enum tcp_ca_event ev)
183 {
184 	struct dctcp *ca = inet_csk_ca(sk);
185 
186 	switch (ev) {
187 	case CA_EVENT_ECN_IS_CE:
188 	case CA_EVENT_ECN_NO_CE:
189 		dctcp_ece_ack_update(sk, ev, &ca->prior_rcv_nxt, &ca->ce_state);
190 		break;
191 	case CA_EVENT_LOSS:
192 		tcp_plb_update_state_upon_rto(sk, &ca->plb);
193 		dctcp_react_to_loss(sk);
194 		break;
195 	case CA_EVENT_TX_START:
196 		tcp_plb_check_rehash(sk, &ca->plb); /* Maybe rehash when inflight is 0 */
197 		break;
198 	default:
199 		/* Don't care for the rest. */
200 		break;
201 	}
202 }
203 
204 static size_t dctcp_get_info(struct sock *sk, u32 ext, int *attr,
205 			     union tcp_cc_info *info)
206 {
207 	const struct dctcp *ca = inet_csk_ca(sk);
208 	const struct tcp_sock *tp = tcp_sk(sk);
209 
210 	/* Fill it also in case of VEGASINFO due to req struct limits.
211 	 * We can still correctly retrieve it later.
212 	 */
213 	if (ext & (1 << (INET_DIAG_DCTCPINFO - 1)) ||
214 	    ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
215 		memset(&info->dctcp, 0, sizeof(info->dctcp));
216 		if (inet_csk(sk)->icsk_ca_ops != &dctcp_reno) {
217 			info->dctcp.dctcp_enabled = 1;
218 			info->dctcp.dctcp_ce_state = (u16) ca->ce_state;
219 			info->dctcp.dctcp_alpha = ca->dctcp_alpha;
220 			info->dctcp.dctcp_ab_ecn = tp->mss_cache *
221 						   (tp->delivered_ce - ca->old_delivered_ce);
222 			info->dctcp.dctcp_ab_tot = tp->mss_cache *
223 						   (tp->delivered - ca->old_delivered);
224 		}
225 
226 		*attr = INET_DIAG_DCTCPINFO;
227 		return sizeof(info->dctcp);
228 	}
229 	return 0;
230 }
231 
232 __bpf_kfunc static u32 dctcp_cwnd_undo(struct sock *sk)
233 {
234 	const struct dctcp *ca = inet_csk_ca(sk);
235 	struct tcp_sock *tp = tcp_sk(sk);
236 
237 	return max(tcp_snd_cwnd(tp), ca->loss_cwnd);
238 }
239 
240 static struct tcp_congestion_ops dctcp __read_mostly = {
241 	.init		= dctcp_init,
242 	.in_ack_event   = dctcp_update_alpha,
243 	.cwnd_event	= dctcp_cwnd_event,
244 	.ssthresh	= dctcp_ssthresh,
245 	.cong_avoid	= tcp_reno_cong_avoid,
246 	.undo_cwnd	= dctcp_cwnd_undo,
247 	.set_state	= dctcp_state,
248 	.get_info	= dctcp_get_info,
249 	.flags		= TCP_CONG_NEEDS_ECN,
250 	.owner		= THIS_MODULE,
251 	.name		= "dctcp",
252 };
253 
254 static struct tcp_congestion_ops dctcp_reno __read_mostly = {
255 	.ssthresh	= tcp_reno_ssthresh,
256 	.cong_avoid	= tcp_reno_cong_avoid,
257 	.undo_cwnd	= tcp_reno_undo_cwnd,
258 	.get_info	= dctcp_get_info,
259 	.owner		= THIS_MODULE,
260 	.name		= "dctcp-reno",
261 };
262 
263 BTF_SET8_START(tcp_dctcp_check_kfunc_ids)
264 #ifdef CONFIG_X86
265 #ifdef CONFIG_DYNAMIC_FTRACE
266 BTF_ID_FLAGS(func, dctcp_init)
267 BTF_ID_FLAGS(func, dctcp_update_alpha)
268 BTF_ID_FLAGS(func, dctcp_cwnd_event)
269 BTF_ID_FLAGS(func, dctcp_ssthresh)
270 BTF_ID_FLAGS(func, dctcp_cwnd_undo)
271 BTF_ID_FLAGS(func, dctcp_state)
272 #endif
273 #endif
274 BTF_SET8_END(tcp_dctcp_check_kfunc_ids)
275 
276 static const struct btf_kfunc_id_set tcp_dctcp_kfunc_set = {
277 	.owner = THIS_MODULE,
278 	.set   = &tcp_dctcp_check_kfunc_ids,
279 };
280 
281 static int __init dctcp_register(void)
282 {
283 	int ret;
284 
285 	BUILD_BUG_ON(sizeof(struct dctcp) > ICSK_CA_PRIV_SIZE);
286 
287 	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &tcp_dctcp_kfunc_set);
288 	if (ret < 0)
289 		return ret;
290 	return tcp_register_congestion_control(&dctcp);
291 }
292 
293 static void __exit dctcp_unregister(void)
294 {
295 	tcp_unregister_congestion_control(&dctcp);
296 }
297 
298 module_init(dctcp_register);
299 module_exit(dctcp_unregister);
300 
301 MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
302 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
303 MODULE_AUTHOR("Glenn Judd <glenn.judd@morganstanley.com>");
304 
305 MODULE_LICENSE("GPL v2");
306 MODULE_DESCRIPTION("DataCenter TCP (DCTCP)");
307