1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/module.h>
26#include <linux/slab.h>
27#include <linux/types.h>
28#include <linux/kernel.h>
29#include <linux/errno.h>
30#include <linux/skbuff.h>
31#include <net/pkt_sched.h>
32#include <net/inet_ecn.h>
33
34#define QUEUE_THRESHOLD 10000
35#define DQCOUNT_INVALID -1
36#define MAX_PROB 0xffffffff
37#define PIE_SCALE 8
38
39
40struct pie_params {
41 psched_time_t target;
42 u32 tupdate;
43 u32 limit;
44 u32 alpha;
45 u32 beta;
46 bool ecn;
47 bool bytemode;
48};
49
50
51struct pie_vars {
52 u32 prob;
53 psched_time_t burst_time;
54 psched_time_t qdelay;
55 psched_time_t qdelay_old;
56 u64 dq_count;
57 psched_time_t dq_tstamp;
58 u32 avg_dq_rate;
59 u32 qlen_old;
60};
61
62
63struct pie_stats {
64 u32 packets_in;
65 u32 dropped;
66 u32 overlimit;
67 u32 maxq;
68 u32 ecn_mark;
69};
70
71
72struct pie_sched_data {
73 struct pie_params params;
74 struct pie_vars vars;
75 struct pie_stats stats;
76 struct timer_list adapt_timer;
77 struct Qdisc *sch;
78};
79
80static void pie_params_init(struct pie_params *params)
81{
82 params->alpha = 2;
83 params->beta = 20;
84 params->tupdate = usecs_to_jiffies(30 * USEC_PER_MSEC);
85 params->limit = 1000;
86 params->target = PSCHED_NS2TICKS(20 * NSEC_PER_MSEC);
87 params->ecn = false;
88 params->bytemode = false;
89}
90
91static void pie_vars_init(struct pie_vars *vars)
92{
93 vars->dq_count = DQCOUNT_INVALID;
94 vars->avg_dq_rate = 0;
95
96 vars->burst_time = PSCHED_NS2TICKS(100 * NSEC_PER_MSEC);
97}
98
99static bool drop_early(struct Qdisc *sch, u32 packet_size)
100{
101 struct pie_sched_data *q = qdisc_priv(sch);
102 u32 rnd;
103 u32 local_prob = q->vars.prob;
104 u32 mtu = psched_mtu(qdisc_dev(sch));
105
106
107 if (q->vars.burst_time > 0)
108 return false;
109
110
111
112
113 if ((q->vars.qdelay < q->params.target / 2)
114 && (q->vars.prob < MAX_PROB / 5))
115 return false;
116
117
118
119
120 if (sch->qstats.backlog < 2 * mtu)
121 return false;
122
123
124
125
126 if (q->params.bytemode && packet_size <= mtu)
127 local_prob = (local_prob / mtu) * packet_size;
128 else
129 local_prob = q->vars.prob;
130
131 rnd = prandom_u32();
132 if (rnd < local_prob)
133 return true;
134
135 return false;
136}
137
138static int pie_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
139 struct sk_buff **to_free)
140{
141 struct pie_sched_data *q = qdisc_priv(sch);
142 bool enqueue = false;
143
144 if (unlikely(qdisc_qlen(sch) >= sch->limit)) {
145 q->stats.overlimit++;
146 goto out;
147 }
148
149 if (!drop_early(sch, skb->len)) {
150 enqueue = true;
151 } else if (q->params.ecn && (q->vars.prob <= MAX_PROB / 10) &&
152 INET_ECN_set_ce(skb)) {
153
154
155
156 q->stats.ecn_mark++;
157 enqueue = true;
158 }
159
160
161 if (enqueue) {
162 q->stats.packets_in++;
163 if (qdisc_qlen(sch) > q->stats.maxq)
164 q->stats.maxq = qdisc_qlen(sch);
165
166 return qdisc_enqueue_tail(skb, sch);
167 }
168
169out:
170 q->stats.dropped++;
171 return qdisc_drop(skb, sch, to_free);
172}
173
174static const struct nla_policy pie_policy[TCA_PIE_MAX + 1] = {
175 [TCA_PIE_TARGET] = {.type = NLA_U32},
176 [TCA_PIE_LIMIT] = {.type = NLA_U32},
177 [TCA_PIE_TUPDATE] = {.type = NLA_U32},
178 [TCA_PIE_ALPHA] = {.type = NLA_U32},
179 [TCA_PIE_BETA] = {.type = NLA_U32},
180 [TCA_PIE_ECN] = {.type = NLA_U32},
181 [TCA_PIE_BYTEMODE] = {.type = NLA_U32},
182};
183
184static int pie_change(struct Qdisc *sch, struct nlattr *opt,
185 struct netlink_ext_ack *extack)
186{
187 struct pie_sched_data *q = qdisc_priv(sch);
188 struct nlattr *tb[TCA_PIE_MAX + 1];
189 unsigned int qlen, dropped = 0;
190 int err;
191
192 if (!opt)
193 return -EINVAL;
194
195 err = nla_parse_nested(tb, TCA_PIE_MAX, opt, pie_policy, NULL);
196 if (err < 0)
197 return err;
198
199 sch_tree_lock(sch);
200
201
202 if (tb[TCA_PIE_TARGET]) {
203
204 u32 target = nla_get_u32(tb[TCA_PIE_TARGET]);
205
206
207 q->params.target = PSCHED_NS2TICKS((u64)target * NSEC_PER_USEC);
208 }
209
210
211 if (tb[TCA_PIE_TUPDATE])
212 q->params.tupdate = usecs_to_jiffies(nla_get_u32(tb[TCA_PIE_TUPDATE]));
213
214 if (tb[TCA_PIE_LIMIT]) {
215 u32 limit = nla_get_u32(tb[TCA_PIE_LIMIT]);
216
217 q->params.limit = limit;
218 sch->limit = limit;
219 }
220
221 if (tb[TCA_PIE_ALPHA])
222 q->params.alpha = nla_get_u32(tb[TCA_PIE_ALPHA]);
223
224 if (tb[TCA_PIE_BETA])
225 q->params.beta = nla_get_u32(tb[TCA_PIE_BETA]);
226
227 if (tb[TCA_PIE_ECN])
228 q->params.ecn = nla_get_u32(tb[TCA_PIE_ECN]);
229
230 if (tb[TCA_PIE_BYTEMODE])
231 q->params.bytemode = nla_get_u32(tb[TCA_PIE_BYTEMODE]);
232
233
234 qlen = sch->q.qlen;
235 while (sch->q.qlen > sch->limit) {
236 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
237
238 dropped += qdisc_pkt_len(skb);
239 qdisc_qstats_backlog_dec(sch, skb);
240 rtnl_qdisc_drop(skb, sch);
241 }
242 qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
243
244 sch_tree_unlock(sch);
245 return 0;
246}
247
248static void pie_process_dequeue(struct Qdisc *sch, struct sk_buff *skb)
249{
250
251 struct pie_sched_data *q = qdisc_priv(sch);
252 int qlen = sch->qstats.backlog;
253
254
255
256
257
258 if (qlen >= QUEUE_THRESHOLD && q->vars.dq_count == DQCOUNT_INVALID) {
259 q->vars.dq_tstamp = psched_get_time();
260 q->vars.dq_count = 0;
261 }
262
263
264
265
266
267
268
269
270
271
272 if (q->vars.dq_count != DQCOUNT_INVALID) {
273 q->vars.dq_count += skb->len;
274
275 if (q->vars.dq_count >= QUEUE_THRESHOLD) {
276 psched_time_t now = psched_get_time();
277 u32 dtime = now - q->vars.dq_tstamp;
278 u32 count = q->vars.dq_count << PIE_SCALE;
279
280 if (dtime == 0)
281 return;
282
283 count = count / dtime;
284
285 if (q->vars.avg_dq_rate == 0)
286 q->vars.avg_dq_rate = count;
287 else
288 q->vars.avg_dq_rate =
289 (q->vars.avg_dq_rate -
290 (q->vars.avg_dq_rate >> 3)) + (count >> 3);
291
292
293
294
295
296
297 if (qlen < QUEUE_THRESHOLD)
298 q->vars.dq_count = DQCOUNT_INVALID;
299 else {
300 q->vars.dq_count = 0;
301 q->vars.dq_tstamp = psched_get_time();
302 }
303
304 if (q->vars.burst_time > 0) {
305 if (q->vars.burst_time > dtime)
306 q->vars.burst_time -= dtime;
307 else
308 q->vars.burst_time = 0;
309 }
310 }
311 }
312}
313
314static void calculate_probability(struct Qdisc *sch)
315{
316 struct pie_sched_data *q = qdisc_priv(sch);
317 u32 qlen = sch->qstats.backlog;
318 psched_time_t qdelay = 0;
319 psched_time_t qdelay_old = q->vars.qdelay;
320 s32 delta = 0;
321 u32 oldprob;
322 u32 alpha, beta;
323 bool update_prob = true;
324
325 q->vars.qdelay_old = q->vars.qdelay;
326
327 if (q->vars.avg_dq_rate > 0)
328 qdelay = (qlen << PIE_SCALE) / q->vars.avg_dq_rate;
329 else
330 qdelay = 0;
331
332
333
334
335 if (qdelay == 0 && qlen != 0)
336 update_prob = false;
337
338
339
340
341
342
343
344
345
346
347
348
349 if (q->vars.prob < MAX_PROB / 100) {
350 alpha =
351 (q->params.alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 7;
352 beta =
353 (q->params.beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 7;
354 } else if (q->vars.prob < MAX_PROB / 10) {
355 alpha =
356 (q->params.alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 5;
357 beta =
358 (q->params.beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 5;
359 } else {
360 alpha =
361 (q->params.alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
362 beta =
363 (q->params.beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
364 }
365
366
367 delta += alpha * ((qdelay - q->params.target));
368 delta += beta * ((qdelay - qdelay_old));
369
370 oldprob = q->vars.prob;
371
372
373 if (delta > (s32) (MAX_PROB / (100 / 2)) &&
374 q->vars.prob >= MAX_PROB / 10)
375 delta = (MAX_PROB / 100) * 2;
376
377
378
379
380
381
382 if (qdelay > (PSCHED_NS2TICKS(250 * NSEC_PER_MSEC)))
383 delta += MAX_PROB / (100 / 2);
384
385 q->vars.prob += delta;
386
387 if (delta > 0) {
388
389 if (q->vars.prob < oldprob) {
390 q->vars.prob = MAX_PROB;
391
392
393
394
395
396 update_prob = false;
397 }
398 } else {
399
400 if (q->vars.prob > oldprob)
401 q->vars.prob = 0;
402 }
403
404
405
406
407
408 if ((qdelay == 0) && (qdelay_old == 0) && update_prob)
409 q->vars.prob = (q->vars.prob * 98) / 100;
410
411 q->vars.qdelay = qdelay;
412 q->vars.qlen_old = qlen;
413
414
415
416
417
418
419
420 if ((q->vars.qdelay < q->params.target / 2) &&
421 (q->vars.qdelay_old < q->params.target / 2) &&
422 (q->vars.prob == 0) &&
423 (q->vars.avg_dq_rate > 0))
424 pie_vars_init(&q->vars);
425}
426
427static void pie_timer(struct timer_list *t)
428{
429 struct pie_sched_data *q = from_timer(q, t, adapt_timer);
430 struct Qdisc *sch = q->sch;
431 spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch));
432
433 spin_lock(root_lock);
434 calculate_probability(sch);
435
436
437 if (q->params.tupdate)
438 mod_timer(&q->adapt_timer, jiffies + q->params.tupdate);
439 spin_unlock(root_lock);
440
441}
442
443static int pie_init(struct Qdisc *sch, struct nlattr *opt,
444 struct netlink_ext_ack *extack)
445{
446 struct pie_sched_data *q = qdisc_priv(sch);
447
448 pie_params_init(&q->params);
449 pie_vars_init(&q->vars);
450 sch->limit = q->params.limit;
451
452 q->sch = sch;
453 timer_setup(&q->adapt_timer, pie_timer, 0);
454
455 if (opt) {
456 int err = pie_change(sch, opt, extack);
457
458 if (err)
459 return err;
460 }
461
462 mod_timer(&q->adapt_timer, jiffies + HZ / 2);
463 return 0;
464}
465
466static int pie_dump(struct Qdisc *sch, struct sk_buff *skb)
467{
468 struct pie_sched_data *q = qdisc_priv(sch);
469 struct nlattr *opts;
470
471 opts = nla_nest_start(skb, TCA_OPTIONS);
472 if (opts == NULL)
473 goto nla_put_failure;
474
475
476 if (nla_put_u32(skb, TCA_PIE_TARGET,
477 ((u32) PSCHED_TICKS2NS(q->params.target)) /
478 NSEC_PER_USEC) ||
479 nla_put_u32(skb, TCA_PIE_LIMIT, sch->limit) ||
480 nla_put_u32(skb, TCA_PIE_TUPDATE, jiffies_to_usecs(q->params.tupdate)) ||
481 nla_put_u32(skb, TCA_PIE_ALPHA, q->params.alpha) ||
482 nla_put_u32(skb, TCA_PIE_BETA, q->params.beta) ||
483 nla_put_u32(skb, TCA_PIE_ECN, q->params.ecn) ||
484 nla_put_u32(skb, TCA_PIE_BYTEMODE, q->params.bytemode))
485 goto nla_put_failure;
486
487 return nla_nest_end(skb, opts);
488
489nla_put_failure:
490 nla_nest_cancel(skb, opts);
491 return -1;
492
493}
494
495static int pie_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
496{
497 struct pie_sched_data *q = qdisc_priv(sch);
498 struct tc_pie_xstats st = {
499 .prob = q->vars.prob,
500 .delay = ((u32) PSCHED_TICKS2NS(q->vars.qdelay)) /
501 NSEC_PER_USEC,
502
503 .avg_dq_rate = q->vars.avg_dq_rate *
504 (PSCHED_TICKS_PER_SEC) >> PIE_SCALE,
505 .packets_in = q->stats.packets_in,
506 .overlimit = q->stats.overlimit,
507 .maxq = q->stats.maxq,
508 .dropped = q->stats.dropped,
509 .ecn_mark = q->stats.ecn_mark,
510 };
511
512 return gnet_stats_copy_app(d, &st, sizeof(st));
513}
514
515static struct sk_buff *pie_qdisc_dequeue(struct Qdisc *sch)
516{
517 struct sk_buff *skb;
518 skb = qdisc_dequeue_head(sch);
519
520 if (!skb)
521 return NULL;
522
523 pie_process_dequeue(sch, skb);
524 return skb;
525}
526
527static void pie_reset(struct Qdisc *sch)
528{
529 struct pie_sched_data *q = qdisc_priv(sch);
530 qdisc_reset_queue(sch);
531 pie_vars_init(&q->vars);
532}
533
534static void pie_destroy(struct Qdisc *sch)
535{
536 struct pie_sched_data *q = qdisc_priv(sch);
537 q->params.tupdate = 0;
538 del_timer_sync(&q->adapt_timer);
539}
540
541static struct Qdisc_ops pie_qdisc_ops __read_mostly = {
542 .id = "pie",
543 .priv_size = sizeof(struct pie_sched_data),
544 .enqueue = pie_qdisc_enqueue,
545 .dequeue = pie_qdisc_dequeue,
546 .peek = qdisc_peek_dequeued,
547 .init = pie_init,
548 .destroy = pie_destroy,
549 .reset = pie_reset,
550 .change = pie_change,
551 .dump = pie_dump,
552 .dump_stats = pie_dump_stats,
553 .owner = THIS_MODULE,
554};
555
556static int __init pie_module_init(void)
557{
558 return register_qdisc(&pie_qdisc_ops);
559}
560
561static void __exit pie_module_exit(void)
562{
563 unregister_qdisc(&pie_qdisc_ops);
564}
565
566module_init(pie_module_init);
567module_exit(pie_module_exit);
568
569MODULE_DESCRIPTION("Proportional Integral controller Enhanced (PIE) scheduler");
570MODULE_AUTHOR("Vijay Subramanian");
571MODULE_AUTHOR("Mythili Prabhu");
572MODULE_LICENSE("GPL");
573