xref: /freebsd/sys/netpfil/ipfw/dn_sched_fq_pie.c (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1 /*
2  * FQ_PIE - The FlowQueue-PIE scheduler/AQM
3  *
4  * $FreeBSD$
5  *
6  * Copyright (C) 2016 Centre for Advanced Internet Architectures,
7  *  Swinburne University of Technology, Melbourne, Australia.
8  * Portions of this code were made possible in part by a gift from
9  *  The Comcast Innovation Fund.
10  * Implemented by Rasool Al-Saadi <ralsaadi@swin.edu.au>
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 /* Important note:
35  * As there is no an office document for FQ-PIE specification, we used
36  * FQ-CoDel algorithm with some modifications to implement FQ-PIE.
37  * This FQ-PIE implementation is a beta version and have not been tested
38  * extensively. Our FQ-PIE uses stand-alone PIE AQM per sub-queue. By
39  * default, timestamp is used to calculate queue delay instead of departure
40  * rate estimation method. Although departure rate estimation is available
41  * as testing option, the results could be incorrect. Moreover, turning PIE on
42  * and off option is available but it does not work properly in this version.
43  */
44 
45 #ifdef _KERNEL
46 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/kernel.h>
49 #include <sys/mbuf.h>
50 #include <sys/lock.h>
51 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <net/if.h>	/* IFNAMSIZ */
54 #include <netinet/in.h>
55 #include <netinet/ip_var.h>		/* ipfw_rule_ref */
56 #include <netinet/ip_fw.h>	/* flow_id */
57 #include <netinet/ip_dummynet.h>
58 
59 #include <sys/proc.h>
60 #include <sys/rwlock.h>
61 
62 #include <netpfil/ipfw/ip_fw_private.h>
63 #include <sys/sysctl.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip6.h>
66 #include <netinet/ip_icmp.h>
67 #include <netinet/tcp.h>
68 #include <netinet/udp.h>
69 #include <sys/queue.h>
70 #include <sys/hash.h>
71 
72 #include <netpfil/ipfw/dn_heap.h>
73 #include <netpfil/ipfw/ip_dn_private.h>
74 
75 #include <netpfil/ipfw/dn_aqm.h>
76 #include <netpfil/ipfw/dn_aqm_pie.h>
77 #include <netpfil/ipfw/dn_sched.h>
78 
79 #else
80 #include <dn_test.h>
81 #endif
82 
83 #define DN_SCHED_FQ_PIE 7
84 
85 VNET_DECLARE(unsigned long, io_pkt_drop);
86 #define V_io_pkt_drop VNET(io_pkt_drop)
87 
88 /* list of queues */
89 STAILQ_HEAD(fq_pie_list, fq_pie_flow) ;
90 
91 /* FQ_PIE parameters including PIE */
92 struct dn_sch_fq_pie_parms {
93 	struct dn_aqm_pie_parms	pcfg;	/* PIE configuration Parameters */
94 	/* FQ_PIE Parameters */
95 	uint32_t flows_cnt;	/* number of flows */
96 	uint32_t limit;	/* hard limit of FQ_PIE queue size*/
97 	uint32_t quantum;
98 };
99 
100 /* flow (sub-queue) stats */
101 struct flow_stats {
102 	uint64_t tot_pkts;	/* statistics counters  */
103 	uint64_t tot_bytes;
104 	uint32_t length;		/* Queue length, in packets */
105 	uint32_t len_bytes;	/* Queue length, in bytes */
106 	uint32_t drops;
107 };
108 
109 /* A flow of packets (sub-queue)*/
110 struct fq_pie_flow {
111 	struct mq	mq;	/* list of packets */
112 	struct flow_stats stats;	/* statistics */
113 	int deficit;
114 	int active;		/* 1: flow is active (in a list) */
115 	struct pie_status pst;	/* pie status variables */
116 	struct fq_pie_si_extra *psi_extra;
117 	STAILQ_ENTRY(fq_pie_flow) flowchain;
118 };
119 
120 /* extra fq_pie scheduler configurations */
121 struct fq_pie_schk {
122 	struct dn_sch_fq_pie_parms cfg;
123 };
124 
125 /* fq_pie scheduler instance extra state vars.
126  * The purpose of separation this structure is to preserve number of active
127  * sub-queues and the flows array pointer even after the scheduler instance
128  * is destroyed.
129  * Preserving these varaiables allows freeing the allocated memory by
130  * fqpie_callout_cleanup() independently from fq_pie_free_sched().
131  */
132 struct fq_pie_si_extra {
133 	uint32_t nr_active_q;	/* number of active queues */
134 	struct fq_pie_flow *flows;	/* array of flows (queues) */
135 	};
136 
137 /* fq_pie scheduler instance */
138 struct fq_pie_si {
139 	struct dn_sch_inst _si;	/* standard scheduler instance. SHOULD BE FIRST */
140 	struct dn_queue main_q; /* main queue is after si directly */
141 	uint32_t perturbation; 	/* random value */
142 	struct fq_pie_list newflows;	/* list of new queues */
143 	struct fq_pie_list oldflows;	/* list of old queues */
144 	struct fq_pie_si_extra *si_extra; /* extra state vars*/
145 };
146 
147 static struct dn_alg fq_pie_desc;
148 
149 /*  Default FQ-PIE parameters including PIE */
150 /*  PIE defaults
151  * target=15ms, max_burst=150ms, max_ecnth=0.1,
152  * alpha=0.125, beta=1.25, tupdate=15ms
153  * FQ-
154  * flows=1024, limit=10240, quantum =1514
155  */
156 struct dn_sch_fq_pie_parms
157  fq_pie_sysctl = {{15000 * AQM_TIME_1US, 15000 * AQM_TIME_1US,
158 	150000 * AQM_TIME_1US, PIE_SCALE * 0.1, PIE_SCALE * 0.125,
159 	PIE_SCALE * 1.25,	PIE_CAPDROP_ENABLED | PIE_DERAND_ENABLED},
160 	1024, 10240, 1514};
161 
162 static int
163 fqpie_sysctl_alpha_beta_handler(SYSCTL_HANDLER_ARGS)
164 {
165 	int error;
166 	long  value;
167 
168 	if (!strcmp(oidp->oid_name,"alpha"))
169 		value = fq_pie_sysctl.pcfg.alpha;
170 	else
171 		value = fq_pie_sysctl.pcfg.beta;
172 
173 	value = value * 1000 / PIE_SCALE;
174 	error = sysctl_handle_long(oidp, &value, 0, req);
175 	if (error != 0 || req->newptr == NULL)
176 		return (error);
177 	if (value < 1 || value > 7 * PIE_SCALE)
178 		return (EINVAL);
179 	value = (value * PIE_SCALE) / 1000;
180 	if (!strcmp(oidp->oid_name,"alpha"))
181 			fq_pie_sysctl.pcfg.alpha = value;
182 	else
183 		fq_pie_sysctl.pcfg.beta = value;
184 	return (0);
185 }
186 
187 static int
188 fqpie_sysctl_target_tupdate_maxb_handler(SYSCTL_HANDLER_ARGS)
189 {
190 	int error;
191 	long  value;
192 
193 	if (!strcmp(oidp->oid_name,"target"))
194 		value = fq_pie_sysctl.pcfg.qdelay_ref;
195 	else if (!strcmp(oidp->oid_name,"tupdate"))
196 		value = fq_pie_sysctl.pcfg.tupdate;
197 	else
198 		value = fq_pie_sysctl.pcfg.max_burst;
199 
200 	value = value / AQM_TIME_1US;
201 	error = sysctl_handle_long(oidp, &value, 0, req);
202 	if (error != 0 || req->newptr == NULL)
203 		return (error);
204 	if (value < 1 || value > 10 * AQM_TIME_1S)
205 		return (EINVAL);
206 	value = value * AQM_TIME_1US;
207 
208 	if (!strcmp(oidp->oid_name,"target"))
209 		fq_pie_sysctl.pcfg.qdelay_ref  = value;
210 	else if (!strcmp(oidp->oid_name,"tupdate"))
211 		fq_pie_sysctl.pcfg.tupdate  = value;
212 	else
213 		fq_pie_sysctl.pcfg.max_burst = value;
214 	return (0);
215 }
216 
217 static int
218 fqpie_sysctl_max_ecnth_handler(SYSCTL_HANDLER_ARGS)
219 {
220 	int error;
221 	long  value;
222 
223 	value = fq_pie_sysctl.pcfg.max_ecnth;
224 	value = value * 1000 / PIE_SCALE;
225 	error = sysctl_handle_long(oidp, &value, 0, req);
226 	if (error != 0 || req->newptr == NULL)
227 		return (error);
228 	if (value < 1 || value > PIE_SCALE)
229 		return (EINVAL);
230 	value = (value * PIE_SCALE) / 1000;
231 	fq_pie_sysctl.pcfg.max_ecnth = value;
232 	return (0);
233 }
234 
235 /* define FQ- PIE sysctl variables */
236 SYSBEGIN(f4)
237 SYSCTL_DECL(_net_inet);
238 SYSCTL_DECL(_net_inet_ip);
239 SYSCTL_DECL(_net_inet_ip_dummynet);
240 static SYSCTL_NODE(_net_inet_ip_dummynet, OID_AUTO, fqpie,
241     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
242     "FQ_PIE");
243 
244 #ifdef SYSCTL_NODE
245 
246 SYSCTL_PROC(_net_inet_ip_dummynet_fqpie, OID_AUTO, target,
247     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
248     fqpie_sysctl_target_tupdate_maxb_handler, "L",
249     "queue target in microsecond");
250 
251 SYSCTL_PROC(_net_inet_ip_dummynet_fqpie, OID_AUTO, tupdate,
252     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
253     fqpie_sysctl_target_tupdate_maxb_handler, "L",
254     "the frequency of drop probability calculation in microsecond");
255 
256 SYSCTL_PROC(_net_inet_ip_dummynet_fqpie, OID_AUTO, max_burst,
257     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
258     fqpie_sysctl_target_tupdate_maxb_handler, "L",
259     "Burst allowance interval in microsecond");
260 
261 SYSCTL_PROC(_net_inet_ip_dummynet_fqpie, OID_AUTO, max_ecnth,
262     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
263     fqpie_sysctl_max_ecnth_handler, "L",
264     "ECN safeguard threshold scaled by 1000");
265 
266 SYSCTL_PROC(_net_inet_ip_dummynet_fqpie, OID_AUTO, alpha,
267     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
268     fqpie_sysctl_alpha_beta_handler, "L",
269     "PIE alpha scaled by 1000");
270 
271 SYSCTL_PROC(_net_inet_ip_dummynet_fqpie, OID_AUTO, beta,
272     CTLTYPE_LONG | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
273     fqpie_sysctl_alpha_beta_handler, "L",
274     "beta scaled by 1000");
275 
276 SYSCTL_UINT(_net_inet_ip_dummynet_fqpie, OID_AUTO, quantum,
277 	CTLFLAG_RW, &fq_pie_sysctl.quantum, 1514, "quantum for FQ_PIE");
278 SYSCTL_UINT(_net_inet_ip_dummynet_fqpie, OID_AUTO, flows,
279 	CTLFLAG_RW, &fq_pie_sysctl.flows_cnt, 1024, "Number of queues for FQ_PIE");
280 SYSCTL_UINT(_net_inet_ip_dummynet_fqpie, OID_AUTO, limit,
281 	CTLFLAG_RW, &fq_pie_sysctl.limit, 10240, "limit for FQ_PIE");
282 #endif
283 
284 /* Helper function to update queue&main-queue and scheduler statistics.
285  * negative len & drop -> drop
286  * negative len -> dequeue
287  * positive len -> enqueue
288  * positive len + drop -> drop during enqueue
289  */
290 __inline static void
291 fq_update_stats(struct fq_pie_flow *q, struct fq_pie_si *si, int len,
292 	int drop)
293 {
294 	int inc = 0;
295 
296 	if (len < 0)
297 		inc = -1;
298 	else if (len > 0)
299 		inc = 1;
300 
301 	if (drop) {
302 		si->main_q.ni.drops ++;
303 		q->stats.drops ++;
304 		si->_si.ni.drops ++;
305 		V_dn_cfg.io_pkt_drop ++;
306 	}
307 
308 	if (!drop || (drop && len < 0)) {
309 		/* Update stats for the main queue */
310 		si->main_q.ni.length += inc;
311 		si->main_q.ni.len_bytes += len;
312 
313 		/*update sub-queue stats */
314 		q->stats.length += inc;
315 		q->stats.len_bytes += len;
316 
317 		/*update scheduler instance stats */
318 		si->_si.ni.length += inc;
319 		si->_si.ni.len_bytes += len;
320 	}
321 
322 	if (inc > 0) {
323 		si->main_q.ni.tot_bytes += len;
324 		si->main_q.ni.tot_pkts ++;
325 
326 		q->stats.tot_bytes +=len;
327 		q->stats.tot_pkts++;
328 
329 		si->_si.ni.tot_bytes +=len;
330 		si->_si.ni.tot_pkts ++;
331 	}
332 
333 }
334 
335 /*
336  * Extract a packet from the head of sub-queue 'q'
337  * Return a packet or NULL if the queue is empty.
338  * If getts is set, also extract packet's timestamp from mtag.
339  */
340 __inline static struct mbuf *
341 fq_pie_extract_head(struct fq_pie_flow *q, aqm_time_t *pkt_ts,
342 	struct fq_pie_si *si, int getts)
343 {
344 	struct mbuf *m = q->mq.head;
345 
346 	if (m == NULL)
347 		return m;
348 	q->mq.head = m->m_nextpkt;
349 
350 	fq_update_stats(q, si, -m->m_pkthdr.len, 0);
351 
352 	if (si->main_q.ni.length == 0) /* queue is now idle */
353 			si->main_q.q_time = V_dn_cfg.curr_time;
354 
355 	if (getts) {
356 		/* extract packet timestamp*/
357 		struct m_tag *mtag;
358 		mtag = m_tag_locate(m, MTAG_ABI_COMPAT, DN_AQM_MTAG_TS, NULL);
359 		if (mtag == NULL){
360 			D("PIE timestamp mtag not found!");
361 			*pkt_ts = 0;
362 		} else {
363 			*pkt_ts = *(aqm_time_t *)(mtag + 1);
364 			m_tag_delete(m,mtag);
365 		}
366 	}
367 	return m;
368 }
369 
370 /*
371  * Callout function for drop probability calculation
372  * This function is called over tupdate ms and takes pointer of FQ-PIE
373  * flow as an argument
374   */
375 static void
376 fq_calculate_drop_prob(void *x)
377 {
378 	struct fq_pie_flow *q = (struct fq_pie_flow *) x;
379 	struct pie_status *pst = &q->pst;
380 	struct dn_aqm_pie_parms *pprms;
381 	int64_t p, prob, oldprob;
382 	aqm_time_t now;
383 	int p_isneg;
384 
385 	now = AQM_UNOW;
386 	pprms = pst->parms;
387 	prob = pst->drop_prob;
388 
389 	/* calculate current qdelay using DRE method.
390 	 * If TS is used and no data in the queue, reset current_qdelay
391 	 * as it stays at last value during dequeue process.
392 	*/
393 	if (pprms->flags & PIE_DEPRATEEST_ENABLED)
394 		pst->current_qdelay = ((uint64_t)q->stats.len_bytes  * pst->avg_dq_time)
395 			>> PIE_DQ_THRESHOLD_BITS;
396 	else
397 		if (!q->stats.len_bytes)
398 			pst->current_qdelay = 0;
399 
400 	/* calculate drop probability */
401 	p = (int64_t)pprms->alpha *
402 		((int64_t)pst->current_qdelay - (int64_t)pprms->qdelay_ref);
403 	p +=(int64_t) pprms->beta *
404 		((int64_t)pst->current_qdelay - (int64_t)pst->qdelay_old);
405 
406 	/* take absolute value so right shift result is well defined */
407 	p_isneg = p < 0;
408 	if (p_isneg) {
409 		p = -p;
410 	}
411 
412 	/* We PIE_MAX_PROB shift by 12-bits to increase the division precision  */
413 	p *= (PIE_MAX_PROB << 12) / AQM_TIME_1S;
414 
415 	/* auto-tune drop probability */
416 	if (prob < (PIE_MAX_PROB / 1000000)) /* 0.000001 */
417 		p >>= 11 + PIE_FIX_POINT_BITS + 12;
418 	else if (prob < (PIE_MAX_PROB / 100000)) /* 0.00001 */
419 		p >>= 9 + PIE_FIX_POINT_BITS + 12;
420 	else if (prob < (PIE_MAX_PROB / 10000)) /* 0.0001 */
421 		p >>= 7 + PIE_FIX_POINT_BITS + 12;
422 	else if (prob < (PIE_MAX_PROB / 1000)) /* 0.001 */
423 		p >>= 5 + PIE_FIX_POINT_BITS + 12;
424 	else if (prob < (PIE_MAX_PROB / 100)) /* 0.01 */
425 		p >>= 3 + PIE_FIX_POINT_BITS + 12;
426 	else if (prob < (PIE_MAX_PROB / 10)) /* 0.1 */
427 		p >>= 1 + PIE_FIX_POINT_BITS + 12;
428 	else
429 		p >>= PIE_FIX_POINT_BITS + 12;
430 
431 	oldprob = prob;
432 
433 	if (p_isneg) {
434 		prob = prob - p;
435 
436 		/* check for multiplication underflow */
437 		if (prob > oldprob) {
438 			prob= 0;
439 			D("underflow");
440 		}
441 	} else {
442 		/* Cap Drop adjustment */
443 		if ((pprms->flags & PIE_CAPDROP_ENABLED) &&
444 		    prob >= PIE_MAX_PROB / 10 &&
445 		    p > PIE_MAX_PROB / 50 ) {
446 			p = PIE_MAX_PROB / 50;
447 		}
448 
449 		prob = prob + p;
450 
451 		/* check for multiplication overflow */
452 		if (prob<oldprob) {
453 			D("overflow");
454 			prob= PIE_MAX_PROB;
455 		}
456 	}
457 
458 	/*
459 	 * decay the drop probability exponentially
460 	 * and restrict it to range 0 to PIE_MAX_PROB
461 	 */
462 	if (prob < 0) {
463 		prob = 0;
464 	} else {
465 		if (pst->current_qdelay == 0 && pst->qdelay_old == 0) {
466 			/* 0.98 ~= 1- 1/64 */
467 			prob = prob - (prob >> 6);
468 		}
469 
470 		if (prob > PIE_MAX_PROB) {
471 			prob = PIE_MAX_PROB;
472 		}
473 	}
474 
475 	pst->drop_prob = prob;
476 
477 	/* store current delay value */
478 	pst->qdelay_old = pst->current_qdelay;
479 
480 	/* update burst allowance */
481 	if ((pst->sflags & PIE_ACTIVE) && pst->burst_allowance) {
482 		if (pst->burst_allowance > pprms->tupdate)
483 			pst->burst_allowance -= pprms->tupdate;
484 		else
485 			pst->burst_allowance = 0;
486 	}
487 
488 	if (pst->sflags & PIE_ACTIVE)
489 	callout_reset_sbt(&pst->aqm_pie_callout,
490 		(uint64_t)pprms->tupdate * SBT_1US,
491 		0, fq_calculate_drop_prob, q, 0);
492 
493 	mtx_unlock(&pst->lock_mtx);
494 }
495 
496 /*
497  * Reset PIE variables & activate the queue
498  */
499 __inline static void
500 fq_activate_pie(struct fq_pie_flow *q)
501 {
502 	struct pie_status *pst = &q->pst;
503 	struct dn_aqm_pie_parms *pprms;
504 
505 	mtx_lock(&pst->lock_mtx);
506 	pprms = pst->parms;
507 
508 	pprms = pst->parms;
509 	pst->drop_prob = 0;
510 	pst->qdelay_old = 0;
511 	pst->burst_allowance = pprms->max_burst;
512 	pst->accu_prob = 0;
513 	pst->dq_count = 0;
514 	pst->avg_dq_time = 0;
515 	pst->sflags = PIE_INMEASUREMENT | PIE_ACTIVE;
516 	pst->measurement_start = AQM_UNOW;
517 
518 	callout_reset_sbt(&pst->aqm_pie_callout,
519 		(uint64_t)pprms->tupdate * SBT_1US,
520 		0, fq_calculate_drop_prob, q, 0);
521 
522 	mtx_unlock(&pst->lock_mtx);
523 }
524 
525  /*
526   * Deactivate PIE and stop probe update callout
527   */
528 __inline static void
529 fq_deactivate_pie(struct pie_status *pst)
530 {
531 	mtx_lock(&pst->lock_mtx);
532 	pst->sflags &= ~(PIE_ACTIVE | PIE_INMEASUREMENT);
533 	callout_stop(&pst->aqm_pie_callout);
534 	//D("PIE Deactivated");
535 	mtx_unlock(&pst->lock_mtx);
536 }
537 
538  /*
539   * Initialize PIE for sub-queue 'q'
540   */
541 static int
542 pie_init(struct fq_pie_flow *q, struct fq_pie_schk *fqpie_schk)
543 {
544 	struct pie_status *pst=&q->pst;
545 	struct dn_aqm_pie_parms *pprms = pst->parms;
546 
547 	int err = 0;
548 	if (!pprms){
549 		D("AQM_PIE is not configured");
550 		err = EINVAL;
551 	} else {
552 		q->psi_extra->nr_active_q++;
553 
554 		/* For speed optimization, we caculate 1/3 queue size once here */
555 		// XXX limit divided by number of queues divided by 3 ???
556 		pst->one_third_q_size = (fqpie_schk->cfg.limit /
557 			fqpie_schk->cfg.flows_cnt) / 3;
558 
559 		mtx_init(&pst->lock_mtx, "mtx_pie", NULL, MTX_DEF);
560 		callout_init_mtx(&pst->aqm_pie_callout, &pst->lock_mtx,
561 			CALLOUT_RETURNUNLOCKED);
562 	}
563 
564 	return err;
565 }
566 
567 /*
568  * callout function to destroy PIE lock, and free fq_pie flows and fq_pie si
569  * extra memory when number of active sub-queues reaches zero.
570  * 'x' is a fq_pie_flow to be destroyed
571  */
572 static void
573 fqpie_callout_cleanup(void *x)
574 {
575 	struct fq_pie_flow *q = x;
576 	struct pie_status *pst = &q->pst;
577 	struct fq_pie_si_extra *psi_extra;
578 
579 	mtx_unlock(&pst->lock_mtx);
580 	mtx_destroy(&pst->lock_mtx);
581 	psi_extra = q->psi_extra;
582 
583 	DN_BH_WLOCK();
584 	psi_extra->nr_active_q--;
585 
586 	/* when all sub-queues are destroyed, free flows fq_pie extra vars memory */
587 	if (!psi_extra->nr_active_q) {
588 		free(psi_extra->flows, M_DUMMYNET);
589 		free(psi_extra, M_DUMMYNET);
590 		fq_pie_desc.ref_count--;
591 	}
592 	DN_BH_WUNLOCK();
593 }
594 
595 /*
596  * Clean up PIE status for sub-queue 'q'
597  * Stop callout timer and destroy mtx using fqpie_callout_cleanup() callout.
598  */
599 static int
600 pie_cleanup(struct fq_pie_flow *q)
601 {
602 	struct pie_status *pst  = &q->pst;
603 
604 	mtx_lock(&pst->lock_mtx);
605 	callout_reset_sbt(&pst->aqm_pie_callout,
606 		SBT_1US, 0, fqpie_callout_cleanup, q, 0);
607 	mtx_unlock(&pst->lock_mtx);
608 	return 0;
609 }
610 
611 /*
612  * Dequeue and return a pcaket from sub-queue 'q' or NULL if 'q' is empty.
613  * Also, caculate depature time or queue delay using timestamp
614  */
615  static struct mbuf *
616 pie_dequeue(struct fq_pie_flow *q, struct fq_pie_si *si)
617 {
618 	struct mbuf *m;
619 	struct dn_aqm_pie_parms *pprms;
620 	struct pie_status *pst;
621 	aqm_time_t now;
622 	aqm_time_t pkt_ts, dq_time;
623 	int32_t w;
624 
625 	pst  = &q->pst;
626 	pprms = q->pst.parms;
627 
628 	/*we extarct packet ts only when Departure Rate Estimation dis not used*/
629 	m = fq_pie_extract_head(q, &pkt_ts, si,
630 		!(pprms->flags & PIE_DEPRATEEST_ENABLED));
631 
632 	if (!m || !(pst->sflags & PIE_ACTIVE))
633 		return m;
634 
635 	now = AQM_UNOW;
636 	if (pprms->flags & PIE_DEPRATEEST_ENABLED) {
637 		/* calculate average depature time */
638 		if(pst->sflags & PIE_INMEASUREMENT) {
639 			pst->dq_count += m->m_pkthdr.len;
640 
641 			if (pst->dq_count >= PIE_DQ_THRESHOLD) {
642 				dq_time = now - pst->measurement_start;
643 
644 				/*
645 				 * if we don't have old avg dq_time i.e PIE is (re)initialized,
646 				 * don't use weight to calculate new avg_dq_time
647 				 */
648 				if(pst->avg_dq_time == 0)
649 					pst->avg_dq_time = dq_time;
650 				else {
651 					/*
652 					 * weight = PIE_DQ_THRESHOLD/2^6, but we scaled
653 					 * weight by 2^8. Thus, scaled
654 					 * weight = PIE_DQ_THRESHOLD /2^8
655 					 * */
656 					w = PIE_DQ_THRESHOLD >> 8;
657 					pst->avg_dq_time = (dq_time* w
658 						+ (pst->avg_dq_time * ((1L << 8) - w))) >> 8;
659 					pst->sflags &= ~PIE_INMEASUREMENT;
660 				}
661 			}
662 		}
663 
664 		/*
665 		 * Start new measurment cycle when the queue has
666 		 *  PIE_DQ_THRESHOLD worth of bytes.
667 		 */
668 		if(!(pst->sflags & PIE_INMEASUREMENT) &&
669 			q->stats.len_bytes >= PIE_DQ_THRESHOLD) {
670 			pst->sflags |= PIE_INMEASUREMENT;
671 			pst->measurement_start = now;
672 			pst->dq_count = 0;
673 		}
674 	}
675 	/* Optionally, use packet timestamp to estimate queue delay */
676 	else
677 		pst->current_qdelay = now - pkt_ts;
678 
679 	return m;
680 }
681 
682  /*
683  * Enqueue a packet in q, subject to space and FQ-PIE queue management policy
684  * (whose parameters are in q->fs).
685  * Update stats for the queue and the scheduler.
686  * Return 0 on success, 1 on drop. The packet is consumed anyways.
687  */
688 static int
689 pie_enqueue(struct fq_pie_flow *q, struct mbuf* m, struct fq_pie_si *si)
690 {
691 	uint64_t len;
692 	struct pie_status *pst;
693 	struct dn_aqm_pie_parms *pprms;
694 	int t;
695 
696 	len = m->m_pkthdr.len;
697 	pst  = &q->pst;
698 	pprms = pst->parms;
699 	t = ENQUE;
700 
701 	/* drop/mark the packet when PIE is active and burst time elapsed */
702 	if (pst->sflags & PIE_ACTIVE && pst->burst_allowance == 0
703 		&& drop_early(pst, q->stats.len_bytes) == DROP) {
704 			/*
705 			 * if drop_prob over ECN threshold, drop the packet
706 			 * otherwise mark and enqueue it.
707 			 */
708 			if (pprms->flags & PIE_ECN_ENABLED && pst->drop_prob <
709 				(pprms->max_ecnth << (PIE_PROB_BITS - PIE_FIX_POINT_BITS))
710 				&& ecn_mark(m))
711 				t = ENQUE;
712 			else
713 				t = DROP;
714 		}
715 
716 	/* Turn PIE on when 1/3 of the queue is full */
717 	if (!(pst->sflags & PIE_ACTIVE) && q->stats.len_bytes >=
718 		pst->one_third_q_size) {
719 		fq_activate_pie(q);
720 	}
721 
722 	/*  reset burst tolerance and optinally turn PIE off*/
723 	if (pst->drop_prob == 0 && pst->current_qdelay < (pprms->qdelay_ref >> 1)
724 		&& pst->qdelay_old < (pprms->qdelay_ref >> 1)) {
725 
726 			pst->burst_allowance = pprms->max_burst;
727 		if (pprms->flags & PIE_ON_OFF_MODE_ENABLED && q->stats.len_bytes<=0)
728 			fq_deactivate_pie(pst);
729 	}
730 
731 	/* Use timestamp if Departure Rate Estimation mode is disabled */
732 	if (t != DROP && !(pprms->flags & PIE_DEPRATEEST_ENABLED)) {
733 		/* Add TS to mbuf as a TAG */
734 		struct m_tag *mtag;
735 		mtag = m_tag_locate(m, MTAG_ABI_COMPAT, DN_AQM_MTAG_TS, NULL);
736 		if (mtag == NULL)
737 			mtag = m_tag_alloc(MTAG_ABI_COMPAT, DN_AQM_MTAG_TS,
738 				sizeof(aqm_time_t), M_NOWAIT);
739 		if (mtag == NULL) {
740 			t = DROP;
741 		} else {
742 			*(aqm_time_t *)(mtag + 1) = AQM_UNOW;
743 			m_tag_prepend(m, mtag);
744 		}
745 	}
746 
747 	if (t != DROP) {
748 		mq_append(&q->mq, m);
749 		fq_update_stats(q, si, len, 0);
750 		return 0;
751 	} else {
752 		fq_update_stats(q, si, len, 1);
753 		pst->accu_prob = 0;
754 		FREE_PKT(m);
755 		return 1;
756 	}
757 
758 	return 0;
759 }
760 
761 /* Drop a packet form the head of FQ-PIE sub-queue */
762 static void
763 pie_drop_head(struct fq_pie_flow *q, struct fq_pie_si *si)
764 {
765 	struct mbuf *m = q->mq.head;
766 
767 	if (m == NULL)
768 		return;
769 	q->mq.head = m->m_nextpkt;
770 
771 	fq_update_stats(q, si, -m->m_pkthdr.len, 1);
772 
773 	if (si->main_q.ni.length == 0) /* queue is now idle */
774 			si->main_q.q_time = V_dn_cfg.curr_time;
775 	/* reset accu_prob after packet drop */
776 	q->pst.accu_prob = 0;
777 
778 	FREE_PKT(m);
779 }
780 
781 /*
782  * Classify a packet to queue number using Jenkins hash function.
783  * Return: queue number
784  * the input of the hash are protocol no, perturbation, src IP, dst IP,
785  * src port, dst port,
786  */
787 static inline int
788 fq_pie_classify_flow(struct mbuf *m, uint16_t fcount, struct fq_pie_si *si)
789 {
790 	struct ip *ip;
791 	struct tcphdr *th;
792 	struct udphdr *uh;
793 	uint8_t tuple[41];
794 	uint16_t hash=0;
795 
796 	ip = (struct ip *)mtodo(m, dn_tag_get(m)->iphdr_off);
797 //#ifdef INET6
798 	struct ip6_hdr *ip6;
799 	int isip6;
800 	isip6 = (ip->ip_v == 6);
801 
802 	if(isip6) {
803 		ip6 = (struct ip6_hdr *)ip;
804 		*((uint8_t *) &tuple[0]) = ip6->ip6_nxt;
805 		*((uint32_t *) &tuple[1]) = si->perturbation;
806 		memcpy(&tuple[5], ip6->ip6_src.s6_addr, 16);
807 		memcpy(&tuple[21], ip6->ip6_dst.s6_addr, 16);
808 
809 		switch (ip6->ip6_nxt) {
810 		case IPPROTO_TCP:
811 			th = (struct tcphdr *)(ip6 + 1);
812 			*((uint16_t *) &tuple[37]) = th->th_dport;
813 			*((uint16_t *) &tuple[39]) = th->th_sport;
814 			break;
815 
816 		case IPPROTO_UDP:
817 			uh = (struct udphdr *)(ip6 + 1);
818 			*((uint16_t *) &tuple[37]) = uh->uh_dport;
819 			*((uint16_t *) &tuple[39]) = uh->uh_sport;
820 			break;
821 		default:
822 			memset(&tuple[37], 0, 4);
823 		}
824 
825 		hash = jenkins_hash(tuple, 41, HASHINIT) %  fcount;
826 		return hash;
827 	}
828 //#endif
829 
830 	/* IPv4 */
831 	*((uint8_t *) &tuple[0]) = ip->ip_p;
832 	*((uint32_t *) &tuple[1]) = si->perturbation;
833 	*((uint32_t *) &tuple[5]) = ip->ip_src.s_addr;
834 	*((uint32_t *) &tuple[9]) = ip->ip_dst.s_addr;
835 
836 	switch (ip->ip_p) {
837 		case IPPROTO_TCP:
838 			th = (struct tcphdr *)(ip + 1);
839 			*((uint16_t *) &tuple[13]) = th->th_dport;
840 			*((uint16_t *) &tuple[15]) = th->th_sport;
841 			break;
842 
843 		case IPPROTO_UDP:
844 			uh = (struct udphdr *)(ip + 1);
845 			*((uint16_t *) &tuple[13]) = uh->uh_dport;
846 			*((uint16_t *) &tuple[15]) = uh->uh_sport;
847 			break;
848 		default:
849 			memset(&tuple[13], 0, 4);
850 	}
851 	hash = jenkins_hash(tuple, 17, HASHINIT) % fcount;
852 
853 	return hash;
854 }
855 
856 /*
857  * Enqueue a packet into an appropriate queue according to
858  * FQ-CoDe; algorithm.
859  */
860 static int
861 fq_pie_enqueue(struct dn_sch_inst *_si, struct dn_queue *_q,
862 	struct mbuf *m)
863 {
864 	struct fq_pie_si *si;
865 	struct fq_pie_schk *schk;
866 	struct dn_sch_fq_pie_parms *param;
867 	struct dn_queue *mainq;
868 	struct fq_pie_flow *flows;
869 	int idx, drop, i, maxidx;
870 
871 	mainq = (struct dn_queue *)(_si + 1);
872 	si = (struct fq_pie_si *)_si;
873 	flows = si->si_extra->flows;
874 	schk = (struct fq_pie_schk *)(si->_si.sched+1);
875 	param = &schk->cfg;
876 
877 	 /* classify a packet to queue number*/
878 	idx = fq_pie_classify_flow(m, param->flows_cnt, si);
879 
880 	/* enqueue packet into appropriate queue using PIE AQM.
881 	 * Note: 'pie_enqueue' function returns 1 only when it unable to
882 	 * add timestamp to packet (no limit check)*/
883 	drop = pie_enqueue(&flows[idx], m, si);
884 
885 	/* pie unable to timestamp a packet */
886 	if (drop)
887 		return 1;
888 
889 	/* If the flow (sub-queue) is not active ,then add it to tail of
890 	 * new flows list, initialize and activate it.
891 	 */
892 	if (!flows[idx].active) {
893 		STAILQ_INSERT_TAIL(&si->newflows, &flows[idx], flowchain);
894 		flows[idx].deficit = param->quantum;
895 		fq_activate_pie(&flows[idx]);
896 		flows[idx].active = 1;
897 	}
898 
899 	/* check the limit for all queues and remove a packet from the
900 	 * largest one
901 	 */
902 	if (mainq->ni.length > schk->cfg.limit) {
903 		/* find first active flow */
904 		for (maxidx = 0; maxidx < schk->cfg.flows_cnt; maxidx++)
905 			if (flows[maxidx].active)
906 				break;
907 		if (maxidx < schk->cfg.flows_cnt) {
908 			/* find the largest sub- queue */
909 			for (i = maxidx + 1; i < schk->cfg.flows_cnt; i++)
910 				if (flows[i].active && flows[i].stats.length >
911 					flows[maxidx].stats.length)
912 					maxidx = i;
913 			pie_drop_head(&flows[maxidx], si);
914 			drop = 1;
915 		}
916 	}
917 
918 	return drop;
919 }
920 
921 /*
922  * Dequeue a packet from an appropriate queue according to
923  * FQ-CoDel algorithm.
924  */
925 static struct mbuf *
926 fq_pie_dequeue(struct dn_sch_inst *_si)
927 {
928 	struct fq_pie_si *si;
929 	struct fq_pie_schk *schk;
930 	struct dn_sch_fq_pie_parms *param;
931 	struct fq_pie_flow *f;
932 	struct mbuf *mbuf;
933 	struct fq_pie_list *fq_pie_flowlist;
934 
935 	si = (struct fq_pie_si *)_si;
936 	schk = (struct fq_pie_schk *)(si->_si.sched+1);
937 	param = &schk->cfg;
938 
939 	do {
940 		/* select a list to start with */
941 		if (STAILQ_EMPTY(&si->newflows))
942 			fq_pie_flowlist = &si->oldflows;
943 		else
944 			fq_pie_flowlist = &si->newflows;
945 
946 		/* Both new and old queue lists are empty, return NULL */
947 		if (STAILQ_EMPTY(fq_pie_flowlist))
948 			return NULL;
949 
950 		f = STAILQ_FIRST(fq_pie_flowlist);
951 		while (f != NULL)	{
952 			/* if there is no flow(sub-queue) deficit, increase deficit
953 			 * by quantum, move the flow to the tail of old flows list
954 			 * and try another flow.
955 			 * Otherwise, the flow will be used for dequeue.
956 			 */
957 			if (f->deficit < 0) {
958 				 f->deficit += param->quantum;
959 				 STAILQ_REMOVE_HEAD(fq_pie_flowlist, flowchain);
960 				 STAILQ_INSERT_TAIL(&si->oldflows, f, flowchain);
961 			 } else
962 				 break;
963 
964 			f = STAILQ_FIRST(fq_pie_flowlist);
965 		}
966 
967 		/* the new flows list is empty, try old flows list */
968 		if (STAILQ_EMPTY(fq_pie_flowlist))
969 			continue;
970 
971 		/* Dequeue a packet from the selected flow */
972 		mbuf = pie_dequeue(f, si);
973 
974 		/* pie did not return a packet */
975 		if (!mbuf) {
976 			/* If the selected flow belongs to new flows list, then move
977 			 * it to the tail of old flows list. Otherwise, deactivate it and
978 			 * remove it from the old list and
979 			 */
980 			if (fq_pie_flowlist == &si->newflows) {
981 				STAILQ_REMOVE_HEAD(fq_pie_flowlist, flowchain);
982 				STAILQ_INSERT_TAIL(&si->oldflows, f, flowchain);
983 			}	else {
984 				f->active = 0;
985 				fq_deactivate_pie(&f->pst);
986 				STAILQ_REMOVE_HEAD(fq_pie_flowlist, flowchain);
987 			}
988 			/* start again */
989 			continue;
990 		}
991 
992 		/* we have a packet to return,
993 		 * update flow deficit and return the packet*/
994 		f->deficit -= mbuf->m_pkthdr.len;
995 		return mbuf;
996 
997 	} while (1);
998 
999 	/* unreachable point */
1000 	return NULL;
1001 }
1002 
1003 /*
1004  * Initialize fq_pie scheduler instance.
1005  * also, allocate memory for flows array.
1006  */
1007 static int
1008 fq_pie_new_sched(struct dn_sch_inst *_si)
1009 {
1010 	struct fq_pie_si *si;
1011 	struct dn_queue *q;
1012 	struct fq_pie_schk *schk;
1013 	struct fq_pie_flow *flows;
1014 	int i;
1015 
1016 	si = (struct fq_pie_si *)_si;
1017 	schk = (struct fq_pie_schk *)(_si->sched+1);
1018 
1019 	if(si->si_extra) {
1020 		D("si already configured!");
1021 		return 0;
1022 	}
1023 
1024 	/* init the main queue */
1025 	q = &si->main_q;
1026 	set_oid(&q->ni.oid, DN_QUEUE, sizeof(*q));
1027 	q->_si = _si;
1028 	q->fs = _si->sched->fs;
1029 
1030 	/* allocate memory for scheduler instance extra vars */
1031 	si->si_extra = malloc(sizeof(struct fq_pie_si_extra),
1032 		 M_DUMMYNET, M_NOWAIT | M_ZERO);
1033 	if (si->si_extra == NULL) {
1034 		D("cannot allocate memory for fq_pie si extra vars");
1035 		return ENOMEM ;
1036 	}
1037 	/* allocate memory for flows array */
1038 	si->si_extra->flows = mallocarray(schk->cfg.flows_cnt,
1039 	    sizeof(struct fq_pie_flow), M_DUMMYNET, M_NOWAIT | M_ZERO);
1040 	flows = si->si_extra->flows;
1041 	if (flows == NULL) {
1042 		free(si->si_extra, M_DUMMYNET);
1043 		si->si_extra = NULL;
1044 		D("cannot allocate memory for fq_pie flows");
1045 		return ENOMEM ;
1046 	}
1047 
1048 	/* init perturbation for this si */
1049 	si->perturbation = random();
1050 	si->si_extra->nr_active_q = 0;
1051 
1052 	/* init the old and new flows lists */
1053 	STAILQ_INIT(&si->newflows);
1054 	STAILQ_INIT(&si->oldflows);
1055 
1056 	/* init the flows (sub-queues) */
1057 	for (i = 0; i < schk->cfg.flows_cnt; i++) {
1058 		flows[i].pst.parms = &schk->cfg.pcfg;
1059 		flows[i].psi_extra = si->si_extra;
1060 		pie_init(&flows[i], schk);
1061 	}
1062 
1063 	fq_pie_desc.ref_count++;
1064 
1065 	return 0;
1066 }
1067 
1068 /*
1069  * Free fq_pie scheduler instance.
1070  */
1071 static int
1072 fq_pie_free_sched(struct dn_sch_inst *_si)
1073 {
1074 	struct fq_pie_si *si;
1075 	struct fq_pie_schk *schk;
1076 	struct fq_pie_flow *flows;
1077 	int i;
1078 
1079 	si = (struct fq_pie_si *)_si;
1080 	schk = (struct fq_pie_schk *)(_si->sched+1);
1081 	flows = si->si_extra->flows;
1082 	for (i = 0; i < schk->cfg.flows_cnt; i++) {
1083 		pie_cleanup(&flows[i]);
1084 	}
1085 	si->si_extra = NULL;
1086 	return 0;
1087 }
1088 
1089 /*
1090  * Configure FQ-PIE scheduler.
1091  * the configurations for the scheduler is passed fromipfw  userland.
1092  */
1093 static int
1094 fq_pie_config(struct dn_schk *_schk)
1095 {
1096 	struct fq_pie_schk *schk;
1097 	struct dn_extra_parms *ep;
1098 	struct dn_sch_fq_pie_parms *fqp_cfg;
1099 
1100 	schk = (struct fq_pie_schk *)(_schk+1);
1101 	ep = (struct dn_extra_parms *) _schk->cfg;
1102 
1103 	/* par array contains fq_pie configuration as follow
1104 	 * PIE: 0- qdelay_ref,1- tupdate, 2- max_burst
1105 	 * 3- max_ecnth, 4- alpha, 5- beta, 6- flags
1106 	 * FQ_PIE: 7- quantum, 8- limit, 9- flows
1107 	 */
1108 	if (ep && ep->oid.len ==sizeof(*ep) &&
1109 		ep->oid.subtype == DN_SCH_PARAMS) {
1110 		fqp_cfg = &schk->cfg;
1111 		if (ep->par[0] < 0)
1112 			fqp_cfg->pcfg.qdelay_ref = fq_pie_sysctl.pcfg.qdelay_ref;
1113 		else
1114 			fqp_cfg->pcfg.qdelay_ref = ep->par[0];
1115 		if (ep->par[1] < 0)
1116 			fqp_cfg->pcfg.tupdate = fq_pie_sysctl.pcfg.tupdate;
1117 		else
1118 			fqp_cfg->pcfg.tupdate = ep->par[1];
1119 		if (ep->par[2] < 0)
1120 			fqp_cfg->pcfg.max_burst = fq_pie_sysctl.pcfg.max_burst;
1121 		else
1122 			fqp_cfg->pcfg.max_burst = ep->par[2];
1123 		if (ep->par[3] < 0)
1124 			fqp_cfg->pcfg.max_ecnth = fq_pie_sysctl.pcfg.max_ecnth;
1125 		else
1126 			fqp_cfg->pcfg.max_ecnth = ep->par[3];
1127 		if (ep->par[4] < 0)
1128 			fqp_cfg->pcfg.alpha = fq_pie_sysctl.pcfg.alpha;
1129 		else
1130 			fqp_cfg->pcfg.alpha = ep->par[4];
1131 		if (ep->par[5] < 0)
1132 			fqp_cfg->pcfg.beta = fq_pie_sysctl.pcfg.beta;
1133 		else
1134 			fqp_cfg->pcfg.beta = ep->par[5];
1135 		if (ep->par[6] < 0)
1136 			fqp_cfg->pcfg.flags = 0;
1137 		else
1138 			fqp_cfg->pcfg.flags = ep->par[6];
1139 
1140 		/* FQ configurations */
1141 		if (ep->par[7] < 0)
1142 			fqp_cfg->quantum = fq_pie_sysctl.quantum;
1143 		else
1144 			fqp_cfg->quantum = ep->par[7];
1145 		if (ep->par[8] < 0)
1146 			fqp_cfg->limit = fq_pie_sysctl.limit;
1147 		else
1148 			fqp_cfg->limit = ep->par[8];
1149 		if (ep->par[9] < 0)
1150 			fqp_cfg->flows_cnt = fq_pie_sysctl.flows_cnt;
1151 		else
1152 			fqp_cfg->flows_cnt = ep->par[9];
1153 
1154 		/* Bound the configurations */
1155 		fqp_cfg->pcfg.qdelay_ref = BOUND_VAR(fqp_cfg->pcfg.qdelay_ref,
1156 			1, 5 * AQM_TIME_1S);
1157 		fqp_cfg->pcfg.tupdate = BOUND_VAR(fqp_cfg->pcfg.tupdate,
1158 			1, 5 * AQM_TIME_1S);
1159 		fqp_cfg->pcfg.max_burst = BOUND_VAR(fqp_cfg->pcfg.max_burst,
1160 			0, 5 * AQM_TIME_1S);
1161 		fqp_cfg->pcfg.max_ecnth = BOUND_VAR(fqp_cfg->pcfg.max_ecnth,
1162 			0, PIE_SCALE);
1163 		fqp_cfg->pcfg.alpha = BOUND_VAR(fqp_cfg->pcfg.alpha, 0, 7 * PIE_SCALE);
1164 		fqp_cfg->pcfg.beta = BOUND_VAR(fqp_cfg->pcfg.beta, 0, 7 * PIE_SCALE);
1165 
1166 		fqp_cfg->quantum = BOUND_VAR(fqp_cfg->quantum,1,9000);
1167 		fqp_cfg->limit= BOUND_VAR(fqp_cfg->limit,1,20480);
1168 		fqp_cfg->flows_cnt= BOUND_VAR(fqp_cfg->flows_cnt,1,65536);
1169 	}
1170 	else {
1171 		D("Wrong parameters for fq_pie scheduler");
1172 		return 1;
1173 	}
1174 
1175 	return 0;
1176 }
1177 
1178 /*
1179  * Return FQ-PIE scheduler configurations
1180  * the configurations for the scheduler is passed to userland.
1181  */
1182 static int
1183 fq_pie_getconfig (struct dn_schk *_schk, struct dn_extra_parms *ep) {
1184 	struct fq_pie_schk *schk = (struct fq_pie_schk *)(_schk+1);
1185 	struct dn_sch_fq_pie_parms *fqp_cfg;
1186 
1187 	fqp_cfg = &schk->cfg;
1188 
1189 	strcpy(ep->name, fq_pie_desc.name);
1190 	ep->par[0] = fqp_cfg->pcfg.qdelay_ref;
1191 	ep->par[1] = fqp_cfg->pcfg.tupdate;
1192 	ep->par[2] = fqp_cfg->pcfg.max_burst;
1193 	ep->par[3] = fqp_cfg->pcfg.max_ecnth;
1194 	ep->par[4] = fqp_cfg->pcfg.alpha;
1195 	ep->par[5] = fqp_cfg->pcfg.beta;
1196 	ep->par[6] = fqp_cfg->pcfg.flags;
1197 
1198 	ep->par[7] = fqp_cfg->quantum;
1199 	ep->par[8] = fqp_cfg->limit;
1200 	ep->par[9] = fqp_cfg->flows_cnt;
1201 
1202 	return 0;
1203 }
1204 
1205 /*
1206  *  FQ-PIE scheduler descriptor
1207  * contains the type of the scheduler, the name, the size of extra
1208  * data structures, and function pointers.
1209  */
1210 static struct dn_alg fq_pie_desc = {
1211 	_SI( .type = )  DN_SCHED_FQ_PIE,
1212 	_SI( .name = ) "FQ_PIE",
1213 	_SI( .flags = ) 0,
1214 
1215 	_SI( .schk_datalen = ) sizeof(struct fq_pie_schk),
1216 	_SI( .si_datalen = ) sizeof(struct fq_pie_si) - sizeof(struct dn_sch_inst),
1217 	_SI( .q_datalen = ) 0,
1218 
1219 	_SI( .enqueue = ) fq_pie_enqueue,
1220 	_SI( .dequeue = ) fq_pie_dequeue,
1221 	_SI( .config = ) fq_pie_config, /* new sched i.e. sched X config ...*/
1222 	_SI( .destroy = ) NULL,  /*sched x delete */
1223 	_SI( .new_sched = ) fq_pie_new_sched, /* new schd instance */
1224 	_SI( .free_sched = ) fq_pie_free_sched,	/* delete schd instance */
1225 	_SI( .new_fsk = ) NULL,
1226 	_SI( .free_fsk = ) NULL,
1227 	_SI( .new_queue = ) NULL,
1228 	_SI( .free_queue = ) NULL,
1229 	_SI( .getconfig = )  fq_pie_getconfig,
1230 	_SI( .ref_count = ) 0
1231 };
1232 
1233 DECLARE_DNSCHED_MODULE(dn_fq_pie, &fq_pie_desc);
1234