Linux Audio

Check our new training course

Loading...
v4.6
 
  1/* Copyright (C) 2013 Cisco Systems, Inc, 2013.
  2 *
  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
  6 * of the License.
  7 *
  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.
 12 *
 13 * Author: Vijay Subramanian <vijaynsu@cisco.com>
 14 * Author: Mythili Prabhu <mysuryan@cisco.com>
 15 *
 16 * ECN support is added by Naeem Khademi <naeemk@ifi.uio.no>
 17 * University of Oslo, Norway.
 18 *
 19 * References:
 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"
 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/* parameters used */
 40struct pie_params {
 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 */
 48};
 49
 50/* variables used */
 51struct pie_vars {
 52	u32 prob;		/* probability but scaled by u32 limit. */
 53	psched_time_t burst_time;
 54	psched_time_t qdelay;
 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 */
 60};
 61
 62/* statistics gathering */
 63struct pie_stats {
 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 */
 69};
 70
 71/* private data for the Qdisc */
 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};
 78
 79static void pie_params_init(struct pie_params *params)
 80{
 81	params->alpha = 2;
 82	params->beta = 20;
 83	params->tupdate = usecs_to_jiffies(30 * USEC_PER_MSEC);	/* 30 ms */
 84	params->limit = 1000;	/* default of 1000 packets */
 85	params->target = PSCHED_NS2TICKS(20 * NSEC_PER_MSEC);	/* 20 ms */
 86	params->ecn = false;
 87	params->bytemode = false;
 88}
 89
 90static void pie_vars_init(struct pie_vars *vars)
 91{
 92	vars->dq_count = DQCOUNT_INVALID;
 93	vars->avg_dq_rate = 0;
 94	/* default of 100 ms in pschedtime */
 95	vars->burst_time = PSCHED_NS2TICKS(100 * NSEC_PER_MSEC);
 96}
 97
 98static bool drop_early(struct Qdisc *sch, u32 packet_size)
 99{
100	struct pie_sched_data *q = qdisc_priv(sch);
101	u32 rnd;
102	u32 local_prob = q->vars.prob;
103	u32 mtu = psched_mtu(qdisc_dev(sch));
104
105	/* If there is still burst allowance left skip random early drop */
106	if (q->vars.burst_time > 0)
107		return false;
108
109	/* If current delay is less than half of target, and
110	 * if drop prob is low already, disable early_drop
111	 */
112	if ((q->vars.qdelay < q->params.target / 2)
113	    && (q->vars.prob < MAX_PROB / 5))
114		return false;
115
116	/* If we have fewer than 2 mtu-sized packets, disable drop_early,
117	 * similar to min_th in RED
118	 */
119	if (sch->qstats.backlog < 2 * mtu)
120		return false;
121
122	/* If bytemode is turned on, use packet size to compute new
123	 * probablity. Smaller packets will have lower drop prob in this case
124	 */
125	if (q->params.bytemode && packet_size <= mtu)
126		local_prob = (local_prob / mtu) * packet_size;
 
 
 
 
 
127	else
128		local_prob = q->vars.prob;
129
130	rnd = prandom_u32();
131	if (rnd < local_prob)
 
 
 
 
 
 
132		return true;
 
133
134	return false;
135}
 
136
137static int pie_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
 
138{
139	struct pie_sched_data *q = qdisc_priv(sch);
140	bool enqueue = false;
141
142	if (unlikely(qdisc_qlen(sch) >= sch->limit)) {
143		q->stats.overlimit++;
144		goto out;
145	}
146
147	if (!drop_early(sch, skb->len)) {
 
148		enqueue = true;
149	} else if (q->params.ecn && (q->vars.prob <= MAX_PROB / 10) &&
150		   INET_ECN_set_ce(skb)) {
151		/* If packet is ecn capable, mark it if drop probability
152		 * is lower than 10%, else drop it.
153		 */
154		q->stats.ecn_mark++;
155		enqueue = true;
156	}
157
158	/* we can enqueue the packet */
159	if (enqueue) {
 
 
 
 
160		q->stats.packets_in++;
161		if (qdisc_qlen(sch) > q->stats.maxq)
162			q->stats.maxq = qdisc_qlen(sch);
163
164		return qdisc_enqueue_tail(skb, sch);
165	}
166
167out:
168	q->stats.dropped++;
169	return qdisc_drop(skb, sch);
 
170}
171
172static const struct nla_policy pie_policy[TCA_PIE_MAX + 1] = {
173	[TCA_PIE_TARGET] = {.type = NLA_U32},
174	[TCA_PIE_LIMIT] = {.type = NLA_U32},
175	[TCA_PIE_TUPDATE] = {.type = NLA_U32},
176	[TCA_PIE_ALPHA] = {.type = NLA_U32},
177	[TCA_PIE_BETA] = {.type = NLA_U32},
178	[TCA_PIE_ECN] = {.type = NLA_U32},
179	[TCA_PIE_BYTEMODE] = {.type = NLA_U32},
 
180};
181
182static int pie_change(struct Qdisc *sch, struct nlattr *opt)
 
