Blame view

net/sched/sch_pie.c 15.7 KB
2504ba9f5   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
2
3
  /* Copyright (C) 2013 Cisco Systems, Inc, 2013.
   *
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
4
5
6
7
8
   * Author: Vijay Subramanian <vijaynsu@cisco.com>
   * Author: Mythili Prabhu <mysuryan@cisco.com>
   *
   * ECN support is added by Naeem Khademi <naeemk@ifi.uio.no>
   * University of Oslo, Norway.
219e288e8   Vijay Subramanian   net: sched: Clean...
9
10
   *
   * References:
24ed49002   Leslie Monis   net: sched: pie: ...
11
   * RFC 8033: https://tools.ietf.org/html/rfc8033
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
12
13
14
15
16
17
18
19
20
21
   */
  
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/types.h>
  #include <linux/kernel.h>
  #include <linux/errno.h>
  #include <linux/skbuff.h>
  #include <net/pkt_sched.h>
  #include <net/inet_ecn.h>
575090036   Mohit P. Tahiliani   net: sched: pie: ...
22
  #define QUEUE_THRESHOLD 16384
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
23
  #define DQCOUNT_INVALID -1
3f7ae5f3d   Mohit P. Tahiliani   net: sched: pie: ...
24
  #define MAX_PROB 0xffffffffffffffff
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
25
26
27
28
29
30
31
  #define PIE_SCALE 8
  
  /* parameters used */
  struct pie_params {
  	psched_time_t target;	/* user specified target delay in pschedtime */
  	u32 tupdate;		/* timer frequency (in jiffies) */
  	u32 limit;		/* number of packets that can be enqueued */
219e288e8   Vijay Subramanian   net: sched: Clean...
32
  	u32 alpha;		/* alpha and beta are between 0 and 32 */
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
33
34
35
36
37
38
39
  	u32 beta;		/* and are used for shift relative to 1 */
  	bool ecn;		/* true if ecn is enabled */
  	bool bytemode;		/* to scale drop early prob based on pkt size */
  };
  
  /* variables used */
  struct pie_vars {
3f7ae5f3d   Mohit P. Tahiliani   net: sched: pie: ...
40
  	u64 prob;		/* probability but scaled by u64 limit. */
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
41
42
43
44
45
  	psched_time_t burst_time;
  	psched_time_t qdelay;
  	psched_time_t qdelay_old;
  	u64 dq_count;		/* measured in bytes */
  	psched_time_t dq_tstamp;	/* drain rate */
95400b975   Mohit P. Tahiliani   net: sched: pie: ...
46
  	u64 accu_prob;		/* accumulated drop probability */
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
47
48
  	u32 avg_dq_rate;	/* bytes per pschedtime tick,scaled */
  	u32 qlen_old;		/* in bytes */
95400b975   Mohit P. Tahiliani   net: sched: pie: ...
49
  	u8 accu_prob_overflows;	/* overflows of accu_prob */
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  };
  
  /* statistics gathering */
  struct pie_stats {
  	u32 packets_in;		/* total number of packets enqueued */
  	u32 dropped;		/* packets dropped due to pie_action */
  	u32 overlimit;		/* dropped due to lack of space in queue */
  	u32 maxq;		/* maximum queue size */
  	u32 ecn_mark;		/* packets marked with ECN */
  };
  
  /* private data for the Qdisc */
  struct pie_sched_data {
  	struct pie_params params;
  	struct pie_vars vars;
  	struct pie_stats stats;
  	struct timer_list adapt_timer;
cdeabbb88   Kees Cook   net: sched: Conve...
67
  	struct Qdisc *sch;
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
68
69
70
71
72
73
  };
  
  static void pie_params_init(struct pie_params *params)
  {
  	params->alpha = 2;
  	params->beta = 20;
29daa8553   Mohit P. Tahiliani   net: sched: pie: ...
74
  	params->tupdate = usecs_to_jiffies(15 * USEC_PER_MSEC);	/* 15 ms */
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
75
  	params->limit = 1000;	/* default of 1000 packets */
abde7920d   Mohit P. Tahiliani   net: sched: pie: ...
76
  	params->target = PSCHED_NS2TICKS(15 * NSEC_PER_MSEC);	/* 15 ms */
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
77
78
79
80
81
82
83
  	params->ecn = false;
  	params->bytemode = false;
  }
  
  static void pie_vars_init(struct pie_vars *vars)
  {
  	vars->dq_count = DQCOUNT_INVALID;
95400b975   Mohit P. Tahiliani   net: sched: pie: ...
84
  	vars->accu_prob = 0;
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
85
  	vars->avg_dq_rate = 0;
30a92ad70   Mohit P. Tahiliani   net: sched: pie: ...
86
87
  	/* default of 150 ms in pschedtime */
  	vars->burst_time = PSCHED_NS2TICKS(150 * NSEC_PER_MSEC);
95400b975   Mohit P. Tahiliani   net: sched: pie: ...
88
  	vars->accu_prob_overflows = 0;
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
89
90
91
92
93
  }
  
  static bool drop_early(struct Qdisc *sch, u32 packet_size)
  {
  	struct pie_sched_data *q = qdisc_priv(sch);
3f7ae5f3d   Mohit P. Tahiliani   net: sched: pie: ...
94
95
  	u64 rnd;
  	u64 local_prob = q->vars.prob;
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
96
97
98
99
100
101
102
103
104
  	u32 mtu = psched_mtu(qdisc_dev(sch));
  
  	/* If there is still burst allowance left skip random early drop */
  	if (q->vars.burst_time > 0)
  		return false;
  
  	/* If current delay is less than half of target, and
  	 * if drop prob is low already, disable early_drop
  	 */
ac4a02c5a   Leslie Monis   net: sched: pie: ...
105
106
  	if ((q->vars.qdelay < q->params.target / 2) &&
  	    (q->vars.prob < MAX_PROB / 5))
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
107
108
109
110
111
112
113
114
115
116
117
118
  		return false;
  
  	/* If we have fewer than 2 mtu-sized packets, disable drop_early,
  	 * similar to min_th in RED
  	 */
  	if (sch->qstats.backlog < 2 * mtu)
  		return false;
  
  	/* If bytemode is turned on, use packet size to compute new
  	 * probablity. Smaller packets will have lower drop prob in this case
  	 */
  	if (q->params.bytemode && packet_size <= mtu)
3f7ae5f3d   Mohit P. Tahiliani   net: sched: pie: ...
119
  		local_prob = (u64)packet_size * div_u64(local_prob, mtu);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
120
121
  	else
  		local_prob = q->vars.prob;
95400b975   Mohit P. Tahiliani   net: sched: pie: ...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  	if (local_prob == 0) {
  		q->vars.accu_prob = 0;
  		q->vars.accu_prob_overflows = 0;
  	}
  
  	if (local_prob > MAX_PROB - q->vars.accu_prob)
  		q->vars.accu_prob_overflows++;
  
  	q->vars.accu_prob += local_prob;
  
  	if (q->vars.accu_prob_overflows == 0 &&
  	    q->vars.accu_prob < (MAX_PROB / 100) * 85)
  		return false;
  	if (q->vars.accu_prob_overflows == 8 &&
  	    q->vars.accu_prob >= MAX_PROB / 2)
  		return true;
3f7ae5f3d   Mohit P. Tahiliani   net: sched: pie: ...
138
  	prandom_bytes(&rnd, 8);
95400b975   Mohit P. Tahiliani   net: sched: pie: ...
139
140
141
  	if (rnd < local_prob) {
  		q->vars.accu_prob = 0;
  		q->vars.accu_prob_overflows = 0;
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
142
  		return true;
95400b975   Mohit P. Tahiliani   net: sched: pie: ...
143
  	}
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
144
145
146
  
  	return false;
  }
