xref: /freebsd/sys/netinet/cc/cc_cubic.h (revision 891b8ed4672a213bbe6f3f10522eeadb34d01b76)
167fef78bSLawrence Stewart /*-
267fef78bSLawrence Stewart  * Copyright (c) 2008-2010 Lawrence Stewart <lstewart@freebsd.org>
367fef78bSLawrence Stewart  * Copyright (c) 2010 The FreeBSD Foundation
467fef78bSLawrence Stewart  * All rights reserved.
567fef78bSLawrence Stewart  *
667fef78bSLawrence Stewart  * This software was developed by Lawrence Stewart while studying at the Centre
7*891b8ed4SLawrence Stewart  * for Advanced Internet Architectures, Swinburne University of Technology, made
8*891b8ed4SLawrence Stewart  * possible in part by a grant from the Cisco University Research Program Fund
9*891b8ed4SLawrence Stewart  * at Community Foundation Silicon Valley.
1067fef78bSLawrence Stewart  *
1167fef78bSLawrence Stewart  * Portions of this software were developed at the Centre for Advanced
1267fef78bSLawrence Stewart  * Internet Architectures, Swinburne University of Technology, Melbourne,
1367fef78bSLawrence Stewart  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
1467fef78bSLawrence Stewart  *
1567fef78bSLawrence Stewart  * Redistribution and use in source and binary forms, with or without
1667fef78bSLawrence Stewart  * modification, are permitted provided that the following conditions
1767fef78bSLawrence Stewart  * are met:
1867fef78bSLawrence Stewart  * 1. Redistributions of source code must retain the above copyright
1967fef78bSLawrence Stewart  *    notice, this list of conditions and the following disclaimer.
2067fef78bSLawrence Stewart  * 2. Redistributions in binary form must reproduce the above copyright
2167fef78bSLawrence Stewart  *    notice, this list of conditions and the following disclaimer in the
2267fef78bSLawrence Stewart  *    documentation and/or other materials provided with the distribution.
2367fef78bSLawrence Stewart  *
2467fef78bSLawrence Stewart  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2567fef78bSLawrence Stewart  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2667fef78bSLawrence Stewart  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2767fef78bSLawrence Stewart  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2867fef78bSLawrence Stewart  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2967fef78bSLawrence Stewart  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3067fef78bSLawrence Stewart  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3167fef78bSLawrence Stewart  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3267fef78bSLawrence Stewart  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3367fef78bSLawrence Stewart  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3467fef78bSLawrence Stewart  * SUCH DAMAGE.
3567fef78bSLawrence Stewart  *
3667fef78bSLawrence Stewart  * $FreeBSD$
3767fef78bSLawrence Stewart  */
3867fef78bSLawrence Stewart 
3967fef78bSLawrence Stewart #ifndef _NETINET_CC_CUBIC_H_
4067fef78bSLawrence Stewart #define _NETINET_CC_CUBIC_H_
4167fef78bSLawrence Stewart 
4267fef78bSLawrence Stewart /* Number of bits of precision for fixed point math calcs. */
4367fef78bSLawrence Stewart #define	CUBIC_SHIFT		8
4467fef78bSLawrence Stewart 
4567fef78bSLawrence Stewart #define	CUBIC_SHIFT_4		32
4667fef78bSLawrence Stewart 
4767fef78bSLawrence Stewart /* 0.5 << CUBIC_SHIFT. */
4867fef78bSLawrence Stewart #define	RENO_BETA		128
4967fef78bSLawrence Stewart 
5067fef78bSLawrence Stewart /* ~0.8 << CUBIC_SHIFT. */
5167fef78bSLawrence Stewart #define	CUBIC_BETA		204
5267fef78bSLawrence Stewart 
5367fef78bSLawrence Stewart /* ~0.2 << CUBIC_SHIFT. */
5467fef78bSLawrence Stewart #define	ONE_SUB_CUBIC_BETA	51
5567fef78bSLawrence Stewart 
5667fef78bSLawrence Stewart /* 3 * ONE_SUB_CUBIC_BETA. */
5767fef78bSLawrence Stewart #define	THREE_X_PT2		153
5867fef78bSLawrence Stewart 
5967fef78bSLawrence Stewart /* (2 << CUBIC_SHIFT) - ONE_SUB_CUBIC_BETA. */
6067fef78bSLawrence Stewart #define	TWO_SUB_PT2		461
6167fef78bSLawrence Stewart 
6267fef78bSLawrence Stewart /* ~0.4 << CUBIC_SHIFT. */
6367fef78bSLawrence Stewart #define	CUBIC_C_FACTOR		102
6467fef78bSLawrence Stewart 
6567fef78bSLawrence Stewart /* CUBIC fast convergence factor: ~0.9 << CUBIC_SHIFT. */
6667fef78bSLawrence Stewart #define	CUBIC_FC_FACTOR		230
6767fef78bSLawrence Stewart 
6867fef78bSLawrence Stewart /* Don't trust s_rtt until this many rtt samples have been taken. */
6967fef78bSLawrence Stewart #define	CUBIC_MIN_RTT_SAMPLES	8
7067fef78bSLawrence Stewart 
7167fef78bSLawrence Stewart /* Userland only bits. */
7267fef78bSLawrence Stewart #ifndef _KERNEL
7367fef78bSLawrence Stewart 
7467fef78bSLawrence Stewart extern int hz;
7567fef78bSLawrence Stewart 
7667fef78bSLawrence Stewart /*
7767fef78bSLawrence Stewart  * Implementation based on the formulae found in the CUBIC Internet Draft
7867fef78bSLawrence Stewart  * "draft-rhee-tcpm-cubic-02".
7967fef78bSLawrence Stewart  *
8067fef78bSLawrence Stewart  * Note BETA used in cc_cubic is equal to (1-beta) in the I-D
8167fef78bSLawrence Stewart  */
8267fef78bSLawrence Stewart 
8367fef78bSLawrence Stewart static __inline float
8467fef78bSLawrence Stewart theoretical_cubic_k(double wmax_pkts)
8567fef78bSLawrence Stewart {
8667fef78bSLawrence Stewart 	double C;
8767fef78bSLawrence Stewart 
8867fef78bSLawrence Stewart 	C = 0.4;
8967fef78bSLawrence Stewart 
9067fef78bSLawrence Stewart 	return (pow((wmax_pkts * 0.2) / C, (1.0 / 3.0)) * pow(2, CUBIC_SHIFT));
9167fef78bSLawrence Stewart }
9267fef78bSLawrence Stewart 
9367fef78bSLawrence Stewart static __inline unsigned long
9467fef78bSLawrence Stewart theoretical_cubic_cwnd(int ticks_since_cong, unsigned long wmax, uint32_t smss)
9567fef78bSLawrence Stewart {
9667fef78bSLawrence Stewart 	double C, wmax_pkts;
9767fef78bSLawrence Stewart 
9867fef78bSLawrence Stewart 	C = 0.4;
9967fef78bSLawrence Stewart 	wmax_pkts = wmax / (double)smss;
10067fef78bSLawrence Stewart 
10167fef78bSLawrence Stewart 	return (smss * (wmax_pkts +
10267fef78bSLawrence Stewart 	    (C * pow(ticks_since_cong / (double)hz -
10367fef78bSLawrence Stewart 	    theoretical_cubic_k(wmax_pkts) / pow(2, CUBIC_SHIFT), 3.0))));
10467fef78bSLawrence Stewart }
10567fef78bSLawrence Stewart 
10667fef78bSLawrence Stewart static __inline unsigned long
10767fef78bSLawrence Stewart theoretical_reno_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
10867fef78bSLawrence Stewart     uint32_t smss)
10967fef78bSLawrence Stewart {
11067fef78bSLawrence Stewart 
11167fef78bSLawrence Stewart 	return ((wmax * 0.5) + ((ticks_since_cong / (float)rtt_ticks) * smss));
11267fef78bSLawrence Stewart }
11367fef78bSLawrence Stewart 
11467fef78bSLawrence Stewart static __inline unsigned long
11567fef78bSLawrence Stewart theoretical_tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
11667fef78bSLawrence Stewart     uint32_t smss)
11767fef78bSLawrence Stewart {
11867fef78bSLawrence Stewart 
11967fef78bSLawrence Stewart 	return ((wmax * 0.8) + ((3 * 0.2) / (2 - 0.2) *
12067fef78bSLawrence Stewart 	    (ticks_since_cong / (float)rtt_ticks) * smss));
12167fef78bSLawrence Stewart }
12267fef78bSLawrence Stewart 
12367fef78bSLawrence Stewart #endif /* !_KERNEL */
12467fef78bSLawrence Stewart 
12567fef78bSLawrence Stewart /*
12667fef78bSLawrence Stewart  * Compute the CUBIC K value used in the cwnd calculation, using an
12767fef78bSLawrence Stewart  * implementation of eqn 2 in the I-D. The method used
12867fef78bSLawrence Stewart  * here is adapted from Apple Computer Technical Report #KT-32.
12967fef78bSLawrence Stewart  */
13067fef78bSLawrence Stewart static __inline int64_t
13167fef78bSLawrence Stewart cubic_k(unsigned long wmax_pkts)
13267fef78bSLawrence Stewart {
13367fef78bSLawrence Stewart 	int64_t s, K;
13467fef78bSLawrence Stewart 	uint16_t p;
13567fef78bSLawrence Stewart 
13667fef78bSLawrence Stewart 	K = s = 0;
13767fef78bSLawrence Stewart 	p = 0;
13867fef78bSLawrence Stewart 
13967fef78bSLawrence Stewart 	/* (wmax * beta)/C with CUBIC_SHIFT worth of precision. */
14067fef78bSLawrence Stewart 	s = ((wmax_pkts * ONE_SUB_CUBIC_BETA) << CUBIC_SHIFT) / CUBIC_C_FACTOR;
14167fef78bSLawrence Stewart 
14267fef78bSLawrence Stewart 	/* Rebase s to be between 1 and 1/8 with a shift of CUBIC_SHIFT. */
14367fef78bSLawrence Stewart 	while (s >= 256) {
14467fef78bSLawrence Stewart 		s >>= 3;
14567fef78bSLawrence Stewart 		p++;
14667fef78bSLawrence Stewart 	}
14767fef78bSLawrence Stewart 
14867fef78bSLawrence Stewart 	/*
14967fef78bSLawrence Stewart 	 * Some magic constants taken from the Apple TR with appropriate
15067fef78bSLawrence Stewart 	 * shifts: 275 == 1.072302 << CUBIC_SHIFT, 98 == 0.3812513 <<
15167fef78bSLawrence Stewart 	 * CUBIC_SHIFT, 120 == 0.46946116 << CUBIC_SHIFT.
15267fef78bSLawrence Stewart 	 */
15367fef78bSLawrence Stewart 	K = (((s * 275) >> CUBIC_SHIFT) + 98) -
15467fef78bSLawrence Stewart 	    (((s * s * 120) >> CUBIC_SHIFT) >> CUBIC_SHIFT);
15567fef78bSLawrence Stewart 
15667fef78bSLawrence Stewart 	/* Multiply by 2^p to undo the rebasing of s from above. */
15767fef78bSLawrence Stewart 	return (K <<= p);
15867fef78bSLawrence Stewart }
15967fef78bSLawrence Stewart 
16067fef78bSLawrence Stewart /*
16167fef78bSLawrence Stewart  * Compute the new cwnd value using an implementation of eqn 1 from the I-D.
16267fef78bSLawrence Stewart  * Thanks to Kip Macy for help debugging this function.
16367fef78bSLawrence Stewart  *
16467fef78bSLawrence Stewart  * XXXLAS: Characterise bounds for overflow.
16567fef78bSLawrence Stewart  */
16667fef78bSLawrence Stewart static __inline unsigned long
16767fef78bSLawrence Stewart cubic_cwnd(int ticks_since_cong, unsigned long wmax, uint32_t smss, int64_t K)
16867fef78bSLawrence Stewart {
16967fef78bSLawrence Stewart 	int64_t cwnd;
17067fef78bSLawrence Stewart 
17167fef78bSLawrence Stewart 	/* K is in fixed point form with CUBIC_SHIFT worth of precision. */
17267fef78bSLawrence Stewart 
17367fef78bSLawrence Stewart 	/* t - K, with CUBIC_SHIFT worth of precision. */
17467fef78bSLawrence Stewart 	cwnd = ((int64_t)(ticks_since_cong << CUBIC_SHIFT) - (K * hz)) / hz;
17567fef78bSLawrence Stewart 
17667fef78bSLawrence Stewart 	/* (t - K)^3, with CUBIC_SHIFT^3 worth of precision. */
17767fef78bSLawrence Stewart 	cwnd *= (cwnd * cwnd);
17867fef78bSLawrence Stewart 
17967fef78bSLawrence Stewart 	/*
18067fef78bSLawrence Stewart 	 * C(t - K)^3 + wmax
18167fef78bSLawrence Stewart 	 * The down shift by CUBIC_SHIFT_4 is because cwnd has 4 lots of
18267fef78bSLawrence Stewart 	 * CUBIC_SHIFT included in the value. 3 from the cubing of cwnd above,
18367fef78bSLawrence Stewart 	 * and an extra from multiplying through by CUBIC_C_FACTOR.
18467fef78bSLawrence Stewart 	 */
18567fef78bSLawrence Stewart 	cwnd = ((cwnd * CUBIC_C_FACTOR * smss) >> CUBIC_SHIFT_4) + wmax;
18667fef78bSLawrence Stewart 
18767fef78bSLawrence Stewart 	return ((unsigned long)cwnd);
18867fef78bSLawrence Stewart }
18967fef78bSLawrence Stewart 
19067fef78bSLawrence Stewart /*
19167fef78bSLawrence Stewart  * Compute an approximation of the NewReno cwnd some number of ticks after a
19267fef78bSLawrence Stewart  * congestion event. RTT should be the average RTT estimate for the path
19367fef78bSLawrence Stewart  * measured over the previous congestion epoch and wmax is the value of cwnd at
19467fef78bSLawrence Stewart  * the last congestion event. The "TCP friendly" concept in the CUBIC I-D is
19567fef78bSLawrence Stewart  * rather tricky to understand and it turns out this function is not required.
19667fef78bSLawrence Stewart  * It is left here for reference.
19767fef78bSLawrence Stewart  */
19867fef78bSLawrence Stewart static __inline unsigned long
19967fef78bSLawrence Stewart reno_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
20067fef78bSLawrence Stewart     uint32_t smss)
20167fef78bSLawrence Stewart {
20267fef78bSLawrence Stewart 
20367fef78bSLawrence Stewart 	/*
20467fef78bSLawrence Stewart 	 * For NewReno, beta = 0.5, therefore: W_tcp(t) = wmax*0.5 + t/RTT
20567fef78bSLawrence Stewart 	 * W_tcp(t) deals with cwnd/wmax in pkts, so because our cwnd is in
20667fef78bSLawrence Stewart 	 * bytes, we have to multiply by smss.
20767fef78bSLawrence Stewart 	 */
20867fef78bSLawrence Stewart 	return (((wmax * RENO_BETA) + (((ticks_since_cong * smss)
20967fef78bSLawrence Stewart 	    << CUBIC_SHIFT) / rtt_ticks)) >> CUBIC_SHIFT);
21067fef78bSLawrence Stewart }
21167fef78bSLawrence Stewart 
21267fef78bSLawrence Stewart /*
21367fef78bSLawrence Stewart  * Compute an approximation of the "TCP friendly" cwnd some number of ticks
21467fef78bSLawrence Stewart  * after a congestion event that is designed to yield the same average cwnd as
21567fef78bSLawrence Stewart  * NewReno while using CUBIC's beta of 0.8. RTT should be the average RTT
21667fef78bSLawrence Stewart  * estimate for the path measured over the previous congestion epoch and wmax is
21767fef78bSLawrence Stewart  * the value of cwnd at the last congestion event.
21867fef78bSLawrence Stewart  */
21967fef78bSLawrence Stewart static __inline unsigned long
22067fef78bSLawrence Stewart tf_cwnd(int ticks_since_cong, int rtt_ticks, unsigned long wmax,
22167fef78bSLawrence Stewart     uint32_t smss)
22267fef78bSLawrence Stewart {
22367fef78bSLawrence Stewart 
22467fef78bSLawrence Stewart 	/* Equation 4 of I-D. */
22567fef78bSLawrence Stewart 	return (((wmax * CUBIC_BETA) + (((THREE_X_PT2 * ticks_since_cong *
22667fef78bSLawrence Stewart 	    smss) << CUBIC_SHIFT) / TWO_SUB_PT2 / rtt_ticks)) >> CUBIC_SHIFT);
22767fef78bSLawrence Stewart }
22867fef78bSLawrence Stewart 
22967fef78bSLawrence Stewart #endif /* _NETINET_CC_CUBIC_H_ */
230