183{
184	struct pie_sched_data *q = qdisc_priv(sch);
185	struct nlattr *tb[TCA_PIE_MAX + 1];
186	unsigned int qlen, dropped = 0;
187	int err;
188
189	if (!opt)
190		return -EINVAL;
191
192	err = nla_parse_nested(tb, TCA_PIE_MAX, opt, pie_policy);
193	if (err < 0)
194		return err;
195
196	sch_tree_lock(sch);
197
198	/* convert from microseconds to pschedtime */
199	if (tb[TCA_PIE_TARGET]) {
200		/* target is in us */
201		u32 target = nla_get_u32(tb[TCA_PIE_TARGET]);
202
203		/* convert to pschedtime */
204		q->params.target = PSCHED_NS2TICKS((u64)target * NSEC_PER_USEC);
205	}
206
207	/* tupdate is in jiffies */
208	if (tb[TCA_PIE_TUPDATE])
209		q->params.tupdate = usecs_to_jiffies(nla_get_u32(tb[TCA_PIE_TUPDATE]));
 
210
211	if (tb[TCA_PIE_LIMIT]) {
212		u32 limit = nla_get_u32(tb[TCA_PIE_LIMIT]);
213
214		q->params.limit = limit;
215		sch->limit = limit;
216	}
217
218	if (tb[TCA_PIE_ALPHA])
219		q->params.alpha = nla_get_u32(tb[TCA_PIE_ALPHA]);
220
221	if (tb[TCA_PIE_BETA])
222		q->params.beta = nla_get_u32(tb[TCA_PIE_BETA]);
223
224	if (tb[TCA_PIE_ECN])
225		q->params.ecn = nla_get_u32(tb[TCA_PIE_ECN]);
226
227	if (tb[TCA_PIE_BYTEMODE])
228		q->params.bytemode = nla_get_u32(tb[TCA_PIE_BYTEMODE]);
229
 
 
 
 
230	/* Drop excess packets if new limit is lower */
231	qlen = sch->q.qlen;
232	while (sch->q.qlen > sch->limit) {
233		struct sk_buff *skb = __skb_dequeue(&sch->q);
234
235		dropped += qdisc_pkt_len(skb);
236		qdisc_qstats_backlog_dec(sch, skb);
237		qdisc_drop(skb, sch);
238	}
239	qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
240
241	sch_tree_unlock(sch);
242	return 0;
243}
244
245static void pie_process_dequeue(struct Qdisc *sch, struct sk_buff *skb)
 
246{
 
 
247
248	struct pie_sched_data *q = qdisc_priv(sch);
249	int qlen = sch->qstats.backlog;	/* current queue size in bytes */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
251	/* If current queue is about 10 packets or more and dq_count is unset
252	 * we have enough packets to calculate the drain rate. Save
253	 * current time as dq_tstamp and start measurement cycle.
254	 */
255	if (qlen >= QUEUE_THRESHOLD && q->vars.dq_count == DQCOUNT_INVALID) {
256		q->vars.dq_tstamp = psched_get_time();
257		q->vars.dq_count = 0;
258	}
259
260	/* Calculate the average drain rate from this value.  If queue length
261	 * has receded to a small value viz., <= QUEUE_THRESHOLD bytes,reset
262	 * the dq_count to -1 as we don't have enough packets to calculate the
263	 * drain rate anymore The following if block is entered only when we
264	 * have a substantial queue built up (QUEUE_THRESHOLD bytes or more)
265	 * and we calculate the drain rate for the threshold here.  dq_count is
266	 * in bytes, time difference in psched_time, hence rate is in
267	 * bytes/psched_time.
268	 */
269	if (q->vars.dq_count != DQCOUNT_INVALID) {
270		q->vars.dq_count += skb->len;
271
272		if (q->vars.dq_count >= QUEUE_THRESHOLD) {
273			psched_time_t now = psched_get_time();
274			u32 dtime = now - q->vars.dq_tstamp;
275			u32 count = q->vars.dq_count << PIE_SCALE;
276
277			if (dtime == 0)
278				return;
279
280			count = count / dtime;
281
282			if (q->vars.avg_dq_rate == 0)
283				q->vars.avg_dq_rate = count;
284			else
285				q->vars.avg_dq_rate =
286				    (q->vars.avg_dq_rate -
287				     (q->vars.avg_dq_rate >> 3)) + (count >> 3);
288
289			/* If the queue has receded below the threshold, we hold
290			 * on to the last drain rate calculated, else we reset
291			 * dq_count to 0 to re-enter the if block when the next
292			 * packet is dequeued
293			 */
294			if (qlen < QUEUE_THRESHOLD)
295				q->vars.dq_count = DQCOUNT_INVALID;
296			else {
297				q->vars.dq_count = 0;
298				q->vars.dq_tstamp = psched_get_time();
299			}
300
301			if (q->vars.burst_time > 0) {
302				if (q->vars.burst_time > dtime)
303					q->vars.burst_time -= dtime;
304				else
305					q->vars.burst_time = 0;
306			}
307		}
308	}
 
 
 
 
 
 
 
 
 
 
309}
 
310
311static void calculate_probability(struct Qdisc *sch)
 