520ac30f4   Eric Dumazet   net_sched: drop p...
147
148
  static int pie_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  			     struct sk_buff **to_free)
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  {
  	struct pie_sched_data *q = qdisc_priv(sch);
  	bool enqueue = false;
  
  	if (unlikely(qdisc_qlen(sch) >= sch->limit)) {
  		q->stats.overlimit++;
  		goto out;
  	}
  
  	if (!drop_early(sch, skb->len)) {
  		enqueue = true;
  	} else if (q->params.ecn && (q->vars.prob <= MAX_PROB / 10) &&
  		   INET_ECN_set_ce(skb)) {
  		/* If packet is ecn capable, mark it if drop probability
  		 * is lower than 10%, else drop it.
  		 */
  		q->stats.ecn_mark++;
  		enqueue = true;
  	}
  
  	/* we can enqueue the packet */
  	if (enqueue) {
  		q->stats.packets_in++;
  		if (qdisc_qlen(sch) > q->stats.maxq)
  			q->stats.maxq = qdisc_qlen(sch);
  
  		return qdisc_enqueue_tail(skb, sch);
  	}
  
  out:
  	q->stats.dropped++;
95400b975   Mohit P. Tahiliani   net: sched: pie: ...
180
181
  	q->vars.accu_prob = 0;
  	q->vars.accu_prob_overflows = 0;
520ac30f4   Eric Dumazet   net_sched: drop p...
182
  	return qdisc_drop(skb, sch, to_free);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
183
184
185
186
187
188
189
190
191
192
193
  }
  
  static const struct nla_policy pie_policy[TCA_PIE_MAX + 1] = {
  	[TCA_PIE_TARGET] = {.type = NLA_U32},
  	[TCA_PIE_LIMIT] = {.type = NLA_U32},
  	[TCA_PIE_TUPDATE] = {.type = NLA_U32},
  	[TCA_PIE_ALPHA] = {.type = NLA_U32},
  	[TCA_PIE_BETA] = {.type = NLA_U32},
  	[TCA_PIE_ECN] = {.type = NLA_U32},
  	[TCA_PIE_BYTEMODE] = {.type = NLA_U32},
  };
