blob: 079b0a4ea1c2dbc0a3dac2b866f2aab7b8194138 [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
21#include <linux/config.h>
22#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/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/pkt_sched.h>
Thomas Graf22b33422005-11-05 21:14:16 +010028#include <net/red.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Thomas Graff62d6b92005-11-05 21:14:15 +010030#define GRED_DEF_PRIO (MAX_DPs / 2)
Thomas Graf716a1b42005-11-05 21:14:20 +010031#define GRED_VQ_MASK (MAX_DPs - 1)
Thomas Graff62d6b92005-11-05 21:14:15 +010032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033struct gred_sched_data;
34struct gred_sched;
35
36struct gred_sched_data
37{
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 u32 limit; /* HARD maximal queue length */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 u32 DP; /* the drop pramaters */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 u32 bytesin; /* bytes seen on virtualQ so far*/
41 u32 packetsin; /* packets seen on virtualQ so far*/
42 u32 backlog; /* bytes on the virtualQ */
Thomas Graf1e4dfaf92005-11-05 21:14:25 +010043 u8 prio; /* the prio of this vq */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Thomas Graf22b33422005-11-05 21:14:16 +010045 struct red_parms parms;
46 struct red_stats stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Thomas Grafdea3f622005-11-05 21:14:09 +010049enum {
50 GRED_WRED_MODE = 1,
Thomas Grafd6fd4e92005-11-05 21:14:10 +010051 GRED_RIO_MODE,
Thomas Grafdea3f622005-11-05 21:14:09 +010052};
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054struct gred_sched
55{
56 struct gred_sched_data *tab[MAX_DPs];
Thomas Grafdea3f622005-11-05 21:14:09 +010057 unsigned long flags;
Thomas Grafb38c7ee2005-11-05 21:14:27 +010058 u32 red_flags;
Thomas Graf1e4dfaf92005-11-05 21:14:25 +010059 u32 DPs;
60 u32 def;
Thomas Graf70517032005-11-05 21:14:23 +010061 struct red_parms wred_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Thomas Grafdea3f622005-11-05 21:14:09 +010064static inline int gred_wred_mode(struct gred_sched *table)
65{
66 return test_bit(GRED_WRED_MODE, &table->flags);
67}
68
69static inline void gred_enable_wred_mode(struct gred_sched *table)
70{
71 __set_bit(GRED_WRED_MODE, &table->flags);
72}
73
74static inline void gred_disable_wred_mode(struct gred_sched *table)
75{
76 __clear_bit(GRED_WRED_MODE, &table->flags);
77}
78
Thomas Grafd6fd4e92005-11-05 21:14:10 +010079static inline int gred_rio_mode(struct gred_sched *table)
80{
81 return test_bit(GRED_RIO_MODE, &table->flags);
82}
83
84static inline void gred_enable_rio_mode(struct gred_sched *table)
85{
86 __set_bit(GRED_RIO_MODE, &table->flags);
87}
88
89static inline void gred_disable_rio_mode(struct gred_sched *table)
90{
91 __clear_bit(GRED_RIO_MODE, &table->flags);
92}
93
Thomas Grafdea3f622005-11-05 21:14:09 +010094static inline int gred_wred_mode_check(struct Qdisc *sch)
95{
96 struct gred_sched *table = qdisc_priv(sch);
97 int i;
98
99 /* Really ugly O(n^2) but shouldn't be necessary too frequent. */
100 for (i = 0; i < table->DPs; i++) {
101 struct gred_sched_data *q = table->tab[i];
102 int n;
103
104 if (q == NULL)
105 continue;
106
107 for (n = 0; n < table->DPs; n++)
108 if (table->tab[n] && table->tab[n] != q &&
109 table->tab[n]->prio == q->prio)
110 return 1;
111 }
112
113 return 0;
114}
115
Thomas Graf22b33422005-11-05 21:14:16 +0100116static inline unsigned int gred_backlog(struct gred_sched *table,
117 struct gred_sched_data *q,
118 struct Qdisc *sch)
119{
120 if (gred_wred_mode(table))
121 return sch->qstats.backlog;
122 else
123 return q->backlog;
124}
125
Thomas Graf716a1b42005-11-05 21:14:20 +0100126static inline u16 tc_index_to_dp(struct sk_buff *skb)
127{
128 return skb->tc_index & GRED_VQ_MASK;
129}
130
Thomas Graf70517032005-11-05 21:14:23 +0100131static inline void gred_load_wred_set(struct gred_sched *table,
132 struct gred_sched_data *q)
133{
134 q->parms.qavg = table->wred_set.qavg;
135 q->parms.qidlestart = table->wred_set.qidlestart;
136}
137
138static inline void gred_store_wred_set(struct gred_sched *table,
139 struct gred_sched_data *q)
140{
141 table->wred_set.qavg = q->parms.qavg;
142}
143
Thomas Grafb38c7ee2005-11-05 21:14:27 +0100144static inline int gred_use_ecn(struct gred_sched *t)
145{
146 return t->red_flags & TC_RED_ECN;
147}
148
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100149static int gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct gred_sched_data *q=NULL;
152 struct gred_sched *t= qdisc_priv(sch);
Thomas Graf22b33422005-11-05 21:14:16 +0100153 unsigned long qavg = 0;
Thomas Graf4a591832005-11-05 21:14:22 +0100154 u16 dp = tc_index_to_dp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Thomas Graf716a1b42005-11-05 21:14:20 +0100156 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
Thomas Graf18e3fb842005-11-05 21:14:21 +0100157 dp = t->def;
158
159 if ((q = t->tab[dp]) == NULL) {
160 /* Pass through packets not assigned to a DP
161 * if no default DP has been configured. This
162 * allows for DP flows to be left untouched.
163 */
164 if (skb_queue_len(&sch->q) < sch->dev->tx_queue_len)
165 return qdisc_enqueue_tail(skb, sch);
166 else
167 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Thomas Graf18e3fb842005-11-05 21:14:21 +0100169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* fix tc_index? --could be controvesial but needed for
171 requeueing */
Thomas Graf18e3fb842005-11-05 21:14:21 +0100172 skb->tc_index = (skb->tc_index & ~GRED_VQ_MASK) | dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100175 /* sum up all the qaves of prios <= to ours to get the new qave */
Thomas Grafd6fd4e92005-11-05 21:14:10 +0100176 if (!gred_wred_mode(t) && gred_rio_mode(t)) {
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100177 int i;
178
179 for (i = 0; i < t->DPs; i++) {
180 if (t->tab[i] && t->tab[i]->prio < q->prio &&
Thomas Graf22b33422005-11-05 21:14:16 +0100181 !red_is_idling(&t->tab[i]->parms))
182 qavg +=t->tab[i]->parms.qavg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
187 q->packetsin++;
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100188 q->bytesin += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100190 if (gred_wred_mode(t))
Thomas Graf70517032005-11-05 21:14:23 +0100191 gred_load_wred_set(t, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Thomas Graf22b33422005-11-05 21:14:16 +0100193 q->parms.qavg = red_calc_qavg(&q->parms, gred_backlog(t, q, sch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Thomas Graf22b33422005-11-05 21:14:16 +0100195 if (red_is_idling(&q->parms))
196 red_end_of_idle_period(&q->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Thomas Grafdea3f622005-11-05 21:14:09 +0100198 if (gred_wred_mode(t))
Thomas Graf70517032005-11-05 21:14:23 +0100199 gred_store_wred_set(t, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Thomas Graf22b33422005-11-05 21:14:16 +0100201 switch (red_action(&q->parms, q->parms.qavg + qavg)) {
202 case RED_DONT_MARK:
203 break;
204
205 case RED_PROB_MARK:
206 sch->qstats.overlimits++;
Thomas Grafb38c7ee2005-11-05 21:14:27 +0100207 if (!gred_use_ecn(t) || !INET_ECN_set_ce(skb)) {
208 q->stats.prob_drop++;
209 goto congestion_drop;
210 }
211
212 q->stats.prob_mark++;
213 break;
Thomas Graf22b33422005-11-05 21:14:16 +0100214
215 case RED_HARD_MARK:
216 sch->qstats.overlimits++;
Thomas Grafb38c7ee2005-11-05 21:14:27 +0100217 if (!gred_use_ecn(t) || !INET_ECN_set_ce(skb)) {
218 q->stats.forced_drop++;
219 goto congestion_drop;
220 }
221 q->stats.forced_mark++;
222 break;
Thomas Graf22b33422005-11-05 21:14:16 +0100223 }
224
225 if (q->backlog + skb->len <= q->limit) {
226 q->backlog += skb->len;
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100227 return qdisc_enqueue_tail(skb, sch);
Thomas Graf22b33422005-11-05 21:14:16 +0100228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Thomas Graf22b33422005-11-05 21:14:16 +0100230 q->stats.pdrop++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231drop:
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100232 return qdisc_drop(skb, sch);
Thomas Grafc3b553c2005-11-05 21:14:18 +0100233
234congestion_drop:
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100235 qdisc_drop(skb, sch);
Thomas Grafc3b553c2005-11-05 21:14:18 +0100236 return NET_XMIT_CN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100239static int gred_requeue(struct sk_buff *skb, struct Qdisc* sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Thomas Graf716a1b42005-11-05 21:14:20 +0100241 struct gred_sched *t = qdisc_priv(sch);
Thomas Graf18e3fb842005-11-05 21:14:21 +0100242 struct gred_sched_data *q;
243 u16 dp = tc_index_to_dp(skb);
Thomas Graf22b33422005-11-05 21:14:16 +0100244
Thomas Graf18e3fb842005-11-05 21:14:21 +0100245 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
246 if (net_ratelimit())
247 printk(KERN_WARNING "GRED: Unable to relocate VQ 0x%x "
248 "for requeue, screwing up backlog.\n",
249 tc_index_to_dp(skb));
250 } else {
251 if (red_is_idling(&q->parms))
252 red_end_of_idle_period(&q->parms);
253 q->backlog += skb->len;
254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100256 return qdisc_requeue(skb, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100259static struct sk_buff *gred_dequeue(struct Qdisc* sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct sk_buff *skb;
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100262 struct gred_sched *t = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100264 skb = qdisc_dequeue_head(sch);
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (skb) {
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100267 struct gred_sched_data *q;
Thomas Graf18e3fb842005-11-05 21:14:21 +0100268 u16 dp = tc_index_to_dp(skb);
269
270 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
271 if (net_ratelimit())
272 printk(KERN_WARNING "GRED: Unable to relocate "
273 "VQ 0x%x after dequeue, screwing up "
274 "backlog.\n", tc_index_to_dp(skb));
275 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 q->backlog -= skb->len;
Thomas Graf18e3fb842005-11-05 21:14:21 +0100277
Thomas Grafdea3f622005-11-05 21:14:09 +0100278 if (!q->backlog && !gred_wred_mode(t))
Thomas Graf22b33422005-11-05 21:14:16 +0100279 red_start_of_idle_period(&q->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Thomas Graf18e3fb842005-11-05 21:14:21 +0100281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return skb;
283 }
284
Thomas Grafd8f64e12005-11-05 21:14:26 +0100285 if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
Thomas Graf70517032005-11-05 21:14:23 +0100286 red_start_of_idle_period(&t->wred_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 return NULL;
289}
290
291static unsigned int gred_drop(struct Qdisc* sch)
292{
293 struct sk_buff *skb;
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100294 struct gred_sched *t = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100296 skb = qdisc_dequeue_tail(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (skb) {
298 unsigned int len = skb->len;
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100299 struct gred_sched_data *q;
Thomas Graf18e3fb842005-11-05 21:14:21 +0100300 u16 dp = tc_index_to_dp(skb);
301
302 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
303 if (net_ratelimit())
304 printk(KERN_WARNING "GRED: Unable to relocate "
305 "VQ 0x%x while dropping, screwing up "
306 "backlog.\n", tc_index_to_dp(skb));
307 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 q->backlog -= len;
Thomas Graf22b33422005-11-05 21:14:16 +0100309 q->stats.other++;
Thomas Graf18e3fb842005-11-05 21:14:21 +0100310
Thomas Grafdea3f622005-11-05 21:14:09 +0100311 if (!q->backlog && !gred_wred_mode(t))
Thomas Graf22b33422005-11-05 21:14:16 +0100312 red_start_of_idle_period(&q->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100315 qdisc_drop(skb, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return len;
317 }
318
Thomas Grafd8f64e12005-11-05 21:14:26 +0100319 if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
Thomas Graf70517032005-11-05 21:14:23 +0100320 red_start_of_idle_period(&t->wred_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return 0;
323
324}
325
326static void gred_reset(struct Qdisc* sch)
327{
328 int i;
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100329 struct gred_sched *t = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100331 qdisc_reset_queue(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100333 for (i = 0; i < t->DPs; i++) {
334 struct gred_sched_data *q = t->tab[i];
335
336 if (!q)
337 continue;
338
Thomas Graf22b33422005-11-05 21:14:16 +0100339 red_restart(&q->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 q->backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342}
343
Thomas Graf66396072005-11-05 21:14:13 +0100344static inline void gred_destroy_vq(struct gred_sched_data *q)
345{
346 kfree(q);
347}
348
349static inline int gred_change_table_def(struct Qdisc *sch, struct rtattr *dps)
350{
351 struct gred_sched *table = qdisc_priv(sch);
352 struct tc_gred_sopt *sopt;
353 int i;
354
355 if (dps == NULL || RTA_PAYLOAD(dps) < sizeof(*sopt))
356 return -EINVAL;
357
358 sopt = RTA_DATA(dps);
359
360 if (sopt->DPs > MAX_DPs || sopt->DPs == 0 || sopt->def_DP >= sopt->DPs)
361 return -EINVAL;
362
363 sch_tree_lock(sch);
364 table->DPs = sopt->DPs;
365 table->def = sopt->def_DP;
Thomas Grafb38c7ee2005-11-05 21:14:27 +0100366 table->red_flags = sopt->flags;
Thomas Graf66396072005-11-05 21:14:13 +0100367
368 /*
369 * Every entry point to GRED is synchronized with the above code
370 * and the DP is checked against DPs, i.e. shadowed VQs can no
371 * longer be found so we can unlock right here.
372 */
373 sch_tree_unlock(sch);
374
375 if (sopt->grio) {
376 gred_enable_rio_mode(table);
377 gred_disable_wred_mode(table);
378 if (gred_wred_mode_check(sch))
379 gred_enable_wred_mode(table);
380 } else {
381 gred_disable_rio_mode(table);
382 gred_disable_wred_mode(table);
383 }
384
385 for (i = table->DPs; i < MAX_DPs; i++) {
386 if (table->tab[i]) {
387 printk(KERN_WARNING "GRED: Warning: Destroying "
388 "shadowed VQ 0x%x\n", i);
389 gred_destroy_vq(table->tab[i]);
390 table->tab[i] = NULL;
391 }
392 }
393
Thomas Graf66396072005-11-05 21:14:13 +0100394 return 0;
395}
396
Thomas Graff62d6b92005-11-05 21:14:15 +0100397static inline int gred_change_vq(struct Qdisc *sch, int dp,
398 struct tc_gred_qopt *ctl, int prio, u8 *stab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct gred_sched *table = qdisc_priv(sch);
401 struct gred_sched_data *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Thomas Graff62d6b92005-11-05 21:14:15 +0100403 if (table->tab[dp] == NULL) {
404 table->tab[dp] = kmalloc(sizeof(*q), GFP_KERNEL);
405 if (table->tab[dp] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return -ENOMEM;
Thomas Graff62d6b92005-11-05 21:14:15 +0100407 memset(table->tab[dp], 0, sizeof(*q));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Thomas Graff62d6b92005-11-05 21:14:15 +0100410 q = table->tab[dp];
411 q->DP = dp;
412 q->prio = prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 q->limit = ctl->limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Thomas Graf22b33422005-11-05 21:14:16 +0100415 if (q->backlog == 0)
416 red_end_of_idle_period(&q->parms);
417
418 red_set_parms(&q->parms,
419 ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog,
420 ctl->Scell_log, stab);
421
Thomas Graff62d6b92005-11-05 21:14:15 +0100422 return 0;
423}
424
425static int gred_change(struct Qdisc *sch, struct rtattr *opt)
426{
427 struct gred_sched *table = qdisc_priv(sch);
428 struct tc_gred_qopt *ctl;
429 struct rtattr *tb[TCA_GRED_MAX];
430 int err = -EINVAL, prio = GRED_DEF_PRIO;
431 u8 *stab;
432
433 if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
434 return -EINVAL;
435
436 if (tb[TCA_GRED_PARMS-1] == NULL && tb[TCA_GRED_STAB-1] == NULL)
437 return gred_change_table_def(sch, opt);
438
439 if (tb[TCA_GRED_PARMS-1] == NULL ||
440 RTA_PAYLOAD(tb[TCA_GRED_PARMS-1]) < sizeof(*ctl) ||
441 tb[TCA_GRED_STAB-1] == NULL ||
442 RTA_PAYLOAD(tb[TCA_GRED_STAB-1]) < 256)
443 return -EINVAL;
444
445 ctl = RTA_DATA(tb[TCA_GRED_PARMS-1]);
446 stab = RTA_DATA(tb[TCA_GRED_STAB-1]);
447
448 if (ctl->DP >= table->DPs)
449 goto errout;
450
451 if (gred_rio_mode(table)) {
452 if (ctl->prio == 0) {
453 int def_prio = GRED_DEF_PRIO;
454
455 if (table->tab[table->def])
456 def_prio = table->tab[table->def]->prio;
457
458 printk(KERN_DEBUG "GRED: DP %u does not have a prio "
459 "setting default to %d\n", ctl->DP, def_prio);
460
461 prio = def_prio;
462 } else
463 prio = ctl->prio;
464 }
465
466 sch_tree_lock(sch);
467
468 err = gred_change_vq(sch, ctl->DP, ctl, prio, stab);
469 if (err < 0)
470 goto errout_locked;
471
Thomas Grafd6fd4e92005-11-05 21:14:10 +0100472 if (gred_rio_mode(table)) {
Thomas Grafdea3f622005-11-05 21:14:09 +0100473 gred_disable_wred_mode(table);
474 if (gred_wred_mode_check(sch))
475 gred_enable_wred_mode(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477
Thomas Graff62d6b92005-11-05 21:14:15 +0100478 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Thomas Graff62d6b92005-11-05 21:14:15 +0100480errout_locked:
481 sch_tree_unlock(sch);
482errout:
483 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486static int gred_init(struct Qdisc *sch, struct rtattr *opt)
487{
Thomas Graf66396072005-11-05 21:14:13 +0100488 struct rtattr *tb[TCA_GRED_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Thomas Graf66396072005-11-05 21:14:13 +0100490 if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EINVAL;
492
Thomas Graf66396072005-11-05 21:14:13 +0100493 if (tb[TCA_GRED_PARMS-1] || tb[TCA_GRED_STAB-1])
494 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Thomas Graf66396072005-11-05 21:14:13 +0100496 return gred_change_table_def(sch, tb[TCA_GRED_DPS-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
500{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct gred_sched *table = qdisc_priv(sch);
Thomas Graf05f1cc02005-11-05 21:14:11 +0100502 struct rtattr *parms, *opts = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int i;
Thomas Grafe0636822005-11-05 21:14:12 +0100504 struct tc_gred_sopt sopt = {
505 .DPs = table->DPs,
506 .def_DP = table->def,
507 .grio = gred_rio_mode(table),
Thomas Grafb38c7ee2005-11-05 21:14:27 +0100508 .flags = table->red_flags,
Thomas Grafe0636822005-11-05 21:14:12 +0100509 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Thomas Graf05f1cc02005-11-05 21:14:11 +0100511 opts = RTA_NEST(skb, TCA_OPTIONS);
Thomas Grafe0636822005-11-05 21:14:12 +0100512 RTA_PUT(skb, TCA_GRED_DPS, sizeof(sopt), &sopt);
Thomas Graf05f1cc02005-11-05 21:14:11 +0100513 parms = RTA_NEST(skb, TCA_GRED_PARMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Thomas Graf05f1cc02005-11-05 21:14:11 +0100515 for (i = 0; i < MAX_DPs; i++) {
516 struct gred_sched_data *q = table->tab[i];
517 struct tc_gred_qopt opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Thomas Graf05f1cc02005-11-05 21:14:11 +0100519 memset(&opt, 0, sizeof(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 if (!q) {
522 /* hack -- fix at some point with proper message
523 This is how we indicate to tc that there is no VQ
524 at this DP */
525
Thomas Graf05f1cc02005-11-05 21:14:11 +0100526 opt.DP = MAX_DPs + i;
527 goto append_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
Thomas Graf05f1cc02005-11-05 21:14:11 +0100530 opt.limit = q->limit;
531 opt.DP = q->DP;
532 opt.backlog = q->backlog;
533 opt.prio = q->prio;
Thomas Graf22b33422005-11-05 21:14:16 +0100534 opt.qth_min = q->parms.qth_min >> q->parms.Wlog;
535 opt.qth_max = q->parms.qth_max >> q->parms.Wlog;
536 opt.Wlog = q->parms.Wlog;
537 opt.Plog = q->parms.Plog;
538 opt.Scell_log = q->parms.Scell_log;
539 opt.other = q->stats.other;
540 opt.early = q->stats.prob_drop;
541 opt.forced = q->stats.forced_drop;
542 opt.pdrop = q->stats.pdrop;
Thomas Graf05f1cc02005-11-05 21:14:11 +0100543 opt.packets = q->packetsin;
544 opt.bytesin = q->bytesin;
545
Thomas Graf22b33422005-11-05 21:14:16 +0100546 if (gred_wred_mode(table)) {
547 q->parms.qidlestart =
548 table->tab[table->def]->parms.qidlestart;
549 q->parms.qavg = table->tab[table->def]->parms.qavg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Thomas Graf22b33422005-11-05 21:14:16 +0100552 opt.qave = red_calc_qavg(&q->parms, q->parms.qavg);
553
Thomas Graf05f1cc02005-11-05 21:14:11 +0100554append_opt:
555 RTA_APPEND(skb, sizeof(opt), &opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
Thomas Graf05f1cc02005-11-05 21:14:11 +0100558 RTA_NEST_END(skb, parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Thomas Graf05f1cc02005-11-05 21:14:11 +0100560 return RTA_NEST_END(skb, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562rtattr_failure:
Thomas Graf05f1cc02005-11-05 21:14:11 +0100563 return RTA_NEST_CANCEL(skb, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566static void gred_destroy(struct Qdisc *sch)
567{
568 struct gred_sched *table = qdisc_priv(sch);
569 int i;
570
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100571 for (i = 0; i < table->DPs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (table->tab[i])
Thomas Graf66396072005-11-05 21:14:13 +0100573 gred_destroy_vq(table->tab[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575}
576
577static struct Qdisc_ops gred_qdisc_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 .id = "gred",
579 .priv_size = sizeof(struct gred_sched),
580 .enqueue = gred_enqueue,
581 .dequeue = gred_dequeue,
582 .requeue = gred_requeue,
583 .drop = gred_drop,
584 .init = gred_init,
585 .reset = gred_reset,
586 .destroy = gred_destroy,
587 .change = gred_change,
588 .dump = gred_dump,
589 .owner = THIS_MODULE,
590};
591
592static int __init gred_module_init(void)
593{
594 return register_qdisc(&gred_qdisc_ops);
595}
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100596
597static void __exit gred_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 unregister_qdisc(&gred_qdisc_ops);
600}
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602module_init(gred_module_init)
603module_exit(gred_module_exit)
Thomas Graf1e4dfaf92005-11-05 21:14:25 +0100604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605MODULE_LICENSE("GPL");