312{
313	struct pie_sched_data *q = qdisc_priv(sch);
314	u32 qlen = sch->qstats.backlog;	/* queue size in bytes */
315	psched_time_t qdelay = 0;	/* in pschedtime */
316	psched_time_t qdelay_old = q->vars.qdelay;	/* in pschedtime */
317	s32 delta = 0;		/* determines the change in probability */
318	u32 oldprob;
319	u32 alpha, beta;
 
320	bool update_prob = true;
321
322	q->vars.qdelay_old = q->vars.qdelay;
323
324	if (q->vars.avg_dq_rate > 0)
325		qdelay = (qlen << PIE_SCALE) / q->vars.avg_dq_rate;
326	else
327		qdelay = 0;
 
 
 
 
 
 
328
329	/* If qdelay is zero and qlen is not, it means qlen is very small, less
330	 * than dequeue_rate, so we do not update probabilty in this round
331	 */
332	if (qdelay == 0 && qlen != 0)
333		update_prob = false;
334
335	/* In the algorithm, alpha and beta are between 0 and 2 with typical
336	 * value for alpha as 0.125. In this implementation, we use values 0-32
337	 * passed from user space to represent this. Also, alpha and beta have
338	 * unit of HZ and need to be scaled before they can used to update
339	 * probability. alpha/beta are updated locally below by 1) scaling them
340	 * appropriately 2) scaling down by 16 to come to 0-2 range.
341	 * Please see paper for details.
342	 *
343	 * We scale alpha and beta differently depending on whether we are in
344	 * light, medium or high dropping mode.
345	 */
346	if (q->vars.prob < MAX_PROB / 100) {
347		alpha =
348		    (q->params.alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 7;
349		beta =
350		    (q->params.beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 7;
351	} else if (q->vars.prob < MAX_PROB / 10) {
352		alpha =
353		    (q->params.alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 5;
354		beta =
355		    (q->params.beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 5;
356	} else {
357		alpha =
358		    (q->params.alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
359		beta =
360		    (q->params.beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
361	}
362
363	/* alpha and beta should be between 0 and 32, in multiples of 1/16 */
364	delta += alpha * ((qdelay - q->params.target));
365	delta += beta * ((qdelay - qdelay_old));
366
367	oldprob = q->vars.prob;
368
369	/* to ensure we increase probability in steps of no more than 2% */
370	if (delta > (s32) (MAX_PROB / (100 / 2)) &&
371	    q->vars.prob >= MAX_PROB / 10)
372		delta = (MAX_PROB / 100) * 2;
373
374	/* Non-linear drop:
375	 * Tune drop probability to increase quickly for high delays(>= 250ms)
376	 * 250ms is derived through experiments and provides error protection
377	 */
378
379	if (qdelay > (PSCHED_NS2TICKS(250 * NSEC_PER_MSEC)))
380		delta += MAX_PROB / (100 / 2);
381
382	q->vars.prob += delta;
383
384	if (delta > 0) {
385		/* prevent overflow */
386		if (q->vars.prob < oldprob) {
387			q->vars.prob = MAX_PROB;
388			/* Prevent normalization error. If probability is at
389			 * maximum value already, we normalize it here, and
390			 * skip the check to do a non-linear drop in the next
391			 * section.
392			 */
393			update_prob = false;
394		}
395	} else {
396		/* prevent underflow */
397		if (q->vars.prob > oldprob)
398			q->vars.prob = 0;
399	}
400
401	/* Non-linear drop in probability: Reduce drop probability quickly if
402	 * delay is 0 for 2 consecutive Tupdate periods.
403	 */
404
405	if ((qdelay == 0) && (qdelay_old == 0) && update_prob)
406		q->vars.prob = (q->vars.prob * 98) / 100;
 
407
408	q->vars.qdelay = qdelay;
409	q->vars.qlen_old = qlen;
410
411	/* We restart the measurement cycle if the following conditions are met
412	 * 1. If the delay has been low for 2 consecutive Tupdate periods
413	 * 2. Calculated drop probability is zero
414	 * 3. We have atleast one estimate for the avg_dq_rate ie.,
415	 *    is a non-zero value
416	 */
417	if ((q->vars.qdelay < q->params.target / 2) &&
418	    (q->vars.qdelay_old < q->params.target / 2) &&
419	    (q->vars.prob == 0) &&
420	    (q->vars.avg_dq_rate > 0))
421		pie_vars_init(&q->vars);
 
 
 
 
422}
 
423
424static void pie_timer(unsigned long arg)
425{
426	struct Qdisc *sch = (struct Qdisc *)arg;
427	struct pie_sched_data *q = qdisc_priv(sch);
428	spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch));
429
430	spin_lock(root_lock);
431	calculate_probability(sch);
432
433	/* reset the timer to fire after 'tupdate'. tupdate is in jiffies. */
434	if (q->params.tupdate)
435		mod_timer(&q->adapt_timer, jiffies + q->params.tupdate);
436	spin_unlock(root_lock);
437
438}
439
440static int pie_init(struct Qdisc *sch, struct nlattr *opt)
 
441{
442	struct pie_sched_data *q = qdisc_priv(sch);
443
444	pie_params_init(&q->params);
445	pie_vars_init(&q->vars);
446	sch->limit = q->params.limit;
447
448	setup_timer(&q->adapt_timer, pie_timer, (unsigned long)sch);
 
449
450	if (opt) {
451		int err = pie_change(sch, opt);
452
453		if (err)
454			return err;
455	}
456
457	mod_timer(&q->adapt_timer, jiffies + HZ / 2);
458	return 0;
459}
460
461static int pie_dump(struct Qdisc *sch, struct sk_buff *skb)
462{
463	struct pie_sched_data *q = qdisc_priv(sch);
464	struct nlattr *opts;
465
466	opts = nla_nest_start(skb, TCA_OPTIONS);
467	if (opts == NULL)
468		goto nla_put_failure;
469
470	/* convert target from pschedtime to us */
471	if (nla_put_u32(skb, TCA_PIE_TARGET,
472			((u32) PSCHED_TICKS2NS(q->params.target)) /
473			NSEC_PER_USEC) ||
474	    nla_put_u32(skb, TCA_PIE_LIMIT, sch->limit) ||
475	    nla_put_u32(skb, TCA_PIE_TUPDATE, jiffies_to_usecs(q->params.tupdate)) ||
 
476	    nla_put_u32(skb, TCA_PIE_ALPHA, q->params.alpha) ||
477	    nla_put_u32(skb, TCA_PIE_BETA, q->params.beta) ||
478	    nla_put_u32(skb, TCA_PIE_ECN, q->params.ecn) ||
479	    nla_put_u32(skb, TCA_PIE_BYTEMODE, q->params.bytemode))
 
 
480		goto nla_put_failure;
481
482	return nla_nest_end(skb, opts);
483
484nla_put_failure:
485	nla_nest_cancel(skb, opts);
486	return -1;
487
488}
489
490static int pie_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
491{
492	struct pie_sched_data *q = qdisc_priv(sch);
493	struct tc_pie_xstats st = {
494		.prob		= q->vars.prob,
495		.delay		= ((u32) PSCHED_TICKS2NS(q->vars.qdelay)) /
496				   NSEC_PER_USEC,
497		/* unscale and return dq_rate in bytes per sec */
498		.avg_dq_rate	= q->vars.avg_dq_rate *
499				  (PSCHED_TICKS_PER_SEC) >> PIE_SCALE,
500		.packets_in	= q->stats.packets_in,
501		.overlimit	= q->stats.overlimit,
502		.maxq		= q->stats.maxq,
503		.dropped	= q->stats.dropped,
504		.ecn_mark	= q->stats.ecn_mark,
505	};
506
 
 
 
 
 
 
 
 
507	return gnet_stats_copy_app(d, &st, sizeof(st));
508}
509
510static struct sk_buff *pie_qdisc_dequeue(struct Qdisc *sch)
511{
512	struct sk_buff *skb;
513	skb = __qdisc_dequeue_head(sch, &sch->q);
514
515	if (!skb)
516		return NULL;
517
518	pie_process_dequeue(sch, skb);
519	return skb;
520}
521
522static void pie_reset(struct Qdisc *sch)
523{
524	struct pie_sched_data *q = qdisc_priv(sch);
 
525	qdisc_reset_queue(sch);
526	pie_vars_init(&q->vars);
527}
528
529static void pie_destroy(struct Qdisc *sch)
530{
531	struct pie_sched_data *q = qdisc_priv(sch);
 
532	q->params.tupdate = 0;
533	del_timer_sync(&q->adapt_timer);
534}
535
536static struct Qdisc_ops pie_qdisc_ops __read_mostly = {
537	.id = "pie",
538	.priv_size	= sizeof(struct pie_sched_data),
539	.enqueue	= pie_qdisc_enqueue,
540	.dequeue	= pie_qdisc_dequeue,
541	.peek		= qdisc_peek_dequeued,
542	.init		= pie_init,
543	.destroy	= pie_destroy,
544	.reset		= pie_reset,
545	.change		= pie_change,
546	.dump		= pie_dump,
547	.dump_stats	= pie_dump_stats,
548	.owner		= THIS_MODULE,
549};
550
551static int __init pie_module_init(void)
552{
553	return register_qdisc(&pie_qdisc_ops);
554}
555
556static void __exit pie_module_exit(void)
557{
558	unregister_qdisc(&pie_qdisc_ops);
559}
560
561module_init(pie_module_init);
562module_exit(pie_module_exit);
563
564MODULE_DESCRIPTION("Proportional Integral controller Enhanced (PIE) scheduler");
565MODULE_AUTHOR("Vijay Subramanian");
566MODULE_AUTHOR("Mythili Prabhu");
567MODULE_LICENSE("GPL");
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/* Copyright (C) 2013 Cisco Systems, Inc, 2013.
  3 *
 
 
 
 
 
 
 
 
 
 
  4 * Author: Vijay Subramanian <vijaynsu@cisco.com>
  5 * Author: Mythili Prabhu <mysuryan@cisco.com>
  6 *
  7 * ECN support is added by Naeem Khademi <naeemk@ifi.uio.no>
  8 * University of Oslo, Norway.
  9 *
 10 * References:
 11 * RFC 8033: https://tools.ietf.org/html/rfc8033
 
 
 12 */
 13
 14#include <linux/module.h>
 15#include <linux/slab.h>
 16#include <linux/types.h>
 17#include <linux/kernel.h>
 18#include <linux/errno.h>
 19#include <linux/skbuff.h>
 20#include <net/pkt_sched.h>
 21#include <net/inet_ecn.h>
 22#include <net/pie.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 23
 24/* private data for the Qdisc */
 25struct pie_sched_data {
 
 26	struct pie_vars vars;
 27	struct pie_params params;
 28	struct pie_stats stats;
 29	struct timer_list adapt_timer;
 30	struct Qdisc *sch;
 31};
 32
 33bool pie_drop_early(struct Qdisc *sch, struct pie_params *params,
 34		    struct pie_vars *vars, u32 backlog, u32 packet_size)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 35{
 36	u64 rnd;
 37	u64 local_prob = vars->prob;
 
 38	u32 mtu = psched_mtu(qdisc_dev(sch));
 39
 40	/* If there is still burst allowance left skip random early drop */
 41	if (vars->burst_time > 0)
 42		return false;
 43
 44	/* If current delay is less than half of target, and
 45	 * if drop prob is low already, disable early_drop
 46	 */
 47	if ((vars->qdelay < params->target / 2) &&
 48	    (vars->prob < MAX_PROB / 5))
 49		return false;
 50
 51	/* If we have fewer than 2 mtu-sized packets, disable pie_drop_early,
 52	 * similar to min_th in RED
 53	 */
 54	if (backlog < 2 * mtu)
 55		return false;
 56
 57	/* If bytemode is turned on, use packet size to compute new
 58	 * probablity. Smaller packets will have lower drop prob in this case
 59	 */
 60	if (params->bytemode && packet_size <= mtu)
 61		local_prob = (u64)packet_size * div_u64(local_prob, mtu);
 62	else
 63		local_prob = vars->prob;
 64
 65	if (local_prob == 0)
 66		vars->accu_prob = 0;
 67	else
 68		vars->accu_prob += local_prob;
 69
 70	if (vars->accu_prob < (MAX_PROB / 100) * 85)
 71		return false;
 72	if (vars->accu_prob >= (MAX_PROB / 2) * 17)
 73		return true;
 74
 75	get_random_bytes(&rnd, 8);
 76	if ((rnd >> BITS_PER_BYTE) < local_prob) {
 77		vars->accu_prob = 0;
 78		return true;
 79	}
 80
 81	return false;
 82}
 83EXPORT_SYMBOL_GPL(pie_drop_early);
 84
 85static int pie_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 86			     struct sk_buff **to_free)
 87{
 88	struct pie_sched_data *q = qdisc_priv(sch);
 89	bool enqueue = false;
 90
 91	if (unlikely(qdisc_qlen(sch) >= sch->limit)) {
 92		q->stats.overlimit++;
 93		goto out;
 94	}
 95
 96	if (!pie_drop_early(sch, &q->params, &q->vars, sch->qstats.backlog,
 97			    skb->len)) {
 98		enqueue = true;
 99	} else if (q->params.ecn && (q->vars.prob <= MAX_PROB / 10) &&
100		   INET_ECN_set_ce(skb)) {
101		/* If packet is ecn capable, mark it if drop probability
102		 * is lower than 10%, else drop it.
103		 */
104		q->stats.ecn_mark++;
105		enqueue = true;
106	}
107
108	/* we can enqueue the packet */
109	if (enqueue) {
110		/* Set enqueue time only when dq_rate_estimator is disabled. */
111		if (!q->params.dq_rate_estimator)
112			pie_set_enqueue_time(skb);
113
114		q->stats.packets_in++;
115		if (qdisc_qlen(sch) > q->stats.maxq)
116			q->stats.maxq = qdisc_qlen(sch);
117
118		return qdisc_enqueue_tail(skb, sch);
119	}
120
121out:
122	q->stats.dropped++;
123	q->vars.accu_prob = 0;
124	return qdisc_drop(skb, sch, to_free);
125}
126
127static const struct nla_policy pie_policy[TCA_PIE_MAX + 1] = {
128	[TCA_PIE_TARGET]		= {.type = NLA_U32},
129	[TCA_PIE_LIMIT]			= {.type = NLA_U32},
130	[TCA_PIE_TUPDATE]		= {.type = NLA_U32},
131	[TCA_PIE_ALPHA]			= {.type = NLA_U32},
132	[TCA_PIE_BETA]			= {.type = NLA_U32},
133	[TCA_PIE_ECN]			= {.type = NLA_U32},
134	[TCA_PIE_BYTEMODE]		= {.type = NLA_U32},
135	[TCA_PIE_DQ_RATE_ESTIMATOR]	= {.type = NLA_U32},
136};
137
138static int pie_change(struct Qdisc *sch, struct nlattr *opt,
139		      struct netlink_ext_ack *extack)
140{
141	struct pie_sched_data *q = qdisc_priv(sch);
142	struct nlattr *tb[TCA_PIE_MAX + 1];
143	unsigned int qlen, dropped = 0;
144	int err;
145
146	err = nla_parse_nested_deprecated(tb, TCA_PIE_MAX, opt, pie_policy,
147					  NULL);
 
 
148	if (err < 0)
149		return err;
150
151	sch_tree_lock(sch);
152
153	/* convert from microseconds to pschedtime */
154	if (tb[TCA_PIE_TARGET]) {
155		/* target is in us */
156		u32 target = nla_get_u32(tb[TCA_PIE_TARGET]);
157
158		/* convert to pschedtime */
159		q->params.target = PSCHED_NS2TICKS((u64)target * NSEC_PER_USEC);
160	}
161
162	/* tupdate is in jiffies */
163	if (tb[TCA_PIE_TUPDATE])
164		q->params.tupdate =
165			usecs_to_jiffies(nla_get_u32(tb[TCA_PIE_TUPDATE]));
166
167	if (tb[TCA_PIE_LIMIT]) {
168		u32 limit = nla_get_u32(tb[TCA_PIE_LIMIT]);
169
170		q->params.limit = limit;
171		sch->limit = limit;
172	}
173
174	if (tb[TCA_PIE_ALPHA])
175		q->params.alpha = nla_get_u32(tb[TCA_PIE_ALPHA]);
176
177	if (tb[TCA_PIE_BETA])
178		q->params.beta = nla_get_u32(tb[TCA_PIE_BETA]);
179
180	if (tb[TCA_PIE_ECN])
181		q->params.ecn = nla_get_u32(tb[TCA_PIE_ECN]);
182
183	if (tb[TCA_PIE_BYTEMODE])
184		q->params.bytemode = nla_get_u32(tb[TCA_PIE_BYTEMODE]);
185
186	if (tb[TCA_PIE_DQ_RATE_ESTIMATOR])
187		q->params.dq_rate_estimator =
188				nla_get_u32(tb[TCA_PIE_DQ_RATE_ESTIMATOR]);
189
190	/* Drop excess packets if new limit is lower */
191	qlen = sch->q.qlen;
192	while (sch->q.qlen > sch->limit) {
193		struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
194
195		dropped += qdisc_pkt_len(skb);
196		qdisc_qstats_backlog_dec(sch, skb);
197		rtnl_qdisc_drop(skb, sch);
198	}
199	qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
200
201	sch_tree_unlock(sch);
202	return 0;
203}
204
205void pie_process_dequeue(struct sk_buff *skb, struct pie_params *params,
206			 struct pie_vars *vars, u32 backlog)
207{
208	psched_time_t now = psched_get_time();
209	u32 dtime = 0;
210
211	/* If dq_rate_estimator is disabled, calculate qdelay using the
212	 * packet timestamp.
213	 */
214	if (!params->dq_rate_estimator) {
215		vars->qdelay = now - pie_get_enqueue_time(skb);
216
217		if (vars->dq_tstamp != DTIME_INVALID)
218			dtime = now - vars->dq_tstamp;
219
220		vars->dq_tstamp = now;
221
222		if (backlog == 0)
223			vars->qdelay = 0;
224
225		if (dtime == 0)
226			return;
227
228		goto burst_allowance_reduction;
229	}
230
231	/* If current queue is about 10 packets or more and dq_count is unset
232	 * we have enough packets to calculate the drain rate. Save
233	 * current time as dq_tstamp and start measurement cycle.
234	 */
235	if (backlog >= QUEUE_THRESHOLD && vars->dq_count == DQCOUNT_INVALID) {
236		vars->dq_tstamp = psched_get_time();
237		vars->dq_count = 0;
238	}
239
240	/* Calculate the average drain rate from this value. If queue length
241	 * has receded to a small value viz., <= QUEUE_THRESHOLD bytes, reset
242	 * the dq_count to -1 as we don't have enough packets to calculate the
243	 * drain rate anymore. The following if block is entered only when we
244	 * have a substantial queue built up (QUEUE_THRESHOLD bytes or more)
245	 * and we calculate the drain rate for the threshold here.  dq_count is
246	 * in bytes, time difference in psched_time, hence rate is in
247	 * bytes/psched_time.
248	 */
249	if (vars->dq_count != DQCOUNT_INVALID) {
250		vars->dq_count += skb->len;
251
252		if (vars->dq_count >= QUEUE_THRESHOLD) {
253			u32 count = vars->dq_count << PIE_SCALE;
254
255			dtime = now - vars->dq_tstamp;
256
257			if (dtime == 0)
258				return;
259
260			count = count / dtime;
261
262			if (vars->avg_dq_rate == 0)
263				vars->avg_dq_rate = count;
264			else
265				vars->avg_dq_rate =
266				    (vars->avg_dq_rate -
267				     (vars->avg_dq_rate >> 3)) + (count >> 3);
268
269			/* If the queue has receded below the threshold, we hold
270			 * on to the last drain rate calculated, else we reset
271			 * dq_count to 0 to re-enter the if block when the next
272			 * packet is dequeued
273			 */
274			if (backlog < QUEUE_THRESHOLD) {
275				vars->dq_count = DQCOUNT_INVALID;
276			} else {
277				vars->dq_count = 0;
278				vars->dq_tstamp = psched_get_time();
279			}
280
281			goto burst_allowance_reduction;
 
 
 
 
 
282		}
283	}
284
285	return;
286
287burst_allowance_reduction:
288	if (vars->burst_time > 0) {
289		if (vars->burst_time > dtime)
290			vars->burst_time -= dtime;
291		else
292			vars->burst_time = 0;
293	}
294}
295EXPORT_SYMBOL_GPL(pie_process_dequeue);
296
297void pie_calculate_probability(struct pie_params *params, struct pie_vars *vars,
298			       u32 backlog)
299{
 
 
300	psched_time_t qdelay = 0;	/* in pschedtime */
301	psched_time_t qdelay_old = 0;	/* in pschedtime */
302	s64 delta = 0;		/* determines the change in probability */
303	u64 oldprob;
304	u64 alpha, beta;
305	u32 power;
306	bool update_prob = true;
307
308	if (params->dq_rate_estimator) {
309		qdelay_old = vars->qdelay;
310		vars->qdelay_old = vars->qdelay;
311
312		if (vars->avg_dq_rate > 0)
313			qdelay = (backlog << PIE_SCALE) / vars->avg_dq_rate;
314		else
315			qdelay = 0;
316	} else {
317		qdelay = vars->qdelay;
318		qdelay_old = vars->qdelay_old;
319	}
320
321	/* If qdelay is zero and backlog is not, it means backlog is very small,
322	 * so we do not update probabilty in this round.
323	 */
324	if (qdelay == 0 && backlog != 0)
325		update_prob = false;
326
327	/* In the algorithm, alpha and beta are between 0 and 2 with typical
328	 * value for alpha as 0.125. In this implementation, we use values 0-32
329	 * passed from user space to represent this. Also, alpha and beta have
330	 * unit of HZ and need to be scaled before they can used to update
331	 * probability. alpha/beta are updated locally below by scaling down
332	 * by 16 to come to 0-2 range.
333	 */
334	alpha = ((u64)params->alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
335	beta = ((u64)params->beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
336
337	/* We scale alpha and beta differently depending on how heavy the
338	 * congestion is. Please see RFC 8033 for details.
339	 */
340	if (vars->prob < MAX_PROB / 10) {
341		alpha >>= 1;
342		beta >>= 1;
343
344		power = 100;
345		while (vars->prob < div_u64(MAX_PROB, power) &&
346		       power <= 1000000) {
347			alpha >>= 2;
348			beta >>= 2;
349			power *= 10;
350		}
 
 
351	}
352
353	/* alpha and beta should be between 0 and 32, in multiples of 1/16 */
354	delta += alpha * (qdelay - params->target);
355	delta += beta * (qdelay - qdelay_old);
356
357	oldprob = vars->prob;
358
359	/* to ensure we increase probability in steps of no more than 2% */
360	if (delta > (s64)(MAX_PROB / (100 / 2)) &&
361	    vars->prob >= MAX_PROB / 10)
362		delta = (MAX_PROB / 100) * 2;
363
364	/* Non-linear drop:
365	 * Tune drop probability to increase quickly for high delays(>= 250ms)
366	 * 250ms is derived through experiments and provides error protection
367	 */
368
369	if (qdelay > (PSCHED_NS2TICKS(250 * NSEC_PER_MSEC)))
370		delta += MAX_PROB / (100 / 2);
371
372	vars->prob += delta;
373
374	if (delta > 0) {
375		/* prevent overflow */
376		if (vars->prob < oldprob) {
377			vars->prob = MAX_PROB;
378			/* Prevent normalization error. If probability is at
379			 * maximum value already, we normalize it here, and
380			 * skip the check to do a non-linear drop in the next
381			 * section.
382			 */
383			update_prob = false;
384		}
385	} else {
386		/* prevent underflow */
387		if (vars->prob > oldprob)
388			vars->prob = 0;
389	}
390
391	/* Non-linear drop in probability: Reduce drop probability quickly if
392	 * delay is 0 for 2 consecutive Tupdate periods.
393	 */
394
395	if (qdelay == 0 && qdelay_old == 0 && update_prob)
396		/* Reduce drop probability to 98.4% */
397		vars->prob -= vars->prob / 64;
398
399	vars->qdelay = qdelay;
400	vars->backlog_old = backlog;
401
402	/* We restart the measurement cycle if the following conditions are met
403	 * 1. If the delay has been low for 2 consecutive Tupdate periods
404	 * 2. Calculated drop probability is zero
405	 * 3. If average dq_rate_estimator is enabled, we have at least one
406	 *    estimate for the avg_dq_rate ie., is a non-zero value
407	 */
408	if ((vars->qdelay < params->target / 2) &&
409	    (vars->qdelay_old < params->target / 2) &&
410	    vars->prob == 0 &&
411	    (!params->dq_rate_estimator || vars->avg_dq_rate > 0)) {
412		pie_vars_init(vars);
413	}
414
415	if (!params->dq_rate_estimator)
416		vars->qdelay_old = qdelay;
417}
418EXPORT_SYMBOL_GPL(pie_calculate_probability);
419
420static void pie_timer(struct timer_list *t)
421{
422	struct pie_sched_data *q = from_timer(q, t, adapt_timer);
423	struct Qdisc *sch = q->sch;
424	spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch));
425
426	spin_lock(root_lock);
427	pie_calculate_probability(&q->params, &q->vars, sch->qstats.backlog);
428
429	/* reset the timer to fire after 'tupdate'. tupdate is in jiffies. */
430	if (q->params.tupdate)
431		mod_timer(&q->adapt_timer, jiffies + q->params.tupdate);
432	spin_unlock(root_lock);
 
433}
434
435static int pie_init(struct Qdisc *sch, struct nlattr *opt,
436		    struct netlink_ext_ack *extack)
437{
438	struct pie_sched_data *q = qdisc_priv(sch);
439
440	pie_params_init(&q->params);
441	pie_vars_init(&q->vars);
442	sch->limit = q->params.limit;
443
444	q->sch = sch;
445	timer_setup(&q->adapt_timer, pie_timer, 0);
446
447	if (opt) {
448		int err = pie_change(sch, opt, extack);
449
450		if (err)
451			return err;
452	}
453
454	mod_timer(&q->adapt_timer, jiffies + HZ / 2);
455	return 0;
456}
457
458static int pie_dump(struct Qdisc *sch, struct sk_buff *skb)
459{
460	struct pie_sched_data *q = qdisc_priv(sch);
461	struct nlattr *opts;
462
463	opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
464	if (!opts)
465		goto nla_put_failure;
466
467	/* convert target from pschedtime to us */
468	if (nla_put_u32(skb, TCA_PIE_TARGET,
469			((u32)PSCHED_TICKS2NS(q->params.target)) /
470			NSEC_PER_USEC) ||
471	    nla_put_u32(skb, TCA_PIE_LIMIT, sch->limit) ||
472	    nla_put_u32(skb, TCA_PIE_TUPDATE,
473			jiffies_to_usecs(q->params.tupdate)) ||
474	    nla_put_u32(skb, TCA_PIE_ALPHA, q->params.alpha) ||
475	    nla_put_u32(skb, TCA_PIE_BETA, q->params.beta) ||
476	    nla_put_u32(skb, TCA_PIE_ECN, q->params.ecn) ||
477	    nla_put_u32(skb, TCA_PIE_BYTEMODE, q->params.bytemode) ||
478	    nla_put_u32(skb, TCA_PIE_DQ_RATE_ESTIMATOR,
479			q->params.dq_rate_estimator))
480		goto nla_put_failure;
481
482	return nla_nest_end(skb, opts);
483
484nla_put_failure:
485	nla_nest_cancel(skb, opts);
486	return -1;
 
487}
488
489static int pie_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
490{
491	struct pie_sched_data *q = qdisc_priv(sch);
492	struct tc_pie_xstats st = {
493		.prob		= q->vars.prob << BITS_PER_BYTE,
494		.delay		= ((u32)PSCHED_TICKS2NS(q->vars.qdelay)) /
495				   NSEC_PER_USEC,
 
 
 
496		.packets_in	= q->stats.packets_in,
497		.overlimit	= q->stats.overlimit,
498		.maxq		= q->stats.maxq,
499		.dropped	= q->stats.dropped,
500		.ecn_mark	= q->stats.ecn_mark,
501	};
502
503	/* avg_dq_rate is only valid if dq_rate_estimator is enabled */
504	st.dq_rate_estimating = q->params.dq_rate_estimator;
505
506	/* unscale and return dq_rate in bytes per sec */
507	if (q->params.dq_rate_estimator)
508		st.avg_dq_rate = q->vars.avg_dq_rate *
509				 (PSCHED_TICKS_PER_SEC) >> PIE_SCALE;
510
511	return gnet_stats_copy_app(d, &st, sizeof(st));
512}
513
514static struct sk_buff *pie_qdisc_dequeue(struct Qdisc *sch)
515{
516	struct pie_sched_data *q = qdisc_priv(sch);
517	struct sk_buff *skb = qdisc_dequeue_head(sch);
518
519	if (!skb)
520		return NULL;
521
522	pie_process_dequeue(skb, &q->params, &q->vars, sch->qstats.backlog);
523	return skb;
524}
525
526static void pie_reset(struct Qdisc *sch)
527{
528	struct pie_sched_data *q = qdisc_priv(sch);
529
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
538	q->params.tupdate = 0;
539	del_timer_sync(&q->adapt_timer);
540}
541
542static struct Qdisc_ops pie_qdisc_ops __read_mostly = {
543	.id		= "pie",
544	.priv_size	= sizeof(struct pie_sched_data),
545	.enqueue	= pie_qdisc_enqueue,
546	.dequeue	= pie_qdisc_dequeue,
547	.peek		= qdisc_peek_dequeued,
548	.init		= pie_init,
549	.destroy	= pie_destroy,
550	.reset		= pie_reset,
551	.change		= pie_change,
552	.dump		= pie_dump,
553	.dump_stats	= pie_dump_stats,
554	.owner		= THIS_MODULE,
555};
556
557static int __init pie_module_init(void)
558{
559	return register_qdisc(&pie_qdisc_ops);
560}
561
562static void __exit pie_module_exit(void)
563{
564	unregister_qdisc(&pie_qdisc_ops);
565}
566
567module_init(pie_module_init);
568module_exit(pie_module_exit);
569
570MODULE_DESCRIPTION("Proportional Integral controller Enhanced (PIE) scheduler");
571MODULE_AUTHOR("Vijay Subramanian");
572MODULE_AUTHOR("Mythili Prabhu");
573MODULE_LICENSE("GPL");