Skip to content

Commit 8af2a21

Browse files
Eric Dumazetdavem330
authored andcommitted
sch_red: Adaptative RED AQM
Adaptative RED AQM for linux, based on paper from Sally FLoyd, Ramakrishna Gummadi, and Scott Shenker, August 2001 : http://icir.org/floyd/papers/adaptiveRed.pdf Goal of Adaptative RED is to make max_p a dynamic value between 1% and 50% to reach the target average queue : (max_th - min_th) / 2 Every 500 ms: if (avg > target and max_p <= 0.5) increase max_p : max_p += alpha; else if (avg < target and max_p >= 0.01) decrease max_p : max_p *= beta; target :[min_th + 0.4*(min_th - max_th), min_th + 0.6*(min_th - max_th)]. alpha : min(0.01, max_p / 4) beta : 0.9 max_P is a Q0.32 fixed point number (unsigned, with 32 bits mantissa) Changes against our RED implementation are : max_p is no longer a negative power of two (1/(2^Plog)), but a Q0.32 fixed point number, to allow full range described in Adatative paper. To deliver a random number, we now use a reciprocal divide (thats really a multiply), but this operation is done once per marked/droped packet when in RED_BETWEEN_TRESH window, so added cost (compared to previous AND operation) is near zero. dump operation gives current max_p value in a new TCA_RED_MAX_P attribute. Example on a 10Mbit link : tc qdisc add dev $DEV parent 1:1 handle 10: est 1sec 8sec red \ limit 400000 min 30000 max 90000 avpkt 1000 \ burst 55 ecn adaptative bandwidth 10Mbit # tc -s -d qdisc show dev eth3 ... qdisc red 10: parent 1:1 limit 400000b min 30000b max 90000b ecn adaptative ewma 5 max_p=0.113335 Scell_log 15 Sent 50414282 bytes 34504 pkt (dropped 35, overlimits 1392 requeues 0) rate 9749Kbit 831pps backlog 72056b 16p requeues 0 marked 1357 early 35 pdrop 0 other 0 Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 5745918 commit 8af2a21

File tree

4 files changed

+111
-19
lines changed

4 files changed

+111
-19
lines changed

include/linux/pkt_sched.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ enum {
181181
TCA_RED_UNSPEC,
182182
TCA_RED_PARMS,
183183
TCA_RED_STAB,
184+
TCA_RED_MAX_P,
184185
__TCA_RED_MAX,
185186
};
186187

@@ -194,8 +195,9 @@ struct tc_red_qopt {
194195
unsigned char Plog; /* log(P_max/(qth_max-qth_min)) */
195196
unsigned char Scell_log; /* cell size for idle damping */
196197
unsigned char flags;
197-
#define TC_RED_ECN 1
198-
#define TC_RED_HARDDROP 2
198+
#define TC_RED_ECN 1
199+
#define TC_RED_HARDDROP 2
200+
#define TC_RED_ADAPTATIVE 4
199201
};
200202

