xref: /freebsd/sys/netpfil/ipfw/dn_aqm_pie.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
191336b40SDon Lewis /*
291336b40SDon Lewis  * PIE - Proportional Integral controller Enhanced AQM algorithm.
391336b40SDon Lewis  *
491336b40SDon Lewis  * Copyright (C) 2016 Centre for Advanced Internet Architectures,
591336b40SDon Lewis  *  Swinburne University of Technology, Melbourne, Australia.
691336b40SDon Lewis  * Portions of this code were made possible in part by a gift from
791336b40SDon Lewis  *  The Comcast Innovation Fund.
891336b40SDon Lewis  * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
991336b40SDon Lewis  *
1091336b40SDon Lewis  * Redistribution and use in source and binary forms, with or without
1191336b40SDon Lewis  * modification, are permitted provided that the following conditions
1291336b40SDon Lewis  * are met:
1391336b40SDon Lewis  * 1. Redistributions of source code must retain the above copyright
1491336b40SDon Lewis  *    notice, this list of conditions and the following disclaimer.
1591336b40SDon Lewis  * 2. Redistributions in binary form must reproduce the above copyright
1691336b40SDon Lewis  *    notice, this list of conditions and the following disclaimer in the
1791336b40SDon Lewis  *    documentation and/or other materials provided with the distribution.
1891336b40SDon Lewis  *
1991336b40SDon Lewis  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2091336b40SDon Lewis  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2191336b40SDon Lewis  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2291336b40SDon Lewis  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2391336b40SDon Lewis  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2491336b40SDon Lewis  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2591336b40SDon Lewis  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2691336b40SDon Lewis  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2791336b40SDon Lewis  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2891336b40SDon Lewis  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2991336b40SDon Lewis  * SUCH DAMAGE.
3091336b40SDon Lewis  */
3191336b40SDon Lewis 
3291336b40SDon Lewis #include <sys/cdefs.h>
3391336b40SDon Lewis #include "opt_inet6.h"
3491336b40SDon Lewis 
3591336b40SDon Lewis #include <sys/param.h>
3691336b40SDon Lewis #include <sys/systm.h>
3791336b40SDon Lewis #include <sys/malloc.h>
3891336b40SDon Lewis #include <sys/mbuf.h>
3991336b40SDon Lewis #include <sys/kernel.h>
4091336b40SDon Lewis #include <sys/lock.h>
4191336b40SDon Lewis #include <sys/module.h>
4291336b40SDon Lewis #include <sys/mutex.h>
4391336b40SDon Lewis #include <sys/priv.h>
4491336b40SDon Lewis #include <sys/proc.h>
4591336b40SDon Lewis #include <sys/rwlock.h>
4691336b40SDon Lewis #include <sys/socket.h>
4791336b40SDon Lewis #include <sys/time.h>
4891336b40SDon Lewis #include <sys/sysctl.h>
4991336b40SDon Lewis 
5091336b40SDon Lewis #include <net/if.h>	/* IFNAMSIZ, struct ifaddr, ifq head, lock.h mutex.h */
5191336b40SDon Lewis #include <net/netisr.h>
5291336b40SDon Lewis #include <net/vnet.h>
5391336b40SDon Lewis 
5491336b40SDon Lewis #include <netinet/in.h>
5591336b40SDon Lewis #include <netinet/ip.h>		/* ip_len, ip_off */
5691336b40SDon Lewis #include <netinet/ip_var.h>	/* ip_output(), IP_FORWARDING */
5791336b40SDon Lewis #include <netinet/ip_fw.h>
5891336b40SDon Lewis #include <netinet/ip_dummynet.h>
5991336b40SDon Lewis #include <netinet/if_ether.h> /* various ether_* routines */
6091336b40SDon Lewis #include <netinet/ip6.h>       /* for ip6_input, ip6_output prototypes */
6191336b40SDon Lewis #include <netinet6/ip6_var.h>
6291336b40SDon Lewis #include <netpfil/ipfw/dn_heap.h>
6391336b40SDon Lewis 
6491336b40SDon Lewis #ifdef NEW_AQM
6591336b40SDon Lewis #include <netpfil/ipfw/ip_fw_private.h>
6691336b40SDon Lewis #include <netpfil/ipfw/ip_dn_private.h>
6791336b40SDon Lewis #include <netpfil/ipfw/dn_aqm.h>
6891336b40SDon Lewis #include <netpfil/ipfw/dn_aqm_pie.h>
6991336b40SDon Lewis #include <netpfil/ipfw/dn_sched.h>
7091336b40SDon Lewis 
7191336b40SDon Lewis /* for debugging */
7291336b40SDon Lewis #include <sys/syslog.h>
7391336b40SDon Lewis 
7491336b40SDon Lewis static struct dn_aqm pie_desc;
7591336b40SDon Lewis 
7691336b40SDon Lewis /*  PIE defaults
7791336b40SDon Lewis  * target=15ms, tupdate=15ms, max_burst=150ms,
7891336b40SDon Lewis  * max_ecnth=0.1, alpha=0.125, beta=1.25,
7991336b40SDon Lewis  */
8091336b40SDon Lewis struct dn_aqm_pie_parms pie_sysctl =
8191336b40SDon Lewis 	{ 15 * AQM_TIME_1MS,  15 * AQM_TIME_1MS, 150 * AQM_TIME_1MS,
8291336b40SDon Lewis 	PIE_SCALE/10 , PIE_SCALE * 0.125,  PIE_SCALE * 1.25 ,
8391336b40SDon Lewis 	PIE_CAPDROP_ENABLED | PIE_DEPRATEEST_ENABLED | PIE_DERAND_ENABLED };
8491336b40SDon Lewis 
8591336b40SDon Lewis static int
pie_sysctl_alpha_beta_handler(SYSCTL_HANDLER_ARGS)8691336b40SDon Lewis pie_sysctl_alpha_beta_handler(SYSCTL_HANDLER_ARGS)
8791336b40SDon Lewis {
8891336b40SDon Lewis 	int error;
8991336b40SDon Lewis 	long  value;
9091336b40SDon Lewis 
9191336b40SDon Lewis 	if (!strcmp(oidp->oid_name,"alpha"))
9291336b40SDon Lewis 		value = pie_sysctl.alpha;
9391336b40SDon Lewis 	else
9491336b40SDon Lewis 		value = pie_sysctl.beta;
9591336b40SDon Lewis 
9691336b40SDon Lewis 	value = value * 1000 / PIE_SCALE;
9791336b40SDon Lewis 	error = sysctl_handle_long(oidp, &value, 0, req);
9891336b40SDon Lewis 	if (error != 0 || req->newptr == NULL)
9991336b40SDon Lewis 		return (error);
10091336b40SDon Lewis 	if (value < 1 || value > 7 * PIE_SCALE)
10191336b40SDon Lewis 		return (EINVAL);
10291336b40SDon Lewis 	value = (value * PIE_SCALE) / 1000;
10391336b40SDon Lewis 	if (!strcmp(oidp->oid_name,"alpha"))
10491336b40SDon Lewis 			pie_sysctl.alpha = value;
10591336b40SDon Lewis 	else
10691336b40SDon Lewis 		pie_sysctl.beta = value;
10791336b40SDon Lewis 	return (0);
10891336b40SDon Lewis }
10991336b40SDon Lewis 
11091336b40SDon Lewis static int
pie_sysctl_target_tupdate_maxb_handler(SYSCTL_HANDLER_ARGS)11191336b40SDon Lewis pie_sysctl_target_tupdate_maxb_handler(SYSCTL_HANDLER_ARGS)
11291336b40SDon Lewis {
11391336b40SDon Lewis 	int error;
11491336b40SDon Lewis 	long  value;
11591336b40SDon Lewis 
11691336b40SDon Lewis 	if (!strcmp(oidp->oid_name,"target"))
11791336b40SDon Lewis 		value = pie_sysctl.qdelay_ref;
11891336b40SDon Lewis 	else if (!strcmp(oidp->oid_name,"tupdate"))
11991336b40SDon Lewis 		value = pie_sysctl.tupdate;
12091336b40SDon Lewis 	else
12191336b40SDon Lewis 		value = pie_sysctl.max_burst;
12291336b40SDon Lewis 
12391336b40SDon Lewis 	value = value / AQM_TIME_1US;
12491336b40SDon Lewis 	error = sysctl_handle_long(oidp, &value, 0, req);
12591336b40SDon Lewis 	if (error != 0 || req->newptr == NULL)
12691336b40SDon Lewis 		return (error);
12791336b40SDon Lewis 	if (value < 1 || value > 10 * AQM_TIME_1S)
12891336b40SDon Lewis 		return (EINVAL);
12991336b40SDon Lewis 	value = value * AQM_TIME_1US;
13091336b40SDon Lewis 
13191336b40SDon Lewis 	if (!strcmp(oidp->oid_name,"target"))
13291336b40SDon Lewis 		pie_sysctl.qdelay_ref  = value;
13391336b40SDon Lewis 	else if (!strcmp(oidp->oid_name,"tupdate"))
13491336b40SDon Lewis 		pie_sysctl.tupdate  = value;
13591336b40SDon Lewis 	else
13691336b40SDon Lewis 		pie_sysctl.max_burst = value;
13791336b40SDon Lewis 	return (0);
13891336b40SDon Lewis }
13991336b40SDon Lewis 
14091336b40SDon Lewis static int
pie_sysctl_max_ecnth_handler(SYSCTL_HANDLER_ARGS)14191336b40SDon Lewis pie_sysctl_max_ecnth_handler(SYSCTL_HANDLER_ARGS)
14291336b40SDon Lewis {
14391336b40SDon Lewis 	int error;
14491336b40SDon Lewis 	long  value;
14591336b40SDon Lewis 
14691336b40SDon Lewis 	value = pie_sysctl.max_ecnth;
14791336b40SDon Lewis 	value = value * 1000 / PIE_SCALE;
14891336b40SDon Lewis 	error = sysctl_handle_long(oidp, &value, 0, req);
14991336b40SDon Lewis 	if (error != 0 || req->newptr == NULL)
15091336b40SDon Lewis 		return (error);
15191336b40SDon Lewis 	if (value < 1 || value > PIE_SCALE)
15291336b40SDon Lewis 		return (EINVAL);
15391336b40SDon Lewis 	value = (value * PIE_SCALE) / 1000;
15491336b40SDon Lewis 	pie_sysctl.max_ecnth = value;
15591336b40SDon Lewis 	return (0);
15691336b40SDon Lewis }
15791336b40SDon Lewis 
15891336b40SDon Lewis /* define PIE sysctl variables */
15991336b40SDon Lewis SYSBEGIN(f4)
16091336b40SDon Lewis SYSCTL_DECL(_net_inet);
16191336b40SDon Lewis SYSCTL_DECL(_net_inet_ip);
16291336b40SDon Lewis SYSCTL_DECL(_net_inet_ip_dummynet);
1637029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_ip_dummynet, OID_AUTO, pie,
1647029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1657029da5cSPawel Biernacki     "PIE");
16691336b40SDon Lewis 
16791336b40SDon Lewis #ifdef SYSCTL_NODE
16891336b40SDon Lewis SYSCTL_PROC(_net_inet_ip_dummynet_pie, OID_AUTO, target,
1697029da5cSPawel Biernacki     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
17091336b40SDon Lewis     pie_sysctl_target_tupdate_maxb_handler, "L",
17191336b40SDon Lewis     "queue target in microsecond");
17291336b40SDon Lewis SYSCTL_PROC(_net_inet_ip_dummynet_pie, OID_AUTO, tupdate,
1737029da5cSPawel Biernacki     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
17491336b40SDon Lewis     pie_sysctl_target_tupdate_maxb_handler, "L",
17591336b40SDon Lewis     "the frequency of drop probability calculation in microsecond");
17691336b40SDon Lewis SYSCTL_PROC(_net_inet_ip_dummynet_pie, OID_AUTO, max_burst,
1777029da5cSPawel Biernacki     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
17891336b40SDon Lewis     pie_sysctl_target_tupdate_maxb_handler, "L",
17991336b40SDon Lewis     "Burst allowance interval in microsecond");
18091336b40SDon Lewis 
18191336b40SDon Lewis SYSCTL_PROC(_net_inet_ip_dummynet_pie, OID_AUTO, max_ecnth,
1827029da5cSPawel Biernacki     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
18391336b40SDon Lewis     pie_sysctl_max_ecnth_handler, "L",
18491336b40SDon Lewis     "ECN safeguard threshold scaled by 1000");
18591336b40SDon Lewis 
18691336b40SDon Lewis SYSCTL_PROC(_net_inet_ip_dummynet_pie, OID_AUTO, alpha,
1877029da5cSPawel Biernacki     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
18891336b40SDon Lewis     pie_sysctl_alpha_beta_handler, "L",
18991336b40SDon Lewis     "PIE alpha scaled by 1000");
19091336b40SDon Lewis SYSCTL_PROC(_net_inet_ip_dummynet_pie, OID_AUTO, beta,
1917029da5cSPawel Biernacki     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
19291336b40SDon Lewis     pie_sysctl_alpha_beta_handler, "L",
19391336b40SDon Lewis     "beta scaled by 1000");
19491336b40SDon Lewis #endif
19591336b40SDon Lewis 
19691336b40SDon Lewis /*
19791336b40SDon Lewis  * Callout function for drop probability calculation
19891336b40SDon Lewis  * This function is called over tupdate ms and takes pointer of PIE
19991336b40SDon Lewis  * status variables as an argument
20091336b40SDon Lewis   */
20191336b40SDon Lewis static void
calculate_drop_prob(void * x)20291336b40SDon Lewis calculate_drop_prob(void *x)
20391336b40SDon Lewis {
20491336b40SDon Lewis 	int64_t p, prob, oldprob;
20591336b40SDon Lewis 	struct dn_aqm_pie_parms *pprms;
20691336b40SDon Lewis 	struct pie_status *pst = (struct pie_status *) x;
20736fb8be6SDon Lewis 	int p_isneg;
20891336b40SDon Lewis 
20991336b40SDon Lewis 	pprms = pst->parms;
21091336b40SDon Lewis 	prob = pst->drop_prob;
21191336b40SDon Lewis 
212d196c9eeSDon Lewis 	/* calculate current qdelay using DRE method.
213d196c9eeSDon Lewis 	 * If TS is used and no data in the queue, reset current_qdelay
214d196c9eeSDon Lewis 	 * as it stays at last value during dequeue process.
215d196c9eeSDon Lewis 	*/
216d196c9eeSDon Lewis 	if (pprms->flags & PIE_DEPRATEEST_ENABLED)
21791336b40SDon Lewis 		pst->current_qdelay = ((uint64_t)pst->pq->ni.len_bytes *
21891336b40SDon Lewis 			pst->avg_dq_time) >> PIE_DQ_THRESHOLD_BITS;
219d196c9eeSDon Lewis 	else
220d196c9eeSDon Lewis 		if (!pst->pq->ni.len_bytes)
221d196c9eeSDon Lewis 			 pst->current_qdelay = 0;
22291336b40SDon Lewis 
22391336b40SDon Lewis 	/* calculate drop probability */
22491336b40SDon Lewis 	p = (int64_t)pprms->alpha *
22591336b40SDon Lewis 		((int64_t)pst->current_qdelay - (int64_t)pprms->qdelay_ref);
22691336b40SDon Lewis 	p +=(int64_t) pprms->beta *
22791336b40SDon Lewis 		((int64_t)pst->current_qdelay - (int64_t)pst->qdelay_old);
22891336b40SDon Lewis 
22936fb8be6SDon Lewis 	/* take absolute value so right shift result is well defined */
23036fb8be6SDon Lewis 	p_isneg = p < 0;
23136fb8be6SDon Lewis 	if (p_isneg) {
23236fb8be6SDon Lewis 		p = -p;
23336fb8be6SDon Lewis 	}
23436fb8be6SDon Lewis 
23591336b40SDon Lewis 	/* We PIE_MAX_PROB shift by 12-bits to increase the division precision */
23691336b40SDon Lewis 	p *= (PIE_MAX_PROB << 12) / AQM_TIME_1S;
23791336b40SDon Lewis 
23891336b40SDon Lewis 	/* auto-tune drop probability */
239d6736547SDon Lewis 	if (prob < (PIE_MAX_PROB / 1000000)) /* 0.000001 */
24091336b40SDon Lewis 		p >>= 11 + PIE_FIX_POINT_BITS + 12;
241d6736547SDon Lewis 	else if (prob < (PIE_MAX_PROB / 100000)) /* 0.00001 */
24291336b40SDon Lewis 		p >>= 9 + PIE_FIX_POINT_BITS + 12;
243d6736547SDon Lewis 	else if (prob < (PIE_MAX_PROB / 10000)) /* 0.0001 */
24491336b40SDon Lewis 		p >>= 7 + PIE_FIX_POINT_BITS + 12;
245d6736547SDon Lewis 	else if (prob < (PIE_MAX_PROB / 1000)) /* 0.001 */
24691336b40SDon Lewis 		p >>= 5 + PIE_FIX_POINT_BITS + 12;
247d6736547SDon Lewis 	else if (prob < (PIE_MAX_PROB / 100)) /* 0.01 */
24891336b40SDon Lewis 		p >>= 3 + PIE_FIX_POINT_BITS + 12;
249d6736547SDon Lewis 	else if (prob < (PIE_MAX_PROB / 10)) /* 0.1 */
25091336b40SDon Lewis 		p >>= 1 + PIE_FIX_POINT_BITS + 12;
25191336b40SDon Lewis 	else
25291336b40SDon Lewis 		p >>= PIE_FIX_POINT_BITS + 12;
25391336b40SDon Lewis 
25491336b40SDon Lewis 	oldprob = prob;
25591336b40SDon Lewis 
25636fb8be6SDon Lewis 	if (p_isneg) {
25736fb8be6SDon Lewis 		prob = prob - p;
25836fb8be6SDon Lewis 
25936fb8be6SDon Lewis 		/* check for multiplication underflow */
26036fb8be6SDon Lewis 		if (prob > oldprob) {
26136fb8be6SDon Lewis 			prob= 0;
26236fb8be6SDon Lewis 			D("underflow");
26336fb8be6SDon Lewis 		}
26436fb8be6SDon Lewis 	} else {
26591336b40SDon Lewis 		/* Cap Drop adjustment */
26636fb8be6SDon Lewis 		if ((pprms->flags & PIE_CAPDROP_ENABLED) &&
26736fb8be6SDon Lewis 		    prob >= PIE_MAX_PROB / 10 &&
26836fb8be6SDon Lewis 		    p > PIE_MAX_PROB / 50 ) {
26991336b40SDon Lewis 			p = PIE_MAX_PROB / 50;
27036fb8be6SDon Lewis 		}
27191336b40SDon Lewis 
27291336b40SDon Lewis 		prob = prob + p;
27391336b40SDon Lewis 
27436fb8be6SDon Lewis 		/* check for multiplication overflow */
27591336b40SDon Lewis 		if (prob<oldprob) {
27691336b40SDon Lewis 			D("overflow");
27791336b40SDon Lewis 			prob= PIE_MAX_PROB;
27891336b40SDon Lewis 		}
27991336b40SDon Lewis 	}
28036fb8be6SDon Lewis 
28136fb8be6SDon Lewis 	/*
28236fb8be6SDon Lewis 	 * decay the drop probability exponentially
28336fb8be6SDon Lewis 	 * and restrict it to range 0 to PIE_MAX_PROB
28436fb8be6SDon Lewis 	 */
28536fb8be6SDon Lewis 	if (prob < 0) {
28691336b40SDon Lewis 		prob = 0;
28736fb8be6SDon Lewis 	} else {
28836fb8be6SDon Lewis 		if (pst->current_qdelay == 0 && pst->qdelay_old == 0) {
28936fb8be6SDon Lewis 			/* 0.98 ~= 1- 1/64 */
29036fb8be6SDon Lewis 			prob = prob - (prob >> 6);
29191336b40SDon Lewis 		}
29291336b40SDon Lewis 
29336fb8be6SDon Lewis 		if (prob > PIE_MAX_PROB) {
29491336b40SDon Lewis 			prob = PIE_MAX_PROB;
29536fb8be6SDon Lewis 		}
29636fb8be6SDon Lewis 	}
29791336b40SDon Lewis 
29891336b40SDon Lewis 	pst->drop_prob = prob;
29991336b40SDon Lewis 
30091336b40SDon Lewis 	/* store current queue delay value in old queue delay*/
30191336b40SDon Lewis 	pst->qdelay_old = pst->current_qdelay;
30291336b40SDon Lewis 
30391336b40SDon Lewis 	/* update burst allowance */
30491336b40SDon Lewis 	if ((pst->sflags & PIE_ACTIVE) && pst->burst_allowance>0) {
30591336b40SDon Lewis 
30691336b40SDon Lewis 		if (pst->burst_allowance > pprms->tupdate )
30791336b40SDon Lewis 			pst->burst_allowance -= pprms->tupdate;
30891336b40SDon Lewis 		else
30991336b40SDon Lewis 			pst->burst_allowance = 0;
31091336b40SDon Lewis 	}
31191336b40SDon Lewis 
31291336b40SDon Lewis 	/* reschedule calculate_drop_prob function */
31391336b40SDon Lewis 	if (pst->sflags & PIE_ACTIVE)
31491336b40SDon Lewis 		callout_reset_sbt(&pst->aqm_pie_callout,
31591336b40SDon Lewis 			(uint64_t)pprms->tupdate * SBT_1US, 0, calculate_drop_prob, pst, 0);
31691336b40SDon Lewis 
31791336b40SDon Lewis 	mtx_unlock(&pst->lock_mtx);
31891336b40SDon Lewis }
31991336b40SDon Lewis 
32091336b40SDon Lewis /*
32191336b40SDon Lewis  * Extract a packet from the head of queue 'q'
32291336b40SDon Lewis  * Return a packet or NULL if the queue is empty.
32391336b40SDon Lewis  * If getts is set, also extract packet's timestamp from mtag.
32491336b40SDon Lewis  */
32591336b40SDon Lewis static struct mbuf *
pie_extract_head(struct dn_queue * q,aqm_time_t * pkt_ts,int getts)32691336b40SDon Lewis pie_extract_head(struct dn_queue *q, aqm_time_t *pkt_ts, int getts)
32791336b40SDon Lewis {
32891336b40SDon Lewis 	struct m_tag *mtag;
329*8ef7beb2SGleb Smirnoff 	struct mbuf *m;
33091336b40SDon Lewis 
331*8ef7beb2SGleb Smirnoff next:	m = q->mq.head;
33291336b40SDon Lewis 	if (m == NULL)
33391336b40SDon Lewis 		return m;
33491336b40SDon Lewis 	q->mq.head = m->m_nextpkt;
33591336b40SDon Lewis 
33691336b40SDon Lewis 	/* Update stats */
33791336b40SDon Lewis 	update_stats(q, -m->m_pkthdr.len, 0);
33891336b40SDon Lewis 
33991336b40SDon Lewis 	if (q->ni.length == 0) /* queue is now idle */
340fe3bcfbdSTom Jones 			q->q_time = V_dn_cfg.curr_time;
34191336b40SDon Lewis 
34291336b40SDon Lewis 	if (getts) {
34391336b40SDon Lewis 		/* extract packet TS*/
34491336b40SDon Lewis 		mtag = m_tag_locate(m, MTAG_ABI_COMPAT, DN_AQM_MTAG_TS, NULL);
34591336b40SDon Lewis 		if (mtag == NULL) {
34691336b40SDon Lewis 			D("PIE timestamp mtag not found!");
34791336b40SDon Lewis 			*pkt_ts = 0;
34891336b40SDon Lewis 		} else {
34991336b40SDon Lewis 			*pkt_ts = *(aqm_time_t *)(mtag + 1);
35091336b40SDon Lewis 			m_tag_delete(m,mtag);
35191336b40SDon Lewis 		}
35291336b40SDon Lewis 	}
353*8ef7beb2SGleb Smirnoff 	if (m->m_pkthdr.rcvif != NULL &&
354*8ef7beb2SGleb Smirnoff 	    __predict_false(m_rcvif_restore(m) == NULL)) {
355*8ef7beb2SGleb Smirnoff 		m_freem(m);
356*8ef7beb2SGleb Smirnoff 		goto next;
357*8ef7beb2SGleb Smirnoff 	}
35891336b40SDon Lewis 	return m;
35991336b40SDon Lewis }
36091336b40SDon Lewis 
36191336b40SDon Lewis /*
36291336b40SDon Lewis  * Initiate PIE  variable and optionally activate it
36391336b40SDon Lewis  */
36491336b40SDon Lewis __inline static void
init_activate_pie(struct pie_status * pst,int resettimer)36591336b40SDon Lewis init_activate_pie(struct pie_status *pst, int resettimer)
36691336b40SDon Lewis {
36791336b40SDon Lewis 	struct dn_aqm_pie_parms *pprms;
36891336b40SDon Lewis 
36991336b40SDon Lewis 	mtx_lock(&pst->lock_mtx);
37091336b40SDon Lewis 	pprms = pst->parms;
37191336b40SDon Lewis 	pst->drop_prob = 0;
37291336b40SDon Lewis 	pst->qdelay_old = 0;
37391336b40SDon Lewis 	pst->burst_allowance = pprms->max_burst;
37491336b40SDon Lewis 	pst->accu_prob = 0;
37591336b40SDon Lewis 	pst->dq_count = 0;
37691336b40SDon Lewis 	pst->avg_dq_time = 0;
37791336b40SDon Lewis 	pst->sflags = PIE_INMEASUREMENT;
37891336b40SDon Lewis 	pst->measurement_start = AQM_UNOW;
37991336b40SDon Lewis 
38091336b40SDon Lewis 	if (resettimer) {
38191336b40SDon Lewis 		pst->sflags |= PIE_ACTIVE;
38291336b40SDon Lewis 		callout_reset_sbt(&pst->aqm_pie_callout,
38391336b40SDon Lewis 			(uint64_t)pprms->tupdate * SBT_1US,
38491336b40SDon Lewis 			0, calculate_drop_prob, pst, 0);
38591336b40SDon Lewis 	}
38691336b40SDon Lewis 	//DX(2, "PIE Activated");
38791336b40SDon Lewis 	mtx_unlock(&pst->lock_mtx);
38891336b40SDon Lewis }
38991336b40SDon Lewis 
39091336b40SDon Lewis /*
39191336b40SDon Lewis  * Deactivate PIE and stop probe update callout
39291336b40SDon Lewis  */
39391336b40SDon Lewis __inline static void
deactivate_pie(struct pie_status * pst)39491336b40SDon Lewis deactivate_pie(struct pie_status *pst)
39591336b40SDon Lewis {
39691336b40SDon Lewis 	mtx_lock(&pst->lock_mtx);
39791336b40SDon Lewis 	pst->sflags &= ~(PIE_ACTIVE | PIE_INMEASUREMENT);
39891336b40SDon Lewis 	callout_stop(&pst->aqm_pie_callout);
39991336b40SDon Lewis 	//D("PIE Deactivated");
40091336b40SDon Lewis 	mtx_unlock(&pst->lock_mtx);
40191336b40SDon Lewis }
40291336b40SDon Lewis 
40391336b40SDon Lewis /*
40491336b40SDon Lewis  * Dequeue and return a pcaket from queue 'q' or NULL if 'q' is empty.
40591336b40SDon Lewis  * Also, caculate depature time or queue delay using timestamp
40691336b40SDon Lewis  */
40791336b40SDon Lewis static struct mbuf *
aqm_pie_dequeue(struct dn_queue * q)40891336b40SDon Lewis aqm_pie_dequeue(struct dn_queue *q)
40991336b40SDon Lewis {
41091336b40SDon Lewis 	struct mbuf *m;
41191336b40SDon Lewis 	struct dn_aqm_pie_parms *pprms;
41291336b40SDon Lewis 	struct pie_status *pst;
41391336b40SDon Lewis 	aqm_time_t now;
41491336b40SDon Lewis 	aqm_time_t pkt_ts, dq_time;
41591336b40SDon Lewis 	int32_t w;
41691336b40SDon Lewis 
41791336b40SDon Lewis 	pst  = q->aqm_status;
41891336b40SDon Lewis 	pprms = pst->parms;
41991336b40SDon Lewis 
42091336b40SDon Lewis 	/*we extarct packet ts only when Departure Rate Estimation dis not used*/
42191336b40SDon Lewis 	m = pie_extract_head(q, &pkt_ts, !(pprms->flags & PIE_DEPRATEEST_ENABLED));
42291336b40SDon Lewis 
42391336b40SDon Lewis 	if (!m || !(pst->sflags & PIE_ACTIVE))
42491336b40SDon Lewis 		return m;
42591336b40SDon Lewis 
42691336b40SDon Lewis 	now = AQM_UNOW;
42791336b40SDon Lewis 	if (pprms->flags & PIE_DEPRATEEST_ENABLED) {
42891336b40SDon Lewis 		/* calculate average depature time */
42991336b40SDon Lewis 		if(pst->sflags & PIE_INMEASUREMENT) {
43091336b40SDon Lewis 			pst->dq_count += m->m_pkthdr.len;
43191336b40SDon Lewis 
43291336b40SDon Lewis 			if (pst->dq_count >= PIE_DQ_THRESHOLD) {
43391336b40SDon Lewis 				dq_time = now - pst->measurement_start;
43491336b40SDon Lewis 
43591336b40SDon Lewis 				/*
43691336b40SDon Lewis 				 * if we don't have old avg dq_time i.e PIE is (re)initialized,
43791336b40SDon Lewis 				 * don't use weight to calculate new avg_dq_time
43891336b40SDon Lewis 				 */
43991336b40SDon Lewis 				if(pst->avg_dq_time == 0)
44091336b40SDon Lewis 					pst->avg_dq_time = dq_time;
44191336b40SDon Lewis 				else {
44291336b40SDon Lewis 					/*
44391336b40SDon Lewis 					 * weight = PIE_DQ_THRESHOLD/2^6, but we scaled
44491336b40SDon Lewis 					 * weight by 2^8. Thus, scaled
44591336b40SDon Lewis 					 * weight = PIE_DQ_THRESHOLD /2^8
44691336b40SDon Lewis 					 * */
44791336b40SDon Lewis 					w = PIE_DQ_THRESHOLD >> 8;
44891336b40SDon Lewis 					pst->avg_dq_time = (dq_time* w
44991336b40SDon Lewis 						+ (pst->avg_dq_time * ((1L << 8) - w))) >> 8;
45091336b40SDon Lewis 					pst->sflags &= ~PIE_INMEASUREMENT;
45191336b40SDon Lewis 				}
45291336b40SDon Lewis 			}
45391336b40SDon Lewis 		}
45491336b40SDon Lewis 
45591336b40SDon Lewis 		/*
456f70fc437SGordon Bergling 		 * Start new measurement cycle when the queue has
45791336b40SDon Lewis 		 * PIE_DQ_THRESHOLD worth of bytes.
45891336b40SDon Lewis 		 */
45991336b40SDon Lewis 		if(!(pst->sflags & PIE_INMEASUREMENT) &&
46091336b40SDon Lewis 			q->ni.len_bytes >= PIE_DQ_THRESHOLD) {
46191336b40SDon Lewis 			pst->sflags |= PIE_INMEASUREMENT;
46291336b40SDon Lewis 			pst->measurement_start = now;
46391336b40SDon Lewis 			pst->dq_count = 0;
46491336b40SDon Lewis 		}
46591336b40SDon Lewis 	}
46691336b40SDon Lewis 	/* Optionally, use packet timestamp to estimate queue delay */
46791336b40SDon Lewis 	else
46891336b40SDon Lewis 		pst->current_qdelay = now - pkt_ts;
46991336b40SDon Lewis 
47091336b40SDon Lewis 	return m;
47191336b40SDon Lewis }
47291336b40SDon Lewis 
47391336b40SDon Lewis /*
47491336b40SDon Lewis  * Enqueue a packet in q, subject to space and  PIE queue management policy
47591336b40SDon Lewis  * (whose parameters are in q->fs).
47691336b40SDon Lewis  * Update stats for the queue and the scheduler.
47791336b40SDon Lewis  * Return 0 on success, 1 on drop. The packet is consumed anyways.
47891336b40SDon Lewis  */
47991336b40SDon Lewis static int
aqm_pie_enqueue(struct dn_queue * q,struct mbuf * m)48091336b40SDon Lewis aqm_pie_enqueue(struct dn_queue *q, struct mbuf* m)
48191336b40SDon Lewis {
48291336b40SDon Lewis 	struct dn_fs *f;
48391336b40SDon Lewis 	uint64_t len;
48491336b40SDon Lewis 	uint32_t qlen;
48591336b40SDon Lewis 	struct pie_status *pst;
48691336b40SDon Lewis 	struct dn_aqm_pie_parms *pprms;
48791336b40SDon Lewis 	int t;
48891336b40SDon Lewis 
48991336b40SDon Lewis 	len = m->m_pkthdr.len;
49091336b40SDon Lewis 	pst  = q->aqm_status;
49191336b40SDon Lewis 	if(!pst) {
49291336b40SDon Lewis 		DX(2, "PIE queue is not initialized\n");
49391336b40SDon Lewis 		update_stats(q, 0, 1);
49491336b40SDon Lewis 		FREE_PKT(m);
49591336b40SDon Lewis 		return 1;
49691336b40SDon Lewis 	}
49791336b40SDon Lewis 
49891336b40SDon Lewis 	f = &(q->fs->fs);
49991336b40SDon Lewis 	pprms = pst->parms;
50091336b40SDon Lewis 	t = ENQUE;
50191336b40SDon Lewis 
50291336b40SDon Lewis 	/* get current queue length in bytes or packets*/
50391336b40SDon Lewis 	qlen = (f->flags & DN_QSIZE_BYTES) ?
50491336b40SDon Lewis 		q->ni.len_bytes : q->ni.length;
50591336b40SDon Lewis 
50691336b40SDon Lewis 	/* check for queue size and drop the tail if exceed queue limit*/
50791336b40SDon Lewis 	if (qlen >= f->qsize)
50891336b40SDon Lewis 		t = DROP;
50991336b40SDon Lewis 	/* drop/mark the packet when PIE is active and burst time elapsed */
51091336b40SDon Lewis 	else if ((pst->sflags & PIE_ACTIVE) && pst->burst_allowance==0
51191336b40SDon Lewis 			&& drop_early(pst, q->ni.len_bytes) == DROP) {
51291336b40SDon Lewis 				/*
51391336b40SDon Lewis 				 * if drop_prob over ECN threshold, drop the packet
51491336b40SDon Lewis 				 * otherwise mark and enqueue it.
51591336b40SDon Lewis 				 */
51691336b40SDon Lewis 				if ((pprms->flags & PIE_ECN_ENABLED) && pst->drop_prob <
51791336b40SDon Lewis 					(pprms->max_ecnth << (PIE_PROB_BITS - PIE_FIX_POINT_BITS))
51891336b40SDon Lewis 					&& ecn_mark(m))
51991336b40SDon Lewis 					t = ENQUE;
52091336b40SDon Lewis 				else
52191336b40SDon Lewis 					t = DROP;
52291336b40SDon Lewis 	}
52391336b40SDon Lewis 
52491336b40SDon Lewis 	/* Turn PIE on when 1/3 of the queue is full */
52591336b40SDon Lewis 	if (!(pst->sflags & PIE_ACTIVE) && qlen >= pst->one_third_q_size) {
52691336b40SDon Lewis 		init_activate_pie(pst, 1);
52791336b40SDon Lewis 	}
52891336b40SDon Lewis 
52991336b40SDon Lewis 	/*  Reset burst tolerance and optinally turn PIE off*/
53091336b40SDon Lewis 	if ((pst->sflags & PIE_ACTIVE) && pst->drop_prob == 0 &&
53191336b40SDon Lewis 		pst->current_qdelay < (pprms->qdelay_ref >> 1) &&
53291336b40SDon Lewis 		pst->qdelay_old < (pprms->qdelay_ref >> 1)) {
53391336b40SDon Lewis 			pst->burst_allowance = pprms->max_burst;
53491336b40SDon Lewis 			if ((pprms->flags & PIE_ON_OFF_MODE_ENABLED) && qlen<=0)
53591336b40SDon Lewis 				deactivate_pie(pst);
53691336b40SDon Lewis 	}
53791336b40SDon Lewis 
53891336b40SDon Lewis 	/* Timestamp the packet if Departure Rate Estimation is disabled */
53991336b40SDon Lewis 	if (t != DROP && !(pprms->flags & PIE_DEPRATEEST_ENABLED)) {
54091336b40SDon Lewis 		/* Add TS to mbuf as a TAG */
54191336b40SDon Lewis 		struct m_tag *mtag;
54291336b40SDon Lewis 		mtag = m_tag_locate(m, MTAG_ABI_COMPAT, DN_AQM_MTAG_TS, NULL);
54391336b40SDon Lewis 		if (mtag == NULL)
54491336b40SDon Lewis 			mtag = m_tag_alloc(MTAG_ABI_COMPAT, DN_AQM_MTAG_TS,
54591336b40SDon Lewis 				sizeof(aqm_time_t), M_NOWAIT);
54691336b40SDon Lewis 		if (mtag == NULL) {
54791336b40SDon Lewis 			t = DROP;
548c4a6258dSMark Johnston 		} else {
54991336b40SDon Lewis 			*(aqm_time_t *)(mtag + 1) = AQM_UNOW;
55091336b40SDon Lewis 			m_tag_prepend(m, mtag);
55191336b40SDon Lewis 		}
552c4a6258dSMark Johnston 	}
55391336b40SDon Lewis 
55491336b40SDon Lewis 	if (t != DROP) {
55591336b40SDon Lewis 		mq_append(&q->mq, m);
55691336b40SDon Lewis 		update_stats(q, len, 0);
55791336b40SDon Lewis 		return (0);
55891336b40SDon Lewis 	} else {
55991336b40SDon Lewis 		update_stats(q, 0, 1);
56091336b40SDon Lewis 
56191336b40SDon Lewis 		/* reset accu_prob after packet drop */
56291336b40SDon Lewis 		pst->accu_prob = 0;
56391336b40SDon Lewis 		FREE_PKT(m);
56491336b40SDon Lewis 		return 1;
56591336b40SDon Lewis 	}
56691336b40SDon Lewis 	return 0;
56791336b40SDon Lewis }
56891336b40SDon Lewis 
56991336b40SDon Lewis /*
57091336b40SDon Lewis  * initialize PIE for queue 'q'
57191336b40SDon Lewis  * First allocate memory for PIE status.
57291336b40SDon Lewis  */
57391336b40SDon Lewis static int
aqm_pie_init(struct dn_queue * q)57491336b40SDon Lewis aqm_pie_init(struct dn_queue *q)
57591336b40SDon Lewis {
57691336b40SDon Lewis 	struct pie_status *pst;
57791336b40SDon Lewis 	struct dn_aqm_pie_parms *pprms;
57891336b40SDon Lewis 	int err = 0;
57991336b40SDon Lewis 
58091336b40SDon Lewis 	pprms = q->fs->aqmcfg;
58191336b40SDon Lewis 
58291336b40SDon Lewis 	do { /* exit with break when error occurs*/
58391336b40SDon Lewis 		if (!pprms){
58412be18c7SDon Lewis 			DX(2, "AQM_PIE is not configured");
58591336b40SDon Lewis 			err = EINVAL;
58691336b40SDon Lewis 			break;
58791336b40SDon Lewis 		}
58891336b40SDon Lewis 
58991336b40SDon Lewis 		q->aqm_status = malloc(sizeof(struct pie_status),
59091336b40SDon Lewis 				 M_DUMMYNET, M_NOWAIT | M_ZERO);
59191336b40SDon Lewis 		if (q->aqm_status == NULL) {
59291336b40SDon Lewis 			D("cannot allocate PIE private data");
59391336b40SDon Lewis 			err =  ENOMEM ;
59491336b40SDon Lewis 			break;
59591336b40SDon Lewis 		}
59691336b40SDon Lewis 
59791336b40SDon Lewis 		pst = q->aqm_status;
5983f3e4f3cSKristof Provost 		dummynet_sched_lock();
59991336b40SDon Lewis 		/* increase reference count for PIE module */
60091336b40SDon Lewis 		pie_desc.ref_count++;
6013f3e4f3cSKristof Provost 		dummynet_sched_unlock();
60291336b40SDon Lewis 
60391336b40SDon Lewis 		pst->pq = q;
60491336b40SDon Lewis 		pst->parms = pprms;
60591336b40SDon Lewis 
60691336b40SDon Lewis 		/* For speed optimization, we caculate 1/3 queue size once here */
60791336b40SDon Lewis 		// we can use x/3 = (x >>2) + (x >>4) + (x >>7)
60891336b40SDon Lewis 		pst->one_third_q_size = q->fs->fs.qsize/3;
60991336b40SDon Lewis 
61091336b40SDon Lewis 		mtx_init(&pst->lock_mtx, "mtx_pie", NULL, MTX_DEF);
61191336b40SDon Lewis 		callout_init_mtx(&pst->aqm_pie_callout, &pst->lock_mtx,
61291336b40SDon Lewis 			CALLOUT_RETURNUNLOCKED);
61391336b40SDon Lewis 
61491336b40SDon Lewis 		pst->current_qdelay = 0;
61591336b40SDon Lewis 		init_activate_pie(pst, !(pprms->flags & PIE_ON_OFF_MODE_ENABLED));
61691336b40SDon Lewis 
61791336b40SDon Lewis 		//DX(2, "aqm_PIE_init");
61891336b40SDon Lewis 
61991336b40SDon Lewis 	} while(0);
62091336b40SDon Lewis 
62191336b40SDon Lewis 	return err;
62291336b40SDon Lewis }
62391336b40SDon Lewis 
62491336b40SDon Lewis /*
62512be18c7SDon Lewis  * Callout function to destroy pie mtx and free PIE status memory
62612be18c7SDon Lewis  */
62712be18c7SDon Lewis static void
pie_callout_cleanup(void * x)62812be18c7SDon Lewis pie_callout_cleanup(void *x)
62912be18c7SDon Lewis {
63012be18c7SDon Lewis 	struct pie_status *pst = (struct pie_status *) x;
63112be18c7SDon Lewis 
63212be18c7SDon Lewis 	mtx_unlock(&pst->lock_mtx);
63312be18c7SDon Lewis 	mtx_destroy(&pst->lock_mtx);
63412be18c7SDon Lewis 	free(x, M_DUMMYNET);
6353f3e4f3cSKristof Provost 	dummynet_sched_lock();
63612be18c7SDon Lewis 	pie_desc.ref_count--;
6373f3e4f3cSKristof Provost 	dummynet_sched_unlock();
63812be18c7SDon Lewis }
63912be18c7SDon Lewis 
64012be18c7SDon Lewis /*
64191336b40SDon Lewis  * Clean up PIE status for queue 'q'
64291336b40SDon Lewis  * Destroy memory allocated for PIE status.
64391336b40SDon Lewis  */
64491336b40SDon Lewis static int
aqm_pie_cleanup(struct dn_queue * q)64591336b40SDon Lewis aqm_pie_cleanup(struct dn_queue *q)
64691336b40SDon Lewis {
64791336b40SDon Lewis 
64891336b40SDon Lewis 	if(!q) {
64991336b40SDon Lewis 		D("q is null");
65091336b40SDon Lewis 		return 0;
65191336b40SDon Lewis 	}
65291336b40SDon Lewis 	struct pie_status *pst  = q->aqm_status;
65391336b40SDon Lewis 	if(!pst) {
65491336b40SDon Lewis 		//D("queue is already cleaned up");
65591336b40SDon Lewis 		return 0;
65691336b40SDon Lewis 	}
65791336b40SDon Lewis 	if(!q->fs || !q->fs->aqmcfg) {
65891336b40SDon Lewis 		D("fs is null or no cfg");
65991336b40SDon Lewis 		return 1;
66091336b40SDon Lewis 	}
66191336b40SDon Lewis 	if (q->fs->aqmfp && q->fs->aqmfp->type !=DN_AQM_PIE) {
66291336b40SDon Lewis 		D("Not PIE fs (%d)", q->fs->fs.fs_nr);
66391336b40SDon Lewis 		return 1;
66491336b40SDon Lewis 	}
66591336b40SDon Lewis 
66612be18c7SDon Lewis 	/*
66712be18c7SDon Lewis 	 * Free PIE status allocated memory using pie_callout_cleanup() callout
66812be18c7SDon Lewis 	 * function to avoid any potential race.
66912be18c7SDon Lewis 	 * We reset aqm_pie_callout to call pie_callout_cleanup() in next 1um. This
67012be18c7SDon Lewis 	 * stops the scheduled calculate_drop_prob() callout and call pie_callout_cleanup()
67112be18c7SDon Lewis 	 * which does memory freeing.
67212be18c7SDon Lewis 	 */
67391336b40SDon Lewis 	mtx_lock(&pst->lock_mtx);
67412be18c7SDon Lewis 	callout_reset_sbt(&pst->aqm_pie_callout,
67512be18c7SDon Lewis 		SBT_1US, 0, pie_callout_cleanup, pst, 0);
67612be18c7SDon Lewis 	q->aqm_status = NULL;
67712be18c7SDon Lewis 	mtx_unlock(&pst->lock_mtx);
67891336b40SDon Lewis 
67991336b40SDon Lewis 	return 0;
68091336b40SDon Lewis }
68191336b40SDon Lewis 
68291336b40SDon Lewis /*
68391336b40SDon Lewis  * Config PIE parameters
68491336b40SDon Lewis  * also allocate memory for PIE configurations
68591336b40SDon Lewis  */
68691336b40SDon Lewis static int
aqm_pie_config(struct dn_fsk * fs,struct dn_extra_parms * ep,int len)68791336b40SDon Lewis aqm_pie_config(struct dn_fsk* fs, struct dn_extra_parms *ep, int len)
68891336b40SDon Lewis {
68991336b40SDon Lewis 	struct dn_aqm_pie_parms *pcfg;
69091336b40SDon Lewis 
69191336b40SDon Lewis 	int l = sizeof(struct dn_extra_parms);
69291336b40SDon Lewis 	if (len < l) {
69391336b40SDon Lewis 		D("invalid sched parms length got %d need %d", len, l);
69491336b40SDon Lewis 		return EINVAL;
69591336b40SDon Lewis 	}
69691336b40SDon Lewis 	/* we free the old cfg because maybe the orignal allocation
69791336b40SDon Lewis 	 * was used for diffirent AQM type.
69891336b40SDon Lewis 	 */
69991336b40SDon Lewis 	if (fs->aqmcfg) {
70091336b40SDon Lewis 		free(fs->aqmcfg, M_DUMMYNET);
70191336b40SDon Lewis 		fs->aqmcfg = NULL;
70291336b40SDon Lewis 	}
70391336b40SDon Lewis 
70491336b40SDon Lewis 	fs->aqmcfg = malloc(sizeof(struct dn_aqm_pie_parms),
70591336b40SDon Lewis 			 M_DUMMYNET, M_NOWAIT | M_ZERO);
70691336b40SDon Lewis 	if (fs->aqmcfg== NULL) {
70791336b40SDon Lewis 		D("cannot allocate PIE configuration parameters");
70891336b40SDon Lewis 		return ENOMEM;
70991336b40SDon Lewis 	}
71091336b40SDon Lewis 
71191336b40SDon Lewis 	/* par array contains pie configuration as follow
71291336b40SDon Lewis 	 * 0- qdelay_ref,1- tupdate, 2- max_burst
71391336b40SDon Lewis 	 * 3- max_ecnth, 4- alpha, 5- beta, 6- flags
71491336b40SDon Lewis 	 */
71591336b40SDon Lewis 
71691336b40SDon Lewis 	/* configure PIE parameters */
71791336b40SDon Lewis 	pcfg = fs->aqmcfg;
71891336b40SDon Lewis 
71991336b40SDon Lewis 	if (ep->par[0] < 0)
72091336b40SDon Lewis 		pcfg->qdelay_ref = pie_sysctl.qdelay_ref * AQM_TIME_1US;
72191336b40SDon Lewis 	else
72291336b40SDon Lewis 		pcfg->qdelay_ref = ep->par[0];
72391336b40SDon Lewis 	if (ep->par[1] < 0)
72491336b40SDon Lewis 		pcfg->tupdate = pie_sysctl.tupdate * AQM_TIME_1US;
72591336b40SDon Lewis 	else
72691336b40SDon Lewis 		pcfg->tupdate = ep->par[1];
72791336b40SDon Lewis 	if (ep->par[2] < 0)
72891336b40SDon Lewis 		pcfg->max_burst = pie_sysctl.max_burst * AQM_TIME_1US;
72991336b40SDon Lewis 	else
73091336b40SDon Lewis 		pcfg->max_burst = ep->par[2];
73191336b40SDon Lewis 	if (ep->par[3] < 0)
73291336b40SDon Lewis 		pcfg->max_ecnth = pie_sysctl.max_ecnth;
73391336b40SDon Lewis 	else
73491336b40SDon Lewis 		pcfg->max_ecnth = ep->par[3];
73591336b40SDon Lewis 	if (ep->par[4] < 0)
73691336b40SDon Lewis 		pcfg->alpha = pie_sysctl.alpha;
73791336b40SDon Lewis 	else
73891336b40SDon Lewis 		pcfg->alpha = ep->par[4];
73991336b40SDon Lewis 	if (ep->par[5] < 0)
74091336b40SDon Lewis 		pcfg->beta = pie_sysctl.beta;
74191336b40SDon Lewis 	else
74291336b40SDon Lewis 		pcfg->beta = ep->par[5];
74391336b40SDon Lewis 	if (ep->par[6] < 0)
74491336b40SDon Lewis 		pcfg->flags = pie_sysctl.flags;
74591336b40SDon Lewis 	else
74691336b40SDon Lewis 		pcfg->flags = ep->par[6];
74791336b40SDon Lewis 
74891336b40SDon Lewis 	/* bound PIE configurations */
74991336b40SDon Lewis 	pcfg->qdelay_ref = BOUND_VAR(pcfg->qdelay_ref, 1, 10 * AQM_TIME_1S);
75091336b40SDon Lewis 	pcfg->tupdate = BOUND_VAR(pcfg->tupdate, 1, 10 * AQM_TIME_1S);
75191336b40SDon Lewis 	pcfg->max_burst = BOUND_VAR(pcfg->max_burst, 0, 10 * AQM_TIME_1S);
75291336b40SDon Lewis 	pcfg->max_ecnth = BOUND_VAR(pcfg->max_ecnth, 0, PIE_SCALE);
75391336b40SDon Lewis 	pcfg->alpha = BOUND_VAR(pcfg->alpha, 0, 7 * PIE_SCALE);
75491336b40SDon Lewis 	pcfg->beta = BOUND_VAR(pcfg->beta, 0 , 7 * PIE_SCALE);
75591336b40SDon Lewis 
75691336b40SDon Lewis 	pie_desc.cfg_ref_count++;
75791336b40SDon Lewis 	//D("pie cfg_ref_count=%d", pie_desc.cfg_ref_count);
75891336b40SDon Lewis 	return 0;
75991336b40SDon Lewis }
76091336b40SDon Lewis 
76191336b40SDon Lewis /*
76291336b40SDon Lewis  * Deconfigure PIE and free memory allocation
76391336b40SDon Lewis  */
76491336b40SDon Lewis static int
aqm_pie_deconfig(struct dn_fsk * fs)76591336b40SDon Lewis aqm_pie_deconfig(struct dn_fsk* fs)
76691336b40SDon Lewis {
76791336b40SDon Lewis 	if (fs && fs->aqmcfg) {
76891336b40SDon Lewis 		free(fs->aqmcfg, M_DUMMYNET);
76991336b40SDon Lewis 		fs->aqmcfg = NULL;
77091336b40SDon Lewis 		pie_desc.cfg_ref_count--;
77191336b40SDon Lewis 	}
77291336b40SDon Lewis 	return 0;
77391336b40SDon Lewis }
77491336b40SDon Lewis 
77591336b40SDon Lewis /*
77691336b40SDon Lewis  * Retrieve PIE configuration parameters.
77791336b40SDon Lewis  */
77891336b40SDon Lewis static int
aqm_pie_getconfig(struct dn_fsk * fs,struct dn_extra_parms * ep)77991336b40SDon Lewis aqm_pie_getconfig (struct dn_fsk *fs, struct dn_extra_parms * ep)
78091336b40SDon Lewis {
78191336b40SDon Lewis 	struct dn_aqm_pie_parms *pcfg;
78291336b40SDon Lewis 	if (fs->aqmcfg) {
783bcd8d3b8SConrad Meyer 		strlcpy(ep->name, pie_desc.name, sizeof(ep->name));
78491336b40SDon Lewis 		pcfg = fs->aqmcfg;
78591336b40SDon Lewis 		ep->par[0] = pcfg->qdelay_ref / AQM_TIME_1US;
78691336b40SDon Lewis 		ep->par[1] = pcfg->tupdate / AQM_TIME_1US;
78791336b40SDon Lewis 		ep->par[2] = pcfg->max_burst / AQM_TIME_1US;
78891336b40SDon Lewis 		ep->par[3] = pcfg->max_ecnth;
78991336b40SDon Lewis 		ep->par[4] = pcfg->alpha;
79091336b40SDon Lewis 		ep->par[5] = pcfg->beta;
79191336b40SDon Lewis 		ep->par[6] = pcfg->flags;
79291336b40SDon Lewis 
79391336b40SDon Lewis 		return 0;
79491336b40SDon Lewis 	}
79591336b40SDon Lewis 	return 1;
79691336b40SDon Lewis }
79791336b40SDon Lewis 
79891336b40SDon Lewis static struct dn_aqm pie_desc = {
79991336b40SDon Lewis 	_SI( .type = )  DN_AQM_PIE,
80091336b40SDon Lewis 	_SI( .name = )  "PIE",
80191336b40SDon Lewis 	_SI( .ref_count = )  0,
80291336b40SDon Lewis 	_SI( .cfg_ref_count = )  0,
80391336b40SDon Lewis 	_SI( .enqueue = )  aqm_pie_enqueue,
80491336b40SDon Lewis 	_SI( .dequeue = )  aqm_pie_dequeue,
80591336b40SDon Lewis 	_SI( .config = )  aqm_pie_config,
80691336b40SDon Lewis 	_SI( .deconfig = )  aqm_pie_deconfig,
80791336b40SDon Lewis 	_SI( .getconfig = )  aqm_pie_getconfig,
80891336b40SDon Lewis 	_SI( .init = )  aqm_pie_init,
80991336b40SDon Lewis 	_SI( .cleanup = )  aqm_pie_cleanup,
81091336b40SDon Lewis };
81191336b40SDon Lewis 
81291336b40SDon Lewis DECLARE_DNAQM_MODULE(dn_aqm_pie, &pie_desc);
81391336b40SDon Lewis #endif
814