Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __NET_SCHED_RED_H
0003 #define __NET_SCHED_RED_H
0004 
0005 #include <linux/types.h>
0006 #include <linux/bug.h>
0007 #include <net/pkt_sched.h>
0008 #include <net/inet_ecn.h>
0009 #include <net/dsfield.h>
0010 #include <linux/reciprocal_div.h>
0011 
0012 /*  Random Early Detection (RED) algorithm.
0013     =======================================
0014 
0015     Source: Sally Floyd and Van Jacobson, "Random Early Detection Gateways
0016     for Congestion Avoidance", 1993, IEEE/ACM Transactions on Networking.
0017 
0018     This file codes a "divisionless" version of RED algorithm
0019     as written down in Fig.17 of the paper.
0020 
0021     Short description.
0022     ------------------
0023 
0024     When a new packet arrives we calculate the average queue length:
0025 
0026     avg = (1-W)*avg + W*current_queue_len,
0027 
0028     W is the filter time constant (chosen as 2^(-Wlog)), it controls
0029     the inertia of the algorithm. To allow larger bursts, W should be
0030     decreased.
0031 
0032     if (avg > th_max) -> packet marked (dropped).
0033     if (avg < th_min) -> packet passes.
0034     if (th_min < avg < th_max) we calculate probability:
0035 
0036     Pb = max_P * (avg - th_min)/(th_max-th_min)
0037 
0038     and mark (drop) packet with this probability.
0039     Pb changes from 0 (at avg==th_min) to max_P (avg==th_max).
0040     max_P should be small (not 1), usually 0.01..0.02 is good value.
0041 
0042     max_P is chosen as a number, so that max_P/(th_max-th_min)
0043     is a negative power of two in order arithmetics to contain
0044     only shifts.
0045 
0046 
0047     Parameters, settable by user:
0048     -----------------------------
0049 
0050     qth_min     - bytes (should be < qth_max/2)
0051     qth_max     - bytes (should be at least 2*qth_min and less limit)
0052     Wlog            - bits (<32) log(1/W).
0053     Plog            - bits (<32)
0054 
0055     Plog is related to max_P by formula:
0056 
0057     max_P = (qth_max-qth_min)/2^Plog;
0058 
0059     F.e. if qth_max=128K and qth_min=32K, then Plog=22
0060     corresponds to max_P=0.02
0061 
0062     Scell_log
0063     Stab
0064 
0065     Lookup table for log((1-W)^(t/t_ave).
0066 
0067 
0068     NOTES:
0069 
0070     Upper bound on W.
0071     -----------------
0072 
0073     If you want to allow bursts of L packets of size S,
0074     you should choose W:
0075 
0076     L + 1 - th_min/S < (1-(1-W)^L)/W
0077 
0078     th_min/S = 32         th_min/S = 4
0079 
0080     log(W)  L
0081     -1  33
0082     -2  35
0083     -3  39
0084     -4  46
0085     -5  57
0086     -6  75
0087     -7  101
0088     -8  135
0089     -9  190
0090     etc.
0091  */
0092 
0093 /*
0094  * Adaptative RED : An Algorithm for Increasing the Robustness of RED's AQM
0095  * (Sally FLoyd, Ramakrishna Gummadi, and Scott Shenker) August 2001
0096  *
0097  * Every 500 ms:
0098  *  if (avg > target and max_p <= 0.5)
0099  *   increase max_p : max_p += alpha;
0100  *  else if (avg < target and max_p >= 0.01)
0101  *   decrease max_p : max_p *= beta;
0102  *
0103  * target :[qth_min + 0.4*(qth_min - qth_max),
0104  *          qth_min + 0.6*(qth_min - qth_max)].
0105  * alpha : min(0.01, max_p / 4)
0106  * beta : 0.9
0107  * max_P is a Q0.32 fixed point number (with 32 bits mantissa)
0108  * max_P between 0.01 and 0.5 (1% - 50%) [ Its no longer a negative power of two ]
0109  */
0110 #define RED_ONE_PERCENT ((u32)DIV_ROUND_CLOSEST(1ULL<<32, 100))
0111 
0112 #define MAX_P_MIN (1 * RED_ONE_PERCENT)
0113 #define MAX_P_MAX (50 * RED_ONE_PERCENT)
0114 #define MAX_P_ALPHA(val) min(MAX_P_MIN, val / 4)
0115 
0116 #define RED_STAB_SIZE   256
0117 #define RED_STAB_MASK   (RED_STAB_SIZE - 1)
0118 
0119 struct red_stats {
0120     u32     prob_drop;  /* Early probability drops */
0121     u32     prob_mark;  /* Early probability marks */
0122     u32     forced_drop;    /* Forced drops, qavg > max_thresh */
0123     u32     forced_mark;    /* Forced marks, qavg > max_thresh */
0124     u32     pdrop;          /* Drops due to queue limits */
0125     u32     other;          /* Drops due to drop() calls */
0126 };
0127 
0128 struct red_parms {
0129     /* Parameters */
0130     u32     qth_min;    /* Min avg length threshold: Wlog scaled */
0131     u32     qth_max;    /* Max avg length threshold: Wlog scaled */
0132     u32     Scell_max;
0133     u32     max_P;      /* probability, [0 .. 1.0] 32 scaled */
0134     /* reciprocal_value(max_P / qth_delta) */
0135     struct reciprocal_value max_P_reciprocal;
0136     u32     qth_delta;  /* max_th - min_th */
0137     u32     target_min; /* min_th + 0.4*(max_th - min_th) */
0138     u32     target_max; /* min_th + 0.6*(max_th - min_th) */
0139     u8      Scell_log;
0140     u8      Wlog;       /* log(W)       */
0141     u8      Plog;       /* random number bits   */
0142     u8      Stab[RED_STAB_SIZE];
0143 };
0144 
0145 struct red_vars {
0146     /* Variables */
0147     int     qcount;     /* Number of packets since last random
0148                        number generation */
0149     u32     qR;     /* Cached random number */
0150 
0151     unsigned long   qavg;       /* Average queue length: Wlog scaled */
0152     ktime_t     qidlestart; /* Start of current idle period */
0153 };
0154 
0155 static inline u32 red_maxp(u8 Plog)
0156 {
0157     return Plog < 32 ? (~0U >> Plog) : ~0U;
0158 }
0159 
0160 static inline void red_set_vars(struct red_vars *v)
0161 {
0162     /* Reset average queue length, the value is strictly bound
0163      * to the parameters below, reseting hurts a bit but leaving
0164      * it might result in an unreasonable qavg for a while. --TGR
0165      */
0166     v->qavg     = 0;
0167 
0168     v->qcount   = -1;
0169 }
0170 
0171 static inline bool red_check_params(u32 qth_min, u32 qth_max, u8 Wlog,
0172                     u8 Scell_log, u8 *stab)
0173 {
0174     if (fls(qth_min) + Wlog >= 32)
0175         return false;
0176     if (fls(qth_max) + Wlog >= 32)
0177         return false;
0178     if (Scell_log >= 32)
0179         return false;
0180     if (qth_max < qth_min)
0181         return false;
0182     if (stab) {
0183         int i;
0184 
0185         for (i = 0; i < RED_STAB_SIZE; i++)
0186             if (stab[i] >= 32)
0187                 return false;
0188     }
0189     return true;
0190 }
0191 
0192 static inline int red_get_flags(unsigned char qopt_flags,
0193                 unsigned char historic_mask,
0194                 struct nlattr *flags_attr,
0195                 unsigned char supported_mask,
0196                 struct nla_bitfield32 *p_flags,
0197                 unsigned char *p_userbits,
0198                 struct netlink_ext_ack *extack)
0199 {
0200     struct nla_bitfield32 flags;
0201 
0202     if (qopt_flags && flags_attr) {
0203         NL_SET_ERR_MSG_MOD(extack, "flags should be passed either through qopt, or through a dedicated attribute");
0204         return -EINVAL;
0205     }
0206 
0207     if (flags_attr) {
0208         flags = nla_get_bitfield32(flags_attr);
0209     } else {
0210         flags.selector = historic_mask;
0211         flags.value = qopt_flags & historic_mask;
0212     }
0213 
0214     *p_flags = flags;
0215     *p_userbits = qopt_flags & ~historic_mask;
0216     return 0;
0217 }
0218 
0219 static inline int red_validate_flags(unsigned char flags,
0220                      struct netlink_ext_ack *extack)
0221 {
0222     if ((flags & TC_RED_NODROP) && !(flags & TC_RED_ECN)) {
0223         NL_SET_ERR_MSG_MOD(extack, "nodrop mode is only meaningful with ECN");
0224         return -EINVAL;
0225     }
0226 
0227     return 0;
0228 }
0229 
0230 static inline void red_set_parms(struct red_parms *p,
0231                  u32 qth_min, u32 qth_max, u8 Wlog, u8 Plog,
0232                  u8 Scell_log, u8 *stab, u32 max_P)
0233 {
0234     int delta = qth_max - qth_min;
0235     u32 max_p_delta;
0236 
0237     p->qth_min  = qth_min << Wlog;
0238     p->qth_max  = qth_max << Wlog;
0239     p->Wlog     = Wlog;
0240     p->Plog     = Plog;
0241     if (delta <= 0)
0242         delta = 1;
0243     p->qth_delta    = delta;
0244     if (!max_P) {
0245         max_P = red_maxp(Plog);
0246         max_P *= delta; /* max_P = (qth_max - qth_min)/2^Plog */
0247     }
0248     p->max_P = max_P;
0249     max_p_delta = max_P / delta;
0250     max_p_delta = max(max_p_delta, 1U);
0251     p->max_P_reciprocal  = reciprocal_value(max_p_delta);
0252 
0253     /* RED Adaptative target :
0254      * [min_th + 0.4*(min_th - max_th),
0255      *  min_th + 0.6*(min_th - max_th)].
0256      */
0257     delta /= 5;
0258     p->target_min = qth_min + 2*delta;
0259     p->target_max = qth_min + 3*delta;
0260 
0261     p->Scell_log    = Scell_log;
0262     p->Scell_max    = (255 << Scell_log);
0263 
0264     if (stab)
0265         memcpy(p->Stab, stab, sizeof(p->Stab));
0266 }
0267 
0268 static inline int red_is_idling(const struct red_vars *v)
0269 {
0270     return v->qidlestart != 0;
0271 }
0272 
0273 static inline void red_start_of_idle_period(struct red_vars *v)
0274 {
0275     v->qidlestart = ktime_get();
0276 }
0277 
0278 static inline void red_end_of_idle_period(struct red_vars *v)
0279 {
0280     v->qidlestart = 0;
0281 }
0282 
0283 static inline void red_restart(struct red_vars *v)
0284 {
0285     red_end_of_idle_period(v);
0286     v->qavg = 0;
0287     v->qcount = -1;
0288 }
0289 
0290 static inline unsigned long red_calc_qavg_from_idle_time(const struct red_parms *p,
0291                              const struct red_vars *v)
0292 {
0293     s64 delta = ktime_us_delta(ktime_get(), v->qidlestart);
0294     long us_idle = min_t(s64, delta, p->Scell_max);
0295     int  shift;
0296 
0297     /*
0298      * The problem: ideally, average length queue recalculation should
0299      * be done over constant clock intervals. This is too expensive, so
0300      * that the calculation is driven by outgoing packets.
0301      * When the queue is idle we have to model this clock by hand.
0302      *
0303      * SF+VJ proposed to "generate":
0304      *
0305      *  m = idletime / (average_pkt_size / bandwidth)
0306      *
0307      * dummy packets as a burst after idle time, i.e.
0308      *
0309      *  v->qavg *= (1-W)^m
0310      *
0311      * This is an apparently overcomplicated solution (f.e. we have to
0312      * precompute a table to make this calculation in reasonable time)
0313      * I believe that a simpler model may be used here,
0314      * but it is field for experiments.
0315      */
0316 
0317     shift = p->Stab[(us_idle >> p->Scell_log) & RED_STAB_MASK];
0318 
0319     if (shift)
0320         return v->qavg >> shift;
0321     else {
0322         /* Approximate initial part of exponent with linear function:
0323          *
0324          *  (1-W)^m ~= 1-mW + ...
0325          *
0326          * Seems, it is the best solution to
0327          * problem of too coarse exponent tabulation.
0328          */
0329         us_idle = (v->qavg * (u64)us_idle) >> p->Scell_log;
0330 
0331         if (us_idle < (v->qavg >> 1))
0332             return v->qavg - us_idle;
0333         else
0334             return v->qavg >> 1;
0335     }
0336 }
0337 
0338 static inline unsigned long red_calc_qavg_no_idle_time(const struct red_parms *p,
0339                                const struct red_vars *v,
0340                                unsigned int backlog)
0341 {
0342     /*
0343      * NOTE: v->qavg is fixed point number with point at Wlog.
0344      * The formula below is equvalent to floating point
0345      * version:
0346      *
0347      *  qavg = qavg*(1-W) + backlog*W;
0348      *
0349      * --ANK (980924)
0350      */
0351     return v->qavg + (backlog - (v->qavg >> p->Wlog));
0352 }
0353 
0354 static inline unsigned long red_calc_qavg(const struct red_parms *p,
0355                       const struct red_vars *v,
0356                       unsigned int backlog)
0357 {
0358     if (!red_is_idling(v))
0359         return red_calc_qavg_no_idle_time(p, v, backlog);
0360     else
0361         return red_calc_qavg_from_idle_time(p, v);
0362 }
0363 
0364 
0365 static inline u32 red_random(const struct red_parms *p)
0366 {
0367     return reciprocal_divide(prandom_u32(), p->max_P_reciprocal);
0368 }
0369 
0370 static inline int red_mark_probability(const struct red_parms *p,
0371                        const struct red_vars *v,
0372                        unsigned long qavg)
0373 {
0374     /* The formula used below causes questions.
0375 
0376        OK. qR is random number in the interval
0377         (0..1/max_P)*(qth_max-qth_min)
0378        i.e. 0..(2^Plog). If we used floating point
0379        arithmetics, it would be: (2^Plog)*rnd_num,
0380        where rnd_num is less 1.
0381 
0382        Taking into account, that qavg have fixed
0383        point at Wlog, two lines
0384        below have the following floating point equivalent:
0385 
0386        max_P*(qavg - qth_min)/(qth_max-qth_min) < rnd/qcount
0387 
0388        Any questions? --ANK (980924)
0389      */
0390     return !(((qavg - p->qth_min) >> p->Wlog) * v->qcount < v->qR);
0391 }
0392 
0393 enum {
0394     RED_BELOW_MIN_THRESH,
0395     RED_BETWEEN_TRESH,
0396     RED_ABOVE_MAX_TRESH,
0397 };
0398 
0399 static inline int red_cmp_thresh(const struct red_parms *p, unsigned long qavg)
0400 {
0401     if (qavg < p->qth_min)
0402         return RED_BELOW_MIN_THRESH;
0403     else if (qavg >= p->qth_max)
0404         return RED_ABOVE_MAX_TRESH;
0405     else
0406         return RED_BETWEEN_TRESH;
0407 }
0408 
0409 enum {
0410     RED_DONT_MARK,
0411     RED_PROB_MARK,
0412     RED_HARD_MARK,
0413 };
0414 
0415 static inline int red_action(const struct red_parms *p,
0416                  struct red_vars *v,
0417                  unsigned long qavg)
0418 {
0419     switch (red_cmp_thresh(p, qavg)) {
0420         case RED_BELOW_MIN_THRESH:
0421             v->qcount = -1;
0422             return RED_DONT_MARK;
0423 
0424         case RED_BETWEEN_TRESH:
0425             if (++v->qcount) {
0426                 if (red_mark_probability(p, v, qavg)) {
0427                     v->qcount = 0;
0428                     v->qR = red_random(p);
0429                     return RED_PROB_MARK;
0430                 }
0431             } else
0432                 v->qR = red_random(p);
0433 
0434             return RED_DONT_MARK;
0435 
0436         case RED_ABOVE_MAX_TRESH:
0437             v->qcount = -1;
0438             return RED_HARD_MARK;
0439     }
0440 
0441     BUG();
0442     return RED_DONT_MARK;
0443 }
0444 
0445 static inline void red_adaptative_algo(struct red_parms *p, struct red_vars *v)
0446 {
0447     unsigned long qavg;
0448     u32 max_p_delta;
0449 
0450     qavg = v->qavg;
0451     if (red_is_idling(v))
0452         qavg = red_calc_qavg_from_idle_time(p, v);
0453 
0454     /* v->qavg is fixed point number with point at Wlog */
0455     qavg >>= p->Wlog;
0456 
0457     if (qavg > p->target_max && p->max_P <= MAX_P_MAX)
0458         p->max_P += MAX_P_ALPHA(p->max_P); /* maxp = maxp + alpha */
0459     else if (qavg < p->target_min && p->max_P >= MAX_P_MIN)
0460         p->max_P = (p->max_P/10)*9; /* maxp = maxp * Beta */
0461 
0462     max_p_delta = DIV_ROUND_CLOSEST(p->max_P, p->qth_delta);
0463     max_p_delta = max(max_p_delta, 1U);
0464     p->max_P_reciprocal = reciprocal_value(max_p_delta);
0465 }
0466 #endif