1 /*-
2  * Copyright (c) 2009-2013
3  * 	Swinburne University of Technology, Melbourne, Australia
4  * All rights reserved.
5  *
6  * This software was developed at the Centre for Advanced Internet
7  * Architectures, Swinburne University of Technology, by David Hayes, made
8  * possible in part by a gift from The Cisco University Research Program Fund,
9  * a corporate advised fund of Silicon Valley Community Foundation. Development
10  * and testing were further assisted by a grant from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * CAIA Delay-Gradient (CDG) congestion control algorithm
36  *
37  * An implemention of the delay-gradient congestion control algorithm proposed
38  * in the following paper:
39  *
40  * D. A. Hayes and G. Armitage, "Revisiting TCP Congestion Control using Delay
41  * Gradients", in IFIP Networking, Valencia, Spain, 9-13 May 2011.
42  *
43  * Developed as part of the NewTCP research project at Swinburne University of
44  * Technology's Centre for Advanced Internet Architectures, Melbourne,
45  * Australia. More details are available at:
46  *   http://caia.swin.edu.au/urp/newtcp/
47  */
48 
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD: stable/9/sys/netinet/cc/cc_cdg.c 271653 2014-09-16 00:04:38Z lstewart $");
51 
52 #include <sys/param.h>
53 #include <sys/hhook.h>
54 #include <sys/kernel.h>
55 #include <sys/khelp.h>
56 #include <sys/limits.h>
57 #include <sys/lock.h>
58 #include <sys/malloc.h>
59 #include <sys/module.h>
60 #include <sys/queue.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/sysctl.h>
64 #include <sys/systm.h>
65 
66 #include <net/if.h>
67 #include <net/vnet.h>
68 
69 #include <netinet/cc.h>
70 #include <netinet/tcp_seq.h>
71 #include <netinet/tcp_timer.h>
72 #include <netinet/tcp_var.h>
73 
74 #include <netinet/cc/cc_module.h>
75 
76 #include <netinet/khelp/h_ertt.h>
77 
78 #include <vm/uma.h>
79 
80 #define	CDG_VERSION "0.1"
81 
82 #define	CAST_PTR_INT(X) (*((int*)(X)))
83 
84 #ifndef	VIMAGE
85 #define	vnet_sysctl_handle_uint(oidp, arg1, arg2, req) \
86     sysctl_handle_int(oidp, arg1, arg2, req)
87 #endif
88 
89 /* Private delay-gradient induced congestion control signal. */
90 #define	CC_CDG_DELAY 0x01000000
91 
92 /* NewReno window deflation factor on loss (as a percentage). */
93 #define	RENO_BETA 50
94 
95 /* Queue states. */
96 #define	CDG_Q_EMPTY	1
97 #define	CDG_Q_RISING	2
98 #define	CDG_Q_FALLING	3
99 #define	CDG_Q_FULL	4
100 #define	CDG_Q_UNKNOWN	9999
101 
102 /* Number of bit shifts used in probexp lookup table. */
103 #define	EXP_PREC 15
104 
105 /* Largest gradient represented in probexp lookup table. */
106 #define	MAXGRAD 5
107 
108 /*
109  * Delay Precision Enhance - number of bit shifts used for qtrend related
110  * integer arithmetic precision.
111  */
112 #define	D_P_E 7
113 
114 struct qdiff_sample {
115 	long qdiff;
116 	STAILQ_ENTRY(qdiff_sample) qdiff_lnk;
117 };
118 
119 struct cdg {
120 	long max_qtrend;
121 	long min_qtrend;
122 	STAILQ_HEAD(minrtts_head, qdiff_sample) qdiffmin_q;
123 	STAILQ_HEAD(maxrtts_head, qdiff_sample) qdiffmax_q;
124 	long window_incr;
125 	/* rttcount for window increase when in congestion avoidance */
126 	long rtt_count;
127 	/* maximum measured rtt within an rtt period */
128 	int maxrtt_in_rtt;
129 	/* maximum measured rtt within prev rtt period */
130 	int maxrtt_in_prevrtt;
131 	/* minimum measured rtt within an rtt period */
132 	int minrtt_in_rtt;
133 	/* minimum measured rtt within prev rtt period */
134 	int minrtt_in_prevrtt;
135 	/* consecutive congestion episode counter */
136 	uint32_t consec_cong_cnt;
137 	/* when tracking a new reno type loss window */
138 	uint32_t shadow_w;
139 	/* maximum number of samples in the moving average queue */
140 	int sample_q_size;
141 	/* number of samples in the moving average queue */
142 	int num_samples;
143 	/* estimate of the queue state of the path */
144 	int queue_state;
145 };
146 
147 /*
148  * Lookup table for:
149  *   (1 - exp(-x)) << EXP_PREC, where x = [0,MAXGRAD] in 2^-7 increments
150  *
151  * Note: probexp[0] is set to 10 (not 0) as a safety for very low increase
152  * gradients.
153  */
154 static const int probexp[641] = {
155    10,255,508,759,1008,1255,1501,1744,1985,2225,2463,2698,2932,3165,3395,3624,
156    3850,4075,4299,4520,4740,4958,5175,5389,5602,5814,6024,6232,6438,6643,6846,
157    7048,7248,7447,7644,7839,8033,8226,8417,8606,8794,8981,9166,9350,9532,9713,
158    9892,10070,10247,10422,10596,10769,10940,11110,11278,11445,11611,11776,11939,
159    12101,12262,12422,12580,12737,12893,13048,13201,13354,13505,13655,13803,13951,
160    14097,14243,14387,14530,14672,14813,14952,15091,15229,15365,15500,15635,15768,
161    15900,16032,16162,16291,16419,16547,16673,16798,16922,17046,17168,17289,17410,
162    17529,17648,17766,17882,17998,18113,18227,18340,18453,18564,18675,18784,18893,
163    19001,19108,19215,19320,19425,19529,19632,19734,19835,19936,20036,20135,20233,
164    20331,20427,20523,20619,20713,20807,20900,20993,21084,21175,21265,21355,21444,
165    21532,21619,21706,21792,21878,21962,22046,22130,22213,22295,22376,22457,22537,
166    22617,22696,22774,22852,22929,23006,23082,23157,23232,23306,23380,23453,23525,
167    23597,23669,23739,23810,23879,23949,24017,24085,24153,24220,24286,24352,24418,
168    24483,24547,24611,24675,24738,24800,24862,24924,24985,25045,25106,25165,25224,
169    25283,25341,25399,25456,25513,25570,25626,25681,25737,25791,25846,25899,25953,
170    26006,26059,26111,26163,26214,26265,26316,26366,26416,26465,26514,26563,26611,
171    26659,26707,26754,26801,26847,26893,26939,26984,27029,27074,27118,27162,27206,
172    27249,27292,27335,27377,27419,27460,27502,27543,27583,27624,27664,27703,27743,
173    27782,27821,27859,27897,27935,27973,28010,28047,28084,28121,28157,28193,28228,
174    28263,28299,28333,28368,28402,28436,28470,28503,28536,28569,28602,28634,28667,
175    28699,28730,28762,28793,28824,28854,28885,28915,28945,28975,29004,29034,29063,
176    29092,29120,29149,29177,29205,29232,29260,29287,29314,29341,29368,29394,29421,
177    29447,29472,29498,29524,29549,29574,29599,29623,29648,29672,29696,29720,29744,
178    29767,29791,29814,29837,29860,29882,29905,29927,29949,29971,29993,30014,30036,
179    30057,30078,30099,30120,30141,30161,30181,30201,30221,30241,30261,30280,30300,
180    30319,30338,30357,30376,30394,30413,30431,30449,30467,30485,30503,30521,30538,
181    30555,30573,30590,30607,30624,30640,30657,30673,30690,30706,30722,30738,30753,
182    30769,30785,30800,30815,30831,30846,30861,30876,30890,30905,30919,30934,30948,
183    30962,30976,30990,31004,31018,31031,31045,31058,31072,31085,31098,31111,31124,
184    31137,31149,31162,31174,31187,31199,31211,31223,31235,31247,31259,31271,31283,
185    31294,31306,31317,31328,31339,31351,31362,31373,31383,31394,31405,31416,31426,
186    31436,31447,31457,31467,31477,31487,31497,31507,31517,31527,31537,31546,31556,
187    31565,31574,31584,31593,31602,31611,31620,31629,31638,31647,31655,31664,31673,
188    31681,31690,31698,31706,31715,31723,31731,31739,31747,31755,31763,31771,31778,
189    31786,31794,31801,31809,31816,31824,31831,31838,31846,31853,31860,31867,31874,
190    31881,31888,31895,31902,31908,31915,31922,31928,31935,31941,31948,31954,31960,
191    31967,31973,31979,31985,31991,31997,32003,32009,32015,32021,32027,32033,32038,
192    32044,32050,32055,32061,32066,32072,32077,32083,32088,32093,32098,32104,32109,
193    32114,32119,32124,32129,32134,32139,32144,32149,32154,32158,32163,32168,32173,
194    32177,32182,32186,32191,32195,32200,32204,32209,32213,32217,32222,32226,32230,
195    32234,32238,32242,32247,32251,32255,32259,32263,32267,32270,32274,32278,32282,
196    32286,32290,32293,32297,32301,32304,32308,32311,32315,32318,32322,32325,32329,
197    32332,32336,32339,32342,32346,32349,32352,32356,32359,32362,32365,32368,32371,
198    32374,32377,32381,32384,32387,32389,32392,32395,32398,32401,32404,32407,32410,
199    32412,32415,32418,32421,32423,32426,32429,32431,32434,32437,32439,32442,32444,
200    32447,32449,32452,32454,32457,32459,32461,32464,32466,32469,32471,32473,32476,
201    32478,32480,32482,32485,32487,32489,32491,32493,32495,32497,32500,32502,32504,
202    32506,32508,32510,32512,32514,32516,32518,32520,32522,32524,32526,32527,32529,
203    32531,32533,32535,32537,32538,32540,32542,32544,32545,32547};
204 
205 static uma_zone_t qdiffsample_zone;
206 
207 static MALLOC_DEFINE(M_CDG, "cdg data",
208   "Per connection data required for the CDG congestion control algorithm");
209 
210 static int ertt_id;
211 
212 static VNET_DEFINE(uint32_t, cdg_alpha_inc);
213 static VNET_DEFINE(uint32_t, cdg_beta_delay);
214 static VNET_DEFINE(uint32_t, cdg_beta_loss);
215 static VNET_DEFINE(uint32_t, cdg_smoothing_factor);
216 static VNET_DEFINE(uint32_t, cdg_exp_backoff_scale);
217 static VNET_DEFINE(uint32_t, cdg_consec_cong);
218 static VNET_DEFINE(uint32_t, cdg_hold_backoff);
219 #define	V_cdg_alpha_inc		VNET(cdg_alpha_inc)
220 #define	V_cdg_beta_delay	VNET(cdg_beta_delay)
221 #define	V_cdg_beta_loss		VNET(cdg_beta_loss)
222 #define	V_cdg_smoothing_factor	VNET(cdg_smoothing_factor)
223 #define	V_cdg_exp_backoff_scale	VNET(cdg_exp_backoff_scale)
224 #define	V_cdg_consec_cong	VNET(cdg_consec_cong)
225 #define	V_cdg_hold_backoff	VNET(cdg_hold_backoff)
226 
227 /* Function prototypes. */
228 static int cdg_mod_init(void);
229 static int cdg_mod_destroy(void);
230 static void cdg_conn_init(struct cc_var *ccv);
231 static int cdg_cb_init(struct cc_var *ccv);
232 static void cdg_cb_destroy(struct cc_var *ccv);
233 static void cdg_cong_signal(struct cc_var *ccv, uint32_t signal_type);
234 static void cdg_ack_received(struct cc_var *ccv, uint16_t ack_type);
235 
236 struct cc_algo cdg_cc_algo = {
237 	.name = "cdg",
238 	.mod_init = cdg_mod_init,
239 	.ack_received = cdg_ack_received,
240 	.cb_destroy = cdg_cb_destroy,
241 	.cb_init = cdg_cb_init,
242 	.conn_init = cdg_conn_init,
243 	.cong_signal = cdg_cong_signal,
244 	.mod_destroy = cdg_mod_destroy
245 };
246 
247 /* Vnet created and being initialised. */
248 static void
cdg_init_vnet(const void * unused __unused)249 cdg_init_vnet(const void *unused __unused)
250 {
251 
252 	V_cdg_alpha_inc = 0;
253 	V_cdg_beta_delay = 70;
254 	V_cdg_beta_loss = 50;
255 	V_cdg_smoothing_factor = 8;
256 	V_cdg_exp_backoff_scale = 3;
257 	V_cdg_consec_cong = 5;
258 	V_cdg_hold_backoff = 5;
259 }
260 
261 static int
cdg_mod_init(void)262 cdg_mod_init(void)
263 {
264 	VNET_ITERATOR_DECL(v);
265 
266 	ertt_id = khelp_get_id("ertt");
267 	if (ertt_id <= 0)
268 		return (EINVAL);
269 
270 	qdiffsample_zone = uma_zcreate("cdg_qdiffsample",
271 	    sizeof(struct qdiff_sample), NULL, NULL, NULL, NULL, 0, 0);
272 
273 	VNET_LIST_RLOCK();
274 	VNET_FOREACH(v) {
275 		CURVNET_SET(v);
276 		cdg_init_vnet(NULL);
277 		CURVNET_RESTORE();
278 	}
279 	VNET_LIST_RUNLOCK();
280 
281 	cdg_cc_algo.post_recovery = newreno_cc_algo.post_recovery;
282 	cdg_cc_algo.after_idle = newreno_cc_algo.after_idle;
283 
284 	return (0);
285 }
286 
287 static int
cdg_mod_destroy(void)288 cdg_mod_destroy(void)
289 {
290 
291 	uma_zdestroy(qdiffsample_zone);
292 	return (0);
293 }
294 
295 static int
cdg_cb_init(struct cc_var * ccv)296 cdg_cb_init(struct cc_var *ccv)
297 {
298 	struct cdg *cdg_data;
299 
300 	cdg_data = malloc(sizeof(struct cdg), M_CDG, M_NOWAIT);
301 	if (cdg_data == NULL)
302 		return (ENOMEM);
303 
304 	cdg_data->shadow_w = 0;
305 	cdg_data->max_qtrend = 0;
306 	cdg_data->min_qtrend = 0;
307 	cdg_data->queue_state = CDG_Q_UNKNOWN;
308 	cdg_data->maxrtt_in_rtt = 0;
309 	cdg_data->maxrtt_in_prevrtt = 0;
310 	cdg_data->minrtt_in_rtt = INT_MAX;
311 	cdg_data->minrtt_in_prevrtt = 0;
312 	cdg_data->window_incr = 0;
313 	cdg_data->rtt_count = 0;
314 	cdg_data->consec_cong_cnt = 0;
315 	cdg_data->sample_q_size = V_cdg_smoothing_factor;
316 	cdg_data->num_samples = 0;
317 	STAILQ_INIT(&cdg_data->qdiffmin_q);
318 	STAILQ_INIT(&cdg_data->qdiffmax_q);
319 
320 	ccv->cc_data = cdg_data;
321 
322 	return (0);
323 }
324 
325 static void
cdg_conn_init(struct cc_var * ccv)326 cdg_conn_init(struct cc_var *ccv)
327 {
328 	struct cdg *cdg_data = ccv->cc_data;
329 
330 	/*
331 	 * Initialise the shadow_cwnd in case we are competing with loss based
332 	 * flows from the start
333 	 */
334 	cdg_data->shadow_w = CCV(ccv, snd_cwnd);
335 }
336 
337 static void
cdg_cb_destroy(struct cc_var * ccv)338 cdg_cb_destroy(struct cc_var *ccv)
339 {
340 	struct cdg *cdg_data;
341 	struct qdiff_sample *qds, *qds_n;
342 
343 	cdg_data = ccv->cc_data;
344 
345 	qds = STAILQ_FIRST(&cdg_data->qdiffmin_q);
346 	while (qds != NULL) {
347 		qds_n = STAILQ_NEXT(qds, qdiff_lnk);
348 		uma_zfree(qdiffsample_zone,qds);
349 		qds = qds_n;
350 	}
351 
352 	qds = STAILQ_FIRST(&cdg_data->qdiffmax_q);
353 	while (qds != NULL) {
354 		qds_n = STAILQ_NEXT(qds, qdiff_lnk);
355 		uma_zfree(qdiffsample_zone,qds);
356 		qds = qds_n;
357 	}
358 
359 	free(ccv->cc_data, M_CDG);
360 }
361 
362 static int
cdg_beta_handler(SYSCTL_HANDLER_ARGS)363 cdg_beta_handler(SYSCTL_HANDLER_ARGS)
364 {
365 
366 	if (req->newptr != NULL &&
367 	    (CAST_PTR_INT(req->newptr) == 0 || CAST_PTR_INT(req->newptr) > 100))
368 		return (EINVAL);
369 
370 	return (vnet_sysctl_handle_uint(oidp, arg1, arg2, req));
371 }
372 
373 static int
cdg_exp_backoff_scale_handler(SYSCTL_HANDLER_ARGS)374 cdg_exp_backoff_scale_handler(SYSCTL_HANDLER_ARGS)
375 {
376 
377 	if (req->newptr != NULL && CAST_PTR_INT(req->newptr) < 1)
378 		return (EINVAL);
379 
380 	return (vnet_sysctl_handle_uint(oidp, arg1, arg2, req));
381 }
382 
383 static inline unsigned long
cdg_window_decrease(struct cc_var * ccv,unsigned long owin,unsigned int beta)384 cdg_window_decrease(struct cc_var *ccv, unsigned long owin, unsigned int beta)
385 {
386 
387 	return ((ulmin(CCV(ccv, snd_wnd), owin) * beta) / 100);
388 }
389 
390 /*
391  * Window increase function
392  * This window increase function is independent of the initial window size
393  * to ensure small window flows are not discriminated against (i.e. fairness).
394  * It increases at 1pkt/rtt like Reno for alpha_inc rtts, and then 2pkts/rtt for
395  * the next alpha_inc rtts, etc.
396  */
397 static void
cdg_window_increase(struct cc_var * ccv,int new_measurement)398 cdg_window_increase(struct cc_var *ccv, int new_measurement)
399 {
400 	struct cdg *cdg_data;
401 	int incr, s_w_incr;
402 
403 	cdg_data = ccv->cc_data;
404 	incr = s_w_incr = 0;
405 
406 	if (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh)) {
407 		/* Slow start. */
408 		incr = CCV(ccv, t_maxseg);
409 		s_w_incr = incr;
410 		cdg_data->window_incr = cdg_data->rtt_count = 0;
411 	} else {
412 		/* Congestion avoidance. */
413 		if (new_measurement) {
414 			s_w_incr = CCV(ccv, t_maxseg);
415 			if (V_cdg_alpha_inc == 0) {
416 				incr = CCV(ccv, t_maxseg);
417 			} else {
418 				if (++cdg_data->rtt_count >= V_cdg_alpha_inc) {
419 					cdg_data->window_incr++;
420 					cdg_data->rtt_count = 0;
421 				}
422 				incr = CCV(ccv, t_maxseg) *
423 				    cdg_data->window_incr;
424 			}
425 		}
426 	}
427 
428 	if (cdg_data->shadow_w > 0)
429 		cdg_data->shadow_w = ulmin(cdg_data->shadow_w + s_w_incr,
430 		    TCP_MAXWIN << CCV(ccv, snd_scale));
431 
432 	CCV(ccv, snd_cwnd) = ulmin(CCV(ccv, snd_cwnd) + incr,
433 	    TCP_MAXWIN << CCV(ccv, snd_scale));
434 }
435 
436 static void
cdg_cong_signal(struct cc_var * ccv,uint32_t signal_type)437 cdg_cong_signal(struct cc_var *ccv, uint32_t signal_type)
438 {
439 	struct cdg *cdg_data = ccv->cc_data;
440 
441 	switch(signal_type) {
442 	case CC_CDG_DELAY:
443 		CCV(ccv, snd_ssthresh) = cdg_window_decrease(ccv,
444 		    CCV(ccv, snd_cwnd), V_cdg_beta_delay);
445 		CCV(ccv, snd_cwnd) = CCV(ccv, snd_ssthresh);
446 		CCV(ccv, snd_recover) = CCV(ccv, snd_max);
447 		cdg_data->window_incr = cdg_data->rtt_count = 0;
448 		ENTER_CONGRECOVERY(CCV(ccv, t_flags));
449 		break;
450 	case CC_NDUPACK:
451 		/*
452 		 * If already responding to congestion OR we have guessed no
453 		 * queue in the path is full.
454 		 */
455 		if (IN_CONGRECOVERY(CCV(ccv, t_flags)) ||
456 		    cdg_data->queue_state < CDG_Q_FULL) {
457 			CCV(ccv, snd_ssthresh) = CCV(ccv, snd_cwnd);
458 			CCV(ccv, snd_recover) = CCV(ccv, snd_max);
459 		} else {
460 			/*
461 			 * Loss is likely to be congestion related. We have
462 			 * inferred a queue full state, so have shadow window
463 			 * react to loss as NewReno would.
464 			 */
465 			if (cdg_data->shadow_w > 0)
466 				cdg_data->shadow_w = cdg_window_decrease(ccv,
467 				    cdg_data->shadow_w, RENO_BETA);
468 
469 			CCV(ccv, snd_ssthresh) = ulmax(cdg_data->shadow_w,
470 			    cdg_window_decrease(ccv, CCV(ccv, snd_cwnd),
471 			    V_cdg_beta_loss));
472 
473 			cdg_data->window_incr = cdg_data->rtt_count = 0;
474 		}
475 		ENTER_RECOVERY(CCV(ccv, t_flags));
476 		break;
477 	default:
478 		newreno_cc_algo.cong_signal(ccv, signal_type);
479 		break;
480 	}
481 }
482 
483 /*
484  * Using a negative exponential probabilistic backoff so that sources with
485  * varying RTTs which share the same link will, on average, have the same
486  * probability of backoff over time.
487  *
488  * Prob_backoff = 1 - exp(-qtrend / V_cdg_exp_backoff_scale), where
489  * V_cdg_exp_backoff_scale is the average qtrend for the exponential backoff.
490  */
491 static inline int
prob_backoff(long qtrend)492 prob_backoff(long qtrend)
493 {
494 	int backoff, idx, p;
495 
496 	backoff = (qtrend > ((MAXGRAD * V_cdg_exp_backoff_scale) << D_P_E));
497 
498 	if (!backoff) {
499 		if (V_cdg_exp_backoff_scale > 1)
500 			idx = (qtrend + V_cdg_exp_backoff_scale / 2) /
501 			    V_cdg_exp_backoff_scale;
502 		else
503 			idx = qtrend;
504 
505 		/* Backoff probability proportional to rate of queue growth. */
506 		p = (INT_MAX / (1 << EXP_PREC)) * probexp[idx];
507 		backoff = (random() < p);
508 	}
509 
510 	return (backoff);
511 }
512 
513 static inline void
calc_moving_average(struct cdg * cdg_data,long qdiff_max,long qdiff_min)514 calc_moving_average(struct cdg *cdg_data, long qdiff_max, long qdiff_min)
515 {
516 	struct qdiff_sample *qds;
517 
518 	++cdg_data->num_samples;
519 	if (cdg_data->num_samples > cdg_data->sample_q_size) {
520 		/* Minimum RTT. */
521 		qds = STAILQ_FIRST(&cdg_data->qdiffmin_q);
522 		cdg_data->min_qtrend =  cdg_data->min_qtrend +
523 		    (qdiff_min - qds->qdiff) / cdg_data->sample_q_size;
524 		STAILQ_REMOVE_HEAD(&cdg_data->qdiffmin_q, qdiff_lnk);
525 		qds->qdiff = qdiff_min;
526 		STAILQ_INSERT_TAIL(&cdg_data->qdiffmin_q, qds, qdiff_lnk);
527 
528 		/* Maximum RTT. */
529 		qds = STAILQ_FIRST(&cdg_data->qdiffmax_q);
530 		cdg_data->max_qtrend =  cdg_data->max_qtrend +
531 		    (qdiff_max - qds->qdiff) / cdg_data->sample_q_size;
532 		STAILQ_REMOVE_HEAD(&cdg_data->qdiffmax_q, qdiff_lnk);
533 		qds->qdiff = qdiff_max;
534 		STAILQ_INSERT_TAIL(&cdg_data->qdiffmax_q, qds, qdiff_lnk);
535 		--cdg_data->num_samples;
536 	} else {
537 		qds = uma_zalloc(qdiffsample_zone, M_NOWAIT);
538 		if (qds != NULL) {
539 			cdg_data->min_qtrend = cdg_data->min_qtrend +
540 			    qdiff_min / cdg_data->sample_q_size;
541 			qds->qdiff = qdiff_min;
542 			STAILQ_INSERT_TAIL(&cdg_data->qdiffmin_q, qds,
543 			    qdiff_lnk);
544 		}
545 
546 		qds = uma_zalloc(qdiffsample_zone, M_NOWAIT);
547 		if (qds) {
548 			cdg_data->max_qtrend = cdg_data->max_qtrend +
549 			    qdiff_max / cdg_data->sample_q_size;
550 			qds->qdiff = qdiff_max;
551 			STAILQ_INSERT_TAIL(&cdg_data->qdiffmax_q, qds,
552 			    qdiff_lnk);
553 		}
554 	}
555 }
556 
557 static void
cdg_ack_received(struct cc_var * ccv,uint16_t ack_type)558 cdg_ack_received(struct cc_var *ccv, uint16_t ack_type)
559 {
560 	struct cdg *cdg_data;
561 	struct ertt *e_t;
562 	long qdiff_max, qdiff_min;
563 	int congestion, new_measurement, slowstart;
564 
565 	cdg_data = ccv->cc_data;
566 	e_t = (struct ertt *)khelp_get_osd(CCV(ccv, osd), ertt_id);
567 	new_measurement = e_t->flags & ERTT_NEW_MEASUREMENT;
568 	congestion = 0;
569 	cdg_data->maxrtt_in_rtt = imax(e_t->rtt, cdg_data->maxrtt_in_rtt);
570 	cdg_data->minrtt_in_rtt = imin(e_t->rtt, cdg_data->minrtt_in_rtt);
571 
572 	if (new_measurement) {
573 		slowstart = (CCV(ccv, snd_cwnd) <= CCV(ccv, snd_ssthresh));
574 		/*
575 		 * Update smoothed gradient measurements. Since we are only
576 		 * using one measurement per RTT, use max or min rtt_in_rtt.
577 		 * This is also less noisy than a sample RTT measurement. Max
578 		 * RTT measurements can have trouble due to OS issues.
579 		 */
580 		if (cdg_data->maxrtt_in_prevrtt) {
581 			qdiff_max = ((long)(cdg_data->maxrtt_in_rtt -
582 			    cdg_data->maxrtt_in_prevrtt) << D_P_E );
583 			qdiff_min = ((long)(cdg_data->minrtt_in_rtt -
584 			    cdg_data->minrtt_in_prevrtt) << D_P_E );
585 
586 			calc_moving_average(cdg_data, qdiff_max, qdiff_min);
587 
588 			/* Probabilistic backoff with respect to gradient. */
589 			if (slowstart && qdiff_min > 0)
590 				congestion = prob_backoff(qdiff_min);
591 			else if (cdg_data->min_qtrend > 0)
592 				congestion = prob_backoff(cdg_data->min_qtrend);
593 			else if (slowstart && qdiff_max > 0)
594 				congestion = prob_backoff(qdiff_max);
595 			else if (cdg_data->max_qtrend > 0)
596 				congestion = prob_backoff(cdg_data->max_qtrend);
597 
598 			/* Update estimate of queue state. */
599 			if (cdg_data->min_qtrend > 0 &&
600 			    cdg_data->max_qtrend <= 0) {
601 				cdg_data->queue_state = CDG_Q_FULL;
602 			} else if (cdg_data->min_qtrend >= 0 &&
603 			    cdg_data->max_qtrend < 0) {
604 				cdg_data->queue_state = CDG_Q_EMPTY;
605 				cdg_data->shadow_w = 0;
606 			} else if (cdg_data->min_qtrend > 0 &&
607 			    cdg_data->max_qtrend > 0) {
608 				cdg_data->queue_state = CDG_Q_RISING;
609 			} else if (cdg_data->min_qtrend < 0 &&
610 			    cdg_data->max_qtrend < 0) {
611 				cdg_data->queue_state = CDG_Q_FALLING;
612 			}
613 
614 			if (cdg_data->min_qtrend < 0 ||
615 			    cdg_data->max_qtrend < 0)
616 				cdg_data->consec_cong_cnt = 0;
617 		}
618 
619 		cdg_data->minrtt_in_prevrtt = cdg_data->minrtt_in_rtt;
620 		cdg_data->minrtt_in_rtt = INT_MAX;
621 		cdg_data->maxrtt_in_prevrtt = cdg_data->maxrtt_in_rtt;
622 		cdg_data->maxrtt_in_rtt = 0;
623 		e_t->flags &= ~ERTT_NEW_MEASUREMENT;
624 	}
625 
626 	if (congestion) {
627 		cdg_data->consec_cong_cnt++;
628 		if (!IN_RECOVERY(CCV(ccv, t_flags))) {
629 			if (cdg_data->consec_cong_cnt <= V_cdg_consec_cong)
630 				cdg_cong_signal(ccv, CC_CDG_DELAY);
631 			else
632 				/*
633 				 * We have been backing off but the queue is not
634 				 * falling. Assume we are competing with
635 				 * loss-based flows and don't back off for the
636 				 * next V_cdg_hold_backoff RTT periods.
637 				 */
638 				if (cdg_data->consec_cong_cnt >=
639 				    V_cdg_consec_cong + V_cdg_hold_backoff)
640 					cdg_data->consec_cong_cnt = 0;
641 
642 			/* Won't see effect until 2nd RTT. */
643 			cdg_data->maxrtt_in_prevrtt = 0;
644 			/*
645 			 * Resync shadow window in case we are competing with a
646 			 * loss based flow
647 			 */
648 			cdg_data->shadow_w = ulmax(CCV(ccv, snd_cwnd),
649 			    cdg_data->shadow_w);
650 		}
651 	} else if (ack_type == CC_ACK)
652 		cdg_window_increase(ccv, new_measurement);
653 }
654 
655 /* When a vnet is created and being initialised, init the per-stack CDG vars. */
656 VNET_SYSINIT(cdg_init_vnet, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST,
657     cdg_init_vnet, NULL);
658 
659 SYSCTL_DECL(_net_inet_tcp_cc_cdg);
660 SYSCTL_NODE(_net_inet_tcp_cc, OID_AUTO, cdg, CTLFLAG_RW, NULL,
661     "CAIA delay-gradient congestion control related settings");
662 
663 SYSCTL_STRING(_net_inet_tcp_cc_cdg, OID_AUTO, version,
664     CTLFLAG_RD, CDG_VERSION, sizeof(CDG_VERSION) - 1,
665     "Current algorithm/implementation version number");
666 
667 SYSCTL_VNET_UINT(_net_inet_tcp_cc_cdg, OID_AUTO, alpha_inc,
668     CTLFLAG_RW, &VNET_NAME(cdg_alpha_inc), 0,
669     "Increment the window increase factor alpha by 1 MSS segment every "
670     "alpha_inc RTTs during congestion avoidance mode.");
671 
672 SYSCTL_VNET_PROC(_net_inet_tcp_cc_cdg, OID_AUTO, beta_delay,
673     CTLTYPE_UINT|CTLFLAG_RW, &VNET_NAME(cdg_beta_delay), 70,
674     &cdg_beta_handler, "IU",
675     "Delay-based window decrease factor as a percentage "
676     "(on delay-based backoff, w = w * beta_delay / 100)");
677 
678 SYSCTL_VNET_PROC(_net_inet_tcp_cc_cdg, OID_AUTO, beta_loss,
679     CTLTYPE_UINT|CTLFLAG_RW, &VNET_NAME(cdg_beta_loss), 50,
680     &cdg_beta_handler, "IU",
681     "Loss-based window decrease factor as a percentage "
682     "(on loss-based backoff, w = w * beta_loss / 100)");
683 
684 SYSCTL_VNET_PROC(_net_inet_tcp_cc_cdg, OID_AUTO, exp_backoff_scale,
685     CTLTYPE_UINT|CTLFLAG_RW, &VNET_NAME(cdg_exp_backoff_scale), 2,
686     &cdg_exp_backoff_scale_handler, "IU",
687     "Scaling parameter for the probabilistic exponential backoff");
688 
689 SYSCTL_VNET_UINT(_net_inet_tcp_cc_cdg,  OID_AUTO, smoothing_factor,
690     CTLFLAG_RW, &VNET_NAME(cdg_smoothing_factor), 8,
691     "Number of samples used for moving average smoothing (0 = no smoothing)");
692 
693 SYSCTL_VNET_UINT(_net_inet_tcp_cc_cdg, OID_AUTO, loss_compete_consec_cong,
694     CTLFLAG_RW, &VNET_NAME(cdg_consec_cong), 5,
695     "Number of consecutive delay-gradient based congestion episodes which will "
696     "trigger loss based CC compatibility");
697 
698 SYSCTL_VNET_UINT(_net_inet_tcp_cc_cdg, OID_AUTO, loss_compete_hold_backoff,
699     CTLFLAG_RW, &VNET_NAME(cdg_hold_backoff), 5,
700     "Number of consecutive delay-gradient based congestion episodes to hold "
701     "the window backoff for loss based CC compatibility");
702 
703 DECLARE_CC_MODULE(cdg, &cdg_cc_algo);
704 
705 MODULE_DEPEND(cdg, ertt, 1, 1, 1);
706