blob: a545532be2c465fb5cfdcdd0ac156efd54dc2bd4 [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 *
18 *
19 *
20 * For all the glorious comments look at Alexey's sch_red.c
21 */
22
23#include <linux/config.h>
24#include <linux/module.h>
25#include <asm/uaccess.h>
26#include <asm/system.h>
27#include <linux/bitops.h>
28#include <linux/types.h>
29#include <linux/kernel.h>
30#include <linux/sched.h>
31#include <linux/string.h>
32#include <linux/mm.h>
33#include <linux/socket.h>
34#include <linux/sockios.h>
35#include <linux/in.h>
36#include <linux/errno.h>
37#include <linux/interrupt.h>
38#include <linux/if_ether.h>
39#include <linux/inet.h>
40#include <linux/netdevice.h>
41#include <linux/etherdevice.h>
42#include <linux/notifier.h>
43#include <net/ip.h>
44#include <net/route.h>
45#include <linux/skbuff.h>
46#include <net/sock.h>
47#include <net/pkt_sched.h>
Thomas Graf22b33422005-11-05 21:14:16 +010048#include <net/red.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#if 1 /* control */
51#define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
52#else
53#define DPRINTK(format,args...)
54#endif
55
56#if 0 /* data */
57#define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
58#else
59#define D2PRINTK(format,args...)
60#endif
61
Thomas Graff62d6b92005-11-05 21:14:15 +010062#define GRED_DEF_PRIO (MAX_DPs / 2)
Thomas Graf716a1b42005-11-05 21:14:20 +010063#define GRED_VQ_MASK (MAX_DPs - 1)
Thomas Graff62d6b92005-11-05 21:14:15 +010064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065struct gred_sched_data;
66struct gred_sched;
67
68struct gred_sched_data
69{
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 u32 limit; /* HARD maximal queue length */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u32 DP; /* the drop pramaters */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 u32 bytesin; /* bytes seen on virtualQ so far*/
73 u32 packetsin; /* packets seen on virtualQ so far*/
74 u32 backlog; /* bytes on the virtualQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u8 prio; /* the prio of this vq */
76
Thomas Graf22b33422005-11-05 21:14:16 +010077 struct red_parms parms;
78 struct red_stats stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Thomas Grafdea3f622005-11-05 21:14:09 +010081enum {
82 GRED_WRED_MODE = 1,
Thomas Grafd6fd4e92005-11-05 21:14:10 +010083 GRED_RIO_MODE,
Thomas Grafdea3f622005-11-05 21:14:09 +010084};
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086struct gred_sched
87{
88 struct gred_sched_data *tab[MAX_DPs];
Thomas Grafdea3f622005-11-05 21:14:09 +010089 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 u32 DPs;
91 u32 def;
Thomas Graf70517032005-11-05 21:14:23 +010092 struct red_parms wred_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
Thomas Grafdea3f622005-11-05 21:14:09 +010095static inline int gred_wred_mode(struct gred_sched *table)
96{
97 return test_bit(GRED_WRED_MODE, &table->flags);
98}
99
100static inline void gred_enable_wred_mode(struct gred_sched *table)
101{
102 __set_bit(GRED_WRED_MODE, &table->flags);
103}
104
105static inline void gred_disable_wred_mode(struct gred_sched *table)
106{
107 __clear_bit(GRED_WRED_MODE, &table->flags);
108}
109
Thomas Grafd6fd4e92005-11-05 21:14:10 +0100110static inline int gred_rio_mode(struct gred_sched *table)
111{
112 return test_bit(GRED_RIO_MODE, &table->flags);
113}
114
115static inline void gred_enable_rio_mode(struct gred_sched *table)
116{
117 __set_bit(GRED_RIO_MODE, &table->flags);
118}
119
120static inline void gred_disable_rio_mode(struct gred_sched *table)
121{
122 __clear_bit(GRED_RIO_MODE, &table->flags);
123}
124
Thomas Grafdea3f622005-11-05 21:14:09 +0100125static inline int gred_wred_mode_check(struct Qdisc *sch)
126{
127 struct gred_sched *table = qdisc_priv(sch);
128 int i;
129
130 /* Really ugly O(n^2) but shouldn't be necessary too frequent. */
131 for (i = 0; i < table->DPs; i++) {
132 struct gred_sched_data *q = table->tab[i];
133 int n;
134
135 if (q == NULL)
136 continue;
137
138 for (n = 0; n < table->DPs; n++)
139 if (table->tab[n] && table->tab[n] != q &&
140 table->tab[n]->prio == q->prio)
141 return 1;
142 }
143
144 return 0;
145}
146
Thomas Graf22b33422005-11-05 21:14:16 +0100147static inline unsigned int gred_backlog(struct gred_sched *table,
148 struct gred_sched_data *q,
149 struct Qdisc *sch)
150{
151 if (gred_wred_mode(table))
152 return sch->qstats.backlog;
153 else
154 return q->backlog;
155}
156
Thomas Graf716a1b42005-11-05 21:14:20 +0100157static inline u16 tc_index_to_dp(struct sk_buff *skb)
158{
159 return skb->tc_index & GRED_VQ_MASK;
160}
161
Thomas Graf70517032005-11-05 21:14:23 +0100162static inline void gred_load_wred_set(struct gred_sched *table,
163 struct gred_sched_data *q)
164{
165 q->parms.qavg = table->wred_set.qavg;
166 q->parms.qidlestart = table->wred_set.qidlestart;
167}
168
169static inline void gred_store_wred_set(struct gred_sched *table,
170 struct gred_sched_data *q)
171{
172 table->wred_set.qavg = q->parms.qavg;
173}
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static int
176gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
177{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct gred_sched_data *q=NULL;
179 struct gred_sched *t= qdisc_priv(sch);
Thomas Graf22b33422005-11-05 21:14:16 +0100180 unsigned long qavg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int i=0;
Thomas Graf4a591832005-11-05 21:14:22 +0100182 u16 dp = tc_index_to_dp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Thomas Graf716a1b42005-11-05 21:14:20 +0100184 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
Thomas Graf18e3fb842005-11-05 21:14:21 +0100185 dp = t->def;
186
187 if ((q = t->tab[dp]) == NULL) {
188 /* Pass through packets not assigned to a DP
189 * if no default DP has been configured. This
190 * allows for DP flows to be left untouched.
191 */
192 if (skb_queue_len(&sch->q) < sch->dev->tx_queue_len)
193 return qdisc_enqueue_tail(skb, sch);
194 else
195 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Thomas Graf18e3fb842005-11-05 21:14:21 +0100197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* fix tc_index? --could be controvesial but needed for
199 requeueing */
Thomas Graf18e3fb842005-11-05 21:14:21 +0100200 skb->tc_index = (skb->tc_index & ~GRED_VQ_MASK) | dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* sum up all the qaves of prios <= to ours to get the new qave*/
Thomas Grafd6fd4e92005-11-05 21:14:10 +0100204 if (!gred_wred_mode(t) && gred_rio_mode(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 for (i=0;i<t->DPs;i++) {
206 if ((!t->tab[i]) || (i==q->DP))
207 continue;
208
Thomas Graf22b33422005-11-05 21:14:16 +0100209 if (t->tab[i]->prio < q->prio &&
210 !red_is_idling(&t->tab[i]->parms))
211 qavg +=t->tab[i]->parms.qavg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
214 }
215
216 q->packetsin++;
217 q->bytesin+=skb->len;
218
Thomas Grafdea3f622005-11-05 21:14:09 +0100219 if (gred_wred_mode(t)) {
Thomas Graf22b33422005-11-05 21:14:16 +0100220 qavg = 0;
Thomas Graf70517032005-11-05 21:14:23 +0100221 gred_load_wred_set(t, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Thomas Graf22b33422005-11-05 21:14:16 +0100224 q->parms.qavg = red_calc_qavg(&q->parms, gred_backlog(t, q, sch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Thomas Graf22b33422005-11-05 21:14:16 +0100226 if (red_is_idling(&q->parms))
227 red_end_of_idle_period(&q->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Thomas Grafdea3f622005-11-05 21:14:09 +0100229 if (gred_wred_mode(t))
Thomas Graf70517032005-11-05 21:14:23 +0100230 gred_store_wred_set(t, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Thomas Graf22b33422005-11-05 21:14:16 +0100232 switch (red_action(&q->parms, q->parms.qavg + qavg)) {
233 case RED_DONT_MARK:
234 break;
235
236 case RED_PROB_MARK:
237 sch->qstats.overlimits++;
238 q->stats.prob_drop++;
Thomas Grafc3b553c2005-11-05 21:14:18 +0100239 goto congestion_drop;
Thomas Graf22b33422005-11-05 21:14:16 +0100240
241 case RED_HARD_MARK:
242 sch->qstats.overlimits++;
243 q->stats.forced_drop++;
Thomas Grafc3b553c2005-11-05 21:14:18 +0100244 goto congestion_drop;
Thomas Graf22b33422005-11-05 21:14:16 +0100245 }
246
247 if (q->backlog + skb->len <= q->limit) {
248 q->backlog += skb->len;
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100249 return qdisc_enqueue_tail(skb, sch);
Thomas Graf22b33422005-11-05 21:14:16 +0100250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Thomas Graf22b33422005-11-05 21:14:16 +0100252 q->stats.pdrop++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253drop:
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100254 return qdisc_drop(skb, sch);
Thomas Grafc3b553c2005-11-05 21:14:18 +0100255
256congestion_drop:
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100257 qdisc_drop(skb, sch);
Thomas Grafc3b553c2005-11-05 21:14:18 +0100258 return NET_XMIT_CN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261static int
262gred_requeue(struct sk_buff *skb, struct Qdisc* sch)
263{
Thomas Graf716a1b42005-11-05 21:14:20 +0100264 struct gred_sched *t = qdisc_priv(sch);
Thomas Graf18e3fb842005-11-05 21:14:21 +0100265 struct gred_sched_data *q;
266 u16 dp = tc_index_to_dp(skb);
Thomas Graf22b33422005-11-05 21:14:16 +0100267
Thomas Graf18e3fb842005-11-05 21:14:21 +0100268 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
269 if (net_ratelimit())
270 printk(KERN_WARNING "GRED: Unable to relocate VQ 0x%x "
271 "for requeue, screwing up backlog.\n",
272 tc_index_to_dp(skb));
273 } else {
274 if (red_is_idling(&q->parms))
275 red_end_of_idle_period(&q->parms);
276 q->backlog += skb->len;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100279 return qdisc_requeue(skb, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static struct sk_buff *
283gred_dequeue(struct Qdisc* sch)
284{
285 struct sk_buff *skb;
286 struct gred_sched_data *q;
287 struct gred_sched *t= qdisc_priv(sch);
288
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100289 skb = qdisc_dequeue_head(sch);
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (skb) {
Thomas Graf18e3fb842005-11-05 21:14:21 +0100292 u16 dp = tc_index_to_dp(skb);
293
294 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
295 if (net_ratelimit())
296 printk(KERN_WARNING "GRED: Unable to relocate "
297 "VQ 0x%x after dequeue, screwing up "
298 "backlog.\n", tc_index_to_dp(skb));
299 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 q->backlog -= skb->len;
Thomas Graf18e3fb842005-11-05 21:14:21 +0100301
Thomas Grafdea3f622005-11-05 21:14:09 +0100302 if (!q->backlog && !gred_wred_mode(t))
Thomas Graf22b33422005-11-05 21:14:16 +0100303 red_start_of_idle_period(&q->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Thomas Graf18e3fb842005-11-05 21:14:21 +0100305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return skb;
307 }
308
Thomas Graf70517032005-11-05 21:14:23 +0100309 if (gred_wred_mode(t))
310 red_start_of_idle_period(&t->wred_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 return NULL;
313}
314
315static unsigned int gred_drop(struct Qdisc* sch)
316{
317 struct sk_buff *skb;
318
319 struct gred_sched_data *q;
320 struct gred_sched *t= qdisc_priv(sch);
321
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100322 skb = qdisc_dequeue_tail(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (skb) {
324 unsigned int len = skb->len;
Thomas Graf18e3fb842005-11-05 21:14:21 +0100325 u16 dp = tc_index_to_dp(skb);
326
327 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
328 if (net_ratelimit())
329 printk(KERN_WARNING "GRED: Unable to relocate "
330 "VQ 0x%x while dropping, screwing up "
331 "backlog.\n", tc_index_to_dp(skb));
332 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 q->backlog -= len;
Thomas Graf22b33422005-11-05 21:14:16 +0100334 q->stats.other++;
Thomas Graf18e3fb842005-11-05 21:14:21 +0100335
Thomas Grafdea3f622005-11-05 21:14:09 +0100336 if (!q->backlog && !gred_wred_mode(t))
Thomas Graf22b33422005-11-05 21:14:16 +0100337 red_start_of_idle_period(&q->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100340 qdisc_drop(skb, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return len;
342 }
343
Thomas Graf70517032005-11-05 21:14:23 +0100344 if (gred_wred_mode(t))
345 red_start_of_idle_period(&t->wred_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348
349}
350
351static void gred_reset(struct Qdisc* sch)
352{
353 int i;
354 struct gred_sched_data *q;
355 struct gred_sched *t= qdisc_priv(sch);
356
Thomas Grafedf7a7b2005-11-05 21:14:19 +0100357 qdisc_reset_queue(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 for (i=0;i<t->DPs;i++) {
360 q= t->tab[i];
361 if (!q)
362 continue;
Thomas Graf22b33422005-11-05 21:14:16 +0100363 red_restart(&q->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 q->backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366}
367
Thomas Graf66396072005-11-05 21:14:13 +0100368static inline void gred_destroy_vq(struct gred_sched_data *q)
369{
370 kfree(q);
371}
372
373static inline int gred_change_table_def(struct Qdisc *sch, struct rtattr *dps)
374{
375 struct gred_sched *table = qdisc_priv(sch);
376 struct tc_gred_sopt *sopt;
377 int i;
378
379 if (dps == NULL || RTA_PAYLOAD(dps) < sizeof(*sopt))
380 return -EINVAL;
381
382 sopt = RTA_DATA(dps);
383
384 if (sopt->DPs > MAX_DPs || sopt->DPs == 0 || sopt->def_DP >= sopt->DPs)
385 return -EINVAL;
386
387 sch_tree_lock(sch);
388 table->DPs = sopt->DPs;
389 table->def = sopt->def_DP;
390
391 /*
392 * Every entry point to GRED is synchronized with the above code
393 * and the DP is checked against DPs, i.e. shadowed VQs can no
394 * longer be found so we can unlock right here.
395 */
396 sch_tree_unlock(sch);
397
398 if (sopt->grio) {
399 gred_enable_rio_mode(table);
400 gred_disable_wred_mode(table);
401 if (gred_wred_mode_check(sch))
402 gred_enable_wred_mode(table);
403 } else {
404 gred_disable_rio_mode(table);
405 gred_disable_wred_mode(table);
406 }
407
408 for (i = table->DPs; i < MAX_DPs; i++) {
409 if (table->tab[i]) {
410 printk(KERN_WARNING "GRED: Warning: Destroying "
411 "shadowed VQ 0x%x\n", i);
412 gred_destroy_vq(table->tab[i]);
413 table->tab[i] = NULL;
414 }
415 }
416
Thomas Graf66396072005-11-05 21:14:13 +0100417 return 0;
418}
419
Thomas Graff62d6b92005-11-05 21:14:15 +0100420static inline int gred_change_vq(struct Qdisc *sch, int dp,
421 struct tc_gred_qopt *ctl, int prio, u8 *stab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct gred_sched *table = qdisc_priv(sch);
424 struct gred_sched_data *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Thomas Graff62d6b92005-11-05 21:14:15 +0100426 if (table->tab[dp] == NULL) {
427 table->tab[dp] = kmalloc(sizeof(*q), GFP_KERNEL);
428 if (table->tab[dp] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -ENOMEM;
Thomas Graff62d6b92005-11-05 21:14:15 +0100430 memset(table->tab[dp], 0, sizeof(*q));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
Thomas Graff62d6b92005-11-05 21:14:15 +0100433 q = table->tab[dp];
434 q->DP = dp;
435 q->prio = prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 q->limit = ctl->limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Thomas Graf22b33422005-11-05 21:14:16 +0100438 if (q->backlog == 0)
439 red_end_of_idle_period(&q->parms);
440
441 red_set_parms(&q->parms,
442 ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog,
443 ctl->Scell_log, stab);
444
Thomas Graff62d6b92005-11-05 21:14:15 +0100445 return 0;
446}
447
448static int gred_change(struct Qdisc *sch, struct rtattr *opt)
449{
450 struct gred_sched *table = qdisc_priv(sch);
451 struct tc_gred_qopt *ctl;
452 struct rtattr *tb[TCA_GRED_MAX];
453 int err = -EINVAL, prio = GRED_DEF_PRIO;
454 u8 *stab;
455
456 if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
457 return -EINVAL;
458
459 if (tb[TCA_GRED_PARMS-1] == NULL && tb[TCA_GRED_STAB-1] == NULL)
460 return gred_change_table_def(sch, opt);
461
462 if (tb[TCA_GRED_PARMS-1] == NULL ||
463 RTA_PAYLOAD(tb[TCA_GRED_PARMS-1]) < sizeof(*ctl) ||
464 tb[TCA_GRED_STAB-1] == NULL ||
465 RTA_PAYLOAD(tb[TCA_GRED_STAB-1]) < 256)
466 return -EINVAL;
467
468 ctl = RTA_DATA(tb[TCA_GRED_PARMS-1]);
469 stab = RTA_DATA(tb[TCA_GRED_STAB-1]);
470
471 if (ctl->DP >= table->DPs)
472 goto errout;
473
474 if (gred_rio_mode(table)) {
475 if (ctl->prio == 0) {
476 int def_prio = GRED_DEF_PRIO;
477
478 if (table->tab[table->def])
479 def_prio = table->tab[table->def]->prio;
480
481 printk(KERN_DEBUG "GRED: DP %u does not have a prio "
482 "setting default to %d\n", ctl->DP, def_prio);
483
484 prio = def_prio;
485 } else
486 prio = ctl->prio;
487 }
488
489 sch_tree_lock(sch);
490
491 err = gred_change_vq(sch, ctl->DP, ctl, prio, stab);
492 if (err < 0)
493 goto errout_locked;
494
495 if (table->tab[table->def] == NULL) {
496 if (gred_rio_mode(table))
497 prio = table->tab[ctl->DP]->prio;
498
499 err = gred_change_vq(sch, table->def, ctl, prio, stab);
500 if (err < 0)
501 goto errout_locked;
502 }
503
Thomas Grafd6fd4e92005-11-05 21:14:10 +0100504 if (gred_rio_mode(table)) {
Thomas Grafdea3f622005-11-05 21:14:09 +0100505 gred_disable_wred_mode(table);
506 if (gred_wred_mode_check(sch))
507 gred_enable_wred_mode(table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
Thomas Graff62d6b92005-11-05 21:14:15 +0100510 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Thomas Graff62d6b92005-11-05 21:14:15 +0100512errout_locked:
513 sch_tree_unlock(sch);
514errout:
515 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518static int gred_init(struct Qdisc *sch, struct rtattr *opt)
519{
Thomas Graf66396072005-11-05 21:14:13 +0100520 struct rtattr *tb[TCA_GRED_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Thomas Graf66396072005-11-05 21:14:13 +0100522 if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -EINVAL;
524
Thomas Graf66396072005-11-05 21:14:13 +0100525 if (tb[TCA_GRED_PARMS-1] || tb[TCA_GRED_STAB-1])
526 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Thomas Graf66396072005-11-05 21:14:13 +0100528 return gred_change_table_def(sch, tb[TCA_GRED_DPS-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
532{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 struct gred_sched *table = qdisc_priv(sch);
Thomas Graf05f1cc02005-11-05 21:14:11 +0100534 struct rtattr *parms, *opts = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int i;
Thomas Grafe0636822005-11-05 21:14:12 +0100536 struct tc_gred_sopt sopt = {
537 .DPs = table->DPs,
538 .def_DP = table->def,
539 .grio = gred_rio_mode(table),
540 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Thomas Graf05f1cc02005-11-05 21:14:11 +0100542 opts = RTA_NEST(skb, TCA_OPTIONS);
Thomas Grafe0636822005-11-05 21:14:12 +0100543 RTA_PUT(skb, TCA_GRED_DPS, sizeof(sopt), &sopt);
Thomas Graf05f1cc02005-11-05 21:14:11 +0100544 parms = RTA_NEST(skb, TCA_GRED_PARMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Thomas Graf05f1cc02005-11-05 21:14:11 +0100546 for (i = 0; i < MAX_DPs; i++) {
547 struct gred_sched_data *q = table->tab[i];
548 struct tc_gred_qopt opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Thomas Graf05f1cc02005-11-05 21:14:11 +0100550 memset(&opt, 0, sizeof(opt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (!q) {
553 /* hack -- fix at some point with proper message
554 This is how we indicate to tc that there is no VQ
555 at this DP */
556
Thomas Graf05f1cc02005-11-05 21:14:11 +0100557 opt.DP = MAX_DPs + i;
558 goto append_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
Thomas Graf05f1cc02005-11-05 21:14:11 +0100561 opt.limit = q->limit;
562 opt.DP = q->DP;
563 opt.backlog = q->backlog;
564 opt.prio = q->prio;
Thomas Graf22b33422005-11-05 21:14:16 +0100565 opt.qth_min = q->parms.qth_min >> q->parms.Wlog;
566 opt.qth_max = q->parms.qth_max >> q->parms.Wlog;
567 opt.Wlog = q->parms.Wlog;
568 opt.Plog = q->parms.Plog;
569 opt.Scell_log = q->parms.Scell_log;
570 opt.other = q->stats.other;
571 opt.early = q->stats.prob_drop;
572 opt.forced = q->stats.forced_drop;
573 opt.pdrop = q->stats.pdrop;
Thomas Graf05f1cc02005-11-05 21:14:11 +0100574 opt.packets = q->packetsin;
575 opt.bytesin = q->bytesin;
576
Thomas Graf22b33422005-11-05 21:14:16 +0100577 if (gred_wred_mode(table)) {
578 q->parms.qidlestart =
579 table->tab[table->def]->parms.qidlestart;
580 q->parms.qavg = table->tab[table->def]->parms.qavg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Thomas Graf22b33422005-11-05 21:14:16 +0100583 opt.qave = red_calc_qavg(&q->parms, q->parms.qavg);
584
Thomas Graf05f1cc02005-11-05 21:14:11 +0100585append_opt:
586 RTA_APPEND(skb, sizeof(opt), &opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
Thomas Graf05f1cc02005-11-05 21:14:11 +0100589 RTA_NEST_END(skb, parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Thomas Graf05f1cc02005-11-05 21:14:11 +0100591 return RTA_NEST_END(skb, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593rtattr_failure:
Thomas Graf05f1cc02005-11-05 21:14:11 +0100594 return RTA_NEST_CANCEL(skb, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597static void gred_destroy(struct Qdisc *sch)
598{
599 struct gred_sched *table = qdisc_priv(sch);
600 int i;
601
602 for (i = 0;i < table->DPs; i++) {
603 if (table->tab[i])
Thomas Graf66396072005-11-05 21:14:13 +0100604 gred_destroy_vq(table->tab[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606}
607
608static struct Qdisc_ops gred_qdisc_ops = {
609 .next = NULL,
610 .cl_ops = NULL,
611 .id = "gred",
612 .priv_size = sizeof(struct gred_sched),
613 .enqueue = gred_enqueue,
614 .dequeue = gred_dequeue,
615 .requeue = gred_requeue,
616 .drop = gred_drop,
617 .init = gred_init,
618 .reset = gred_reset,
619 .destroy = gred_destroy,
620 .change = gred_change,
621 .dump = gred_dump,
622 .owner = THIS_MODULE,
623};
624
625static int __init gred_module_init(void)
626{
627 return register_qdisc(&gred_qdisc_ops);
628}
629static void __exit gred_module_exit(void)
630{
631 unregister_qdisc(&gred_qdisc_ops);
632}
633module_init(gred_module_init)
634module_exit(gred_module_exit)
635MODULE_LICENSE("GPL");