2030721cc   Alexander Aring   net: sched: sch: ...
194
195
  static int pie_change(struct Qdisc *sch, struct nlattr *opt,
  		      struct netlink_ext_ack *extack)
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
196
197
198
  {
  	struct pie_sched_data *q = qdisc_priv(sch);
  	struct nlattr *tb[TCA_PIE_MAX + 1];
2ccccf5fb   WANG Cong   net_sched: update...
199
  	unsigned int qlen, dropped = 0;
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
200
201
202
203
  	int err;
  
  	if (!opt)
  		return -EINVAL;
8cb081746   Johannes Berg   netlink: make val...
204
205
  	err = nla_parse_nested_deprecated(tb, TCA_PIE_MAX, opt, pie_policy,
  					  NULL);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  	if (err < 0)
  		return err;
  
  	sch_tree_lock(sch);
  
  	/* convert from microseconds to pschedtime */
  	if (tb[TCA_PIE_TARGET]) {
  		/* target is in us */
  		u32 target = nla_get_u32(tb[TCA_PIE_TARGET]);
  
  		/* convert to pschedtime */
  		q->params.target = PSCHED_NS2TICKS((u64)target * NSEC_PER_USEC);
  	}
  
  	/* tupdate is in jiffies */
  	if (tb[TCA_PIE_TUPDATE])
ac4a02c5a   Leslie Monis   net: sched: pie: ...
222
223
  		q->params.tupdate =
  			usecs_to_jiffies(nla_get_u32(tb[TCA_PIE_TUPDATE]));
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  
  	if (tb[TCA_PIE_LIMIT]) {
  		u32 limit = nla_get_u32(tb[TCA_PIE_LIMIT]);
  
  		q->params.limit = limit;
  		sch->limit = limit;
  	}
  
  	if (tb[TCA_PIE_ALPHA])
  		q->params.alpha = nla_get_u32(tb[TCA_PIE_ALPHA]);
  
  	if (tb[TCA_PIE_BETA])
  		q->params.beta = nla_get_u32(tb[TCA_PIE_BETA]);
  
  	if (tb[TCA_PIE_ECN])
  		q->params.ecn = nla_get_u32(tb[TCA_PIE_ECN]);
  
  	if (tb[TCA_PIE_BYTEMODE])
  		q->params.bytemode = nla_get_u32(tb[TCA_PIE_BYTEMODE]);
  
  	/* Drop excess packets if new limit is lower */
  	qlen = sch->q.qlen;
  	while (sch->q.qlen > sch->limit) {
ed760cb8a   Florian Westphal   sched: replace __...
247
  		struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
248

2ccccf5fb   WANG Cong   net_sched: update...
249
  		dropped += qdisc_pkt_len(skb);
25331d6ce   John Fastabend   net: sched: imple...
250
  		qdisc_qstats_backlog_dec(sch, skb);
db4879d93   Eric Dumazet   net_sched: sch_pi...
251
  		rtnl_qdisc_drop(skb, sch);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
252
  	}
2ccccf5fb   WANG Cong   net_sched: update...
253
  	qdisc_tree_reduce_backlog(sch, qlen - sch->q.qlen, dropped);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
254
255
256
257
258
259
260
  
  	sch_tree_unlock(sch);
  	return 0;
  }
  
  static void pie_process_dequeue(struct Qdisc *sch, struct sk_buff *skb)
  {
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  	struct pie_sched_data *q = qdisc_priv(sch);
  	int qlen = sch->qstats.backlog;	/* current queue size in bytes */
  
  	/* If current queue is about 10 packets or more and dq_count is unset
  	 * we have enough packets to calculate the drain rate. Save
  	 * current time as dq_tstamp and start measurement cycle.
  	 */
  	if (qlen >= QUEUE_THRESHOLD && q->vars.dq_count == DQCOUNT_INVALID) {
  		q->vars.dq_tstamp = psched_get_time();
  		q->vars.dq_count = 0;
  	}
  
  	/* Calculate the average drain rate from this value.  If queue length
  	 * has receded to a small value viz., <= QUEUE_THRESHOLD bytes,reset
  	 * the dq_count to -1 as we don't have enough packets to calculate the
  	 * drain rate anymore The following if block is entered only when we
  	 * have a substantial queue built up (QUEUE_THRESHOLD bytes or more)
  	 * and we calculate the drain rate for the threshold here.  dq_count is
  	 * in bytes, time difference in psched_time, hence rate is in
  	 * bytes/psched_time.
  	 */
  	if (q->vars.dq_count != DQCOUNT_INVALID) {
  		q->vars.dq_count += skb->len;
  
  		if (q->vars.dq_count >= QUEUE_THRESHOLD) {
  			psched_time_t now = psched_get_time();
  			u32 dtime = now - q->vars.dq_tstamp;
  			u32 count = q->vars.dq_count << PIE_SCALE;
  
  			if (dtime == 0)
  				return;
  
  			count = count / dtime;
  
  			if (q->vars.avg_dq_rate == 0)
  				q->vars.avg_dq_rate = count;
  			else
  				q->vars.avg_dq_rate =
  				    (q->vars.avg_dq_rate -
  				     (q->vars.avg_dq_rate >> 3)) + (count >> 3);
  
  			/* If the queue has receded below the threshold, we hold
  			 * on to the last drain rate calculated, else we reset
  			 * dq_count to 0 to re-enter the if block when the next
  			 * packet is dequeued
  			 */
ac4a02c5a   Leslie Monis   net: sched: pie: ...
307
  			if (qlen < QUEUE_THRESHOLD) {
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
308
  				q->vars.dq_count = DQCOUNT_INVALID;
ac4a02c5a   Leslie Monis   net: sched: pie: ...
309
  			} else {
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  				q->vars.dq_count = 0;
  				q->vars.dq_tstamp = psched_get_time();
  			}
  
  			if (q->vars.burst_time > 0) {
  				if (q->vars.burst_time > dtime)
  					q->vars.burst_time -= dtime;
  				else
  					q->vars.burst_time = 0;
  			}
  		}
  	}
  }
  
  static void calculate_probability(struct Qdisc *sch)
  {
  	struct pie_sched_data *q = qdisc_priv(sch);
  	u32 qlen = sch->qstats.backlog;	/* queue size in bytes */
  	psched_time_t qdelay = 0;	/* in pschedtime */
  	psched_time_t qdelay_old = q->vars.qdelay;	/* in pschedtime */
3f7ae5f3d   Mohit P. Tahiliani   net: sched: pie: ...
330
331
332
333
  	s64 delta = 0;		/* determines the change in probability */
  	u64 oldprob;
  	u64 alpha, beta;
  	u32 power;
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
334
335
336
337
338
339
340
341
342
343
344
345
346
347
  	bool update_prob = true;
  
  	q->vars.qdelay_old = q->vars.qdelay;
  
  	if (q->vars.avg_dq_rate > 0)
  		qdelay = (qlen << PIE_SCALE) / q->vars.avg_dq_rate;
  	else
  		qdelay = 0;
  
  	/* If qdelay is zero and qlen is not, it means qlen is very small, less
  	 * than dequeue_rate, so we do not update probabilty in this round
  	 */
  	if (qdelay == 0 && qlen != 0)
  		update_prob = false;
219e288e8   Vijay Subramanian   net: sched: Clean...
348
349
350
351
  	/* In the algorithm, alpha and beta are between 0 and 2 with typical
  	 * value for alpha as 0.125. In this implementation, we use values 0-32
  	 * passed from user space to represent this. Also, alpha and beta have
  	 * unit of HZ and need to be scaled before they can used to update
3f7ae5f3d   Mohit P. Tahiliani   net: sched: pie: ...
352
353
  	 * probability. alpha/beta are updated locally below by scaling down
  	 * by 16 to come to 0-2 range.
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
354
  	 */
3f7ae5f3d   Mohit P. Tahiliani   net: sched: pie: ...
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  	alpha = ((u64)q->params.alpha * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
  	beta = ((u64)q->params.beta * (MAX_PROB / PSCHED_TICKS_PER_SEC)) >> 4;
  
  	/* We scale alpha and beta differently depending on how heavy the
  	 * congestion is. Please see RFC 8033 for details.
  	 */
  	if (q->vars.prob < MAX_PROB / 10) {
  		alpha >>= 1;
  		beta >>= 1;
  
  		power = 100;
  		while (q->vars.prob < div_u64(MAX_PROB, power) &&
  		       power <= 1000000) {
  			alpha >>= 2;
  			beta >>= 2;
  			power *= 10;
  		}
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
372
373
374
  	}
  
  	/* alpha and beta should be between 0 and 32, in multiples of 1/16 */
3f7ae5f3d   Mohit P. Tahiliani   net: sched: pie: ...
375
376
  	delta += alpha * (u64)(qdelay - q->params.target);
  	delta += beta * (u64)(qdelay - qdelay_old);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
377
378
379
380
  
  	oldprob = q->vars.prob;
  
  	/* to ensure we increase probability in steps of no more than 2% */
3f7ae5f3d   Mohit P. Tahiliani   net: sched: pie: ...
381
  	if (delta > (s64)(MAX_PROB / (100 / 2)) &&
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
  	    q->vars.prob >= MAX_PROB / 10)
  		delta = (MAX_PROB / 100) * 2;
  
  	/* Non-linear drop:
  	 * Tune drop probability to increase quickly for high delays(>= 250ms)
  	 * 250ms is derived through experiments and provides error protection
  	 */
  
  	if (qdelay > (PSCHED_NS2TICKS(250 * NSEC_PER_MSEC)))
  		delta += MAX_PROB / (100 / 2);
  
  	q->vars.prob += delta;
  
  	if (delta > 0) {
  		/* prevent overflow */
  		if (q->vars.prob < oldprob) {
  			q->vars.prob = MAX_PROB;
  			/* Prevent normalization error. If probability is at
  			 * maximum value already, we normalize it here, and
  			 * skip the check to do a non-linear drop in the next
  			 * section.
  			 */
  			update_prob = false;
  		}
  	} else {
  		/* prevent underflow */
  		if (q->vars.prob > oldprob)
  			q->vars.prob = 0;
  	}
  
  	/* Non-linear drop in probability: Reduce drop probability quickly if
  	 * delay is 0 for 2 consecutive Tupdate periods.
  	 */
ac4a02c5a   Leslie Monis   net: sched: pie: ...
415
  	if (qdelay == 0 && qdelay_old == 0 && update_prob)
6c97da141   Leslie Monis   net: sched: pie: ...
416
417
  		/* Reduce drop probability to 98.4% */
  		q->vars.prob -= q->vars.prob / 64u;
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
418
419
420
421
422
423
424
425
426
427
428
429
  
  	q->vars.qdelay = qdelay;
  	q->vars.qlen_old = qlen;
  
  	/* We restart the measurement cycle if the following conditions are met
  	 * 1. If the delay has been low for 2 consecutive Tupdate periods
  	 * 2. Calculated drop probability is zero
  	 * 3. We have atleast one estimate for the avg_dq_rate ie.,
  	 *    is a non-zero value
  	 */
  	if ((q->vars.qdelay < q->params.target / 2) &&
  	    (q->vars.qdelay_old < q->params.target / 2) &&
ac4a02c5a   Leslie Monis   net: sched: pie: ...
430
431
  	    q->vars.prob == 0 &&
  	    q->vars.avg_dq_rate > 0)
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
432
433
  		pie_vars_init(&q->vars);
  }
cdeabbb88   Kees Cook   net: sched: Conve...
434
  static void pie_timer(struct timer_list *t)
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
435
  {
cdeabbb88   Kees Cook   net: sched: Conve...
436
437
  	struct pie_sched_data *q = from_timer(q, t, adapt_timer);
  	struct Qdisc *sch = q->sch;
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
438
439
440
441
442
443
444
445
446
  	spinlock_t *root_lock = qdisc_lock(qdisc_root_sleeping(sch));
  
  	spin_lock(root_lock);
  	calculate_probability(sch);
  
  	/* reset the timer to fire after 'tupdate'. tupdate is in jiffies. */
  	if (q->params.tupdate)
  		mod_timer(&q->adapt_timer, jiffies + q->params.tupdate);
  	spin_unlock(root_lock);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
447
  }
e63d7dfd2   Alexander Aring   net: sched: sch: ...
448
449
  static int pie_init(struct Qdisc *sch, struct nlattr *opt,
  		    struct netlink_ext_ack *extack)
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
450
451
452
453
454
455
  {
  	struct pie_sched_data *q = qdisc_priv(sch);
  
  	pie_params_init(&q->params);
  	pie_vars_init(&q->vars);
  	sch->limit = q->params.limit;
cdeabbb88   Kees Cook   net: sched: Conve...
456
457
  	q->sch = sch;
  	timer_setup(&q->adapt_timer, pie_timer, 0);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
458
459
  
  	if (opt) {
2030721cc   Alexander Aring   net: sched: sch: ...
460
  		int err = pie_change(sch, opt, extack);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
461
462
463
464
  
  		if (err)
  			return err;
  	}
d56109020   WANG Cong   sch_pie: schedule...
465
  	mod_timer(&q->adapt_timer, jiffies + HZ / 2);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
466
467
468
469
470
471
472
  	return 0;
  }
  
  static int pie_dump(struct Qdisc *sch, struct sk_buff *skb)
  {
  	struct pie_sched_data *q = qdisc_priv(sch);
  	struct nlattr *opts;
ae0be8de9   Michal Kubecek   netlink: make nla...
473
  	opts = nla_nest_start_noflag(skb, TCA_OPTIONS);
ac4a02c5a   Leslie Monis   net: sched: pie: ...
474
  	if (!opts)
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
475
476
477
478
  		goto nla_put_failure;
  
  	/* convert target from pschedtime to us */
  	if (nla_put_u32(skb, TCA_PIE_TARGET,
ac4a02c5a   Leslie Monis   net: sched: pie: ...
479
  			((u32)PSCHED_TICKS2NS(q->params.target)) /
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
480
481
  			NSEC_PER_USEC) ||
  	    nla_put_u32(skb, TCA_PIE_LIMIT, sch->limit) ||
ac4a02c5a   Leslie Monis   net: sched: pie: ...
482
483
  	    nla_put_u32(skb, TCA_PIE_TUPDATE,
  			jiffies_to_usecs(q->params.tupdate)) ||
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
484
485
486
487
488
489
490
491
492
493
494
  	    nla_put_u32(skb, TCA_PIE_ALPHA, q->params.alpha) ||
  	    nla_put_u32(skb, TCA_PIE_BETA, q->params.beta) ||
  	    nla_put_u32(skb, TCA_PIE_ECN, q->params.ecn) ||
  	    nla_put_u32(skb, TCA_PIE_BYTEMODE, q->params.bytemode))
  		goto nla_put_failure;
  
  	return nla_nest_end(skb, opts);
  
  nla_put_failure:
  	nla_nest_cancel(skb, opts);
  	return -1;
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
495
496
497
498
499
500
501
  }
  
  static int pie_dump_stats(struct Qdisc *sch, struct gnet_dump *d)
  {
  	struct pie_sched_data *q = qdisc_priv(sch);
  	struct tc_pie_xstats st = {
  		.prob		= q->vars.prob,
ac4a02c5a   Leslie Monis   net: sched: pie: ...
502
  		.delay		= ((u32)PSCHED_TICKS2NS(q->vars.qdelay)) /
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
  				   NSEC_PER_USEC,
  		/* unscale and return dq_rate in bytes per sec */
  		.avg_dq_rate	= q->vars.avg_dq_rate *
  				  (PSCHED_TICKS_PER_SEC) >> PIE_SCALE,
  		.packets_in	= q->stats.packets_in,
  		.overlimit	= q->stats.overlimit,
  		.maxq		= q->stats.maxq,
  		.dropped	= q->stats.dropped,
  		.ecn_mark	= q->stats.ecn_mark,
  	};
  
  	return gnet_stats_copy_app(d, &st, sizeof(st));
  }
  
  static struct sk_buff *pie_qdisc_dequeue(struct Qdisc *sch)
  {
ac4a02c5a   Leslie Monis   net: sched: pie: ...
519
  	struct sk_buff *skb = qdisc_dequeue_head(sch);
d4b36210c   Vijay Subramanian   net: pkt_sched: P...
520
521
522
523
524
525
526
527
528
529
530
  
  	if (!skb)
  		return NULL;
  
  	pie_process_dequeue(sch, skb);
  	return skb;
  }
  
  static void pie_reset(struct Qdisc *sch)
  {
  	struct pie_sched_data *q = qdisc_priv(sch);
ac4a02c5a   Leslie Monis   net: sched: pie: ...
531

d4b36210c   Vijay Subramanian   net: pkt_sched: P...
532
533
534
535
536
537
538
  	qdisc_reset_queue(sch);
  	pie_vars_init(&q->vars);
  }
  
  static void pie_destroy(struct Qdisc *sch)
  {
  	struct pie_sched_data *q = qdisc_priv(sch);
ac4a02c5a   Leslie Monis   net: sched: pie: ...
539

d4b36210c   Vijay Subramanian   net: pkt_sched: P...
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  	q->params.tupdate = 0;
  	del_timer_sync(&q->adapt_timer);
  }
  
  static struct Qdisc_ops pie_qdisc_ops __read_mostly = {
  	.id = "pie",
  	.priv_size	= sizeof(struct pie_sched_data),
  	.enqueue	= pie_qdisc_enqueue,
  	.dequeue	= pie_qdisc_dequeue,
  	.peek		= qdisc_peek_dequeued,
  	.init		= pie_init,
  	.destroy	= pie_destroy,
  	.reset		= pie_reset,
  	.change		= pie_change,
  	.dump		= pie_dump,
  	.dump_stats	= pie_dump_stats,
  	.owner		= THIS_MODULE,
  };
  
  static int __init pie_module_init(void)
  {
  	return register_qdisc(&pie_qdisc_ops);
  }
  
  static void __exit pie_module_exit(void)
  {
  	unregister_qdisc(&pie_qdisc_ops);
  }
  
  module_init(pie_module_init);
  module_exit(pie_module_exit);
  
  MODULE_DESCRIPTION("Proportional Integral controller Enhanced (PIE) scheduler");
  MODULE_AUTHOR("Vijay Subramanian");
  MODULE_AUTHOR("Mythili Prabhu");
  MODULE_LICENSE("GPL");