xref: /freebsd/sys/netinet/cc/cc_dctcp.c (revision c697fb7f)
1 /*-
2  * Copyright (c) 2007-2008
3  *	Swinburne University of Technology, Melbourne, Australia
4  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
5  * Copyright (c) 2014 Midori Kato <katoon@sfc.wide.ad.jp>
6  * Copyright (c) 2014 The FreeBSD Foundation
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * An implementation of the DCTCP algorithm for FreeBSD, based on
33  * "Data Center TCP (DCTCP)" by M. Alizadeh, A. Greenberg, D. A. Maltz,
34  * J. Padhye, P. Patel, B. Prabhakar, S. Sengupta, and M. Sridharan.,
35  * in ACM Conference on SIGCOMM 2010, New York, USA,
36  * Originally released as the contribution of Microsoft Research project.
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 
51 #include <net/vnet.h>
52 
53 #include <netinet/tcp.h>
54 #include <netinet/tcp_seq.h>
55 #include <netinet/tcp_var.h>
56 #include <netinet/cc/cc.h>
57 #include <netinet/cc/cc_module.h>
58 
59 #define DCTCP_SHIFT 10
60 #define MAX_ALPHA_VALUE (1<<DCTCP_SHIFT)
61 VNET_DEFINE_STATIC(uint32_t, dctcp_alpha) = MAX_ALPHA_VALUE;
62 #define V_dctcp_alpha	    VNET(dctcp_alpha)
63 VNET_DEFINE_STATIC(uint32_t, dctcp_shift_g) = 4;
64 #define	V_dctcp_shift_g	    VNET(dctcp_shift_g)
65 VNET_DEFINE_STATIC(uint32_t, dctcp_slowstart) = 0;
66 #define	V_dctcp_slowstart   VNET(dctcp_slowstart)
67 
68 struct dctcp {
69 	uint32_t bytes_ecn;	  /* # of marked bytes during a RTT */
70 	uint32_t bytes_total;	  /* # of acked bytes during a RTT */
71 	int      alpha;		  /* the fraction of marked bytes */
72 	int      ce_prev;	  /* CE state of the last segment */
73 	tcp_seq  save_sndnxt;	  /* end sequence number of the current window */
74 	int      ece_curr;	  /* ECE flag in this segment */
75 	int      ece_prev;	  /* ECE flag in the last segment */
76 	uint32_t num_cong_events; /* # of congestion events */
77 };
78 
79 static MALLOC_DEFINE(M_dctcp, "dctcp data",
80     "Per connection data required for the dctcp algorithm");
81 
82 static void	dctcp_ack_received(struct cc_var *ccv, uint16_t type);
83 static void	dctcp_after_idle(struct cc_var *ccv);
84 static void	dctcp_cb_destroy(struct cc_var *ccv);
85 static int	dctcp_cb_init(struct cc_var *ccv);
86 static void	dctcp_cong_signal(struct cc_var *ccv, uint32_t type);
87 static void	dctcp_conn_init(struct cc_var *ccv);
88 static void	dctcp_post_recovery(struct cc_var *ccv);
89 static void	dctcp_ecnpkt_handler(struct cc_var *ccv);
90 static void	dctcp_update_alpha(struct cc_var *ccv);
91 
92 struct cc_algo dctcp_cc_algo = {
93 	.name = "dctcp",
94 	.ack_received = dctcp_ack_received,
95 	.cb_destroy = dctcp_cb_destroy,
96 	.cb_init = dctcp_cb_init,
97 	.cong_signal = dctcp_cong_signal,
98 	.conn_init = dctcp_conn_init,
99 	.post_recovery = dctcp_post_recovery,
100 	.ecnpkt_handler = dctcp_ecnpkt_handler,
101 	.after_idle = dctcp_after_idle,
102 };
103 
104 static void
105 dctcp_ack_received(struct cc_var *ccv, uint16_t type)
106 {
107 	struct dctcp *dctcp_data;
108 	int bytes_acked = 0;
109 
110 	dctcp_data = ccv->cc_data;
111 
112 	if (CCV(ccv, t_flags2) & TF2_ECN_PERMIT) {
113 		/*
114 		 * DCTCP doesn't treat receipt of ECN marked packet as a
115 		 * congestion event. Thus, DCTCP always executes the ACK
116 		 * processing out of congestion recovery.
117 		 */
118 		if (IN_CONGRECOVERY(CCV(ccv, t_flags))) {
119 			EXIT_CONGRECOVERY(CCV(ccv, t_flags));
120 			newreno_cc_algo.ack_received(ccv, type);
121 			ENTER_CONGRECOVERY(CCV(ccv, t_flags));
122 		} else
123 			newreno_cc_algo.ack_received(ccv, type);
124 
125 		if (type == CC_DUPACK)
126 			bytes_acked = min(ccv->bytes_this_ack, CCV(ccv, t_maxseg));
127 
128 		if (type == CC_ACK)
129 			bytes_acked = ccv->bytes_this_ack;
130 
131 		/* Update total bytes. */
132 		dctcp_data->bytes_total += bytes_acked;
133 
134 		/* Update total marked bytes. */
135 		if (dctcp_data->ece_curr) {
136 			//XXRMS: For fluid-model DCTCP, update
137 			//cwnd here during for RTT fairness
138 			if (!dctcp_data->ece_prev
139 			    && bytes_acked > CCV(ccv, t_maxseg)) {
140 				dctcp_data->bytes_ecn +=
141 				    (bytes_acked - CCV(ccv, t_maxseg));
142 			} else
143 				dctcp_data->bytes_ecn += bytes_acked;
144 			dctcp_data->ece_prev = 1;
145 		} else {
146 			if (dctcp_data->ece_prev
147 			    && bytes_acked > CCV(ccv, t_maxseg))
148 				dctcp_data->bytes_ecn += CCV(ccv, t_maxseg);
149 			dctcp_data->ece_prev = 0;
150 		}
151 		dctcp_data->ece_curr = 0;
152 
153 		/*
154 		 * Update the fraction of marked bytes at the end of
155 		 * current window size.
156 		 */
157 		if ((IN_FASTRECOVERY(CCV(ccv, t_flags)) &&
158 		    SEQ_GEQ(ccv->curack, CCV(ccv, snd_recover))) ||
159 		    (!IN_FASTRECOVERY(CCV(ccv, t_flags)) &&
160 		    SEQ_GT(ccv->curack, dctcp_data->save_sndnxt)))
161 			dctcp_update_alpha(ccv);
162 	} else
163 		newreno_cc_algo.ack_received(ccv, type);
164 }
165 
166 static void
167 dctcp_after_idle(struct cc_var *ccv)
168 {
169 	struct dctcp *dctcp_data;
170 
171 	if (CCV(ccv, t_flags2) & TF2_ECN_PERMIT) {
172 		dctcp_data = ccv->cc_data;
173 
174 		/* Initialize internal parameters after idle time */
175 		dctcp_data->bytes_ecn = 0;
176 		dctcp_data->bytes_total = 0;
177 		dctcp_data->save_sndnxt = CCV(ccv, snd_nxt);
178 		dctcp_data->alpha = V_dctcp_alpha;
179 		dctcp_data->ece_curr = 0;
180 		dctcp_data->ece_prev = 0;
181 		dctcp_data->num_cong_events = 0;
182 	}
183 
184 	newreno_cc_algo.after_idle(ccv);
185 }
186 
187 static void
188 dctcp_cb_destroy(struct cc_var *ccv)
189 {
190 	free(ccv->cc_data, M_dctcp);
191 }
192 
193 static int
194 dctcp_cb_init(struct cc_var *ccv)
195 {
196 	struct dctcp *dctcp_data;
197 
198 	dctcp_data = malloc(sizeof(struct dctcp), M_dctcp, M_NOWAIT|M_ZERO);
199 
200 	if (dctcp_data == NULL)
201 		return (ENOMEM);
202 
203 	/* Initialize some key variables with sensible defaults. */
204 	dctcp_data->bytes_ecn = 0;
205 	dctcp_data->bytes_total = 0;
206 	/*
207 	 * When alpha is set to 0 in the beginning, DCTCP sender transfers as
208 	 * much data as possible until the value converges which may expand the
209 	 * queueing delay at the switch. When alpha is set to 1, queueing delay
210 	 * is kept small.
211 	 * Throughput-sensitive applications should have alpha = 0
212 	 * Latency-sensitive applications should have alpha = 1
213 	 *
214 	 * Note: DCTCP draft suggests initial alpha to be 1 but we've decided to
215 	 * keep it 0 as default.
216 	 */
217 	dctcp_data->alpha = V_dctcp_alpha;
218 	dctcp_data->save_sndnxt = 0;
219 	dctcp_data->ce_prev = 0;
220 	dctcp_data->ece_curr = 0;
221 	dctcp_data->ece_prev = 0;
222 	dctcp_data->num_cong_events = 0;
223 
224 	ccv->cc_data = dctcp_data;
225 	return (0);
226 }
227 
228 /*
229  * Perform any necessary tasks before we enter congestion recovery.
230  */
231 static void
232 dctcp_cong_signal(struct cc_var *ccv, uint32_t type)
233 {
234 	struct dctcp *dctcp_data;
235 	u_int cwin, mss;
236 
237 	if (CCV(ccv, t_flags2) & TF2_ECN_PERMIT) {
238 		dctcp_data = ccv->cc_data;
239 		cwin = CCV(ccv, snd_cwnd);
240 		mss = CCV(ccv, t_maxseg);
241 
242 		switch (type) {
243 		case CC_NDUPACK:
244 			if (!IN_FASTRECOVERY(CCV(ccv, t_flags))) {
245 				if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
246 					CCV(ccv, snd_ssthresh) =
247 					    max(cwin / 2, 2 * mss);
248 					dctcp_data->num_cong_events++;
249 				} else {
250 					/* cwnd has already updated as congestion
251 					 * recovery. Reverse cwnd value using
252 					 * snd_cwnd_prev and recalculate snd_ssthresh
253 					 */
254 					cwin = CCV(ccv, snd_cwnd_prev);
255 					CCV(ccv, snd_ssthresh) =
256 					    max(cwin / 2, 2 * mss);
257 				}
258 				ENTER_RECOVERY(CCV(ccv, t_flags));
259 			}
260 			break;
261 		case CC_ECN:
262 			/*
263 			 * Save current snd_cwnd when the host encounters both
264 			 * congestion recovery and fast recovery.
265 			 */
266 			CCV(ccv, snd_cwnd_prev) = cwin;
267 			if (!IN_CONGRECOVERY(CCV(ccv, t_flags))) {
268 				if (V_dctcp_slowstart &&
269 				    dctcp_data->num_cong_events++ == 0) {
270 					CCV(ccv, snd_ssthresh) =
271 					    max(cwin / 2, 2 * mss);
272 					dctcp_data->alpha = MAX_ALPHA_VALUE;
273 					dctcp_data->bytes_ecn = 0;
274 					dctcp_data->bytes_total = 0;
275 					dctcp_data->save_sndnxt = CCV(ccv, snd_nxt);
276 				} else
277 					CCV(ccv, snd_ssthresh) =
278 					    max((cwin - (((uint64_t)cwin *
279 					    dctcp_data->alpha) >> (DCTCP_SHIFT+1))),
280 					    2 * mss);
281 				CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
282 				ENTER_CONGRECOVERY(CCV(ccv, t_flags));
283 			}
284 			dctcp_data->ece_curr = 1;
285 			break;
286 		case CC_RTO:
287 			dctcp_update_alpha(ccv);
288 			dctcp_data->save_sndnxt += CCV(ccv, t_maxseg);
289 			dctcp_data->num_cong_events++;
290 			break;
291 		}
292 	} else
293 		newreno_cc_algo.cong_signal(ccv, type);
294 }
295 
296 static void
297 dctcp_conn_init(struct cc_var *ccv)
298 {
299 	struct dctcp *dctcp_data;
300 
301 	dctcp_data = ccv->cc_data;
302 
303 	if (CCV(ccv, t_flags2) & TF2_ECN_PERMIT)
304 		dctcp_data->save_sndnxt = CCV(ccv, snd_nxt);
305 }
306 
307 /*
308  * Perform any necessary tasks before we exit congestion recovery.
309  */
310 static void
311 dctcp_post_recovery(struct cc_var *ccv)
312 {
313 	newreno_cc_algo.post_recovery(ccv);
314 
315 	if (CCV(ccv, t_flags2) & TF2_ECN_PERMIT)
316 		dctcp_update_alpha(ccv);
317 }
318 
319 /*
320  * Execute an additional ECN processing using ECN field in IP header
321  * and the CWR bit in TCP header.
322  */
323 static void
324 dctcp_ecnpkt_handler(struct cc_var *ccv)
325 {
326 	struct dctcp *dctcp_data;
327 	uint32_t ccflag;
328 	int acknow;
329 
330 	dctcp_data = ccv->cc_data;
331 	ccflag = ccv->flags;
332 	acknow = 0;
333 
334 	/*
335 	 * DCTCP responds with an ACK immediately when the CE state
336 	 * in between this segment and the last segment has changed.
337 	 */
338 	if (ccflag & CCF_IPHDR_CE) {
339 		if (!dctcp_data->ce_prev) {
340 			acknow = 1;
341 			dctcp_data->ce_prev = 1;
342 			CCV(ccv, t_flags2) |= TF2_ECN_SND_ECE;
343 		}
344 	} else {
345 		if (dctcp_data->ce_prev) {
346 			acknow = 1;
347 			dctcp_data->ce_prev = 0;
348 			CCV(ccv, t_flags2) &= ~TF2_ECN_SND_ECE;
349 		}
350 	}
351 
352 	if ((acknow) || (ccflag & CCF_TCPHDR_CWR)) {
353 		ccv->flags |= CCF_ACKNOW;
354 	} else {
355 		ccv->flags &= ~CCF_ACKNOW;
356 	}
357 }
358 
359 /*
360  * Update the fraction of marked bytes represented as 'alpha'.
361  * Also initialize several internal parameters at the end of this function.
362  */
363 static void
364 dctcp_update_alpha(struct cc_var *ccv)
365 {
366 	struct dctcp *dctcp_data;
367 	int alpha_prev;
368 
369 	dctcp_data = ccv->cc_data;
370 	alpha_prev = dctcp_data->alpha;
371 	dctcp_data->bytes_total = max(dctcp_data->bytes_total, 1);
372 
373 	/*
374 	 * Update alpha: alpha = (1 - g) * alpha + g * M.
375 	 * Here:
376 	 * g is weight factor
377 	 *	recommaded to be set to 1/16
378 	 *	small g = slow convergence between competitive DCTCP flows
379 	 *	large g = impacts low utilization of bandwidth at switches
380 	 * M is fraction of marked segments in last RTT
381 	 *	updated every RTT
382 	 * Alpha must be round to 0 - MAX_ALPHA_VALUE.
383 	 */
384 	dctcp_data->alpha = ulmin(alpha_prev - (alpha_prev >> V_dctcp_shift_g) +
385 	    ((uint64_t)dctcp_data->bytes_ecn << (DCTCP_SHIFT - V_dctcp_shift_g)) /
386 	    dctcp_data->bytes_total, MAX_ALPHA_VALUE);
387 
388 	/* Initialize internal parameters for next alpha calculation */
389 	dctcp_data->bytes_ecn = 0;
390 	dctcp_data->bytes_total = 0;
391 	dctcp_data->save_sndnxt = CCV(ccv, snd_nxt);
392 }
393 
394 static int
395 dctcp_alpha_handler(SYSCTL_HANDLER_ARGS)
396 {
397 	uint32_t new;
398 	int error;
399 
400 	new = V_dctcp_alpha;
401 	error = sysctl_handle_int(oidp, &new, 0, req);
402 	if (error == 0 && req->newptr != NULL) {
403 		if (new > MAX_ALPHA_VALUE)
404 			error = EINVAL;
405 		else
406 			V_dctcp_alpha = new;
407 	}
408 
409 	return (error);
410 }
411 
412 static int
413 dctcp_shift_g_handler(SYSCTL_HANDLER_ARGS)
414 {
415 	uint32_t new;
416 	int error;
417 
418 	new = V_dctcp_shift_g;
419 	error = sysctl_handle_int(oidp, &new, 0, req);
420 	if (error == 0 && req->newptr != NULL) {
421 		if (new > DCTCP_SHIFT)
422 			error = EINVAL;
423 		else
424 			V_dctcp_shift_g = new;
425 	}
426 
427 	return (error);
428 }
429 
430 static int
431 dctcp_slowstart_handler(SYSCTL_HANDLER_ARGS)
432 {
433 	uint32_t new;
434 	int error;
435 
436 	new = V_dctcp_slowstart;
437 	error = sysctl_handle_int(oidp, &new, 0, req);
438 	if (error == 0 && req->newptr != NULL) {
439 		if (new > 1)
440 			error = EINVAL;
441 		else
442 			V_dctcp_slowstart = new;
443 	}
444 
445 	return (error);
446 }
447 
448 SYSCTL_DECL(_net_inet_tcp_cc_dctcp);
449 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, dctcp,
450     CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
451     "dctcp congestion control related settings");
452 
453 SYSCTL_PROC(_net_inet_tcp_cc_dctcp, OID_AUTO, alpha,
454     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
455     &VNET_NAME(dctcp_alpha), 0, &dctcp_alpha_handler, "IU",
456     "dctcp alpha parameter at start of session");
457 
458 SYSCTL_PROC(_net_inet_tcp_cc_dctcp, OID_AUTO, shift_g,
459     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
460     &VNET_NAME(dctcp_shift_g), 4, &dctcp_shift_g_handler, "IU",
461     "dctcp shift parameter");
462 
463 SYSCTL_PROC(_net_inet_tcp_cc_dctcp, OID_AUTO, slowstart,
464     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
465     &VNET_NAME(dctcp_slowstart), 0, &dctcp_slowstart_handler, "IU",
466     "half CWND reduction after the first slow start");
467 
468 DECLARE_CC_MODULE(dctcp, &dctcp_cc_algo);
469