blob: e19d4ebfea1c7294474ef413d6cb0ca490238997 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/sch_gred.c Generic Random Early Detection queue.
3 *
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Authors: J Hadi Salim (hadi@cyberus.ca) 1998-2002
11 *
12 * 991129: - Bug fix with grio mode
13 * - a better sing. AvgQ mode with Grio(WRED)
14 * - A finer grained VQ dequeue based on sugestion
15 * from Ren Liu
16 * - More error checks
17 *
Thomas Graf1e4dfaf92005-11-05 21:14:25 +010018 * For all the glorious comments look at include/net/red.h
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
24#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/pkt_sched.h>
Thomas Graf22b33422005-11-05 21:14:16 +010027#include <net/red.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Thomas Graff62d6b92005-11-05 21:14:15 +010029#define GRED_DEF_PRIO (MAX_DPs / 2)
Thomas Graf716a1b42005-11-05 21:14:20 +010030#define GRED_VQ_MASK (MAX_DPs - 1)
Thomas Graff62d6b92005-11-05 21:14:15 +010031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct gred_sched_data;
33struct gred_sched;
34
Eric Dumazetcc7ec452011-01-19 19:26:56 +000035struct gred_sched_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 u32 limit; /* HARD maximal queue length */
Eric Dumazeta73ed262011-12-09 02:46:45 +000037 u32 DP; /* the drop parameters */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 u32 bytesin; /* bytes seen on virtualQ so far*/
39 u32 packetsin; /* packets seen on virtualQ so far*/
40 u32 backlog; /* bytes on the virtualQ */
Thomas Graf1e4dfaf92005-11-05 21:14:25 +010041 u8 prio; /* the prio of this vq */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Thomas Graf22b33422005-11-05 21:14:16 +010043 struct red_parms parms;
Eric Dumazeteeca6682012-01-05 02:25:16 +000044 struct red_vars vars;
Thomas Graf22b33422005-11-05 21:14:16 +010045 struct red_stats stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Thomas Grafdea3f622005-11-05 21:14:09 +010048enum {
49 GRED_WRED_MODE = 1,
Thomas Grafd6fd4e92005-11-05 21:14:10 +010050 GRED_RIO_MODE,
Thomas Grafdea3f622005-11-05 21:14:09 +010051};
52
Eric Dumazetcc7ec452011-01-19 19:26:56 +000053struct gred_sched {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct gred_sched_data *tab[MAX_DPs];
Thomas Grafdea3f622005-11-05 21:14:09 +010055 unsigned long flags;
Thomas Grafb38c7ee2005-11-05 21:14:27 +010056 u32 red_flags;
Thomas Graf1e4dfaf92005-11-05 21:14:25 +010057 u32 DPs;
58 u32 def;
Eric Dumazeteeca6682012-01-05 02:25:16 +000059 struct red_vars wred_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Thomas Grafdea3f622005-11-05 21:14:09 +010062static inline int gred_wred_mode(struct gred_sched *table)
63{
64 return test_bit(GRED_WRED_MODE, &table->flags);
65}
66
67static inline void gred_enable_wred_mode(struct gred_sched *table)
68{
69 __set_bit(GRED_WRED_MODE, &table->flags);
70}
71
72static inline void gred_disable_wred_mode(struct gred_sched *table)
73{
74 __clear_bit(GRED_WRED_MODE, &table->flags);
75}
76
Thomas Grafd6fd4e92005-11-05 21:14:10 +010077static inline int gred_rio_mode(struct gred_sched *table)
78{
79 return test_bit(GRED_RIO_MODE, &table->flags);
80}
81
82static inline void gred_enable_rio_mode(struct gred_sched *table)
83{
84 __set_bit(GRED_RIO_MODE, &table->flags);
85}
86
87static inline void gred_disable_rio_mode(struct gred_sched *table)
88{
89 __clear_bit(GRED_RIO_MODE, &table->flags);
90}
91
Thomas Grafdea3f622005-11-05 21:14:09 +010092static inline int gred_wred_mode_check(struct Qdisc *sch)
93{
94 struct gred_sched *table = qdisc_priv(sch);
95 int i;
96
97 /* Really ugly O(n^2) but shouldn't be necessary too frequent. */
98 for (i = 0; i < table->DPs; i++) {
99 struct gred_sched_data *q = table->tab[i];
100 int n;
101
102 if (q == NULL)
103 continue;
104
David Wardc22e4642012-09-13 05:22:33 +0000105 for (n = i + 1; n < table->DPs; n++)
106 if (table->tab[n] && table->tab[n]->prio == q->prio)
Thomas Grafdea3f622005-11-05 21:14:09 +0100107 return 1;
108 }
109
110 return 0;
111}
112
Thomas Graf22b33422005-11-05 21:14:16 +0100113static inline unsigned int gred_backlog(struct gred_sched *table,
114 struct gred_sched_data *q,
115 struct Qdisc *sch)
116{
117 if (gred_wred_mode(table))
118 return sch->qstats.backlog;
119 else
120 return q->backlog;
121}
122
Thomas Graf716a1b42005-11-05 21:14:20 +0100123static inline u16 tc_index_to_dp(struct sk_buff *skb)
124{
125 return skb->tc_index & GRED_VQ_MASK;
126}
127
Eric Dumazeteeca6682012-01-05 02:25:16 +0000128static inline void gred_load_wred_set(const struct gred_sched *table,
Thomas Graf70517032005-11-05 21:14:23 +0100129 struct gred_sched_data *q)
130{
Eric Dumazeteeca6682012-01-05 02:25:16 +0000131 q->vars.qavg = table->wred_set.qavg;
132 q->vars.qidlestart = table->wred_set.qidlestart;
Thomas Graf70517032005-11-05 21:14:23 +0100133}
134
135static inline void gred_store_wred_set(struct gred_sched *table,
136 struct gred_sched_data *q)
137{
Eric Dumazeteeca6682012-01-05 02:25:16 +0000138 table->wred_set.qavg = q->vars.qavg;
Thomas Graf70517032005-11-05 21:14:23 +0100139}
140
Thomas Grafb38c7ee2005-11-05 21:14:27 +0100141static inline int gred_use_ecn(struct gred_sched *t)
142{
143 return t->red_flags & TC_RED_ECN;
144}
145
Thomas Grafbdc450a2005-11-05 21:14:28 +0100146static inline int gred_use_harddrop(struct gred_sched *t)
147{
148 return t->red_flags & TC_RED_HARDDROP;
149}
150
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000151static int gred_enqueue(struct sk_buff *skb, struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000153 struct gred_sched_data *q = NULL;
154 struct gred_sched *t = qdisc_priv(sch);
Thomas Graf22b33422005-11-05 21:14:16 +0100155 unsigned long qavg = 0;
Thomas Graf4a591832005-11-05 21:14:22 +0100156 u16 dp = tc_index_to_dp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000158 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
Thomas Graf18e3fb842005-11-05 21:14:21 +0100159 dp = t->def;
160
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000161 q = t->tab[dp];
162 if (!q) {
Thomas Graf18e3fb842005-11-05 21:14:21 +0100163 /* Pass through packets not assigned to a DP
164 * if no default DP has been configured. This
165 * allows for DP flows to be left untouched.
166 */
David S. Miller5ce2d482008-07-08 17:06:30 -0700167 if (skb_queue_len(&sch->q) < qdisc_dev(sch)->tx_queue_len)
Thomas Graf18e3fb842005-11-05 21:14:21 +0100168 return qdisc_enqueue_tail(skb, sch);
169 else
170 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Thomas Graf18e3fb842005-11-05 21:14:21 +0100172
Eric Dumazeteeca6682012-01-05 02:25:16 +0000173 /* fix tc_index? --could be controversial but needed for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 requeueing */
Thomas Graf18e3fb842005-11-05 21:14:21 +0100175 skb->tc_index = (skb->tc_index & ~GRED_VQ_MASK) | dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
David Warde29fe832012-09-13 05:22:32 +0000178 /* sum up all the qaves of prios < ours to get the new qave */
Thomas Grafd6fd4e92005-11-05 21:14:10 +0100179 if (!gred_wred_mode(t) && gred_rio_mode(t)) {
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100180 int i;
181
182 for (i = 0; i < t->DPs; i++) {
183 if (t->tab[i] && t->tab[i]->prio < q->prio &&
Eric Dumazeteeca6682012-01-05 02:25:16 +0000184 !red_is_idling(&t->tab[i]->vars))
185 qavg += t->tab[i]->vars.qavg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
190 q->packetsin++;
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700191 q->bytesin += qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100193 if (gred_wred_mode(t))
Thomas Graf70517032005-11-05 21:14:23 +0100194 gred_load_wred_set(t, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Eric Dumazeteeca6682012-01-05 02:25:16 +0000196 q->vars.qavg = red_calc_qavg(&q->parms,
197 &q->vars,
198 gred_backlog(t, q, sch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Eric Dumazeteeca6682012-01-05 02:25:16 +0000200 if (red_is_idling(&q->vars))
201 red_end_of_idle_period(&q->vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Thomas Grafdea3f622005-11-05 21:14:09 +0100203 if (gred_wred_mode(t))
Thomas Graf70517032005-11-05 21:14:23 +0100204 gred_store_wred_set(t, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Eric Dumazeteeca6682012-01-05 02:25:16 +0000206 switch (red_action(&q->parms, &q->vars, q->vars.qavg + qavg)) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000207 case RED_DONT_MARK:
208 break;
Thomas Graf22b33422005-11-05 21:14:16 +0100209
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000210 case RED_PROB_MARK:
211 sch->qstats.overlimits++;
212 if (!gred_use_ecn(t) || !INET_ECN_set_ce(skb)) {
213 q->stats.prob_drop++;
214 goto congestion_drop;
215 }
Thomas Grafb38c7ee2005-11-05 21:14:27 +0100216
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000217 q->stats.prob_mark++;
218 break;
Thomas Graf22b33422005-11-05 21:14:16 +0100219
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000220 case RED_HARD_MARK:
221 sch->qstats.overlimits++;
222 if (gred_use_harddrop(t) || !gred_use_ecn(t) ||
223 !INET_ECN_set_ce(skb)) {
224 q->stats.forced_drop++;
225 goto congestion_drop;
226 }
227 q->stats.forced_mark++;
228 break;
Thomas Graf22b33422005-11-05 21:14:16 +0100229 }
230
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700231 if (q->backlog + qdisc_pkt_len(skb) <= q->limit) {
232 q->backlog += qdisc_pkt_len(skb);
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100233 return qdisc_enqueue_tail(skb, sch);
Thomas Graf22b33422005-11-05 21:14:16 +0100234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Thomas Graf22b33422005-11-05 21:14:16 +0100236 q->stats.pdrop++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237drop:
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100238 return qdisc_drop(skb, sch);
Thomas Grafc3b553c2005-11-05 21:14:18 +0100239
240congestion_drop:
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100241 qdisc_drop(skb, sch);
Thomas Grafc3b553c2005-11-05 21:14:18 +0100242 return NET_XMIT_CN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000245static struct sk_buff *gred_dequeue(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct sk_buff *skb;
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100248 struct gred_sched *t = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100250 skb = qdisc_dequeue_head(sch);
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (skb) {
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100253 struct gred_sched_data *q;
Thomas Graf18e3fb842005-11-05 21:14:21 +0100254 u16 dp = tc_index_to_dp(skb);
255
256 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
Joe Perchese87cc472012-05-13 21:56:26 +0000257 net_warn_ratelimited("GRED: Unable to relocate VQ 0x%x after dequeue, screwing up backlog\n",
258 tc_index_to_dp(skb));
Thomas Graf18e3fb842005-11-05 21:14:21 +0100259 } else {
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700260 q->backlog -= qdisc_pkt_len(skb);
Thomas Graf18e3fb842005-11-05 21:14:21 +0100261
Thomas Grafdea3f622005-11-05 21:14:09 +0100262 if (!q->backlog && !gred_wred_mode(t))
Eric Dumazeteeca6682012-01-05 02:25:16 +0000263 red_start_of_idle_period(&q->vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Thomas Graf18e3fb842005-11-05 21:14:21 +0100265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return skb;
267 }
268
Thomas Grafd8f64e12005-11-05 21:14:26 +0100269 if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
Thomas Graf70517032005-11-05 21:14:23 +0100270 red_start_of_idle_period(&t->wred_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return NULL;
273}
274
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000275static unsigned int gred_drop(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct sk_buff *skb;
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100278 struct gred_sched *t = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100280 skb = qdisc_dequeue_tail(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (skb) {
Jussi Kivilinna0abf77e2008-07-20 00:08:27 -0700282 unsigned int len = qdisc_pkt_len(skb);
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100283 struct gred_sched_data *q;
Thomas Graf18e3fb842005-11-05 21:14:21 +0100284 u16 dp = tc_index_to_dp(skb);
285
286 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
Joe Perchese87cc472012-05-13 21:56:26 +0000287 net_warn_ratelimited("GRED: Unable to relocate VQ 0x%x while dropping, screwing up backlog\n",
288 tc_index_to_dp(skb));
Thomas Graf18e3fb842005-11-05 21:14:21 +0100289 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 q->backlog -= len;
Thomas Graf22b33422005-11-05 21:14:16 +0100291 q->stats.other++;
Thomas Graf18e3fb842005-11-05 21:14:21 +0100292
Thomas Grafdea3f622005-11-05 21:14:09 +0100293 if (!q->backlog && !gred_wred_mode(t))
Eric Dumazeteeca6682012-01-05 02:25:16 +0000294 red_start_of_idle_period(&q->vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100297 qdisc_drop(skb, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return len;
299 }
300
Thomas Grafd8f64e12005-11-05 21:14:26 +0100301 if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
Thomas Graf70517032005-11-05 21:14:23 +0100302 red_start_of_idle_period(&t->wred_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305
306}
307
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000308static void gred_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 int i;
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100311 struct gred_sched *t = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100313 qdisc_reset_queue(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900315 for (i = 0; i < t->DPs; i++) {
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100316 struct gred_sched_data *q = t->tab[i];
317
318 if (!q)
319 continue;
320
Eric Dumazeteeca6682012-01-05 02:25:16 +0000321 red_restart(&q->vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 q->backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324}
325
Thomas Graf66396072005-11-05 21:14:13 +0100326static inline void gred_destroy_vq(struct gred_sched_data *q)
327{
328 kfree(q);
329}
330
Patrick McHardy1e904742008-01-22 22:11:17 -0800331static inline int gred_change_table_def(struct Qdisc *sch, struct nlattr *dps)
Thomas Graf66396072005-11-05 21:14:13 +0100332{
333 struct gred_sched *table = qdisc_priv(sch);
334 struct tc_gred_sopt *sopt;
335 int i;
336
Patrick McHardy27a34212008-01-23 20:35:39 -0800337 if (dps == NULL)
Thomas Graf66396072005-11-05 21:14:13 +0100338 return -EINVAL;
339
Patrick McHardy1e904742008-01-22 22:11:17 -0800340 sopt = nla_data(dps);
Thomas Graf66396072005-11-05 21:14:13 +0100341
342 if (sopt->DPs > MAX_DPs || sopt->DPs == 0 || sopt->def_DP >= sopt->DPs)
343 return -EINVAL;
344
345 sch_tree_lock(sch);
346 table->DPs = sopt->DPs;
347 table->def = sopt->def_DP;
Thomas Grafb38c7ee2005-11-05 21:14:27 +0100348 table->red_flags = sopt->flags;
Thomas Graf66396072005-11-05 21:14:13 +0100349
350 /*
351 * Every entry point to GRED is synchronized with the above code
352 * and the DP is checked against DPs, i.e. shadowed VQs can no
353 * longer be found so we can unlock right here.
354 */
355 sch_tree_unlock(sch);
356
357 if (sopt->grio) {
358 gred_enable_rio_mode(table);
359 gred_disable_wred_mode(table);
360 if (gred_wred_mode_check(sch))
361 gred_enable_wred_mode(table);
362 } else {
363 gred_disable_rio_mode(table);
364 gred_disable_wred_mode(table);
365 }
366
367 for (i = table->DPs; i < MAX_DPs; i++) {
368 if (table->tab[i]) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000369 pr_warning("GRED: Warning: Destroying "
370 "shadowed VQ 0x%x\n", i);
Thomas Graf66396072005-11-05 21:14:13 +0100371 gred_destroy_vq(table->tab[i]);
372 table->tab[i] = NULL;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900373 }
Thomas Graf66396072005-11-05 21:14:13 +0100374 }
375
Thomas Graf66396072005-11-05 21:14:13 +0100376 return 0;
377}
378
Thomas Graff62d6b92005-11-05 21:14:15 +0100379static inline int gred_change_vq(struct Qdisc *sch, int dp,
Eric Dumazeta73ed262011-12-09 02:46:45 +0000380 struct tc_gred_qopt *ctl, int prio,
Eric Dumazet869aa412011-12-15 22:09:45 +0000381 u8 *stab, u32 max_P,
382 struct gred_sched_data **prealloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct gred_sched *table = qdisc_priv(sch);
Eric Dumazet869aa412011-12-15 22:09:45 +0000385 struct gred_sched_data *q = table->tab[dp];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Eric Dumazet869aa412011-12-15 22:09:45 +0000387 if (!q) {
388 table->tab[dp] = q = *prealloc;
389 *prealloc = NULL;
390 if (!q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Thomas Graff62d6b92005-11-05 21:14:15 +0100394 q->DP = dp;
395 q->prio = prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 q->limit = ctl->limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Thomas Graf22b33422005-11-05 21:14:16 +0100398 if (q->backlog == 0)
Eric Dumazeteeca6682012-01-05 02:25:16 +0000399 red_end_of_idle_period(&q->vars);
Thomas Graf22b33422005-11-05 21:14:16 +0100400
401 red_set_parms(&q->parms,
402 ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog,
Eric Dumazeta73ed262011-12-09 02:46:45 +0000403 ctl->Scell_log, stab, max_P);
Eric Dumazeteeca6682012-01-05 02:25:16 +0000404 red_set_vars(&q->vars);
Thomas Graff62d6b92005-11-05 21:14:15 +0100405 return 0;
406}
407
Patrick McHardy27a34212008-01-23 20:35:39 -0800408static const struct nla_policy gred_policy[TCA_GRED_MAX + 1] = {
409 [TCA_GRED_PARMS] = { .len = sizeof(struct tc_gred_qopt) },
410 [TCA_GRED_STAB] = { .len = 256 },
411 [TCA_GRED_DPS] = { .len = sizeof(struct tc_gred_sopt) },
Eric Dumazeta73ed262011-12-09 02:46:45 +0000412 [TCA_GRED_MAX_P] = { .type = NLA_U32 },
Patrick McHardy27a34212008-01-23 20:35:39 -0800413};
414
Patrick McHardy1e904742008-01-22 22:11:17 -0800415static int gred_change(struct Qdisc *sch, struct nlattr *opt)
Thomas Graff62d6b92005-11-05 21:14:15 +0100416{
417 struct gred_sched *table = qdisc_priv(sch);
418 struct tc_gred_qopt *ctl;
Patrick McHardy1e904742008-01-22 22:11:17 -0800419 struct nlattr *tb[TCA_GRED_MAX + 1];
Patrick McHardycee63722008-01-23 20:33:32 -0800420 int err, prio = GRED_DEF_PRIO;
Thomas Graff62d6b92005-11-05 21:14:15 +0100421 u8 *stab;
Eric Dumazeta73ed262011-12-09 02:46:45 +0000422 u32 max_P;
Eric Dumazet869aa412011-12-15 22:09:45 +0000423 struct gred_sched_data *prealloc;
Thomas Graff62d6b92005-11-05 21:14:15 +0100424
Patrick McHardycee63722008-01-23 20:33:32 -0800425 if (opt == NULL)
Thomas Graff62d6b92005-11-05 21:14:15 +0100426 return -EINVAL;
427
Patrick McHardy27a34212008-01-23 20:35:39 -0800428 err = nla_parse_nested(tb, TCA_GRED_MAX, opt, gred_policy);
Patrick McHardycee63722008-01-23 20:33:32 -0800429 if (err < 0)
430 return err;
431
Patrick McHardy1e904742008-01-22 22:11:17 -0800432 if (tb[TCA_GRED_PARMS] == NULL && tb[TCA_GRED_STAB] == NULL)
Thomas Graff62d6b92005-11-05 21:14:15 +0100433 return gred_change_table_def(sch, opt);
434
Patrick McHardy1e904742008-01-22 22:11:17 -0800435 if (tb[TCA_GRED_PARMS] == NULL ||
Patrick McHardy27a34212008-01-23 20:35:39 -0800436 tb[TCA_GRED_STAB] == NULL)
Thomas Graff62d6b92005-11-05 21:14:15 +0100437 return -EINVAL;
438
Eric Dumazeta73ed262011-12-09 02:46:45 +0000439 max_P = tb[TCA_GRED_MAX_P] ? nla_get_u32(tb[TCA_GRED_MAX_P]) : 0;
440
Patrick McHardycee63722008-01-23 20:33:32 -0800441 err = -EINVAL;
Patrick McHardy1e904742008-01-22 22:11:17 -0800442 ctl = nla_data(tb[TCA_GRED_PARMS]);
443 stab = nla_data(tb[TCA_GRED_STAB]);
Thomas Graff62d6b92005-11-05 21:14:15 +0100444
445 if (ctl->DP >= table->DPs)
446 goto errout;
447
448 if (gred_rio_mode(table)) {
449 if (ctl->prio == 0) {
450 int def_prio = GRED_DEF_PRIO;
451
452 if (table->tab[table->def])
453 def_prio = table->tab[table->def]->prio;
454
455 printk(KERN_DEBUG "GRED: DP %u does not have a prio "
456 "setting default to %d\n", ctl->DP, def_prio);
457
458 prio = def_prio;
459 } else
460 prio = ctl->prio;
461 }
462
Eric Dumazet869aa412011-12-15 22:09:45 +0000463 prealloc = kzalloc(sizeof(*prealloc), GFP_KERNEL);
Thomas Graff62d6b92005-11-05 21:14:15 +0100464 sch_tree_lock(sch);
465
Eric Dumazet869aa412011-12-15 22:09:45 +0000466 err = gred_change_vq(sch, ctl->DP, ctl, prio, stab, max_P, &prealloc);
Thomas Graff62d6b92005-11-05 21:14:15 +0100467 if (err < 0)
468 goto errout_locked;
469
Thomas Grafd6fd4e92005-11-05 21:14:10 +0100470 if (gred_rio_mode(table)) {
Thomas Grafdea3f622005-11-05 21:14:09 +0100471 gred_disable_wred_mode(table);
472 if (gred_wred_mode_check(sch))
473 gred_enable_wred_mode(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
Thomas Graff62d6b92005-11-05 21:14:15 +0100476 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Thomas Graff62d6b92005-11-05 21:14:15 +0100478errout_locked:
479 sch_tree_unlock(sch);
Eric Dumazet869aa412011-12-15 22:09:45 +0000480 kfree(prealloc);
Thomas Graff62d6b92005-11-05 21:14:15 +0100481errout:
482 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Patrick McHardy1e904742008-01-22 22:11:17 -0800485static int gred_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Patrick McHardy1e904742008-01-22 22:11:17 -0800487 struct nlattr *tb[TCA_GRED_MAX + 1];
Patrick McHardycee63722008-01-23 20:33:32 -0800488 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Patrick McHardycee63722008-01-23 20:33:32 -0800490 if (opt == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EINVAL;
492
Patrick McHardy27a34212008-01-23 20:35:39 -0800493 err = nla_parse_nested(tb, TCA_GRED_MAX, opt, gred_policy);
Patrick McHardycee63722008-01-23 20:33:32 -0800494 if (err < 0)
495 return err;
496
Patrick McHardy1e904742008-01-22 22:11:17 -0800497 if (tb[TCA_GRED_PARMS] || tb[TCA_GRED_STAB])
Thomas Graf66396072005-11-05 21:14:13 +0100498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Patrick McHardy1e904742008-01-22 22:11:17 -0800500 return gred_change_table_def(sch, tb[TCA_GRED_DPS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
504{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct gred_sched *table = qdisc_priv(sch);
Patrick McHardy1e904742008-01-22 22:11:17 -0800506 struct nlattr *parms, *opts = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int i;
Eric Dumazeta73ed262011-12-09 02:46:45 +0000508 u32 max_p[MAX_DPs];
Thomas Grafe0636822005-11-05 21:14:12 +0100509 struct tc_gred_sopt sopt = {
510 .DPs = table->DPs,
511 .def_DP = table->def,
512 .grio = gred_rio_mode(table),
Thomas Grafb38c7ee2005-11-05 21:14:27 +0100513 .flags = table->red_flags,
Thomas Grafe0636822005-11-05 21:14:12 +0100514 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Patrick McHardy1e904742008-01-22 22:11:17 -0800516 opts = nla_nest_start(skb, TCA_OPTIONS);
517 if (opts == NULL)
518 goto nla_put_failure;
David S. Miller1b34ec42012-03-29 05:11:39 -0400519 if (nla_put(skb, TCA_GRED_DPS, sizeof(sopt), &sopt))
520 goto nla_put_failure;
Eric Dumazeta73ed262011-12-09 02:46:45 +0000521
522 for (i = 0; i < MAX_DPs; i++) {
523 struct gred_sched_data *q = table->tab[i];
524
525 max_p[i] = q ? q->parms.max_P : 0;
526 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400527 if (nla_put(skb, TCA_GRED_MAX_P, sizeof(max_p), max_p))
528 goto nla_put_failure;
Eric Dumazeta73ed262011-12-09 02:46:45 +0000529
Patrick McHardy1e904742008-01-22 22:11:17 -0800530 parms = nla_nest_start(skb, TCA_GRED_PARMS);
531 if (parms == NULL)
532 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Thomas Graf05f1cc02005-11-05 21:14:11 +0100534 for (i = 0; i < MAX_DPs; i++) {
535 struct gred_sched_data *q = table->tab[i];
536 struct tc_gred_qopt opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Thomas Graf05f1cc02005-11-05 21:14:11 +0100538 memset(&opt, 0, sizeof(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (!q) {
541 /* hack -- fix at some point with proper message
542 This is how we indicate to tc that there is no VQ
543 at this DP */
544
Thomas Graf05f1cc02005-11-05 21:14:11 +0100545 opt.DP = MAX_DPs + i;
546 goto append_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
Thomas Graf05f1cc02005-11-05 21:14:11 +0100549 opt.limit = q->limit;
550 opt.DP = q->DP;
551 opt.backlog = q->backlog;
552 opt.prio = q->prio;
Thomas Graf22b33422005-11-05 21:14:16 +0100553 opt.qth_min = q->parms.qth_min >> q->parms.Wlog;
554 opt.qth_max = q->parms.qth_max >> q->parms.Wlog;
555 opt.Wlog = q->parms.Wlog;
556 opt.Plog = q->parms.Plog;
557 opt.Scell_log = q->parms.Scell_log;
558 opt.other = q->stats.other;
559 opt.early = q->stats.prob_drop;
560 opt.forced = q->stats.forced_drop;
561 opt.pdrop = q->stats.pdrop;
Thomas Graf05f1cc02005-11-05 21:14:11 +0100562 opt.packets = q->packetsin;
563 opt.bytesin = q->bytesin;
564
David Ward244b65d2012-04-15 12:31:45 +0000565 if (gred_wred_mode(table))
566 gred_load_wred_set(table, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Eric Dumazeteeca6682012-01-05 02:25:16 +0000568 opt.qave = red_calc_qavg(&q->parms, &q->vars, q->vars.qavg);
Thomas Graf22b33422005-11-05 21:14:16 +0100569
Thomas Graf05f1cc02005-11-05 21:14:11 +0100570append_opt:
Patrick McHardy1e904742008-01-22 22:11:17 -0800571 if (nla_append(skb, sizeof(opt), &opt) < 0)
572 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
Patrick McHardy1e904742008-01-22 22:11:17 -0800575 nla_nest_end(skb, parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Patrick McHardy1e904742008-01-22 22:11:17 -0800577 return nla_nest_end(skb, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Patrick McHardy1e904742008-01-22 22:11:17 -0800579nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700580 nla_nest_cancel(skb, opts);
581 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584static void gred_destroy(struct Qdisc *sch)
585{
586 struct gred_sched *table = qdisc_priv(sch);
587 int i;
588
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100589 for (i = 0; i < table->DPs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (table->tab[i])
Thomas Graf66396072005-11-05 21:14:13 +0100591 gred_destroy_vq(table->tab[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593}
594
Eric Dumazet20fea082007-11-14 01:44:41 -0800595static struct Qdisc_ops gred_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 .id = "gred",
597 .priv_size = sizeof(struct gred_sched),
598 .enqueue = gred_enqueue,
599 .dequeue = gred_dequeue,
Jarek Poplawski8e3af972008-10-31 00:45:55 -0700600 .peek = qdisc_peek_head,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 .drop = gred_drop,
602 .init = gred_init,
603 .reset = gred_reset,
604 .destroy = gred_destroy,
605 .change = gred_change,
606 .dump = gred_dump,
607 .owner = THIS_MODULE,
608};
609
610static int __init gred_module_init(void)
611{
612 return register_qdisc(&gred_qdisc_ops);
613}
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100614
615static void __exit gred_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 unregister_qdisc(&gred_qdisc_ops);
618}
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620module_init(gred_module_init)
621module_exit(gred_module_exit)
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623MODULE_LICENSE("GPL");