1#ifndef __NET_SCHED_RED_H
2#define __NET_SCHED_RED_H
3
4#include <linux/types.h>
5#include <linux/bug.h>
6#include <net/pkt_sched.h>
7#include <net/inet_ecn.h>
8#include <net/dsfield.h>
9#include <linux/reciprocal_div.h>
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109#define RED_ONE_PERCENT ((u32)DIV_ROUND_CLOSEST(1ULL<<32, 100))
110
111#define MAX_P_MIN (1 * RED_ONE_PERCENT)
112#define MAX_P_MAX (50 * RED_ONE_PERCENT)
113#define MAX_P_ALPHA(val) min(MAX_P_MIN, val / 4)
114
115#define RED_STAB_SIZE 256
116#define RED_STAB_MASK (RED_STAB_SIZE - 1)
117
118struct red_stats {
119 u32 prob_drop;
120 u32 prob_mark;
121 u32 forced_drop;
122 u32 forced_mark;
123 u32 pdrop;
124 u32 other;
125};
126
127struct red_parms {
128
129 u32 qth_min;
130 u32 qth_max;
131 u32 Scell_max;
132 u32 max_P;
133
134 struct reciprocal_value max_P_reciprocal;
135 u32 qth_delta;
136 u32 target_min;
137 u32 target_max;
138 u8 Scell_log;
139 u8 Wlog;
140 u8 Plog;
141 u8 Stab[RED_STAB_SIZE];
142};
143
144struct red_vars {
145
146 int qcount;
147
148 u32 qR;
149
150 unsigned long qavg;
151 ktime_t qidlestart;
152};
153
154static inline u32 red_maxp(u8 Plog)
155{
156 return Plog < 32 ? (~0U >> Plog) : ~0U;
157}
158
159static inline void red_set_vars(struct red_vars *v)
160{
161
162
163
164
165 v->qavg = 0;
166
167 v->qcount = -1;
168}
169
170static inline void red_set_parms(struct red_parms *p,
171 u32 qth_min, u32 qth_max, u8 Wlog, u8 Plog,
172 u8 Scell_log, u8 *stab, u32 max_P)
173{
174 int delta = qth_max - qth_min;
175 u32 max_p_delta;
176
177 p->qth_min = qth_min << Wlog;
178 p->qth_max = qth_max << Wlog;
179 p->Wlog = Wlog;
180 p->Plog = Plog;
181 if (delta < 0)
182 delta = 1;
183 p->qth_delta = delta;
184 if (!max_P) {
185 max_P = red_maxp(Plog);
186 max_P *= delta;
187 }
188 p->max_P = max_P;
189 max_p_delta = max_P / delta;
190 max_p_delta = max(max_p_delta, 1U);
191 p->max_P_reciprocal = reciprocal_value(max_p_delta);
192
193
194
195
196
197 delta /= 5;
198 p->target_min = qth_min + 2*delta;
199 p->target_max = qth_min + 3*delta;
200
201 p->Scell_log = Scell_log;
202 p->Scell_max = (255 << Scell_log);
203
204 if (stab)
205 memcpy(p->Stab, stab, sizeof(p->Stab));
206}
207
208static inline int red_is_idling(const struct red_vars *v)
209{
210 return v->qidlestart.tv64 != 0;
211}
212
213static inline void red_start_of_idle_period(struct red_vars *v)
214{
215 v->qidlestart = ktime_get();
216}
217
218static inline void red_end_of_idle_period(struct red_vars *v)
219{
220 v->qidlestart.tv64 = 0;
221}
222
223static inline void red_restart(struct red_vars *v)
224{
225 red_end_of_idle_period(v);
226 v->qavg = 0;
227 v->qcount = -1;
228}
229
230static inline unsigned long red_calc_qavg_from_idle_time(const struct red_parms *p,
231 const struct red_vars *v)
232{
233 s64 delta = ktime_us_delta(ktime_get(), v->qidlestart);
234 long us_idle = min_t(s64, delta, p->Scell_max);
235 int shift;
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 shift = p->Stab[(us_idle >> p->Scell_log) & RED_STAB_MASK];
258
259 if (shift)
260 return v->qavg >> shift;
261 else {
262
263
264
265
266
267
268
269 us_idle = (v->qavg * (u64)us_idle) >> p->Scell_log;
270
271 if (us_idle < (v->qavg >> 1))
272 return v->qavg - us_idle;
273 else
274 return v->qavg >> 1;
275 }
276}
277
278static inline unsigned long red_calc_qavg_no_idle_time(const struct red_parms *p,
279 const struct red_vars *v,
280 unsigned int backlog)
281{
282
283
284
285
286
287
288
289
290
291 return v->qavg + (backlog - (v->qavg >> p->Wlog));
292}
293
294static inline unsigned long red_calc_qavg(const struct red_parms *p,
295 const struct red_vars *v,
296 unsigned int backlog)
297{
298 if (!red_is_idling(v))
299 return red_calc_qavg_no_idle_time(p, v, backlog);
300 else
301 return red_calc_qavg_from_idle_time(p, v);
302}
303
304
305static inline u32 red_random(const struct red_parms *p)
306{
307 return reciprocal_divide(prandom_u32(), p->max_P_reciprocal);
308}
309
310static inline int red_mark_probability(const struct red_parms *p,
311 const struct red_vars *v,
312 unsigned long qavg)
313{
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330 return !(((qavg - p->qth_min) >> p->Wlog) * v->qcount < v->qR);
331}
332
333enum {
334 RED_BELOW_MIN_THRESH,
335 RED_BETWEEN_TRESH,
336 RED_ABOVE_MAX_TRESH,
337};
338
339static inline int red_cmp_thresh(const struct red_parms *p, unsigned long qavg)
340{
341 if (qavg < p->qth_min)
342 return RED_BELOW_MIN_THRESH;
343 else if (qavg >= p->qth_max)
344 return RED_ABOVE_MAX_TRESH;
345 else
346 return RED_BETWEEN_TRESH;
347}
348
349enum {
350 RED_DONT_MARK,
351 RED_PROB_MARK,
352 RED_HARD_MARK,
353};
354
355static inline int red_action(const struct red_parms *p,
356 struct red_vars *v,
357 unsigned long qavg)
358{
359 switch (red_cmp_thresh(p, qavg)) {
360 case RED_BELOW_MIN_THRESH:
361 v->qcount = -1;
362 return RED_DONT_MARK;
363
364 case RED_BETWEEN_TRESH:
365 if (++v->qcount) {
366 if (red_mark_probability(p, v, qavg)) {
367 v->qcount = 0;
368 v->qR = red_random(p);
369 return RED_PROB_MARK;
370 }
371 } else
372 v->qR = red_random(p);
373
374 return RED_DONT_MARK;
375
376 case RED_ABOVE_MAX_TRESH:
377 v->qcount = -1;
378 return RED_HARD_MARK;
379 }
380
381 BUG();
382 return RED_DONT_MARK;
383}
384
385static inline void red_adaptative_algo(struct red_parms *p, struct red_vars *v)
386{
387 unsigned long qavg;
388 u32 max_p_delta;
389
390 qavg = v->qavg;
391 if (red_is_idling(v))
392 qavg = red_calc_qavg_from_idle_time(p, v);
393
394
395 qavg >>= p->Wlog;
396
397 if (qavg > p->target_max && p->max_P <= MAX_P_MAX)
398 p->max_P += MAX_P_ALPHA(p->max_P);
399 else if (qavg < p->target_min && p->max_P >= MAX_P_MIN)
400 p->max_P = (p->max_P/10)*9;
401
402 max_p_delta = DIV_ROUND_CLOSEST(p->max_P, p->qth_delta);
403 max_p_delta = max(max_p_delta, 1U);
404 p->max_P_reciprocal = reciprocal_value(max_p_delta);
405}
406#endif
407