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