2 * FQ_PIE - The FlowQueue-PIE scheduler/AQM
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>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
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.
46 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/kernel.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>
60 #include <sys/rwlock.h>
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>
72 #include <netpfil/ipfw/dn_heap.h>
73 #include <netpfil/ipfw/ip_dn_private.h>
75 #include <netpfil/ipfw/dn_aqm.h>
76 #include <netpfil/ipfw/dn_aqm_pie.h>
77 #include <netpfil/ipfw/dn_sched.h>
83 #define DN_SCHED_FQ_PIE 7
86 STAILQ_HEAD(fq_pie_list, fq_pie_flow) ;
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*/
97 /* flow (sub-queue) stats */
99 uint64_t tot_pkts; /* statistics counters */
101 uint32_t length; /* Queue length, in packets */
102 uint32_t len_bytes; /* Queue length, in bytes */
106 /* A flow of packets (sub-queue)*/
108 struct mq mq; /* list of packets */
109 struct flow_stats stats; /* statistics */
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;
117 /* extra fq_pie scheduler configurations */
119 struct dn_sch_fq_pie_parms cfg;
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
126 * Preserving these varaiables allows freeing the allocated memory by
127 * fqpie_callout_cleanup() independently from fq_pie_free_sched().
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) */
134 /* fq_pie scheduler instance */
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*/
144 static struct dn_alg fq_pie_desc;
146 /* Default FQ-PIE parameters including PIE */
148 * target=15ms, max_burst=150ms, max_ecnth=0.1,
149 * alpha=0.125, beta=1.25, tupdate=15ms
151 * flows=1024, limit=10240, quantum =1514
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},
160 fqpie_sysctl_alpha_beta_handler(SYSCTL_HANDLER_ARGS)
165 if (!strcmp(oidp->oid_name,"alpha"))
166 value = fq_pie_sysctl.pcfg.alpha;
168 value = fq_pie_sysctl.pcfg.beta;
170 value = value * 1000 / PIE_SCALE;
171 error = sysctl_handle_long(oidp, &value, 0, req);
172 if (error != 0 || req->newptr == NULL)
174 if (value < 1 || value > 7 * PIE_SCALE)
176 value = (value * PIE_SCALE) / 1000;
177 if (!strcmp(oidp->oid_name,"alpha"))
178 fq_pie_sysctl.pcfg.alpha = value;
180 fq_pie_sysctl.pcfg.beta = value;
185 fqpie_sysctl_target_tupdate_maxb_handler(SYSCTL_HANDLER_ARGS)
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;
195 value = fq_pie_sysctl.pcfg.max_burst;
197 value = value / AQM_TIME_1US;
198 error = sysctl_handle_long(oidp, &value, 0, req);
199 if (error != 0 || req->newptr == NULL)
201 if (value < 1 || value > 10 * AQM_TIME_1S)
203 value = value * AQM_TIME_1US;
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;
210 fq_pie_sysctl.pcfg.max_burst = value;
215 fqpie_sysctl_max_ecnth_handler(SYSCTL_HANDLER_ARGS)
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)
225 if (value < 1 || value > PIE_SCALE)
227 value = (value * PIE_SCALE) / 1000;
228 fq_pie_sysctl.pcfg.max_ecnth = value;
232 /* define FQ- PIE sysctl variables */
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,
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");
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");
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");
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");
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");
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");
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");
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
288 fq_update_stats(struct fq_pie_flow *q, struct fq_pie_si *si, int len,
299 si->main_q.ni.drops ++;
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;
310 /*update sub-queue stats */
311 q->stats.length += inc;
312 q->stats.len_bytes += len;
314 /*update scheduler instance stats */
315 si->_si.ni.length += inc;
316 si->_si.ni.len_bytes += len;
320 si->main_q.ni.tot_bytes += len;
321 si->main_q.ni.tot_pkts ++;
323 q->stats.tot_bytes +=len;
326 si->_si.ni.tot_bytes +=len;
327 si->_si.ni.tot_pkts ++;
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.
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)
341 struct mbuf *m = q->mq.head;
345 q->mq.head = m->m_nextpkt;
347 fq_update_stats(q, si, -m->m_pkthdr.len, 0);
349 if (si->main_q.ni.length == 0) /* queue is now idle */
350 si->main_q.q_time = dn_cfg.curr_time;
353 /* extract packet timestamp*/
355 mtag = m_tag_locate(m, MTAG_ABI_COMPAT, DN_AQM_MTAG_TS, NULL);
357 D("PIE timestamp mtag not found!");
360 *pkt_ts = *(aqm_time_t *)(mtag + 1);
361 m_tag_delete(m,mtag);
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
373 fq_calculate_drop_prob(void *x)
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;
384 prob = pst->drop_prob;
386 /* calculate current qdelay using DRE method.
387 * If TS is used and no data in the queue, reset current_qdelay
388 * as it stays at last value during dequeue process.
390 if (pprms->flags & PIE_DEPRATEEST_ENABLED)
391 pst->current_qdelay = ((uint64_t)q->stats.len_bytes * pst->avg_dq_time)
392 >> PIE_DQ_THRESHOLD_BITS;
394 if (!q->stats.len_bytes)
395 pst->current_qdelay = 0;
397 /* calculate drop probability */
398 p = (int64_t)pprms->alpha *
399 ((int64_t)pst->current_qdelay - (int64_t)pprms->qdelay_ref);
400 p +=(int64_t) pprms->beta *
401 ((int64_t)pst->current_qdelay - (int64_t)pst->qdelay_old);
403 /* take absolute value so right shift result is well defined */
409 /* We PIE_MAX_PROB shift by 12-bits to increase the division precision */
410 p *= (PIE_MAX_PROB << 12) / AQM_TIME_1S;
412 /* auto-tune drop probability */
413 if (prob < (PIE_MAX_PROB / 1000000)) /* 0.000001 */
414 p >>= 11 + PIE_FIX_POINT_BITS + 12;
415 else if (prob < (PIE_MAX_PROB / 100000)) /* 0.00001 */
416 p >>= 9 + PIE_FIX_POINT_BITS + 12;
417 else if (prob < (PIE_MAX_PROB / 10000)) /* 0.0001 */
418 p >>= 7 + PIE_FIX_POINT_BITS + 12;
419 else if (prob < (PIE_MAX_PROB / 1000)) /* 0.001 */
420 p >>= 5 + PIE_FIX_POINT_BITS + 12;
421 else if (prob < (PIE_MAX_PROB / 100)) /* 0.01 */
422 p >>= 3 + PIE_FIX_POINT_BITS + 12;
423 else if (prob < (PIE_MAX_PROB / 10)) /* 0.1 */
424 p >>= 1 + PIE_FIX_POINT_BITS + 12;
426 p >>= PIE_FIX_POINT_BITS + 12;
433 /* check for multiplication underflow */
434 if (prob > oldprob) {
439 /* Cap Drop adjustment */
440 if ((pprms->flags & PIE_CAPDROP_ENABLED) &&
441 prob >= PIE_MAX_PROB / 10 &&
442 p > PIE_MAX_PROB / 50 ) {
443 p = PIE_MAX_PROB / 50;
448 /* check for multiplication overflow */
456 * decay the drop probability exponentially
457 * and restrict it to range 0 to PIE_MAX_PROB
462 if (pst->current_qdelay == 0 && pst->qdelay_old == 0) {
463 /* 0.98 ~= 1- 1/64 */
464 prob = prob - (prob >> 6);
467 if (prob > PIE_MAX_PROB) {
472 pst->drop_prob = prob;
474 /* store current delay value */
475 pst->qdelay_old = pst->current_qdelay;
477 /* update burst allowance */
478 if ((pst->sflags & PIE_ACTIVE) && pst->burst_allowance) {
479 if (pst->burst_allowance > pprms->tupdate)
480 pst->burst_allowance -= pprms->tupdate;
482 pst->burst_allowance = 0;
485 if (pst->sflags & PIE_ACTIVE)
486 callout_reset_sbt(&pst->aqm_pie_callout,
487 (uint64_t)pprms->tupdate * SBT_1US,
488 0, fq_calculate_drop_prob, q, 0);
490 mtx_unlock(&pst->lock_mtx);
494 * Reset PIE variables & activate the queue
497 fq_activate_pie(struct fq_pie_flow *q)
499 struct pie_status *pst = &q->pst;
500 struct dn_aqm_pie_parms *pprms;
502 mtx_lock(&pst->lock_mtx);
508 pst->burst_allowance = pprms->max_burst;
511 pst->avg_dq_time = 0;
512 pst->sflags = PIE_INMEASUREMENT | PIE_ACTIVE;
513 pst->measurement_start = AQM_UNOW;
515 callout_reset_sbt(&pst->aqm_pie_callout,
516 (uint64_t)pprms->tupdate * SBT_1US,
517 0, fq_calculate_drop_prob, q, 0);
519 mtx_unlock(&pst->lock_mtx);
523 * Deactivate PIE and stop probe update callout
526 fq_deactivate_pie(struct pie_status *pst)
528 mtx_lock(&pst->lock_mtx);
529 pst->sflags &= ~(PIE_ACTIVE | PIE_INMEASUREMENT);
530 callout_stop(&pst->aqm_pie_callout);
531 //D("PIE Deactivated");
532 mtx_unlock(&pst->lock_mtx);
536 * Initialize PIE for sub-queue 'q'
539 pie_init(struct fq_pie_flow *q, struct fq_pie_schk *fqpie_schk)
541 struct pie_status *pst=&q->pst;
542 struct dn_aqm_pie_parms *pprms = pst->parms;
546 D("AQM_PIE is not configured");
549 q->psi_extra->nr_active_q++;
551 /* For speed optimization, we caculate 1/3 queue size once here */
552 // XXX limit divided by number of queues divided by 3 ???
553 pst->one_third_q_size = (fqpie_schk->cfg.limit /
554 fqpie_schk->cfg.flows_cnt) / 3;
556 mtx_init(&pst->lock_mtx, "mtx_pie", NULL, MTX_DEF);
557 callout_init_mtx(&pst->aqm_pie_callout, &pst->lock_mtx,
558 CALLOUT_RETURNUNLOCKED);
565 * callout function to destroy PIE lock, and free fq_pie flows and fq_pie si
566 * extra memory when number of active sub-queues reaches zero.
567 * 'x' is a fq_pie_flow to be destroyed
570 fqpie_callout_cleanup(void *x)
572 struct fq_pie_flow *q = x;
573 struct pie_status *pst = &q->pst;
574 struct fq_pie_si_extra *psi_extra;
576 mtx_unlock(&pst->lock_mtx);
577 mtx_destroy(&pst->lock_mtx);
578 psi_extra = q->psi_extra;
581 psi_extra->nr_active_q--;
583 /* when all sub-queues are destroyed, free flows fq_pie extra vars memory */
584 if (!psi_extra->nr_active_q) {
585 free(psi_extra->flows, M_DUMMYNET);
586 free(psi_extra, M_DUMMYNET);
587 fq_pie_desc.ref_count--;
593 * Clean up PIE status for sub-queue 'q'
594 * Stop callout timer and destroy mtx using fqpie_callout_cleanup() callout.
597 pie_cleanup(struct fq_pie_flow *q)
599 struct pie_status *pst = &q->pst;
601 mtx_lock(&pst->lock_mtx);
602 callout_reset_sbt(&pst->aqm_pie_callout,
603 SBT_1US, 0, fqpie_callout_cleanup, q, 0);
604 mtx_unlock(&pst->lock_mtx);
609 * Dequeue and return a pcaket from sub-queue 'q' or NULL if 'q' is empty.
610 * Also, caculate depature time or queue delay using timestamp
613 pie_dequeue(struct fq_pie_flow *q, struct fq_pie_si *si)
616 struct dn_aqm_pie_parms *pprms;
617 struct pie_status *pst;
619 aqm_time_t pkt_ts, dq_time;
623 pprms = q->pst.parms;
625 /*we extarct packet ts only when Departure Rate Estimation dis not used*/
626 m = fq_pie_extract_head(q, &pkt_ts, si,
627 !(pprms->flags & PIE_DEPRATEEST_ENABLED));
629 if (!m || !(pst->sflags & PIE_ACTIVE))
633 if (pprms->flags & PIE_DEPRATEEST_ENABLED) {
634 /* calculate average depature time */
635 if(pst->sflags & PIE_INMEASUREMENT) {
636 pst->dq_count += m->m_pkthdr.len;
638 if (pst->dq_count >= PIE_DQ_THRESHOLD) {
639 dq_time = now - pst->measurement_start;
642 * if we don't have old avg dq_time i.e PIE is (re)initialized,
643 * don't use weight to calculate new avg_dq_time
645 if(pst->avg_dq_time == 0)
646 pst->avg_dq_time = dq_time;
649 * weight = PIE_DQ_THRESHOLD/2^6, but we scaled
650 * weight by 2^8. Thus, scaled
651 * weight = PIE_DQ_THRESHOLD /2^8
653 w = PIE_DQ_THRESHOLD >> 8;
654 pst->avg_dq_time = (dq_time* w
655 + (pst->avg_dq_time * ((1L << 8) - w))) >> 8;
656 pst->sflags &= ~PIE_INMEASUREMENT;
662 * Start new measurment cycle when the queue has
663 * PIE_DQ_THRESHOLD worth of bytes.
665 if(!(pst->sflags & PIE_INMEASUREMENT) &&
666 q->stats.len_bytes >= PIE_DQ_THRESHOLD) {
667 pst->sflags |= PIE_INMEASUREMENT;
668 pst->measurement_start = now;
672 /* Optionally, use packet timestamp to estimate queue delay */
674 pst->current_qdelay = now - pkt_ts;
680 * Enqueue a packet in q, subject to space and FQ-PIE queue management policy
681 * (whose parameters are in q->fs).
682 * Update stats for the queue and the scheduler.
683 * Return 0 on success, 1 on drop. The packet is consumed anyways.
686 pie_enqueue(struct fq_pie_flow *q, struct mbuf* m, struct fq_pie_si *si)
689 struct pie_status *pst;
690 struct dn_aqm_pie_parms *pprms;
693 len = m->m_pkthdr.len;
698 /* drop/mark the packet when PIE is active and burst time elapsed */
699 if (pst->sflags & PIE_ACTIVE && pst->burst_allowance == 0
700 && drop_early(pst, q->stats.len_bytes) == DROP) {
702 * if drop_prob over ECN threshold, drop the packet
703 * otherwise mark and enqueue it.
705 if (pprms->flags & PIE_ECN_ENABLED && pst->drop_prob <
706 (pprms->max_ecnth << (PIE_PROB_BITS - PIE_FIX_POINT_BITS))
713 /* Turn PIE on when 1/3 of the queue is full */
714 if (!(pst->sflags & PIE_ACTIVE) && q->stats.len_bytes >=
715 pst->one_third_q_size) {
719 /* reset burst tolerance and optinally turn PIE off*/
720 if (pst->drop_prob == 0 && pst->current_qdelay < (pprms->qdelay_ref >> 1)
721 && pst->qdelay_old < (pprms->qdelay_ref >> 1)) {
723 pst->burst_allowance = pprms->max_burst;
724 if (pprms->flags & PIE_ON_OFF_MODE_ENABLED && q->stats.len_bytes<=0)
725 fq_deactivate_pie(pst);
728 /* Use timestamp if Departure Rate Estimation mode is disabled */
729 if (t != DROP && !(pprms->flags & PIE_DEPRATEEST_ENABLED)) {
730 /* Add TS to mbuf as a TAG */
732 mtag = m_tag_locate(m, MTAG_ABI_COMPAT, DN_AQM_MTAG_TS, NULL);
734 mtag = m_tag_alloc(MTAG_ABI_COMPAT, DN_AQM_MTAG_TS,
735 sizeof(aqm_time_t), M_NOWAIT);
740 *(aqm_time_t *)(mtag + 1) = AQM_UNOW;
741 m_tag_prepend(m, mtag);
745 mq_append(&q->mq, m);
746 fq_update_stats(q, si, len, 0);
749 fq_update_stats(q, si, len, 1);
758 /* Drop a packet form the head of FQ-PIE sub-queue */
760 pie_drop_head(struct fq_pie_flow *q, struct fq_pie_si *si)
762 struct mbuf *m = q->mq.head;
766 q->mq.head = m->m_nextpkt;
768 fq_update_stats(q, si, -m->m_pkthdr.len, 1);
770 if (si->main_q.ni.length == 0) /* queue is now idle */
771 si->main_q.q_time = dn_cfg.curr_time;
772 /* reset accu_prob after packet drop */
773 q->pst.accu_prob = 0;
779 * Classify a packet to queue number using Jenkins hash function.
780 * Return: queue number
781 * the input of the hash are protocol no, perturbation, src IP, dst IP,
782 * src port, dst port,
785 fq_pie_classify_flow(struct mbuf *m, uint16_t fcount, struct fq_pie_si *si)
793 ip = (struct ip *)mtodo(m, dn_tag_get(m)->iphdr_off);
797 isip6 = (ip->ip_v == 6);
800 ip6 = (struct ip6_hdr *)ip;
801 *((uint8_t *) &tuple[0]) = ip6->ip6_nxt;
802 *((uint32_t *) &tuple[1]) = si->perturbation;
803 memcpy(&tuple[5], ip6->ip6_src.s6_addr, 16);
804 memcpy(&tuple[21], ip6->ip6_dst.s6_addr, 16);
806 switch (ip6->ip6_nxt) {
808 th = (struct tcphdr *)(ip6 + 1);
809 *((uint16_t *) &tuple[37]) = th->th_dport;
810 *((uint16_t *) &tuple[39]) = th->th_sport;
814 uh = (struct udphdr *)(ip6 + 1);
815 *((uint16_t *) &tuple[37]) = uh->uh_dport;
816 *((uint16_t *) &tuple[39]) = uh->uh_sport;
819 memset(&tuple[37], 0, 4);
822 hash = jenkins_hash(tuple, 41, HASHINIT) % fcount;
828 *((uint8_t *) &tuple[0]) = ip->ip_p;
829 *((uint32_t *) &tuple[1]) = si->perturbation;
830 *((uint32_t *) &tuple[5]) = ip->ip_src.s_addr;
831 *((uint32_t *) &tuple[9]) = ip->ip_dst.s_addr;
835 th = (struct tcphdr *)(ip + 1);
836 *((uint16_t *) &tuple[13]) = th->th_dport;
837 *((uint16_t *) &tuple[15]) = th->th_sport;
841 uh = (struct udphdr *)(ip + 1);
842 *((uint16_t *) &tuple[13]) = uh->uh_dport;
843 *((uint16_t *) &tuple[15]) = uh->uh_sport;
846 memset(&tuple[13], 0, 4);
848 hash = jenkins_hash(tuple, 17, HASHINIT) % fcount;
854 * Enqueue a packet into an appropriate queue according to
855 * FQ-CoDe; algorithm.
858 fq_pie_enqueue(struct dn_sch_inst *_si, struct dn_queue *_q,
861 struct fq_pie_si *si;
862 struct fq_pie_schk *schk;
863 struct dn_sch_fq_pie_parms *param;
864 struct dn_queue *mainq;
865 struct fq_pie_flow *flows;
866 int idx, drop, i, maxidx;
868 mainq = (struct dn_queue *)(_si + 1);
869 si = (struct fq_pie_si *)_si;
870 flows = si->si_extra->flows;
871 schk = (struct fq_pie_schk *)(si->_si.sched+1);
874 /* classify a packet to queue number*/
875 idx = fq_pie_classify_flow(m, param->flows_cnt, si);
877 /* enqueue packet into appropriate queue using PIE AQM.
878 * Note: 'pie_enqueue' function returns 1 only when it unable to
879 * add timestamp to packet (no limit check)*/
880 drop = pie_enqueue(&flows[idx], m, si);
882 /* pie unable to timestamp a packet */
886 /* If the flow (sub-queue) is not active ,then add it to tail of
887 * new flows list, initialize and activate it.
889 if (!flows[idx].active) {
890 STAILQ_INSERT_TAIL(&si->newflows, &flows[idx], flowchain);
891 flows[idx].deficit = param->quantum;
892 fq_activate_pie(&flows[idx]);
893 flows[idx].active = 1;
896 /* check the limit for all queues and remove a packet from the
899 if (mainq->ni.length > schk->cfg.limit) {
900 /* find first active flow */
901 for (maxidx = 0; maxidx < schk->cfg.flows_cnt; maxidx++)
902 if (flows[maxidx].active)
904 if (maxidx < schk->cfg.flows_cnt) {
905 /* find the largest sub- queue */
906 for (i = maxidx + 1; i < schk->cfg.flows_cnt; i++)
907 if (flows[i].active && flows[i].stats.length >
908 flows[maxidx].stats.length)
910 pie_drop_head(&flows[maxidx], si);
919 * Dequeue a packet from an appropriate queue according to
920 * FQ-CoDel algorithm.
923 fq_pie_dequeue(struct dn_sch_inst *_si)
925 struct fq_pie_si *si;
926 struct fq_pie_schk *schk;
927 struct dn_sch_fq_pie_parms *param;
928 struct fq_pie_flow *f;
930 struct fq_pie_list *fq_pie_flowlist;
932 si = (struct fq_pie_si *)_si;
933 schk = (struct fq_pie_schk *)(si->_si.sched+1);
937 /* select a list to start with */
938 if (STAILQ_EMPTY(&si->newflows))
939 fq_pie_flowlist = &si->oldflows;
941 fq_pie_flowlist = &si->newflows;
943 /* Both new and old queue lists are empty, return NULL */
944 if (STAILQ_EMPTY(fq_pie_flowlist))
947 f = STAILQ_FIRST(fq_pie_flowlist);
949 /* if there is no flow(sub-queue) deficit, increase deficit
950 * by quantum, move the flow to the tail of old flows list
951 * and try another flow.
952 * Otherwise, the flow will be used for dequeue.
954 if (f->deficit < 0) {
955 f->deficit += param->quantum;
956 STAILQ_REMOVE_HEAD(fq_pie_flowlist, flowchain);
957 STAILQ_INSERT_TAIL(&si->oldflows, f, flowchain);
961 f = STAILQ_FIRST(fq_pie_flowlist);
964 /* the new flows list is empty, try old flows list */
965 if (STAILQ_EMPTY(fq_pie_flowlist))
968 /* Dequeue a packet from the selected flow */
969 mbuf = pie_dequeue(f, si);
971 /* pie did not return a packet */
973 /* If the selected flow belongs to new flows list, then move
974 * it to the tail of old flows list. Otherwise, deactivate it and
975 * remove it from the old list and
977 if (fq_pie_flowlist == &si->newflows) {
978 STAILQ_REMOVE_HEAD(fq_pie_flowlist, flowchain);
979 STAILQ_INSERT_TAIL(&si->oldflows, f, flowchain);
982 fq_deactivate_pie(&f->pst);
983 STAILQ_REMOVE_HEAD(fq_pie_flowlist, flowchain);
989 /* we have a packet to return,
990 * update flow deficit and return the packet*/
991 f->deficit -= mbuf->m_pkthdr.len;
996 /* unreachable point */
1001 * Initialize fq_pie scheduler instance.
1002 * also, allocate memory for flows array.
1005 fq_pie_new_sched(struct dn_sch_inst *_si)
1007 struct fq_pie_si *si;
1009 struct fq_pie_schk *schk;
1010 struct fq_pie_flow *flows;
1013 si = (struct fq_pie_si *)_si;
1014 schk = (struct fq_pie_schk *)(_si->sched+1);
1017 D("si already configured!");
1021 /* init the main queue */
1023 set_oid(&q->ni.oid, DN_QUEUE, sizeof(*q));
1025 q->fs = _si->sched->fs;
1027 /* allocate memory for scheduler instance extra vars */
1028 si->si_extra = malloc(sizeof(struct fq_pie_si_extra),
1029 M_DUMMYNET, M_NOWAIT | M_ZERO);
1030 if (si->si_extra == NULL) {
1031 D("cannot allocate memory for fq_pie si extra vars");
1034 /* allocate memory for flows array */
1035 si->si_extra->flows = mallocarray(schk->cfg.flows_cnt,
1036 sizeof(struct fq_pie_flow), M_DUMMYNET, M_NOWAIT | M_ZERO);
1037 flows = si->si_extra->flows;
1038 if (flows == NULL) {
1039 free(si->si_extra, M_DUMMYNET);
1040 si->si_extra = NULL;
1041 D("cannot allocate memory for fq_pie flows");
1045 /* init perturbation for this si */
1046 si->perturbation = random();
1047 si->si_extra->nr_active_q = 0;
1049 /* init the old and new flows lists */
1050 STAILQ_INIT(&si->newflows);
1051 STAILQ_INIT(&si->oldflows);
1053 /* init the flows (sub-queues) */
1054 for (i = 0; i < schk->cfg.flows_cnt; i++) {
1055 flows[i].pst.parms = &schk->cfg.pcfg;
1056 flows[i].psi_extra = si->si_extra;
1057 pie_init(&flows[i], schk);
1060 fq_pie_desc.ref_count++;
1066 * Free fq_pie scheduler instance.
1069 fq_pie_free_sched(struct dn_sch_inst *_si)
1071 struct fq_pie_si *si;
1072 struct fq_pie_schk *schk;
1073 struct fq_pie_flow *flows;
1076 si = (struct fq_pie_si *)_si;
1077 schk = (struct fq_pie_schk *)(_si->sched+1);
1078 flows = si->si_extra->flows;
1079 for (i = 0; i < schk->cfg.flows_cnt; i++) {
1080 pie_cleanup(&flows[i]);
1082 si->si_extra = NULL;
1087 * Configure FQ-PIE scheduler.
1088 * the configurations for the scheduler is passed fromipfw userland.
1091 fq_pie_config(struct dn_schk *_schk)
1093 struct fq_pie_schk *schk;
1094 struct dn_extra_parms *ep;
1095 struct dn_sch_fq_pie_parms *fqp_cfg;
1097 schk = (struct fq_pie_schk *)(_schk+1);
1098 ep = (struct dn_extra_parms *) _schk->cfg;
1100 /* par array contains fq_pie configuration as follow
1101 * PIE: 0- qdelay_ref,1- tupdate, 2- max_burst
1102 * 3- max_ecnth, 4- alpha, 5- beta, 6- flags
1103 * FQ_PIE: 7- quantum, 8- limit, 9- flows
1105 if (ep && ep->oid.len ==sizeof(*ep) &&
1106 ep->oid.subtype == DN_SCH_PARAMS) {
1107 fqp_cfg = &schk->cfg;
1109 fqp_cfg->pcfg.qdelay_ref = fq_pie_sysctl.pcfg.qdelay_ref;
1111 fqp_cfg->pcfg.qdelay_ref = ep->par[0];
1113 fqp_cfg->pcfg.tupdate = fq_pie_sysctl.pcfg.tupdate;
1115 fqp_cfg->pcfg.tupdate = ep->par[1];
1117 fqp_cfg->pcfg.max_burst = fq_pie_sysctl.pcfg.max_burst;
1119 fqp_cfg->pcfg.max_burst = ep->par[2];
1121 fqp_cfg->pcfg.max_ecnth = fq_pie_sysctl.pcfg.max_ecnth;
1123 fqp_cfg->pcfg.max_ecnth = ep->par[3];
1125 fqp_cfg->pcfg.alpha = fq_pie_sysctl.pcfg.alpha;
1127 fqp_cfg->pcfg.alpha = ep->par[4];
1129 fqp_cfg->pcfg.beta = fq_pie_sysctl.pcfg.beta;
1131 fqp_cfg->pcfg.beta = ep->par[5];
1133 fqp_cfg->pcfg.flags = 0;
1135 fqp_cfg->pcfg.flags = ep->par[6];
1137 /* FQ configurations */
1139 fqp_cfg->quantum = fq_pie_sysctl.quantum;
1141 fqp_cfg->quantum = ep->par[7];
1143 fqp_cfg->limit = fq_pie_sysctl.limit;
1145 fqp_cfg->limit = ep->par[8];
1147 fqp_cfg->flows_cnt = fq_pie_sysctl.flows_cnt;
1149 fqp_cfg->flows_cnt = ep->par[9];
1151 /* Bound the configurations */
1152 fqp_cfg->pcfg.qdelay_ref = BOUND_VAR(fqp_cfg->pcfg.qdelay_ref,
1153 1, 5 * AQM_TIME_1S);
1154 fqp_cfg->pcfg.tupdate = BOUND_VAR(fqp_cfg->pcfg.tupdate,
1155 1, 5 * AQM_TIME_1S);
1156 fqp_cfg->pcfg.max_burst = BOUND_VAR(fqp_cfg->pcfg.max_burst,
1157 0, 5 * AQM_TIME_1S);
1158 fqp_cfg->pcfg.max_ecnth = BOUND_VAR(fqp_cfg->pcfg.max_ecnth,
1160 fqp_cfg->pcfg.alpha = BOUND_VAR(fqp_cfg->pcfg.alpha, 0, 7 * PIE_SCALE);
1161 fqp_cfg->pcfg.beta = BOUND_VAR(fqp_cfg->pcfg.beta, 0, 7 * PIE_SCALE);
1163 fqp_cfg->quantum = BOUND_VAR(fqp_cfg->quantum,1,9000);
1164 fqp_cfg->limit= BOUND_VAR(fqp_cfg->limit,1,20480);
1165 fqp_cfg->flows_cnt= BOUND_VAR(fqp_cfg->flows_cnt,1,65536);
1168 D("Wrong parameters for fq_pie scheduler");
1176 * Return FQ-PIE scheduler configurations
1177 * the configurations for the scheduler is passed to userland.
1180 fq_pie_getconfig (struct dn_schk *_schk, struct dn_extra_parms *ep) {
1181 struct fq_pie_schk *schk = (struct fq_pie_schk *)(_schk+1);
1182 struct dn_sch_fq_pie_parms *fqp_cfg;
1184 fqp_cfg = &schk->cfg;
1186 strcpy(ep->name, fq_pie_desc.name);
1187 ep->par[0] = fqp_cfg->pcfg.qdelay_ref;
1188 ep->par[1] = fqp_cfg->pcfg.tupdate;
1189 ep->par[2] = fqp_cfg->pcfg.max_burst;
1190 ep->par[3] = fqp_cfg->pcfg.max_ecnth;
1191 ep->par[4] = fqp_cfg->pcfg.alpha;
1192 ep->par[5] = fqp_cfg->pcfg.beta;
1193 ep->par[6] = fqp_cfg->pcfg.flags;
1195 ep->par[7] = fqp_cfg->quantum;
1196 ep->par[8] = fqp_cfg->limit;
1197 ep->par[9] = fqp_cfg->flows_cnt;
1203 * FQ-PIE scheduler descriptor
1204 * contains the type of the scheduler, the name, the size of extra
1205 * data structures, and function pointers.
1207 static struct dn_alg fq_pie_desc = {
1208 _SI( .type = ) DN_SCHED_FQ_PIE,
1209 _SI( .name = ) "FQ_PIE",
1212 _SI( .schk_datalen = ) sizeof(struct fq_pie_schk),
1213 _SI( .si_datalen = ) sizeof(struct fq_pie_si) - sizeof(struct dn_sch_inst),
1214 _SI( .q_datalen = ) 0,
1216 _SI( .enqueue = ) fq_pie_enqueue,
1217 _SI( .dequeue = ) fq_pie_dequeue,
1218 _SI( .config = ) fq_pie_config, /* new sched i.e. sched X config ...*/
1219 _SI( .destroy = ) NULL, /*sched x delete */
1220 _SI( .new_sched = ) fq_pie_new_sched, /* new schd instance */
1221 _SI( .free_sched = ) fq_pie_free_sched, /* delete schd instance */
1222 _SI( .new_fsk = ) NULL,
1223 _SI( .free_fsk = ) NULL,
1224 _SI( .new_queue = ) NULL,
1225 _SI( .free_queue = ) NULL,
1226 _SI( .getconfig = ) fq_pie_getconfig,
1227 _SI( .ref_count = ) 0
1230 DECLARE_DNSCHED_MODULE(dn_fq_pie, &fq_pie_desc);