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.
47 #include <sys/malloc.h>
48 #include <sys/socket.h>
49 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/mutex.h>
54 #include <net/if.h> /* IFNAMSIZ */
55 #include <netinet/in.h>
56 #include <netinet/ip_var.h> /* ipfw_rule_ref */
57 #include <netinet/ip_fw.h> /* flow_id */
58 #include <netinet/ip_dummynet.h>
61 #include <sys/rwlock.h>
63 #include <netpfil/ipfw/ip_fw_private.h>
64 #include <sys/sysctl.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip6.h>
67 #include <netinet/ip_icmp.h>
68 #include <netinet/tcp.h>
69 #include <netinet/udp.h>
70 #include <sys/queue.h>
73 #include <netpfil/ipfw/dn_heap.h>
74 #include <netpfil/ipfw/ip_dn_private.h>
76 #include <netpfil/ipfw/dn_aqm.h>
77 #include <netpfil/ipfw/dn_aqm_pie.h>
78 #include <netpfil/ipfw/dn_sched.h>
84 #define DN_SCHED_FQ_PIE 7
87 STAILQ_HEAD(fq_pie_list, fq_pie_flow) ;
89 /* FQ_PIE parameters including PIE */
90 struct dn_sch_fq_pie_parms {
91 struct dn_aqm_pie_parms pcfg; /* PIE configuration Parameters */
92 /* FQ_PIE Parameters */
93 uint32_t flows_cnt; /* number of flows */
94 uint32_t limit; /* hard limit of FQ_PIE queue size*/
98 /* flow (sub-queue) stats */
100 uint64_t tot_pkts; /* statistics counters */
102 uint32_t length; /* Queue length, in packets */
103 uint32_t len_bytes; /* Queue length, in bytes */
107 /* A flow of packets (sub-queue)*/
109 struct mq mq; /* list of packets */
110 struct flow_stats stats; /* statistics */
112 int active; /* 1: flow is active (in a list) */
113 struct pie_status pst; /* pie status variables */
114 struct fq_pie_si_extra *psi_extra;
115 STAILQ_ENTRY(fq_pie_flow) flowchain;
118 /* extra fq_pie scheduler configurations */
120 struct dn_sch_fq_pie_parms cfg;
124 /* fq_pie scheduler instance extra state vars.
125 * The purpose of separation this structure is to preserve number of active
126 * sub-queues and the flows array pointer even after the scheduler instance
128 * Preserving these varaiables allows freeing the allocated memory by
129 * fqpie_callout_cleanup() independently from fq_pie_free_sched().
131 struct fq_pie_si_extra {
132 uint32_t nr_active_q; /* number of active queues */
133 struct fq_pie_flow *flows; /* array of flows (queues) */
136 /* fq_pie scheduler instance */
138 struct dn_sch_inst _si; /* standard scheduler instance. SHOULD BE FIRST */
139 struct dn_queue main_q; /* main queue is after si directly */
140 uint32_t perturbation; /* random value */
141 struct fq_pie_list newflows; /* list of new queues */
142 struct fq_pie_list oldflows; /* list of old queues */
143 struct fq_pie_si_extra *si_extra; /* extra state vars*/
147 static struct dn_alg fq_pie_desc;
149 /* Default FQ-PIE parameters including PIE */
151 * target=15ms, max_burst=150ms, max_ecnth=0.1,
152 * alpha=0.125, beta=1.25, tupdate=15ms
154 * flows=1024, limit=10240, quantum =1514
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},
163 fqpie_sysctl_alpha_beta_handler(SYSCTL_HANDLER_ARGS)
168 if (!strcmp(oidp->oid_name,"alpha"))
169 value = fq_pie_sysctl.pcfg.alpha;
171 value = fq_pie_sysctl.pcfg.beta;
173 value = value * 1000 / PIE_SCALE;
174 error = sysctl_handle_long(oidp, &value, 0, req);
175 if (error != 0 || req->newptr == NULL)
177 if (value < 1 || value > 7 * PIE_SCALE)
179 value = (value * PIE_SCALE) / 1000;
180 if (!strcmp(oidp->oid_name,"alpha"))
181 fq_pie_sysctl.pcfg.alpha = value;
183 fq_pie_sysctl.pcfg.beta = value;
188 fqpie_sysctl_target_tupdate_maxb_handler(SYSCTL_HANDLER_ARGS)
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;
198 value = fq_pie_sysctl.pcfg.max_burst;
200 value = value / AQM_TIME_1US;
201 error = sysctl_handle_long(oidp, &value, 0, req);
202 if (error != 0 || req->newptr == NULL)
204 if (value < 1 || value > 10 * AQM_TIME_1S)
206 value = value * AQM_TIME_1US;
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;
213 fq_pie_sysctl.pcfg.max_burst = value;
218 fqpie_sysctl_max_ecnth_handler(SYSCTL_HANDLER_ARGS)
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)
228 if (value < 1 || value > PIE_SCALE)
230 value = (value * PIE_SCALE) / 1000;
231 fq_pie_sysctl.pcfg.max_ecnth = value;
235 /* define FQ- PIE sysctl variables */
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,
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");
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");
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");
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");
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");
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");
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");
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
291 fq_update_stats(struct fq_pie_flow *q, struct fq_pie_si *si, int len,
302 si->main_q.ni.drops ++;
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;
313 /*update sub-queue stats */
314 q->stats.length += inc;
315 q->stats.len_bytes += len;
317 /*update scheduler instance stats */
318 si->_si.ni.length += inc;
319 si->_si.ni.len_bytes += len;
323 si->main_q.ni.tot_bytes += len;
324 si->main_q.ni.tot_pkts ++;
326 q->stats.tot_bytes +=len;
329 si->_si.ni.tot_bytes +=len;
330 si->_si.ni.tot_pkts ++;
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.
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)
344 struct mbuf *m = q->mq.head;
348 q->mq.head = m->m_nextpkt;
350 fq_update_stats(q, si, -m->m_pkthdr.len, 0);
352 if (si->main_q.ni.length == 0) /* queue is now idle */
353 si->main_q.q_time = dn_cfg.curr_time;
356 /* extract packet timestamp*/
358 mtag = m_tag_locate(m, MTAG_ABI_COMPAT, DN_AQM_MTAG_TS, NULL);
360 D("PIE timestamp mtag not found!");
363 *pkt_ts = *(aqm_time_t *)(mtag + 1);
364 m_tag_delete(m,mtag);
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
376 fq_calculate_drop_prob(void *x)
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;
387 prob = pst->drop_prob;
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.
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;
397 if (!q->stats.len_bytes)
398 pst->current_qdelay = 0;
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);
406 /* take absolute value so right shift result is well defined */
412 /* We PIE_MAX_PROB shift by 12-bits to increase the division precision */
413 p *= (PIE_MAX_PROB << 12) / AQM_TIME_1S;
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;
429 p >>= PIE_FIX_POINT_BITS + 12;
436 /* check for multiplication underflow */
437 if (prob > oldprob) {
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;
451 /* check for multiplication overflow */
459 * decay the drop probability exponentially
460 * and restrict it to range 0 to PIE_MAX_PROB
465 if (pst->current_qdelay == 0 && pst->qdelay_old == 0) {
466 /* 0.98 ~= 1- 1/64 */
467 prob = prob - (prob >> 6);
470 if (prob > PIE_MAX_PROB) {
475 pst->drop_prob = prob;
477 /* store current delay value */
478 pst->qdelay_old = pst->current_qdelay;
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;
485 pst->burst_allowance = 0;
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);
493 mtx_unlock(&pst->lock_mtx);
497 * Reset PIE variables & activate the queue
500 fq_activate_pie(struct fq_pie_flow *q)
502 struct pie_status *pst = &q->pst;
503 struct dn_aqm_pie_parms *pprms;
505 mtx_lock(&pst->lock_mtx);
511 pst->burst_allowance = pprms->max_burst;
514 pst->avg_dq_time = 0;
515 pst->sflags = PIE_INMEASUREMENT | PIE_ACTIVE;
516 pst->measurement_start = AQM_UNOW;
518 callout_reset_sbt(&pst->aqm_pie_callout,
519 (uint64_t)pprms->tupdate * SBT_1US,
520 0, fq_calculate_drop_prob, q, 0);
522 mtx_unlock(&pst->lock_mtx);
527 * Deactivate PIE and stop probe update callout
530 fq_deactivate_pie(struct pie_status *pst)
532 mtx_lock(&pst->lock_mtx);
533 pst->sflags &= ~(PIE_ACTIVE | PIE_INMEASUREMENT);
534 callout_stop(&pst->aqm_pie_callout);
535 //D("PIE Deactivated");
536 mtx_unlock(&pst->lock_mtx);
540 * Initialize PIE for sub-queue 'q'
543 pie_init(struct fq_pie_flow *q, struct fq_pie_schk *fqpie_schk)
545 struct pie_status *pst=&q->pst;
546 struct dn_aqm_pie_parms *pprms = pst->parms;
550 D("AQM_PIE is not configured");
553 q->psi_extra->nr_active_q++;
555 /* For speed optimization, we caculate 1/3 queue size once here */
556 // XXX limit divided by number of queues divided by 3 ???
557 pst->one_third_q_size = (fqpie_schk->cfg.limit /
558 fqpie_schk->cfg.flows_cnt) / 3;
560 mtx_init(&pst->lock_mtx, "mtx_pie", NULL, MTX_DEF);
561 callout_init_mtx(&pst->aqm_pie_callout, &pst->lock_mtx,
562 CALLOUT_RETURNUNLOCKED);
569 * callout function to destroy PIE lock, and free fq_pie flows and fq_pie si
570 * extra memory when number of active sub-queues reaches zero.
571 * 'x' is a fq_pie_flow to be destroyed
574 fqpie_callout_cleanup(void *x)
576 struct fq_pie_flow *q = x;
577 struct pie_status *pst = &q->pst;
578 struct fq_pie_si_extra *psi_extra;
580 mtx_unlock(&pst->lock_mtx);
581 mtx_destroy(&pst->lock_mtx);
582 psi_extra = q->psi_extra;
585 psi_extra->nr_active_q--;
587 /* when all sub-queues are destroyed, free flows fq_pie extra vars memory */
588 if (!psi_extra->nr_active_q) {
589 free(psi_extra->flows, M_DUMMYNET);
590 free(psi_extra, M_DUMMYNET);
591 fq_pie_desc.ref_count--;
597 * Clean up PIE status for sub-queue 'q'
598 * Stop callout timer and destroy mtx using fqpie_callout_cleanup() callout.
601 pie_cleanup(struct fq_pie_flow *q)
603 struct pie_status *pst = &q->pst;
605 mtx_lock(&pst->lock_mtx);
606 callout_reset_sbt(&pst->aqm_pie_callout,
607 SBT_1US, 0, fqpie_callout_cleanup, q, 0);
608 mtx_unlock(&pst->lock_mtx);
613 * Dequeue and return a pcaket from sub-queue 'q' or NULL if 'q' is empty.
614 * Also, caculate depature time or queue delay using timestamp
617 pie_dequeue(struct fq_pie_flow *q, struct fq_pie_si *si)
620 struct dn_aqm_pie_parms *pprms;
621 struct pie_status *pst;
623 aqm_time_t pkt_ts, dq_time;
627 pprms = q->pst.parms;
629 /*we extarct packet ts only when Departure Rate Estimation dis not used*/
630 m = fq_pie_extract_head(q, &pkt_ts, si,
631 !(pprms->flags & PIE_DEPRATEEST_ENABLED));
633 if (!m || !(pst->sflags & PIE_ACTIVE))
637 if (pprms->flags & PIE_DEPRATEEST_ENABLED) {
638 /* calculate average depature time */
639 if(pst->sflags & PIE_INMEASUREMENT) {
640 pst->dq_count += m->m_pkthdr.len;
642 if (pst->dq_count >= PIE_DQ_THRESHOLD) {
643 dq_time = now - pst->measurement_start;
646 * if we don't have old avg dq_time i.e PIE is (re)initialized,
647 * don't use weight to calculate new avg_dq_time
649 if(pst->avg_dq_time == 0)
650 pst->avg_dq_time = dq_time;
653 * weight = PIE_DQ_THRESHOLD/2^6, but we scaled
654 * weight by 2^8. Thus, scaled
655 * weight = PIE_DQ_THRESHOLD /2^8
657 w = PIE_DQ_THRESHOLD >> 8;
658 pst->avg_dq_time = (dq_time* w
659 + (pst->avg_dq_time * ((1L << 8) - w))) >> 8;
660 pst->sflags &= ~PIE_INMEASUREMENT;
666 * Start new measurment cycle when the queue has
667 * PIE_DQ_THRESHOLD worth of bytes.
669 if(!(pst->sflags & PIE_INMEASUREMENT) &&
670 q->stats.len_bytes >= PIE_DQ_THRESHOLD) {
671 pst->sflags |= PIE_INMEASUREMENT;
672 pst->measurement_start = now;
676 /* Optionally, use packet timestamp to estimate queue delay */
678 pst->current_qdelay = now - pkt_ts;
685 * Enqueue a packet in q, subject to space and FQ-PIE queue management policy
686 * (whose parameters are in q->fs).
687 * Update stats for the queue and the scheduler.
688 * Return 0 on success, 1 on drop. The packet is consumed anyways.
691 pie_enqueue(struct fq_pie_flow *q, struct mbuf* m, struct fq_pie_si *si)
694 struct pie_status *pst;
695 struct dn_aqm_pie_parms *pprms;
698 len = m->m_pkthdr.len;
703 /* drop/mark the packet when PIE is active and burst time elapsed */
704 if (pst->sflags & PIE_ACTIVE && pst->burst_allowance == 0
705 && drop_early(pst, q->stats.len_bytes) == DROP) {
707 * if drop_prob over ECN threshold, drop the packet
708 * otherwise mark and enqueue it.
710 if (pprms->flags & PIE_ECN_ENABLED && pst->drop_prob <
711 (pprms->max_ecnth << (PIE_PROB_BITS - PIE_FIX_POINT_BITS))
718 /* Turn PIE on when 1/3 of the queue is full */
719 if (!(pst->sflags & PIE_ACTIVE) && q->stats.len_bytes >=
720 pst->one_third_q_size) {
724 /* reset burst tolerance and optinally turn PIE off*/
725 if (pst->drop_prob == 0 && pst->current_qdelay < (pprms->qdelay_ref >> 1)
726 && pst->qdelay_old < (pprms->qdelay_ref >> 1)) {
728 pst->burst_allowance = pprms->max_burst;
729 if (pprms->flags & PIE_ON_OFF_MODE_ENABLED && q->stats.len_bytes<=0)
730 fq_deactivate_pie(pst);
733 /* Use timestamp if Departure Rate Estimation mode is disabled */
734 if (t != DROP && !(pprms->flags & PIE_DEPRATEEST_ENABLED)) {
735 /* Add TS to mbuf as a TAG */
737 mtag = m_tag_locate(m, MTAG_ABI_COMPAT, DN_AQM_MTAG_TS, NULL);
739 mtag = m_tag_alloc(MTAG_ABI_COMPAT, DN_AQM_MTAG_TS,
740 sizeof(aqm_time_t), M_NOWAIT);
745 *(aqm_time_t *)(mtag + 1) = AQM_UNOW;
746 m_tag_prepend(m, mtag);
750 mq_append(&q->mq, m);
751 fq_update_stats(q, si, len, 0);
754 fq_update_stats(q, si, len, 1);
763 /* Drop a packet form the head of FQ-PIE sub-queue */
765 pie_drop_head(struct fq_pie_flow *q, struct fq_pie_si *si)
767 struct mbuf *m = q->mq.head;
771 q->mq.head = m->m_nextpkt;
773 fq_update_stats(q, si, -m->m_pkthdr.len, 1);
775 if (si->main_q.ni.length == 0) /* queue is now idle */
776 si->main_q.q_time = dn_cfg.curr_time;
777 /* reset accu_prob after packet drop */
778 q->pst.accu_prob = 0;
784 * Classify a packet to queue number using Jenkins hash function.
785 * Return: queue number
786 * the input of the hash are protocol no, perturbation, src IP, dst IP,
787 * src port, dst port,
790 fq_pie_classify_flow(struct mbuf *m, uint16_t fcount, struct fq_pie_si *si)
798 ip = (struct ip *)mtodo(m, dn_tag_get(m)->iphdr_off);
802 isip6 = (ip->ip_v == 6);
805 ip6 = (struct ip6_hdr *)ip;
806 *((uint8_t *) &tuple[0]) = ip6->ip6_nxt;
807 *((uint32_t *) &tuple[1]) = si->perturbation;
808 memcpy(&tuple[5], ip6->ip6_src.s6_addr, 16);
809 memcpy(&tuple[21], ip6->ip6_dst.s6_addr, 16);
811 switch (ip6->ip6_nxt) {
813 th = (struct tcphdr *)(ip6 + 1);
814 *((uint16_t *) &tuple[37]) = th->th_dport;
815 *((uint16_t *) &tuple[39]) = th->th_sport;
819 uh = (struct udphdr *)(ip6 + 1);
820 *((uint16_t *) &tuple[37]) = uh->uh_dport;
821 *((uint16_t *) &tuple[39]) = uh->uh_sport;
824 memset(&tuple[37], 0, 4);
827 hash = jenkins_hash(tuple, 41, HASHINIT) % fcount;
833 *((uint8_t *) &tuple[0]) = ip->ip_p;
834 *((uint32_t *) &tuple[1]) = si->perturbation;
835 *((uint32_t *) &tuple[5]) = ip->ip_src.s_addr;
836 *((uint32_t *) &tuple[9]) = ip->ip_dst.s_addr;
840 th = (struct tcphdr *)(ip + 1);
841 *((uint16_t *) &tuple[13]) = th->th_dport;
842 *((uint16_t *) &tuple[15]) = th->th_sport;
846 uh = (struct udphdr *)(ip + 1);
847 *((uint16_t *) &tuple[13]) = uh->uh_dport;
848 *((uint16_t *) &tuple[15]) = uh->uh_sport;
851 memset(&tuple[13], 0, 4);
853 hash = jenkins_hash(tuple, 17, HASHINIT) % fcount;
859 * Enqueue a packet into an appropriate queue according to
860 * FQ-CoDe; algorithm.
863 fq_pie_enqueue(struct dn_sch_inst *_si, struct dn_queue *_q,
866 struct fq_pie_si *si;
867 struct fq_pie_schk *schk;
868 struct dn_sch_fq_pie_parms *param;
869 struct dn_queue *mainq;
870 struct fq_pie_flow *flows;
871 int idx, drop, i, maxidx;
873 mainq = (struct dn_queue *)(_si + 1);
874 si = (struct fq_pie_si *)_si;
875 flows = si->si_extra->flows;
876 schk = (struct fq_pie_schk *)(si->_si.sched+1);
879 /* classify a packet to queue number*/
880 idx = fq_pie_classify_flow(m, param->flows_cnt, si);
882 /* enqueue packet into appropriate queue using PIE AQM.
883 * Note: 'pie_enqueue' function returns 1 only when it unable to
884 * add timestamp to packet (no limit check)*/
885 drop = pie_enqueue(&flows[idx], m, si);
887 /* pie unable to timestamp a packet */
891 /* If the flow (sub-queue) is not active ,then add it to tail of
892 * new flows list, initialize and activate it.
894 if (!flows[idx].active) {
895 STAILQ_INSERT_TAIL(&si->newflows, &flows[idx], flowchain);
896 flows[idx].deficit = param->quantum;
897 fq_activate_pie(&flows[idx]);
898 flows[idx].active = 1;
901 /* check the limit for all queues and remove a packet from the
904 if (mainq->ni.length > schk->cfg.limit) {
905 /* find first active flow */
906 for (maxidx = 0; maxidx < schk->cfg.flows_cnt; maxidx++)
907 if (flows[maxidx].active)
909 if (maxidx < schk->cfg.flows_cnt) {
910 /* find the largest sub- queue */
911 for (i = maxidx + 1; i < schk->cfg.flows_cnt; i++)
912 if (flows[i].active && flows[i].stats.length >
913 flows[maxidx].stats.length)
915 pie_drop_head(&flows[maxidx], si);
924 * Dequeue a packet from an appropriate queue according to
925 * FQ-CoDel algorithm.
928 fq_pie_dequeue(struct dn_sch_inst *_si)
930 struct fq_pie_si *si;
931 struct fq_pie_schk *schk;
932 struct dn_sch_fq_pie_parms *param;
933 struct fq_pie_flow *f;
935 struct fq_pie_list *fq_pie_flowlist;
937 si = (struct fq_pie_si *)_si;
938 schk = (struct fq_pie_schk *)(si->_si.sched+1);
942 /* select a list to start with */
943 if (STAILQ_EMPTY(&si->newflows))
944 fq_pie_flowlist = &si->oldflows;
946 fq_pie_flowlist = &si->newflows;
948 /* Both new and old queue lists are empty, return NULL */
949 if (STAILQ_EMPTY(fq_pie_flowlist))
952 f = STAILQ_FIRST(fq_pie_flowlist);
954 /* if there is no flow(sub-queue) deficit, increase deficit
955 * by quantum, move the flow to the tail of old flows list
956 * and try another flow.
957 * Otherwise, the flow will be used for dequeue.
959 if (f->deficit < 0) {
960 f->deficit += param->quantum;
961 STAILQ_REMOVE_HEAD(fq_pie_flowlist, flowchain);
962 STAILQ_INSERT_TAIL(&si->oldflows, f, flowchain);
966 f = STAILQ_FIRST(fq_pie_flowlist);
969 /* the new flows list is empty, try old flows list */
970 if (STAILQ_EMPTY(fq_pie_flowlist))
973 /* Dequeue a packet from the selected flow */
974 mbuf = pie_dequeue(f, si);
976 /* pie did not return a packet */
978 /* If the selected flow belongs to new flows list, then move
979 * it to the tail of old flows list. Otherwise, deactivate it and
980 * remove it from the old list and
982 if (fq_pie_flowlist == &si->newflows) {
983 STAILQ_REMOVE_HEAD(fq_pie_flowlist, flowchain);
984 STAILQ_INSERT_TAIL(&si->oldflows, f, flowchain);
987 fq_deactivate_pie(&f->pst);
988 STAILQ_REMOVE_HEAD(fq_pie_flowlist, flowchain);
994 /* we have a packet to return,
995 * update flow deficit and return the packet*/
996 f->deficit -= mbuf->m_pkthdr.len;
1001 /* unreachable point */
1006 * Initialize fq_pie scheduler instance.
1007 * also, allocate memory for flows array.
1010 fq_pie_new_sched(struct dn_sch_inst *_si)
1012 struct fq_pie_si *si;
1014 struct fq_pie_schk *schk;
1015 struct fq_pie_flow *flows;
1018 si = (struct fq_pie_si *)_si;
1019 schk = (struct fq_pie_schk *)(_si->sched+1);
1022 D("si already configured!");
1026 /* init the main queue */
1028 set_oid(&q->ni.oid, DN_QUEUE, sizeof(*q));
1030 q->fs = _si->sched->fs;
1032 /* allocate memory for scheduler instance extra vars */
1033 si->si_extra = malloc(sizeof(struct fq_pie_si_extra),
1034 M_DUMMYNET, M_NOWAIT | M_ZERO);
1035 if (si->si_extra == NULL) {
1036 D("cannot allocate memory for fq_pie si extra vars");
1039 /* allocate memory for flows array */
1040 si->si_extra->flows = mallocarray(schk->cfg.flows_cnt,
1041 sizeof(struct fq_pie_flow), M_DUMMYNET, M_NOWAIT | M_ZERO);
1042 flows = si->si_extra->flows;
1043 if (flows == NULL) {
1044 free(si->si_extra, M_DUMMYNET);
1045 si->si_extra = NULL;
1046 D("cannot allocate memory for fq_pie flows");
1050 /* init perturbation for this si */
1051 si->perturbation = random();
1052 si->si_extra->nr_active_q = 0;
1054 /* init the old and new flows lists */
1055 STAILQ_INIT(&si->newflows);
1056 STAILQ_INIT(&si->oldflows);
1058 /* init the flows (sub-queues) */
1059 for (i = 0; i < schk->cfg.flows_cnt; i++) {
1060 flows[i].pst.parms = &schk->cfg.pcfg;
1061 flows[i].psi_extra = si->si_extra;
1062 pie_init(&flows[i], schk);
1065 fq_pie_desc.ref_count++;
1072 * Free fq_pie scheduler instance.
1075 fq_pie_free_sched(struct dn_sch_inst *_si)
1077 struct fq_pie_si *si;
1078 struct fq_pie_schk *schk;
1079 struct fq_pie_flow *flows;
1082 si = (struct fq_pie_si *)_si;
1083 schk = (struct fq_pie_schk *)(_si->sched+1);
1084 flows = si->si_extra->flows;
1085 for (i = 0; i < schk->cfg.flows_cnt; i++) {
1086 pie_cleanup(&flows[i]);
1088 si->si_extra = NULL;
1093 * Configure FQ-PIE scheduler.
1094 * the configurations for the scheduler is passed fromipfw userland.
1097 fq_pie_config(struct dn_schk *_schk)
1099 struct fq_pie_schk *schk;
1100 struct dn_extra_parms *ep;
1101 struct dn_sch_fq_pie_parms *fqp_cfg;
1103 schk = (struct fq_pie_schk *)(_schk+1);
1104 ep = (struct dn_extra_parms *) _schk->cfg;
1106 /* par array contains fq_pie configuration as follow
1107 * PIE: 0- qdelay_ref,1- tupdate, 2- max_burst
1108 * 3- max_ecnth, 4- alpha, 5- beta, 6- flags
1109 * FQ_PIE: 7- quantum, 8- limit, 9- flows
1111 if (ep && ep->oid.len ==sizeof(*ep) &&
1112 ep->oid.subtype == DN_SCH_PARAMS) {
1114 fqp_cfg = &schk->cfg;
1116 fqp_cfg->pcfg.qdelay_ref = fq_pie_sysctl.pcfg.qdelay_ref;
1118 fqp_cfg->pcfg.qdelay_ref = ep->par[0];
1120 fqp_cfg->pcfg.tupdate = fq_pie_sysctl.pcfg.tupdate;
1122 fqp_cfg->pcfg.tupdate = ep->par[1];
1124 fqp_cfg->pcfg.max_burst = fq_pie_sysctl.pcfg.max_burst;
1126 fqp_cfg->pcfg.max_burst = ep->par[2];
1128 fqp_cfg->pcfg.max_ecnth = fq_pie_sysctl.pcfg.max_ecnth;
1130 fqp_cfg->pcfg.max_ecnth = ep->par[3];
1132 fqp_cfg->pcfg.alpha = fq_pie_sysctl.pcfg.alpha;
1134 fqp_cfg->pcfg.alpha = ep->par[4];
1136 fqp_cfg->pcfg.beta = fq_pie_sysctl.pcfg.beta;
1138 fqp_cfg->pcfg.beta = ep->par[5];
1140 fqp_cfg->pcfg.flags = 0;
1142 fqp_cfg->pcfg.flags = ep->par[6];
1144 /* FQ configurations */
1146 fqp_cfg->quantum = fq_pie_sysctl.quantum;
1148 fqp_cfg->quantum = ep->par[7];
1150 fqp_cfg->limit = fq_pie_sysctl.limit;
1152 fqp_cfg->limit = ep->par[8];
1154 fqp_cfg->flows_cnt = fq_pie_sysctl.flows_cnt;
1156 fqp_cfg->flows_cnt = ep->par[9];
1158 /* Bound the configurations */
1159 fqp_cfg->pcfg.qdelay_ref = BOUND_VAR(fqp_cfg->pcfg.qdelay_ref,
1160 1, 5 * AQM_TIME_1S);
1161 fqp_cfg->pcfg.tupdate = BOUND_VAR(fqp_cfg->pcfg.tupdate,
1162 1, 5 * AQM_TIME_1S);
1163 fqp_cfg->pcfg.max_burst = BOUND_VAR(fqp_cfg->pcfg.max_burst,
1164 0, 5 * AQM_TIME_1S);
1165 fqp_cfg->pcfg.max_ecnth = BOUND_VAR(fqp_cfg->pcfg.max_ecnth,
1167 fqp_cfg->pcfg.alpha = BOUND_VAR(fqp_cfg->pcfg.alpha, 0, 7 * PIE_SCALE);
1168 fqp_cfg->pcfg.beta = BOUND_VAR(fqp_cfg->pcfg.beta, 0, 7 * PIE_SCALE);
1170 fqp_cfg->quantum = BOUND_VAR(fqp_cfg->quantum,1,9000);
1171 fqp_cfg->limit= BOUND_VAR(fqp_cfg->limit,1,20480);
1172 fqp_cfg->flows_cnt= BOUND_VAR(fqp_cfg->flows_cnt,1,65536);
1175 D("Wrong parameters for fq_pie scheduler");
1183 * Return FQ-PIE scheduler configurations
1184 * the configurations for the scheduler is passed to userland.
1187 fq_pie_getconfig (struct dn_schk *_schk, struct dn_extra_parms *ep) {
1189 struct fq_pie_schk *schk = (struct fq_pie_schk *)(_schk+1);
1190 struct dn_sch_fq_pie_parms *fqp_cfg;
1192 fqp_cfg = &schk->cfg;
1194 strcpy(ep->name, fq_pie_desc.name);
1195 ep->par[0] = fqp_cfg->pcfg.qdelay_ref;
1196 ep->par[1] = fqp_cfg->pcfg.tupdate;
1197 ep->par[2] = fqp_cfg->pcfg.max_burst;
1198 ep->par[3] = fqp_cfg->pcfg.max_ecnth;
1199 ep->par[4] = fqp_cfg->pcfg.alpha;
1200 ep->par[5] = fqp_cfg->pcfg.beta;
1201 ep->par[6] = fqp_cfg->pcfg.flags;
1203 ep->par[7] = fqp_cfg->quantum;
1204 ep->par[8] = fqp_cfg->limit;
1205 ep->par[9] = fqp_cfg->flows_cnt;
1211 * FQ-PIE scheduler descriptor
1212 * contains the type of the scheduler, the name, the size of extra
1213 * data structures, and function pointers.
1215 static struct dn_alg fq_pie_desc = {
1216 _SI( .type = ) DN_SCHED_FQ_PIE,
1217 _SI( .name = ) "FQ_PIE",
1220 _SI( .schk_datalen = ) sizeof(struct fq_pie_schk),
1221 _SI( .si_datalen = ) sizeof(struct fq_pie_si) - sizeof(struct dn_sch_inst),
1222 _SI( .q_datalen = ) 0,
1224 _SI( .enqueue = ) fq_pie_enqueue,
1225 _SI( .dequeue = ) fq_pie_dequeue,
1226 _SI( .config = ) fq_pie_config, /* new sched i.e. sched X config ...*/
1227 _SI( .destroy = ) NULL, /*sched x delete */
1228 _SI( .new_sched = ) fq_pie_new_sched, /* new schd instance */
1229 _SI( .free_sched = ) fq_pie_free_sched, /* delete schd instance */
1230 _SI( .new_fsk = ) NULL,
1231 _SI( .free_fsk = ) NULL,
1232 _SI( .new_queue = ) NULL,
1233 _SI( .free_queue = ) NULL,
1234 _SI( .getconfig = ) fq_pie_getconfig,
1235 _SI( .ref_count = ) 0
1238 DECLARE_DNSCHED_MODULE(dn_fq_pie, &fq_pie_desc);