/*-1* Copyright (C) 1998-20032* Sony Computer Science Laboratories Inc. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/25/*-26* Copyright (c) 1990-1994 Regents of the University of California.27* All rights reserved.28*29* Redistribution and use in source and binary forms, with or without30* modification, are permitted provided that the following conditions31* are met:32* 1. Redistributions of source code must retain the above copyright33* notice, this list of conditions and the following disclaimer.34* 2. Redistributions in binary form must reproduce the above copyright35* notice, this list of conditions and the following disclaimer in the36* documentation and/or other materials provided with the distribution.37* 3. All advertising materials mentioning features or use of this software38* must display the following acknowledgement:39* This product includes software developed by the Computer Systems40* Engineering Group at Lawrence Berkeley Laboratory.41* 4. Neither the name of the University nor of the Laboratory may be used42* to endorse or promote products derived from this software without43* specific prior written permission.44*45* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND46* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE47* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE48* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE49* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL50* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS51* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)52* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT53* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY54* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF55* SUCH DAMAGE.56*57* $KAME: altq_rio.c,v 1.17 2003/07/10 12:07:49 kjc Exp $58*/5960#include "opt_altq.h"61#include "opt_inet.h"62#include "opt_inet6.h"63#ifdef ALTQ_RIO /* rio is enabled by ALTQ_RIO option in opt_altq.h */6465#include <sys/param.h>66#include <sys/malloc.h>67#include <sys/mbuf.h>68#include <sys/socket.h>69#include <sys/systm.h>70#include <sys/errno.h>71#if 1 /* ALTQ3_COMPAT */72#include <sys/proc.h>73#include <sys/sockio.h>74#include <sys/kernel.h>75#endif7677#include <net/if.h>78#include <net/if_var.h>7980#include <netinet/in.h>81#include <netinet/in_systm.h>82#include <netinet/ip.h>83#ifdef INET684#include <netinet/ip6.h>85#endif8687#include <netpfil/pf/pf.h>88#include <netpfil/pf/pf_altq.h>89#include <net/altq/altq.h>90#include <net/altq/altq_cdnr.h>91#include <net/altq/altq_red.h>92#include <net/altq/altq_rio.h>9394/*95* RIO: RED with IN/OUT bit96* described in97* "Explicit Allocation of Best Effort Packet Delivery Service"98* David D. Clark and Wenjia Fang, MIT Lab for Computer Science99* http://diffserv.lcs.mit.edu/Papers/exp-alloc-ddc-wf.{ps,pdf}100*101* this implementation is extended to support more than 2 drop precedence102* values as described in RFC2597 (Assured Forwarding PHB Group).103*104*/105/*106* AF DS (differentiated service) codepoints.107* (classes can be mapped to CBQ or H-FSC classes.)108*109* 0 1 2 3 4 5 6 7110* +---+---+---+---+---+---+---+---+111* | CLASS |DropPre| 0 | CU |112* +---+---+---+---+---+---+---+---+113*114* class 1: 001115* class 2: 010116* class 3: 011117* class 4: 100118*119* low drop prec: 01120* medium drop prec: 10121* high drop prec: 01122*/123124/* normal red parameters */125#define W_WEIGHT 512 /* inverse of weight of EWMA (511/512) */126/* q_weight = 0.00195 */127128/* red parameters for a slow link */129#define W_WEIGHT_1 128 /* inverse of weight of EWMA (127/128) */130/* q_weight = 0.0078125 */131132/* red parameters for a very slow link (e.g., dialup) */133#define W_WEIGHT_2 64 /* inverse of weight of EWMA (63/64) */134/* q_weight = 0.015625 */135136/* fixed-point uses 12-bit decimal places */137#define FP_SHIFT 12 /* fixed-point shift */138139/* red parameters for drop probability */140#define INV_P_MAX 10 /* inverse of max drop probability */141#define TH_MIN 5 /* min threshold */142#define TH_MAX 15 /* max threshold */143144#define RIO_LIMIT 60 /* default max queue length */145#define RIO_STATS /* collect statistics */146147#define TV_DELTA(a, b, delta) { \148int xxs; \149\150delta = (a)->tv_usec - (b)->tv_usec; \151if ((xxs = (a)->tv_sec - (b)->tv_sec) != 0) { \152if (xxs < 0) { \153delta = 60000000; \154} else if (xxs > 4) { \155if (xxs > 60) \156delta = 60000000; \157else \158delta += xxs * 1000000; \159} else while (xxs > 0) { \160delta += 1000000; \161xxs--; \162} \163} \164}165166/* default rio parameter values */167static struct redparams default_rio_params[RIO_NDROPPREC] = {168/* th_min, th_max, inv_pmax */169{ TH_MAX * 2 + TH_MIN, TH_MAX * 3, INV_P_MAX }, /* low drop precedence */170{ TH_MAX + TH_MIN, TH_MAX * 2, INV_P_MAX }, /* medium drop precedence */171{ TH_MIN, TH_MAX, INV_P_MAX } /* high drop precedence */172};173174/* internal function prototypes */175static int dscp2index(u_int8_t);176177rio_t *178rio_alloc(int weight, struct redparams *params, int flags, int pkttime)179{180rio_t *rp;181int w, i;182int npkts_per_sec;183184rp = malloc(sizeof(rio_t), M_DEVBUF, M_NOWAIT | M_ZERO);185if (rp == NULL)186return (NULL);187188rp->rio_flags = flags;189if (pkttime == 0)190/* default packet time: 1000 bytes / 10Mbps * 8 * 1000000 */191rp->rio_pkttime = 800;192else193rp->rio_pkttime = pkttime;194195if (weight != 0)196rp->rio_weight = weight;197else {198/* use default */199rp->rio_weight = W_WEIGHT;200201/* when the link is very slow, adjust red parameters */202npkts_per_sec = 1000000 / rp->rio_pkttime;203if (npkts_per_sec < 50) {204/* up to about 400Kbps */205rp->rio_weight = W_WEIGHT_2;206} else if (npkts_per_sec < 300) {207/* up to about 2.4Mbps */208rp->rio_weight = W_WEIGHT_1;209}210}211212/* calculate wshift. weight must be power of 2 */213w = rp->rio_weight;214for (i = 0; w > 1; i++)215w = w >> 1;216rp->rio_wshift = i;217w = 1 << rp->rio_wshift;218if (w != rp->rio_weight) {219printf("invalid weight value %d for red! use %d\n",220rp->rio_weight, w);221rp->rio_weight = w;222}223224/* allocate weight table */225rp->rio_wtab = wtab_alloc(rp->rio_weight);226227for (i = 0; i < RIO_NDROPPREC; i++) {228struct dropprec_state *prec = &rp->rio_precstate[i];229230prec->avg = 0;231prec->idle = 1;232233if (params == NULL || params[i].inv_pmax == 0)234prec->inv_pmax = default_rio_params[i].inv_pmax;235else236prec->inv_pmax = params[i].inv_pmax;237if (params == NULL || params[i].th_min == 0)238prec->th_min = default_rio_params[i].th_min;239else240prec->th_min = params[i].th_min;241if (params == NULL || params[i].th_max == 0)242prec->th_max = default_rio_params[i].th_max;243else244prec->th_max = params[i].th_max;245246/*247* th_min_s and th_max_s are scaled versions of th_min248* and th_max to be compared with avg.249*/250prec->th_min_s = prec->th_min << (rp->rio_wshift + FP_SHIFT);251prec->th_max_s = prec->th_max << (rp->rio_wshift + FP_SHIFT);252253/*254* precompute probability denominator255* probd = (2 * (TH_MAX-TH_MIN) / pmax) in fixed-point256*/257prec->probd = (2 * (prec->th_max - prec->th_min)258* prec->inv_pmax) << FP_SHIFT;259260microtime(&prec->last);261}262263return (rp);264}265266void267rio_destroy(rio_t *rp)268{269wtab_destroy(rp->rio_wtab);270free(rp, M_DEVBUF);271}272273void274rio_getstats(rio_t *rp, struct redstats *sp)275{276int i;277278for (i = 0; i < RIO_NDROPPREC; i++) {279bcopy(&rp->q_stats[i], sp, sizeof(struct redstats));280sp->q_avg = rp->rio_precstate[i].avg >> rp->rio_wshift;281sp++;282}283}284285#if (RIO_NDROPPREC == 3)286/*287* internally, a drop precedence value is converted to an index288* starting from 0.289*/290static int291dscp2index(u_int8_t dscp)292{293int dpindex = dscp & AF_DROPPRECMASK;294295if (dpindex == 0)296return (0);297return ((dpindex >> 3) - 1);298}299#endif300301#if 1302/*303* kludge: when a packet is dequeued, we need to know its drop precedence304* in order to keep the queue length of each drop precedence.305* use m_pkthdr.rcvif to pass this info.306*/307#define RIOM_SET_PRECINDEX(m, idx) \308do { (m)->m_pkthdr.rcvif = (void *)((long)(idx)); } while (0)309#define RIOM_GET_PRECINDEX(m) \310({ long idx; idx = (long)((m)->m_pkthdr.rcvif); \311(m)->m_pkthdr.rcvif = NULL; idx; })312#endif313314int315rio_addq(rio_t *rp, class_queue_t *q, struct mbuf *m,316struct altq_pktattr *pktattr)317{318int avg, droptype;319u_int8_t dsfield, odsfield;320int dpindex, i, n, t;321struct timeval now;322struct dropprec_state *prec;323324dsfield = odsfield = read_dsfield(m, pktattr);325dpindex = dscp2index(dsfield);326327/*328* update avg of the precedence states whose drop precedence329* is larger than or equal to the drop precedence of the packet330*/331now.tv_sec = 0;332for (i = dpindex; i < RIO_NDROPPREC; i++) {333prec = &rp->rio_precstate[i];334avg = prec->avg;335if (prec->idle) {336prec->idle = 0;337if (now.tv_sec == 0)338microtime(&now);339t = (now.tv_sec - prec->last.tv_sec);340if (t > 60)341avg = 0;342else {343t = t * 1000000 +344(now.tv_usec - prec->last.tv_usec);345n = t / rp->rio_pkttime;346/* calculate (avg = (1 - Wq)^n * avg) */347if (n > 0)348avg = (avg >> FP_SHIFT) *349pow_w(rp->rio_wtab, n);350}351}352353/* run estimator. (avg is scaled by WEIGHT in fixed-point) */354avg += (prec->qlen << FP_SHIFT) - (avg >> rp->rio_wshift);355prec->avg = avg; /* save the new value */356/*357* count keeps a tally of arriving traffic that has not358* been dropped.359*/360prec->count++;361}362363prec = &rp->rio_precstate[dpindex];364avg = prec->avg;365366/* see if we drop early */367droptype = DTYPE_NODROP;368if (avg >= prec->th_min_s && prec->qlen > 1) {369if (avg >= prec->th_max_s) {370/* avg >= th_max: forced drop */371droptype = DTYPE_FORCED;372} else if (prec->old == 0) {373/* first exceeds th_min */374prec->count = 1;375prec->old = 1;376} else if (drop_early((avg - prec->th_min_s) >> rp->rio_wshift,377prec->probd, prec->count)) {378/* unforced drop by red */379droptype = DTYPE_EARLY;380}381} else {382/* avg < th_min */383prec->old = 0;384}385386/*387* if the queue length hits the hard limit, it's a forced drop.388*/389if (droptype == DTYPE_NODROP && qlen(q) >= qlimit(q))390droptype = DTYPE_FORCED;391392if (droptype != DTYPE_NODROP) {393/* always drop incoming packet (as opposed to randomdrop) */394for (i = dpindex; i < RIO_NDROPPREC; i++)395rp->rio_precstate[i].count = 0;396#ifdef RIO_STATS397if (droptype == DTYPE_EARLY)398rp->q_stats[dpindex].drop_unforced++;399else400rp->q_stats[dpindex].drop_forced++;401PKTCNTR_ADD(&rp->q_stats[dpindex].drop_cnt, m_pktlen(m));402#endif403m_freem(m);404return (-1);405}406407for (i = dpindex; i < RIO_NDROPPREC; i++)408rp->rio_precstate[i].qlen++;409410/* save drop precedence index in mbuf hdr */411RIOM_SET_PRECINDEX(m, dpindex);412413if (rp->rio_flags & RIOF_CLEARDSCP)414dsfield &= ~DSCP_MASK;415416if (dsfield != odsfield)417write_dsfield(m, pktattr, dsfield);418419_addq(q, m);420421#ifdef RIO_STATS422PKTCNTR_ADD(&rp->q_stats[dpindex].xmit_cnt, m_pktlen(m));423#endif424return (0);425}426427struct mbuf *428rio_getq(rio_t *rp, class_queue_t *q)429{430struct mbuf *m;431int dpindex, i;432433if ((m = _getq(q)) == NULL)434return NULL;435436dpindex = RIOM_GET_PRECINDEX(m);437for (i = dpindex; i < RIO_NDROPPREC; i++) {438if (--rp->rio_precstate[i].qlen == 0) {439if (rp->rio_precstate[i].idle == 0) {440rp->rio_precstate[i].idle = 1;441microtime(&rp->rio_precstate[i].last);442}443}444}445return (m);446}447448#endif /* ALTQ_RIO */449450451