1 /* Copyright (C) 2013 Cisco Systems, Inc, 2013.
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * Author: Vijay Subramanian <vijaynsu@cisco.com>
14 * Author: Mythili Prabhu <mysuryan@cisco.com>
16 * ECN support is added by Naeem Khademi <naeemk@ifi.uio.no>
17 * University of Oslo, Norway.
20 * IETF draft submission: http://tools.ietf.org/html/draft-pan-aqm-pie-00
21 * IEEE Conference on High Performance Switching and Routing 2013 :
22 * "PIE: A * Lightweight Control Scheme to Address the Bufferbloat Problem"
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>
34 #define QUEUE_THRESHOLD 10000
35 #define DQCOUNT_INVALID -1
36 #define MAX_PROB 0xffffffff
41 psched_time_t target
; /* user specified target delay in pschedtime */
42 u32 tupdate
; /* timer frequency (in jiffies) */
43 u32 limit
; /* number of packets that can be enqueued */
44 u32 alpha
; /* alpha and beta are between 0 and 32 */
45 u32 beta
; /* and are used for shift relative to 1 */
46 bool ecn
; /* true if ecn is enabled */
47 bool bytemode
; /* to scale drop early prob based on pkt size */
52 u32 prob
; /* probability but scaled by u32 limit. */
53 psched_time_t burst_time
;
55 psched_time_t qdelay_old
;
56 u64 dq_count
; /* measured in bytes */
57 psched_time_t dq_tstamp
; /* drain rate */
58 u32 avg_dq_rate
; /* bytes per pschedtime tick,scaled */
59 u32 qlen_old
; /* in bytes */
62 /* statistics gathering */
64 u32 packets_in
; /* total number of packets enqueued */
65 u32 dropped
; /* packets dropped due to pie_action */
66 u32 overlimit
; /* dropped due to lack of space in queue */
67 u32 maxq
; /* maximum queue size */
68 u32 ecn_mark
; /* packets marked with ECN */
71 /* private data for the Qdisc */
72 struct pie_sched_data
{
73 struct pie_params params
;
75 struct pie_stats stats
;
76 struct timer_list adapt_timer
;
80 static void pie_params_init(struct pie_params
*params
)
84 params
->tupdate
= usecs_to_jiffies(30 * USEC_PER_MSEC
); /* 30 ms */
85 params
->limit
= 1000; /* default of 1000 packets */
86 params
->target
= PSCHED_NS2TICKS(20 * NSEC_PER_MSEC
); /* 20 ms */
88 params
->bytemode
= false;
91 static void pie_vars_init(struct pie_vars
*vars
)
93 vars
->dq_count
= DQCOUNT_INVALID
;
94 vars
->avg_dq_rate
= 0;
95 /* default of 100 ms in pschedtime */
96 vars
->burst_time
= PSCHED_NS2TICKS(100 * NSEC_PER_MSEC
);
99 static bool drop_early(struct Qdisc
*sch
, u32 packet_size
)
101 struct pie_sched_data
*q
= qdisc_priv(sch
);
103 u32 local_prob
= q
->vars
.prob
;
104 u32 mtu
= psched_mtu(qdisc_dev(sch
));
106 /* If there is still burst allowance left skip random early drop */
107 if (q
->vars
.burst_time
> 0)
110 /* If current delay is less than half of target, and
111 * if drop prob is low already, disable early_drop
113 if ((q
->vars
.qdelay
< q
->params
.target
/ 2) &&
114 (q
->vars
.prob
< MAX_PROB
/ 5))
117 /* If we have fewer than 2 mtu-sized packets, disable drop_early,
118 * similar to min_th in RED
120 if (sch
->qstats
.backlog
< 2 * mtu
)
123 /* If bytemode is turned on, use packet size to compute new
124 * probablity. Smaller packets will have lower drop prob in this case
126 if (q
->params
.bytemode
&& packet_size
<= mtu
)
127 local_prob
= (local_prob
/ mtu
) * packet_size
;
129 local_prob
= q
->vars
.prob
;
132 if (rnd
< local_prob
)
138 static int pie_qdisc_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
139 struct sk_buff
**to_free
)
141 struct pie_sched_data
*q
= qdisc_priv(sch
);
142 bool enqueue
= false;
144 if (unlikely(qdisc_qlen(sch
) >= sch
->limit
)) {
145 q
->stats
.overlimit
++;
149 if (!drop_early(sch
, skb
->len
)) {
151 } else if (q
->params
.ecn
&& (q
->vars
.prob
<= MAX_PROB
/ 10) &&
152 INET_ECN_set_ce(skb
)) {
153 /* If packet is ecn capable, mark it if drop probability
154 * is lower than 10%, else drop it.
160 /* we can enqueue the packet */
162 q
->stats
.packets_in
++;
163 if (qdisc_qlen(sch
) > q
->stats
.maxq
)
164 q
->stats
.maxq
= qdisc_qlen(sch
);
166 return qdisc_enqueue_tail(skb
, sch
);
171 return qdisc_drop(skb
, sch
, to_free
);
174 static 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
},
184 static int pie_change(struct Qdisc
*sch
, struct nlattr
*opt
,
185 struct netlink_ext_ack
*extack
)
187 struct pie_sched_data
*q
= qdisc_priv(sch
);
188 struct nlattr
*tb
[TCA_PIE_MAX
+ 1];
189 unsigned int qlen
, dropped
= 0;
195 err
= nla_parse_nested(tb
, TCA_PIE_MAX
, opt
, pie_policy
, NULL
);
201 /* convert from microseconds to pschedtime */
202 if (tb
[TCA_PIE_TARGET
]) {
203 /* target is in us */
204 u32 target
= nla_get_u32(tb
[TCA_PIE_TARGET
]);
206 /* convert to pschedtime */
207 q
->params
.target
= PSCHED_NS2TICKS((u64
)target
* NSEC_PER_USEC
);
210 /* tupdate is in jiffies */
211 if (tb
[TCA_PIE_TUPDATE
])
213 usecs_to_jiffies(nla_get_u32(tb
[TCA_PIE_TUPDATE
]));
215 if (tb
[TCA_PIE_LIMIT
]) {
216 u32 limit
= nla_get_u32(tb
[TCA_PIE_LIMIT
]);
218 q
->params
.limit
= limit
;
222 if (tb
[TCA_PIE_ALPHA
])
223 q
->params
.alpha
= nla_get_u32(tb
[TCA_PIE_ALPHA
]);
225 if (tb
[TCA_PIE_BETA
])
226 q
->params
.beta
= nla_get_u32(tb
[TCA_PIE_BETA
]);
229 q
->params
.ecn
= nla_get_u32(tb
[TCA_PIE_ECN
]);
231 if (tb
[TCA_PIE_BYTEMODE
])
232 q
->params
.bytemode
= nla_get_u32(tb
[TCA_PIE_BYTEMODE
]);
234 /* Drop excess packets if new limit is lower */
236 while (sch
->q
.qlen
> sch
->limit
) {
237 struct sk_buff
*skb
= __qdisc_dequeue_head(&sch
->q
);
239 dropped
+= qdisc_pkt_len(skb
);
240 qdisc_qstats_backlog_dec(sch
, skb
);
241 rtnl_qdisc_drop(skb
, sch
);
243 qdisc_tree_reduce_backlog(sch
, qlen
- sch
->q
.qlen
, dropped
);
245 sch_tree_unlock(sch
);
249 static void pie_process_dequeue(struct Qdisc
*sch
, struct sk_buff
*skb
)
251 struct pie_sched_data
*q
= qdisc_priv(sch
);
252 int qlen
= sch
->qstats
.backlog
; /* current queue size in bytes */
254 /* If current queue is about 10 packets or more and dq_count is unset
255 * we have enough packets to calculate the drain rate. Save
256 * current time as dq_tstamp and start measurement cycle.
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;
263 /* Calculate the average drain rate from this value. If queue length
264 * has receded to a small value viz., <= QUEUE_THRESHOLD bytes,reset
265 * the dq_count to -1 as we don't have enough packets to calculate the
266 * drain rate anymore The following if block is entered only when we
267 * have a substantial queue built up (QUEUE_THRESHOLD bytes or more)
268 * and we calculate the drain rate for the threshold here. dq_count is
269 * in bytes, time difference in psched_time, hence rate is in
272 if (q
->vars
.dq_count
!= DQCOUNT_INVALID
) {
273 q
->vars
.dq_count
+= skb
->len
;
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
;
283 count
= count
/ dtime
;
285 if (q
->vars
.avg_dq_rate
== 0)
286 q
->vars
.avg_dq_rate
= count
;
288 q
->vars
.avg_dq_rate
=
289 (q
->vars
.avg_dq_rate
-
290 (q
->vars
.avg_dq_rate
>> 3)) + (count
>> 3);
292 /* If the queue has receded below the threshold, we hold
293 * on to the last drain rate calculated, else we reset
294 * dq_count to 0 to re-enter the if block when the next
297 if (qlen
< QUEUE_THRESHOLD
) {
298 q
->vars
.dq_count
= DQCOUNT_INVALID
;
300 q
->vars
.dq_count
= 0;
301 q
->vars
.dq_tstamp
= psched_get_time();
304 if (q
->vars
.burst_time
> 0) {
305 if (q
->vars
.burst_time
> dtime
)
306 q
->vars
.burst_time
-= dtime
;
308 q
->vars
.burst_time
= 0;
314 static void calculate_probability(struct Qdisc
*sch
)
316 struct pie_sched_data
*q
= qdisc_priv(sch
);
317 u32 qlen
= sch
->qstats
.backlog
; /* queue size in bytes */
318 psched_time_t qdelay
= 0; /* in pschedtime */
319 psched_time_t qdelay_old
= q
->vars
.qdelay
; /* in pschedtime */
320 s32 delta
= 0; /* determines the change in probability */
323 bool update_prob
= true;
325 q
->vars
.qdelay_old
= q
->vars
.qdelay
;
327 if (q
->vars
.avg_dq_rate
> 0)
328 qdelay
= (qlen
<< PIE_SCALE
) / q
->vars
.avg_dq_rate
;
332 /* If qdelay is zero and qlen is not, it means qlen is very small, less
333 * than dequeue_rate, so we do not update probabilty in this round
335 if (qdelay
== 0 && qlen
!= 0)
338 /* In the algorithm, alpha and beta are between 0 and 2 with typical
339 * value for alpha as 0.125. In this implementation, we use values 0-32
340 * passed from user space to represent this. Also, alpha and beta have
341 * unit of HZ and need to be scaled before they can used to update
342 * probability. alpha/beta are updated locally below by 1) scaling them
343 * appropriately 2) scaling down by 16 to come to 0-2 range.
344 * Please see paper for details.
346 * We scale alpha and beta differently depending on whether we are in
347 * light, medium or high dropping mode.
349 if (q
->vars
.prob
< MAX_PROB
/ 100) {
351 (q
->params
.alpha
* (MAX_PROB
/ PSCHED_TICKS_PER_SEC
)) >> 7;
353 (q
->params
.beta
* (MAX_PROB
/ PSCHED_TICKS_PER_SEC
)) >> 7;
354 } else if (q
->vars
.prob
< MAX_PROB
/ 10) {
356 (q
->params
.alpha
* (MAX_PROB
/ PSCHED_TICKS_PER_SEC
)) >> 5;
358 (q
->params
.beta
* (MAX_PROB
/ PSCHED_TICKS_PER_SEC
)) >> 5;
361 (q
->params
.alpha
* (MAX_PROB
/ PSCHED_TICKS_PER_SEC
)) >> 4;
363 (q
->params
.beta
* (MAX_PROB
/ PSCHED_TICKS_PER_SEC
)) >> 4;
366 /* alpha and beta should be between 0 and 32, in multiples of 1/16 */
367 delta
+= alpha
* ((qdelay
- q
->params
.target
));
368 delta
+= beta
* ((qdelay
- qdelay_old
));
370 oldprob
= q
->vars
.prob
;
372 /* to ensure we increase probability in steps of no more than 2% */
373 if (delta
> (s32
)(MAX_PROB
/ (100 / 2)) &&
374 q
->vars
.prob
>= MAX_PROB
/ 10)
375 delta
= (MAX_PROB
/ 100) * 2;
378 * Tune drop probability to increase quickly for high delays(>= 250ms)
379 * 250ms is derived through experiments and provides error protection
382 if (qdelay
> (PSCHED_NS2TICKS(250 * NSEC_PER_MSEC
)))
383 delta
+= MAX_PROB
/ (100 / 2);
385 q
->vars
.prob
+= delta
;
388 /* prevent overflow */
389 if (q
->vars
.prob
< oldprob
) {
390 q
->vars
.prob
= MAX_PROB
;
391 /* Prevent normalization error. If probability is at
392 * maximum value already, we normalize it here, and
393 * skip the check to do a non-linear drop in the next
399 /* prevent underflow */
400 if (q
->vars
.prob
> oldprob
)
404 /* Non-linear drop in probability: Reduce drop probability quickly if
405 * delay is 0 for 2 consecutive Tupdate periods.
408 if (qdelay
== 0 && qdelay_old
== 0 && update_prob
)
409 q
->vars
.prob
= (q
->vars
.prob
* 98) / 100;
411 q
->vars
.qdelay
= qdelay
;
412 q
->vars
.qlen_old
= qlen
;
414 /* We restart the measurement cycle if the following conditions are met
415 * 1. If the delay has been low for 2 consecutive Tupdate periods
416 * 2. Calculated drop probability is zero
417 * 3. We have atleast one estimate for the avg_dq_rate ie.,
418 * is a non-zero value
420 if ((q
->vars
.qdelay
< q
->params
.target
/ 2) &&
421 (q
->vars
.qdelay_old
< q
->params
.target
/ 2) &&
423 q
->vars
.avg_dq_rate
> 0)
424 pie_vars_init(&q
->vars
);
427 static void pie_timer(struct timer_list
*t
)
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
));
433 spin_lock(root_lock
);
434 calculate_probability(sch
);
436 /* reset the timer to fire after 'tupdate'. tupdate is in jiffies. */
437 if (q
->params
.tupdate
)
438 mod_timer(&q
->adapt_timer
, jiffies
+ q
->params
.tupdate
);
439 spin_unlock(root_lock
);
442 static int pie_init(struct Qdisc
*sch
, struct nlattr
*opt
,
443 struct netlink_ext_ack
*extack
)
445 struct pie_sched_data
*q
= qdisc_priv(sch
);
447 pie_params_init(&q
->params
);
448 pie_vars_init(&q
->vars
);
449 sch
->limit
= q
->params
.limit
;
452 timer_setup(&q
->adapt_timer
, pie_timer
, 0);
455 int err
= pie_change(sch
, opt
, extack
);
461 mod_timer(&q
->adapt_timer
, jiffies
+ HZ
/ 2);
465 static int pie_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
467 struct pie_sched_data
*q
= qdisc_priv(sch
);
470 opts
= nla_nest_start(skb
, TCA_OPTIONS
);
472 goto nla_put_failure
;
474 /* convert target from pschedtime to us */
475 if (nla_put_u32(skb
, TCA_PIE_TARGET
,
476 ((u32
)PSCHED_TICKS2NS(q
->params
.target
)) /
478 nla_put_u32(skb
, TCA_PIE_LIMIT
, sch
->limit
) ||
479 nla_put_u32(skb
, TCA_PIE_TUPDATE
,
480 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
;
487 return nla_nest_end(skb
, opts
);
490 nla_nest_cancel(skb
, opts
);
494 static int pie_dump_stats(struct Qdisc
*sch
, struct gnet_dump
*d
)
496 struct pie_sched_data
*q
= qdisc_priv(sch
);
497 struct tc_pie_xstats st
= {
498 .prob
= q
->vars
.prob
,
499 .delay
= ((u32
)PSCHED_TICKS2NS(q
->vars
.qdelay
)) /
501 /* unscale and return dq_rate in bytes per sec */
502 .avg_dq_rate
= q
->vars
.avg_dq_rate
*
503 (PSCHED_TICKS_PER_SEC
) >> PIE_SCALE
,
504 .packets_in
= q
->stats
.packets_in
,
505 .overlimit
= q
->stats
.overlimit
,
506 .maxq
= q
->stats
.maxq
,
507 .dropped
= q
->stats
.dropped
,
508 .ecn_mark
= q
->stats
.ecn_mark
,
511 return gnet_stats_copy_app(d
, &st
, sizeof(st
));
514 static struct sk_buff
*pie_qdisc_dequeue(struct Qdisc
*sch
)
516 struct sk_buff
*skb
= qdisc_dequeue_head(sch
);
521 pie_process_dequeue(sch
, skb
);
525 static void pie_reset(struct Qdisc
*sch
)
527 struct pie_sched_data
*q
= qdisc_priv(sch
);
529 qdisc_reset_queue(sch
);
530 pie_vars_init(&q
->vars
);
533 static void pie_destroy(struct Qdisc
*sch
)
535 struct pie_sched_data
*q
= qdisc_priv(sch
);
537 q
->params
.tupdate
= 0;
538 del_timer_sync(&q
->adapt_timer
);
541 static struct Qdisc_ops pie_qdisc_ops __read_mostly
= {
543 .priv_size
= sizeof(struct pie_sched_data
),
544 .enqueue
= pie_qdisc_enqueue
,
545 .dequeue
= pie_qdisc_dequeue
,
546 .peek
= qdisc_peek_dequeued
,
548 .destroy
= pie_destroy
,
550 .change
= pie_change
,
552 .dump_stats
= pie_dump_stats
,
553 .owner
= THIS_MODULE
,
556 static int __init
pie_module_init(void)
558 return register_qdisc(&pie_qdisc_ops
);
561 static void __exit
pie_module_exit(void)
563 unregister_qdisc(&pie_qdisc_ops
);
566 module_init(pie_module_init
);
567 module_exit(pie_module_exit
);
569 MODULE_DESCRIPTION("Proportional Integral controller Enhanced (PIE) scheduler");
570 MODULE_AUTHOR("Vijay Subramanian");
571 MODULE_AUTHOR("Mythili Prabhu");
572 MODULE_LICENSE("GPL");