Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Eric Dumazet and committed by
David S. Miller
8af2a218 57459185

+111 -19
+4 -2
include/linux/pkt_sched.h
··· 181 181 TCA_RED_UNSPEC, 182 182 TCA_RED_PARMS, 183 183 TCA_RED_STAB, 184 + TCA_RED_MAX_P, 184 185 __TCA_RED_MAX, 185 186 }; 186 187 ··· 195 194 unsigned char Plog; /* log(P_max/(qth_max-qth_min)) */ 196 195 unsigned char Scell_log; /* cell size for idle damping */ 197 196 unsigned char flags; 198 - #define TC_RED_ECN 1 199 - #define TC_RED_HARDDROP 2 197 + #define TC_RED_ECN 1 198 + #define TC_RED_HARDDROP 2 199 + #define TC_RED_ADAPTATIVE 4 200 200 }; 201 201 202 202 struct tc_red_xstats {
+84 -17
include/net/red.h
··· 5 5 #include <net/pkt_sched.h> 6 6 #include <net/inet_ecn.h> 7 7 #include <net/dsfield.h> 8 + #include <linux/reciprocal_div.h> 8 9 9 10 /* Random Early Detection (RED) algorithm. 10 11 ======================================= ··· 88 87 etc. 89 88 */ 90 89 90 + /* 91 + * Adaptative RED : An Algorithm for Increasing the Robustness of RED's AQM 92 + * (Sally FLoyd, Ramakrishna Gummadi, and Scott Shenker) August 2001 93 + * 94 + * Every 500 ms: 95 + * if (avg > target and max_p <= 0.5) 96 + * increase max_p : max_p += alpha; 97 + * else if (avg < target and max_p >= 0.01) 98 + * decrease max_p : max_p *= beta; 99 + * 100 + * target :[qth_min + 0.4*(qth_min - qth_max), 101 + * qth_min + 0.6*(qth_min - qth_max)]. 102 + * alpha : min(0.01, max_p / 4) 103 + * beta : 0.9 104 + * max_P is a Q0.32 fixed point number (with 32 bits mantissa) 105 + * max_P between 0.01 and 0.5 (1% - 50%) [ Its no longer a negative power of two ] 106 + */ 107 + #define RED_ONE_PERCENT ((u32)DIV_ROUND_CLOSEST(1ULL<<32, 100)) 108 + 109 + #define MAX_P_MIN (1 * RED_ONE_PERCENT) 110 + #define MAX_P_MAX (50 * RED_ONE_PERCENT) 111 + #define MAX_P_ALPHA(val) min(MAX_P_MIN, val / 4) 112 + 91 113 #define RED_STAB_SIZE 256 92 114 #define RED_STAB_MASK (RED_STAB_SIZE - 1) 93 115 ··· 125 101 126 102 struct red_parms { 127 103 /* Parameters */ 128 - u32 qth_min; /* Min avg length threshold: A scaled */ 129 - u32 qth_max; /* Max avg length threshold: A scaled */ 104 + u32 qth_min; /* Min avg length threshold: Wlog scaled */ 105 + u32 qth_max; /* Max avg length threshold: Wlog scaled */ 130 106 u32 Scell_max; 131 - u32 Rmask; /* Cached random mask, see red_rmask */ 107 + u32 max_P; /* probability, [0 .. 1.0] 32 scaled */ 108 + u32 max_P_reciprocal; /* reciprocal_value(max_P / qth_delta) */ 109 + u32 qth_delta; /* max_th - min_th */ 110 + u32 target_min; /* min_th + 0.4*(max_th - min_th) */ 111 + u32 target_max; /* min_th + 0.6*(max_th - min_th) */ 132 112 u8 Scell_log; 133 113 u8 Wlog; /* log(W) */ 134 114 u8 Plog; /* random number bits */ ··· 143 115 number generation */ 144 116 u32 qR; /* Cached random number */ 145 117 146 - unsigned long qavg; /* Average queue length: A scaled */ 118 + unsigned long qavg; /* Average queue length: Wlog scaled */ 147 119 ktime_t qidlestart; /* Start of current idle period */ 148 120 }; 149 121 150 - static inline u32 red_rmask(u8 Plog) 122 + static inline u32 red_maxp(u8 Plog) 151 123 { 152 - return Plog < 32 ? ((1 << Plog) - 1) : ~0UL; 124 + return Plog < 32 ? (~0U >> Plog) : ~0U; 153 125 } 126 + 154 127 155 128 static inline void red_set_parms(struct red_parms *p, 156 129 u32 qth_min, u32 qth_max, u8 Wlog, u8 Plog, 157 130 u8 Scell_log, u8 *stab) 158 131 { 132 + int delta = qth_max - qth_min; 133 + 159 134 /* Reset average queue length, the value is strictly bound 160 135 * to the parameters below, reseting hurts a bit but leaving 161 136 * it might result in an unreasonable qavg for a while. --TGR ··· 170 139 p->qth_max = qth_max << Wlog; 171 140 p->Wlog = Wlog; 172 141 p->Plog = Plog; 173 - p->Rmask = red_rmask(Plog); 142 + if (delta < 0) 143 + delta = 1; 144 + p->qth_delta = delta; 145 + p->max_P = red_maxp(Plog); 146 + p->max_P *= delta; /* max_P = (qth_max-qth_min)/2^Plog */ 147 + 148 + p->max_P_reciprocal = reciprocal_value(p->max_P / delta); 149 + 150 + /* RED Adaptative target : 151 + * [min_th + 0.4*(min_th - max_th), 152 + * min_th + 0.6*(min_th - max_th)]. 153 + */ 154 + delta /= 5; 155 + p->target_min = qth_min + 2*delta; 156 + p->target_max = qth_min + 3*delta; 157 + 174 158 p->Scell_log = Scell_log; 175 159 p->Scell_max = (255 << Scell_log); 176 160 177 161 memcpy(p->Stab, stab, sizeof(p->Stab)); 178 162 } 179 163 180 - static inline int red_is_idling(struct red_parms *p) 164 + static inline int red_is_idling(const struct red_parms *p) 181 165 { 182 166 return p->qidlestart.tv64 != 0; 183 167 } ··· 214 168 p->qcount = -1; 215 169 } 216 170 217 - static inline unsigned long red_calc_qavg_from_idle_time(struct red_parms *p) 171 + static inline unsigned long red_calc_qavg_from_idle_time(const struct red_parms *p) 218 172 { 219 173 s64 delta = ktime_us_delta(ktime_get(), p->qidlestart); 220 174 long us_idle = min_t(s64, delta, p->Scell_max); ··· 261 215 } 262 216 } 263 217 264 - static inline unsigned long red_calc_qavg_no_idle_time(struct red_parms *p, 218 + static inline unsigned long red_calc_qavg_no_idle_time(const struct red_parms *p, 265 219 unsigned int backlog) 266 220 { 267 221 /* ··· 276 230 return p->qavg + (backlog - (p->qavg >> p->Wlog)); 277 231 } 278 232 279 - static inline unsigned long red_calc_qavg(struct red_parms *p, 233 + static inline unsigned long red_calc_qavg(const struct red_parms *p, 280 234 unsigned int backlog) 281 235 { 282 236 if (!red_is_idling(p)) ··· 285 239 return red_calc_qavg_from_idle_time(p); 286 240 } 287 241 288 - static inline u32 red_random(struct red_parms *p) 242 + 243 + static inline u32 red_random(const struct red_parms *p) 289 244 { 290 - return net_random() & p->Rmask; 245 + return reciprocal_divide(net_random(), p->max_P_reciprocal); 291 246 } 292 247 293 - static inline int red_mark_probability(struct red_parms *p, unsigned long qavg) 248 + static inline int red_mark_probability(const struct red_parms *p, unsigned long qavg) 294 249 { 295 250 /* The formula used below causes questions. 296 251 297 - OK. qR is random number in the interval 0..Rmask 252 + OK. qR is random number in the interval 253 + (0..1/max_P)*(qth_max-qth_min) 298 254 i.e. 0..(2^Plog). If we used floating point 299 255 arithmetics, it would be: (2^Plog)*rnd_num, 300 256 where rnd_num is less 1. 301 257 302 258 Taking into account, that qavg have fixed 303 - point at Wlog, and Plog is related to max_P by 304 - max_P = (qth_max-qth_min)/2^Plog; two lines 259 + point at Wlog, two lines 305 260 below have the following floating point equivalent: 306 261 307 262 max_P*(qavg - qth_min)/(qth_max-qth_min) < rnd/qcount ··· 362 315 return RED_DONT_MARK; 363 316 } 364 317 318 + static inline void red_adaptative_algo(struct red_parms *p) 319 + { 320 + unsigned long qavg; 321 + u32 max_p_delta; 322 + 323 + qavg = p->qavg; 324 + if (red_is_idling(p)) 325 + qavg = red_calc_qavg_from_idle_time(p); 326 + 327 + /* p->qavg is fixed point number with point at Wlog */ 328 + qavg >>= p->Wlog; 329 + 330 + if (qavg > p->target_max && p->max_P <= MAX_P_MAX) 331 + p->max_P += MAX_P_ALPHA(p->max_P); /* maxp = maxp + alpha */ 332 + else if (qavg < p->target_min && p->max_P >= MAX_P_MIN) 333 + p->max_P = (p->max_P/10)*9; /* maxp = maxp * Beta */ 334 + 335 + max_p_delta = DIV_ROUND_CLOSEST(p->max_P, p->qth_delta); 336 + p->max_P_reciprocal = reciprocal_value(max_p_delta); 337 + } 365 338 #endif
+2
lib/reciprocal_div.c
··· 1 1 #include <asm/div64.h> 2 2 #include <linux/reciprocal_div.h> 3 + #include <linux/export.h> 3 4 4 5 u32 reciprocal_value(u32 k) 5 6 { ··· 8 7 do_div(val, k); 9 8 return (u32)val; 10 9 } 10 + EXPORT_SYMBOL(reciprocal_value);
+21
net/sched/sch_red.c
··· 39 39 struct red_sched_data { 40 40 u32 limit; /* HARD maximal queue length */ 41 41 unsigned char flags; 42 + struct timer_list adapt_timer; 42 43 struct red_parms parms; 43 44 struct red_stats stats; 44 45 struct Qdisc *qdisc; ··· 162 161 static void red_destroy(struct Qdisc *sch) 163 162 { 164 163 struct red_sched_data *q = qdisc_priv(sch); 164 + 165 + del_timer_sync(&q->adapt_timer); 165 166 qdisc_destroy(q->qdisc); 166 167 } 167 168 ··· 212 209 ctl->Plog, ctl->Scell_log, 213 210 nla_data(tb[TCA_RED_STAB])); 214 211 212 + del_timer(&q->adapt_timer); 213 + if (ctl->flags & TC_RED_ADAPTATIVE) 214 + mod_timer(&q->adapt_timer, jiffies + HZ/2); 215 + 215 216 if (!q->qdisc->q.qlen) 216 217 red_start_of_idle_period(&q->parms); 217 218 ··· 223 216 return 0; 224 217 } 225 218 219 + static inline void red_adaptative_timer(unsigned long arg) 220 + { 221 + struct Qdisc *sch = (struct Qdisc *)arg; 222 + struct red_sched_data *q = qdisc_priv(sch); 223 + spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch)); 224 + 225 + spin_lock(root_lock); 226 + red_adaptative_algo(&q->parms); 227 + mod_timer(&q->adapt_timer, jiffies + HZ/2); 228 + spin_unlock(root_lock); 229 + } 230 + 226 231 static int red_init(struct Qdisc *sch, struct nlattr *opt) 227 232 { 228 233 struct red_sched_data *q = qdisc_priv(sch); 229 234 230 235 q->qdisc = &noop_qdisc; 236 + setup_timer(&q->adapt_timer, red_adaptative_timer, (unsigned long)sch); 231 237 return red_change(sch, opt); 232 238 } 233 239 ··· 263 243 if (opts == NULL) 264 244 goto nla_put_failure; 265 245 NLA_PUT(skb, TCA_RED_PARMS, sizeof(opt), &opt); 246 + NLA_PUT_U32(skb, TCA_RED_MAX_P, q->parms.max_P); 266 247 return nla_nest_end(skb, opts); 267 248 268 249 nla_put_failure: