1 #ifndef __NET_SCHED_RED_H
2 #define __NET_SCHED_RED_H
4 #include <linux/config.h>
5 #include <linux/types.h>
6 #include <net/pkt_sched.h>
7 #include <net/inet_ecn.h>
8 #include <net/dsfield.h>
10 /* Random Early Detection (RED) algorithm.
11 =======================================
13 Source: Sally Floyd and Van Jacobson, "Random Early Detection Gateways
14 for Congestion Avoidance", 1993, IEEE/ACM Transactions on Networking.
16 This file codes a "divisionless" version of RED algorithm
17 as written down in Fig.17 of the paper.
22 When a new packet arrives we calculate the average queue length:
24 avg = (1-W)*avg + W*current_queue_len,
26 W is the filter time constant (chosen as 2^(-Wlog)), it controls
27 the inertia of the algorithm. To allow larger bursts, W should be
30 if (avg > th_max) -> packet marked (dropped).
31 if (avg < th_min) -> packet passes.
32 if (th_min < avg < th_max) we calculate probability:
34 Pb = max_P * (avg - th_min)/(th_max-th_min)
36 and mark (drop) packet with this probability.
37 Pb changes from 0 (at avg==th_min) to max_P (avg==th_max).
38 max_P should be small (not 1), usually 0.01..0.02 is good value.
40 max_P is chosen as a number, so that max_P/(th_max-th_min)
41 is a negative power of two in order arithmetics to contain
45 Parameters, settable by user:
46 -----------------------------
48 qth_min - bytes (should be < qth_max/2)
49 qth_max - bytes (should be at least 2*qth_min and less limit)
50 Wlog - bits (<32) log(1/W).
53 Plog is related to max_P by formula:
55 max_P = (qth_max-qth_min)/2^Plog;
57 F.e. if qth_max=128K and qth_min=32K, then Plog=22
58 corresponds to max_P=0.02
63 Lookup table for log((1-W)^(t/t_ave).
71 If you want to allow bursts of L packets of size S,
74 L + 1 - th_min/S < (1-(1-W)^L)/W
76 th_min/S = 32 th_min/S = 4
91 #define RED_STAB_SIZE 256
92 #define RED_STAB_MASK (RED_STAB_SIZE - 1)
96 u32 prob_drop; /* Early probability drops */
97 u32 prob_mark; /* Early probability marks */
98 u32 forced_drop; /* Forced drops, qavg > max_thresh */
99 u32 forced_mark; /* Forced marks, qavg > max_thresh */
100 u32 pdrop; /* Drops due to queue limits */
101 u32 other; /* Drops due to drop() calls */
108 u32 qth_min; /* Min avg length threshold: A scaled */
109 u32 qth_max; /* Max avg length threshold: A scaled */
111 u32 Rmask; /* Cached random mask, see red_rmask */
113 u8 Wlog; /* log(W) */
114 u8 Plog; /* random number bits */
115 u8 Stab[RED_STAB_SIZE];
118 int qcount; /* Number of packets since last random
120 u32 qR; /* Cached random number */
122 unsigned long qavg; /* Average queue length: A scaled */
123 psched_time_t qidlestart; /* Start of current idle period */
126 static inline u32 red_rmask(u8 Plog)
128 return Plog < 32 ? ((1 << Plog) - 1) : ~0UL;
131 static inline void red_set_parms(struct red_parms *p,
132 u32 qth_min, u32 qth_max, u8 Wlog, u8 Plog,
133 u8 Scell_log, u8 *stab)
135 /* Reset average queue length, the value is strictly bound
136 * to the parameters below, reseting hurts a bit but leaving
137 * it might result in an unreasonable qavg for a while. --TGR
142 p->qth_min = qth_min << Wlog;
143 p->qth_max = qth_max << Wlog;
146 p->Rmask = red_rmask(Plog);
147 p->Scell_log = Scell_log;
148 p->Scell_max = (255 << Scell_log);
150 memcpy(p->Stab, stab, sizeof(p->Stab));
153 static inline int red_is_idling(struct red_parms *p)
155 return !PSCHED_IS_PASTPERFECT(p->qidlestart);
158 static inline void red_start_of_idle_period(struct red_parms *p)
160 PSCHED_GET_TIME(p->qidlestart);
163 static inline void red_end_of_idle_period(struct red_parms *p)
165 PSCHED_SET_PASTPERFECT(p->qidlestart);
168 static inline void red_restart(struct red_parms *p)
170 red_end_of_idle_period(p);
175 static inline unsigned long red_calc_qavg_from_idle_time(struct red_parms *p)
181 PSCHED_GET_TIME(now);
182 us_idle = PSCHED_TDIFF_SAFE(now, p->qidlestart, p->Scell_max);
185 * The problem: ideally, average length queue recalcultion should
186 * be done over constant clock intervals. This is too expensive, so
187 * that the calculation is driven by outgoing packets.
188 * When the queue is idle we have to model this clock by hand.
190 * SF+VJ proposed to "generate":
192 * m = idletime / (average_pkt_size / bandwidth)
194 * dummy packets as a burst after idle time, i.e.
198 * This is an apparently overcomplicated solution (f.e. we have to
199 * precompute a table to make this calculation in reasonable time)
200 * I believe that a simpler model may be used here,
201 * but it is field for experiments.
204 shift = p->Stab[(us_idle >> p->Scell_log) & RED_STAB_MASK];
207 return p->qavg >> shift;
209 /* Approximate initial part of exponent with linear function:
211 * (1-W)^m ~= 1-mW + ...
213 * Seems, it is the best solution to
214 * problem of too coarse exponent tabulation.
216 us_idle = (p->qavg * us_idle) >> p->Scell_log;
218 if (us_idle < (p->qavg >> 1))
219 return p->qavg - us_idle;
225 static inline unsigned long red_calc_qavg_no_idle_time(struct red_parms *p,
226 unsigned int backlog)
229 * NOTE: p->qavg is fixed point number with point at Wlog.
230 * The formula below is equvalent to floating point
233 * qavg = qavg*(1-W) + backlog*W;
237 return p->qavg + (backlog - (p->qavg >> p->Wlog));
240 static inline unsigned long red_calc_qavg(struct red_parms *p,
241 unsigned int backlog)
243 if (!red_is_idling(p))
244 return red_calc_qavg_no_idle_time(p, backlog);
246 return red_calc_qavg_from_idle_time(p);
249 static inline u32 red_random(struct red_parms *p)
251 return net_random() & p->Rmask;
254 static inline int red_mark_probability(struct red_parms *p, unsigned long qavg)
256 /* The formula used below causes questions.
258 OK. qR is random number in the interval 0..Rmask
259 i.e. 0..(2^Plog). If we used floating point
260 arithmetics, it would be: (2^Plog)*rnd_num,
261 where rnd_num is less 1.
263 Taking into account, that qavg have fixed
264 point at Wlog, and Plog is related to max_P by
265 max_P = (qth_max-qth_min)/2^Plog; two lines
266 below have the following floating point equivalent:
268 max_P*(qavg - qth_min)/(qth_max-qth_min) < rnd/qcount
270 Any questions? --ANK (980924)
272 return !(((qavg - p->qth_min) >> p->Wlog) * p->qcount < p->qR);
276 RED_BELOW_MIN_THRESH,
281 static inline int red_cmp_thresh(struct red_parms *p, unsigned long qavg)
283 if (qavg < p->qth_min)
284 return RED_BELOW_MIN_THRESH;
285 else if (qavg >= p->qth_max)
286 return RED_ABOVE_MAX_TRESH;
288 return RED_BETWEEN_TRESH;
297 static inline int red_action(struct red_parms *p, unsigned long qavg)
299 switch (red_cmp_thresh(p, qavg)) {
300 case RED_BELOW_MIN_THRESH:
302 return RED_DONT_MARK;
304 case RED_BETWEEN_TRESH:
306 if (red_mark_probability(p, qavg)) {
308 p->qR = red_random(p);
309 return RED_PROB_MARK;
312 p->qR = red_random(p);
314 return RED_DONT_MARK;
316 case RED_ABOVE_MAX_TRESH:
318 return RED_HARD_MARK;
322 return RED_DONT_MARK;