201203
struct tc_red_xstats {

include/net/red.h

Lines changed: 84 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <net/pkt_sched.h>
66
#include <net/inet_ecn.h>
77
#include <net/dsfield.h>
8+
#include <linux/reciprocal_div.h>
89

910
/* Random Early Detection (RED) algorithm.
1011
=======================================
@@ -87,6 +88,29 @@
8788
etc.
8889
*/
8990

91+
/*
92+
* Adaptative RED : An Algorithm for Increasing the Robustness of RED's AQM
93+
* (Sally FLoyd, Ramakrishna Gummadi, and Scott Shenker) August 2001
94+
*
95+
* Every 500 ms:
96+
* if (avg > target and max_p <= 0.5)
97+
* increase max_p : max_p += alpha;
98+
* else if (avg < target and max_p >= 0.01)
99+
* decrease max_p : max_p *= beta;
100+
*
101+
* target :[qth_min + 0.4*(qth_min - qth_max),
102+
* qth_min + 0.6*(qth_min - qth_max)].
103+
* alpha : min(0.01, max_p / 4)
104+
* beta : 0.9
105+
* max_P is a Q0.32 fixed point number (with 32 bits mantissa)
106+
* max_P between 0.01 and 0.5 (1% - 50%) [ Its no longer a negative power of two ]
107+
*/
108+
#define RED_ONE_PERCENT ((u32)DIV_ROUND_CLOSEST(1ULL<<32, 100))
109+
110+
#define MAX_P_MIN (1 * RED_ONE_PERCENT)
111+
#define MAX_P_MAX (50 * RED_ONE_PERCENT)
112+
#define MAX_P_ALPHA(val) min(MAX_P_MIN, val / 4)
113+
90114
#define RED_STAB_SIZE 256
91115
#define RED_STAB_MASK (RED_STAB_SIZE - 1)
92116

@@ -101,10 +125,14 @@ struct red_stats {
101125

102126
struct red_parms {
103127
/* Parameters */
104-
u32 qth_min; /* Min avg length threshold: A scaled */
105-
u32 qth_max; /* Max avg length threshold: A scaled */
128+
u32 qth_min; /* Min avg length threshold: Wlog scaled */
129+
u32 qth_max; /* Max avg length threshold: Wlog scaled */
106130
u32 Scell_max;
107-
u32 Rmask; /* Cached random mask, see red_rmask */
131+
u32 max_P; /* probability, [0 .. 1.0] 32 scaled */
132+
u32 max_P_reciprocal; /* reciprocal_value(max_P / qth_delta) */
133+
u32 qth_delta; /* max_th - min_th */
134+
u32 target_min; /* min_th + 0.4*(max_th - min_th) */
135+
u32 target_max; /* min_th + 0.6*(max_th - min_th) */
108136
u8 Scell_log;
109137
u8 Wlog; /* log(W) */
110138
u8 Plog; /* random number bits */
@@ -115,19 +143,22 @@ struct red_parms {
115143
number generation */
116144
u32 qR; /* Cached random number */
117145

118-
unsigned long qavg; /* Average queue length: A scaled */
146+
unsigned long qavg; /* Average queue length: Wlog scaled */
119147
ktime_t qidlestart; /* Start of current idle period */
120148
};
121149

122-
static inline u32 red_rmask(u8 Plog)
150+
static inline u32 red_maxp(u8 Plog)
123151
{
124-
return Plog < 32 ? ((1 << Plog) - 1) : ~0UL;
152+
return Plog < 32 ? (~0U >> Plog) : ~0U;
125153
}
126154

155+
127156
static inline void red_set_parms(struct red_parms *p,
128157
u32 qth_min, u32 qth_max, u8 Wlog, u8 Plog,
129158
u8 Scell_log, u8 *stab)
130159
{
160+
int delta = qth_max - qth_min;
161+
131162
/* Reset average queue length, the value is strictly bound
132163
* to the parameters below, reseting hurts a bit but leaving
133164
* it might result in an unreasonable qavg for a while. --TGR
@@ -139,14 +170,29 @@ static inline void red_set_parms(struct red_parms *p,
139170
p->qth_max = qth_max << Wlog;
140171
p->Wlog = Wlog;
141172
p->Plog = Plog;
142-
p->Rmask = red_rmask(Plog);
173+
if (delta < 0)
174+
delta = 1;
175+
p->qth_delta = delta;
176+
p->max_P = red_maxp(Plog);
177+
p->max_P *= delta; /* max_P = (qth_max-qth_min)/2^Plog */
178+
179+
p->max_P_reciprocal = reciprocal_value(p->max_P / delta);
180+
181+
/* RED Adaptative target :
182+
* [min_th + 0.4*(min_th - max_th),
183+
* min_th + 0.6*(min_th - max_th)].
184+
*/
185+
delta /= 5;
186+
p->target_min = qth_min + 2*delta;
187+
p->target_max = qth_min + 3*delta;
188+
143189
p->Scell_log = Scell_log;
144190
p->Scell_max = (255 << Scell_log);
145191

146192
memcpy(p->Stab, stab, sizeof(p->Stab));
147193
}
148194

149-
static inline int red_is_idling(struct red_parms *p)
195+
static inline int red_is_idling(const struct red_parms *p)
150196
{
151197
return p->qidlestart.tv64 != 0;
152198
}
@@ -168,7 +214,7 @@ static inline void red_restart(struct red_parms *p)
168214
p->qcount = -1;
169215
}
170216

171-
static inline unsigned long red_calc_qavg_from_idle_time(struct red_parms *p)
217+
static inline unsigned long red_calc_qavg_from_idle_time(const struct red_parms *p)
172218
{
173219
s64 delta = ktime_us_delta(ktime_get(), p->qidlestart);
174220
long us_idle = min_t(s64, delta, p->Scell_max);
@@ -215,7 +261,7 @@ static inline unsigned long red_calc_qavg_from_idle_time(struct red_parms *p)
215261
}
216262
}
217263

218-
static inline unsigned long red_calc_qavg_no_idle_time(struct red_parms *p,
264+
static inline unsigned long red_calc_qavg_no_idle_time(const struct red_parms *p,
219265
unsigned int backlog)
220266
{
221267
/*
@@ -230,7 +276,7 @@ static inline unsigned long red_calc_qavg_no_idle_time(struct red_parms *p,
230276
return p->qavg + (backlog - (p->qavg >> p->Wlog));
231277
}
232278

233-
static inline unsigned long red_calc_qavg(struct red_parms *p,
279+
static inline unsigned long red_calc_qavg(const struct red_parms *p,
234280
unsigned int backlog)
235281
{
236282
if (!red_is_idling(p))
@@ -239,23 +285,24 @@ static inline unsigned long red_calc_qavg(struct red_parms *p,
239285
return red_calc_qavg_from_idle_time(p);
240286
}
241287

242-
static inline u32 red_random(struct red_parms *p)
288+
289+
static inline u32 red_random(const struct red_parms *p)
243290
{
244-
return net_random() & p->Rmask;
291+
return reciprocal_divide(net_random(), p->max_P_reciprocal);
245292
}
246293

247-
static inline int red_mark_probability(struct red_parms *p, unsigned long qavg)
294+
static inline int red_mark_probability(const struct red_parms *p, unsigned long qavg)
248295
{
249296
/* The formula used below causes questions.
250297
251-
OK. qR is random number in the interval 0..Rmask
298+
OK. qR is random number in the interval
299+
(0..1/max_P)*(qth_max-qth_min)
252300
i.e. 0..(2^Plog). If we used floating point
253301
arithmetics, it would be: (2^Plog)*rnd_num,
254302
where rnd_num is less 1.
255303
256304
Taking into account, that qavg have fixed
257-
point at Wlog, and Plog is related to max_P by
258-
max_P = (qth_max-qth_min)/2^Plog; two lines
305+
point at Wlog, two lines
259306
below have the following floating point equivalent:
260307
261308
max_P*(qavg - qth_min)/(qth_max-qth_min) < rnd/qcount
@@ -315,4 +362,24 @@ static inline int red_action(struct red_parms *p, unsigned long qavg)
315362
return RED_DONT_MARK;
316363
}
317364

365+
static inline void red_adaptative_algo(struct red_parms *p)
366+
{
367+
unsigned long qavg;
368+
u32 max_p_delta;
369+
370+
qavg = p->qavg;
371+
if (red_is_idling(p))
372+
qavg = red_calc_qavg_from_idle_time(p);
373+
374+
/* p->qavg is fixed point number with point at Wlog */
375+
qavg >>= p->Wlog;
376+
377+
if (qavg > p->target_max && p->max_P <= MAX_P_MAX)
378+
p->max_P += MAX_P_ALPHA(p->max_P); /* maxp = maxp + alpha */
379+
else if (qavg < p->target_min && p->max_P >= MAX_P_MIN)
380+
p->max_P = (p->max_P/10)*9; /* maxp = maxp * Beta */
381+
382+
max_p_delta = DIV_ROUND_CLOSEST(p->max_P, p->qth_delta);
383+
p->max_P_reciprocal = reciprocal_value(max_p_delta);
384+
}
318385
#endif

lib/reciprocal_div.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include <asm/div64.h>
22
#include <linux/reciprocal_div.h>
3+
#include <linux/export.h>
34

45
u32 reciprocal_value(u32 k)
56
{
67
u64 val = (1LL << 32) + (k - 1);
78
do_div(val, k);
89
return (u32)val;
910
}
11+
EXPORT_SYMBOL(reciprocal_value);

net/sched/sch_red.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
struct red_sched_data {
4040
u32 limit; /* HARD maximal queue length */
4141
unsigned char flags;
42+
struct timer_list adapt_timer;
4243
struct red_parms parms;
4344
struct red_stats stats;
4445
struct Qdisc *qdisc;
@@ -161,6 +162,8 @@ static void red_reset(struct Qdisc *sch)
161162
static void red_destroy(struct Qdisc *sch)
162163
{
163164
struct red_sched_data *q = qdisc_priv(sch);
165+
166+
del_timer_sync(&q->adapt_timer);
164167
qdisc_destroy(q->qdisc);
165168
}
166169

@@ -209,18 +212,35 @@ static int red_change(struct Qdisc *sch, struct nlattr *opt)
209212
ctl->Plog, ctl->Scell_log,
210213
nla_data(tb[TCA_RED_STAB]));
211214

215+
del_timer(&q->adapt_timer);
216+
if (ctl->flags & TC_RED_ADAPTATIVE)
217+
mod_timer(&q->adapt_timer, jiffies + HZ/2);
218+
212219
if (!q->qdisc->q.qlen)
213220
red_start_of_idle_period(&q->parms);
214221

215222
sch_tree_unlock(sch);
216223
return 0;
217224
}
218225

226+
static inline void red_adaptative_timer(unsigned long arg)
227+
{
228+
struct Qdisc *sch = (struct Qdisc *)arg;
229+
struct red_sched_data *q = qdisc_priv(sch);
230+
spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch));
231+
232+
spin_lock(root_lock);
233+
red_adaptative_algo(&q->parms);
234+
mod_timer(&q->adapt_timer, jiffies + HZ/2);
235+
spin_unlock(root_lock);
236+
}
237+
219238
static int red_init(struct Qdisc *sch, struct nlattr *opt)
220239
{
221240
struct red_sched_data *q = qdisc_priv(sch);
222241

223242
q->qdisc = &noop_qdisc;
243+
setup_timer(&q->adapt_timer, red_adaptative_timer, (unsigned long)sch);
224244
return red_change(sch, opt);
225245
}
226246

@@ -243,6 +263,7 @@ static int red_dump(struct Qdisc *sch, struct sk_buff *skb)
243263
if (opts == NULL)
244264
goto nla_put_failure;
245265
NLA_PUT(skb, TCA_RED_PARMS, sizeof(opt), &opt);
266+
NLA_PUT_U32(skb, TCA_RED_MAX_P, q->parms.max_P);
246267
return nla_nest_end(skb, opts);
247268

248269
nla_put_failure:

0 commit comments

Comments
 (0)