/*1* PIE - Proportional Integral controller Enhanced AQM algorithm.2*3* Copyright (C) 2016 Centre for Advanced Internet Architectures,4* Swinburne University of Technology, Melbourne, Australia.5* Portions of this code were made possible in part by a gift from6* The Comcast Innovation Fund.7* Implemented by Rasool Al-Saadi <[email protected]>8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#ifndef _IP_DN_AQM_PIE_H32#define _IP_DN_AQM_PIE_H3334#define DN_AQM_PIE 235#define PIE_DQ_THRESHOLD_BITS 1436/* 2^14 =16KB */37#define PIE_DQ_THRESHOLD (1L << PIE_DQ_THRESHOLD_BITS)38#define MEAN_PKTSIZE 8003940/* 31-bits because random() generates range from 0->(2**31)-1 */41#define PIE_PROB_BITS 3142#define PIE_MAX_PROB ((1LL<<PIE_PROB_BITS) -1)4344/* for 16-bits, we have 3-bits for integer part and 13-bits for fraction */45#define PIE_FIX_POINT_BITS 1346#define PIE_SCALE (1L<<PIE_FIX_POINT_BITS)4748/* PIE options */49enum {50PIE_ECN_ENABLED =1,51PIE_CAPDROP_ENABLED = 2,52PIE_ON_OFF_MODE_ENABLED = 4,53PIE_DEPRATEEST_ENABLED = 8,54PIE_DERAND_ENABLED = 1655};5657/* PIE parameters */58struct dn_aqm_pie_parms {59aqm_time_t qdelay_ref; /* AQM Latency Target (default: 15ms) */60aqm_time_t tupdate; /* a period to calculate drop probability (default:15ms) */61aqm_time_t max_burst; /* AQM Max Burst Allowance (default: 150ms) */62uint16_t max_ecnth; /*AQM Max ECN Marking Threshold (default: 10%) */63uint16_t alpha; /* (default: 1/8) */64uint16_t beta; /* (default: 1+1/4) */65uint32_t flags; /* PIE options */66};6768/* PIE status variables */69struct pie_status{70struct callout aqm_pie_callout;71aqm_time_t burst_allowance;72uint32_t drop_prob;73aqm_time_t current_qdelay;74aqm_time_t qdelay_old;75uint64_t accu_prob;76aqm_time_t measurement_start;77aqm_time_t avg_dq_time;78uint32_t dq_count;79uint32_t sflags;80struct dn_aqm_pie_parms *parms; /* pointer to PIE configurations */81/* pointer to parent queue of FQ-PIE sub-queues, or queue of owner fs. */82struct dn_queue *pq;83struct mtx lock_mtx;84uint32_t one_third_q_size; /* 1/3 of queue size, for speed optization */85};8687enum {88ENQUE = 1,89DROP,90MARKECN91};9293/* PIE current state */94enum {95PIE_ACTIVE = 1,96PIE_INMEASUREMENT = 297};9899/*100* Check if eneque should drop packet to control delay or not based on101* PIe algorithm.102* return DROP if it is time to drop or ENQUE otherwise.103* This function is used by PIE and FQ-PIE.104*/105__inline static int106drop_early(struct pie_status *pst, uint32_t qlen)107{108struct dn_aqm_pie_parms *pprms;109110pprms = pst->parms;111112/* queue is not congested */113114if ((pst->qdelay_old < (pprms->qdelay_ref >> 1)115&& pst->drop_prob < PIE_MAX_PROB / 5 )116|| qlen <= 2 * MEAN_PKTSIZE)117return ENQUE;118119if (pst->drop_prob == 0)120pst->accu_prob = 0;121122/* increment accu_prob */123if (pprms->flags & PIE_DERAND_ENABLED)124pst->accu_prob += pst->drop_prob;125126/* De-randomize option127* if accu_prob < 0.85 -> enqueue128* if accu_prob>8.5 ->drop129* between 0.85 and 8.5 || !De-randomize --> drop on prob130*131* (0.85 = 17/20 ,8.5 = 17/2)132*/133if (pprms->flags & PIE_DERAND_ENABLED) {134if(pst->accu_prob < (uint64_t) (PIE_MAX_PROB * 17 / 20))135return ENQUE;136if( pst->accu_prob >= (uint64_t) (PIE_MAX_PROB * 17 / 2))137return DROP;138}139140if (random() < pst->drop_prob) {141pst->accu_prob = 0;142return DROP;143}144145return ENQUE;146}147148#endif149150151