1#ifndef __NET_SCHED_RED_H
2#define __NET_SCHED_RED_H
3
4#include <linux/types.h>
5#include <net/pkt_sched.h>
6#include <net/inet_ecn.h>
7#include <net/dsfield.h>
8#include <linux/reciprocal_div.h>
9
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#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
114#define RED_STAB_SIZE 256
115#define RED_STAB_MASK (RED_STAB_SIZE - 1)
116
117struct red_stats {
118 u32 prob_drop;
119 u32 prob_mark;
120 u32 forced_drop;
121 u32 forced_mark;
122 u32 pdrop;
123 u32 other;
124};
125
126struct red_parms {
127
128 u32 qth_min;
129 u32 qth_max;
130 u32 Scell_max;
131 u32 max_P;
132 u32 max_P_reciprocal;
133 u32 qth_delta;
134 u32 target_min;
135 u32 target_max;
136 u8 Scell_log;
137 u8 Wlog;
138 u8 Plog;
139 u8 Stab[RED_STAB_SIZE];
140};
141
142struct red_vars {
143
144 int qcount;
145
146 u32 qR;
147
148 unsigned long qavg;
149 ktime_t qidlestart;
150};
151
152static inline u32 red_maxp(u8 Plog)
153{
154 return Plog < 32 ? (~0U >> Plog) : ~0U;
155}
156
157static inline void red_set_vars(struct red_vars *v)
158{
159
160
161
162
163 v->qavg = 0;
164
165 v->qcount = -1;
166}
167
168static inline void red_set_parms(struct red_parms *p,
169 u32 qth_min, u32 qth_max, u8 Wlog, u8 Plog,
170 u8 Scell_log, u8 *stab, u32 max_P)
171{
172 int delta = qth_max - qth_min;
173 u32 max_p_delta;
174
175 p->qth_min = qth_min << Wlog;
176 p->qth_max = qth_max << Wlog;
177 p->Wlog = Wlog;
178 p->Plog = Plog;
179 if (delta < 0)
180 delta = 1;
181 p->qth_delta = delta;
182 if (!max_P) {
183 max_P = red_maxp(Plog);
184 max_P *= delta;
185 }
186 p->max_P = max_P;
187 max_p_delta = max_P / delta;
188 max_p_delta = max(max_p_delta, 1U);
189 p->max_P_reciprocal = reciprocal_value(max_p_delta);
190
191
192
193
194
195 delta /= 5;
196 p->target_min = qth_min + 2*delta;
197 p->target_max = qth_min + 3*delta;
198
199 p->Scell_log = Scell_log;
200 p->Scell_max = (255 << Scell_log);
201
202 if (stab)
203 memcpy(p->Stab, stab, sizeof(p->Stab));
204}
205
206static inline int red_is_idling(const struct red_vars *v)
207{
208 return v->qidlestart.tv64 != 0;
209}
210
211static inline void red_start_of_idle_period(struct red_vars *v)
212{
213 v->qidlestart = ktime_get();
214}
215
216static inline void red_end_of_idle_period(struct red_vars *v)
217{
218 v->qidlestart.tv64 = 0;
219}
220
221static inline void red_restart(struct red_vars *v)
222{
223 red_end_of_idle_period(v);
224 v->qavg = 0;
225 v->qcount = -1;
226}
227
228static inline unsigned long red_calc_qavg_from_idle_time(const struct red_parms *p,
229 const struct red_vars *v)
230{
231 s64 delta = ktime_us_delta(ktime_get(), v->qidlestart);
232 long us_idle = min_t(s64, delta, p->Scell_max);
233 int shift;
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 shift = p->Stab[(us_idle >> p->Scell_log) & RED_STAB_MASK];
256
257 if (shift)
258 return v->qavg >> shift;
259 else {
260
261
262
263
264
265
266
267 us_idle = (v->qavg * (u64)us_idle) >> p->Scell_log;
268
269 if (us_idle < (v->qavg >> 1))
270 return v->qavg - us_idle;
271 else
272 return v->qavg >> 1;
273 }
274}
275
276static inline unsigned long red_calc_qavg_no_idle_time(const struct red_parms *p,
277 const struct red_vars *v,
278 unsigned int backlog)
279{
280
281
282
283
284
285
286
287
288
289 return v->qavg + (backlog - (v->qavg >> p->Wlog));
290}
291
292static inline unsigned long red_calc_qavg(const struct red_parms *p,
293 const struct red_vars *v,
294 unsigned int backlog)
295{
296 if (!red_is_idling(v))
297 return red_calc_qavg_no_idle_time(p, v, backlog);
298 else
299 return red_calc_qavg_from_idle_time(p, v);
300}
301
302
303static inline u32 red_random(const struct red_parms *p)
304{
305 return reciprocal_divide(net_random(), p->max_P_reciprocal);
306}
307
308static inline int red_mark_probability(const struct red_parms *p,
309 const struct red_vars *v,
310 unsigned long qavg)
311{
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328 return !(((qavg - p->qth_min) >> p->Wlog) * v->qcount < v->qR);
329}
330
331enum {
332 RED_BELOW_MIN_THRESH,
333 RED_BETWEEN_TRESH,
334 RED_ABOVE_MAX_TRESH,
335};
336
337static inline int red_cmp_thresh(const struct red_parms *p, unsigned long qavg)
338{
339 if (qavg < p->qth_min)
340 return RED_BELOW_MIN_THRESH;
341 else if (qavg >= p->qth_max)
342 return RED_ABOVE_MAX_TRESH;
343 else
344 return RED_BETWEEN_TRESH;
345}
346
347enum {
348 RED_DONT_MARK,
349 RED_PROB_MARK,
350 RED_HARD_MARK,
351};
352
353static inline int red_action(const struct red_parms *p,
354 struct red_vars *v,
355 unsigned long qavg)
356{
357 switch (red_cmp_thresh(p, qavg)) {
358 case RED_BELOW_MIN_THRESH:
359 v->qcount = -1;
360 return RED_DONT_MARK;
361
362 case RED_BETWEEN_TRESH:
363 if (++v->qcount) {
364 if (red_mark_probability(p, v, qavg)) {
365 v->qcount = 0;
366 v->qR = red_random(p);
367 return RED_PROB_MARK;
368 }
369 } else
370 v->qR = red_random(p);
371
372 return RED_DONT_MARK;
373
374 case RED_ABOVE_MAX_TRESH:
375 v->qcount = -1;
376 return RED_HARD_MARK;
377 }
378
379 BUG();
380 return RED_DONT_MARK;
381}
382
383static inline void red_adaptative_algo(struct red_parms *p, struct red_vars *v)
384{
385 unsigned long qavg;
386 u32 max_p_delta;
387
388 qavg = v->qavg;
389 if (red_is_idling(v))
390 qavg = red_calc_qavg_from_idle_time(p, v);
391
392
393 qavg >>= p->Wlog;
394
395 if (qavg > p->target_max && p->max_P <= MAX_P_MAX)
396 p->max_P += MAX_P_ALPHA(p->max_P);
397 else if (qavg < p->target_min && p->max_P >= MAX_P_MIN)
398 p->max_P = (p->max_P/10)*9;
399
400 max_p_delta = DIV_ROUND_CLOSEST(p->max_P, p->qth_delta);
401 max_p_delta = max(max_p_delta, 1U);
402 p->max_P_reciprocal = reciprocal_value(max_p_delta);
403}
404#endif
405