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