2 * net/sched/sch_gred.c Generic Random Early Detection queue.
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.
10 * Authors: J Hadi Salim (hadi@cyberus.ca) 1998-2002
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
18 * For all the glorious comments look at include/net/red.h
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
27 #include <net/pkt_sched.h>
30 #define GRED_DEF_PRIO (MAX_DPs / 2)
31 #define GRED_VQ_MASK (MAX_DPs - 1)
33 struct gred_sched_data;
36 struct gred_sched_data
38 u32 limit; /* HARD maximal queue length */
39 u32 DP; /* the drop pramaters */
40 u32 bytesin; /* bytes seen on virtualQ so far*/
41 u32 packetsin; /* packets seen on virtualQ so far*/
42 u32 backlog; /* bytes on the virtualQ */
43 u8 prio; /* the prio of this vq */
45 struct red_parms parms;
46 struct red_stats stats;
56 struct gred_sched_data *tab[MAX_DPs];
61 struct red_parms wred_set;
64 static inline int gred_wred_mode(struct gred_sched *table)
66 return test_bit(GRED_WRED_MODE, &table->flags);
69 static inline void gred_enable_wred_mode(struct gred_sched *table)
71 __set_bit(GRED_WRED_MODE, &table->flags);
74 static inline void gred_disable_wred_mode(struct gred_sched *table)
76 __clear_bit(GRED_WRED_MODE, &table->flags);
79 static inline int gred_rio_mode(struct gred_sched *table)
81 return test_bit(GRED_RIO_MODE, &table->flags);
84 static inline void gred_enable_rio_mode(struct gred_sched *table)
86 __set_bit(GRED_RIO_MODE, &table->flags);
89 static inline void gred_disable_rio_mode(struct gred_sched *table)
91 __clear_bit(GRED_RIO_MODE, &table->flags);
94 static inline int gred_wred_mode_check(struct Qdisc *sch)
96 struct gred_sched *table = qdisc_priv(sch);
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];
107 for (n = 0; n < table->DPs; n++)
108 if (table->tab[n] && table->tab[n] != q &&
109 table->tab[n]->prio == q->prio)
116 static inline unsigned int gred_backlog(struct gred_sched *table,
117 struct gred_sched_data *q,
120 if (gred_wred_mode(table))
121 return sch->qstats.backlog;
126 static inline u16 tc_index_to_dp(struct sk_buff *skb)
128 return skb->tc_index & GRED_VQ_MASK;
131 static inline void gred_load_wred_set(struct gred_sched *table,
132 struct gred_sched_data *q)
134 q->parms.qavg = table->wred_set.qavg;
135 q->parms.qidlestart = table->wred_set.qidlestart;
138 static inline void gred_store_wred_set(struct gred_sched *table,
139 struct gred_sched_data *q)
141 table->wred_set.qavg = q->parms.qavg;
144 static inline int gred_use_ecn(struct gred_sched *t)
146 return t->red_flags & TC_RED_ECN;
149 static int gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
151 struct gred_sched_data *q=NULL;
152 struct gred_sched *t= qdisc_priv(sch);
153 unsigned long qavg = 0;
154 u16 dp = tc_index_to_dp(skb);
156 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
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.
164 if (skb_queue_len(&sch->q) < sch->dev->tx_queue_len)
165 return qdisc_enqueue_tail(skb, sch);
170 /* fix tc_index? --could be controvesial but needed for
172 skb->tc_index = (skb->tc_index & ~GRED_VQ_MASK) | dp;
175 /* sum up all the qaves of prios <= to ours to get the new qave */
176 if (!gred_wred_mode(t) && gred_rio_mode(t)) {
179 for (i = 0; i < t->DPs; i++) {
180 if (t->tab[i] && t->tab[i]->prio < q->prio &&
181 !red_is_idling(&t->tab[i]->parms))
182 qavg +=t->tab[i]->parms.qavg;
188 q->bytesin += skb->len;
190 if (gred_wred_mode(t))
191 gred_load_wred_set(t, q);
193 q->parms.qavg = red_calc_qavg(&q->parms, gred_backlog(t, q, sch));
195 if (red_is_idling(&q->parms))
196 red_end_of_idle_period(&q->parms);
198 if (gred_wred_mode(t))
199 gred_store_wred_set(t, q);
201 switch (red_action(&q->parms, q->parms.qavg + qavg)) {
206 sch->qstats.overlimits++;
207 if (!gred_use_ecn(t) || !INET_ECN_set_ce(skb)) {
208 q->stats.prob_drop++;
209 goto congestion_drop;
212 q->stats.prob_mark++;
216 sch->qstats.overlimits++;
217 if (!gred_use_ecn(t) || !INET_ECN_set_ce(skb)) {
218 q->stats.forced_drop++;
219 goto congestion_drop;
221 q->stats.forced_mark++;
225 if (q->backlog + skb->len <= q->limit) {
226 q->backlog += skb->len;
227 return qdisc_enqueue_tail(skb, sch);
232 return qdisc_drop(skb, sch);
235 qdisc_drop(skb, sch);
239 static int gred_requeue(struct sk_buff *skb, struct Qdisc* sch)
241 struct gred_sched *t = qdisc_priv(sch);
242 struct gred_sched_data *q;
243 u16 dp = tc_index_to_dp(skb);
245 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
247 printk(KERN_WARNING "GRED: Unable to relocate VQ 0x%x "
248 "for requeue, screwing up backlog.\n",
249 tc_index_to_dp(skb));
251 if (red_is_idling(&q->parms))
252 red_end_of_idle_period(&q->parms);
253 q->backlog += skb->len;
256 return qdisc_requeue(skb, sch);
259 static struct sk_buff *gred_dequeue(struct Qdisc* sch)
262 struct gred_sched *t = qdisc_priv(sch);
264 skb = qdisc_dequeue_head(sch);
267 struct gred_sched_data *q;
268 u16 dp = tc_index_to_dp(skb);
270 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
272 printk(KERN_WARNING "GRED: Unable to relocate "
273 "VQ 0x%x after dequeue, screwing up "
274 "backlog.\n", tc_index_to_dp(skb));
276 q->backlog -= skb->len;
278 if (!q->backlog && !gred_wred_mode(t))
279 red_start_of_idle_period(&q->parms);
285 if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
286 red_start_of_idle_period(&t->wred_set);
291 static unsigned int gred_drop(struct Qdisc* sch)
294 struct gred_sched *t = qdisc_priv(sch);
296 skb = qdisc_dequeue_tail(sch);
298 unsigned int len = skb->len;
299 struct gred_sched_data *q;
300 u16 dp = tc_index_to_dp(skb);
302 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
304 printk(KERN_WARNING "GRED: Unable to relocate "
305 "VQ 0x%x while dropping, screwing up "
306 "backlog.\n", tc_index_to_dp(skb));
311 if (!q->backlog && !gred_wred_mode(t))
312 red_start_of_idle_period(&q->parms);
315 qdisc_drop(skb, sch);
319 if (gred_wred_mode(t) && !red_is_idling(&t->wred_set))
320 red_start_of_idle_period(&t->wred_set);
326 static void gred_reset(struct Qdisc* sch)
329 struct gred_sched *t = qdisc_priv(sch);
331 qdisc_reset_queue(sch);
333 for (i = 0; i < t->DPs; i++) {
334 struct gred_sched_data *q = t->tab[i];
339 red_restart(&q->parms);
344 static inline void gred_destroy_vq(struct gred_sched_data *q)
349 static inline int gred_change_table_def(struct Qdisc *sch, struct rtattr *dps)
351 struct gred_sched *table = qdisc_priv(sch);
352 struct tc_gred_sopt *sopt;
355 if (dps == NULL || RTA_PAYLOAD(dps) < sizeof(*sopt))
358 sopt = RTA_DATA(dps);
360 if (sopt->DPs > MAX_DPs || sopt->DPs == 0 || sopt->def_DP >= sopt->DPs)
364 table->DPs = sopt->DPs;
365 table->def = sopt->def_DP;
366 table->red_flags = sopt->flags;
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.
373 sch_tree_unlock(sch);
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);
381 gred_disable_rio_mode(table);
382 gred_disable_wred_mode(table);
385 for (i = table->DPs; i < MAX_DPs; 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;
397 static inline int gred_change_vq(struct Qdisc *sch, int dp,
398 struct tc_gred_qopt *ctl, int prio, u8 *stab)
400 struct gred_sched *table = qdisc_priv(sch);
401 struct gred_sched_data *q;
403 if (table->tab[dp] == NULL) {
404 table->tab[dp] = kmalloc(sizeof(*q), GFP_KERNEL);
405 if (table->tab[dp] == NULL)
407 memset(table->tab[dp], 0, sizeof(*q));
413 q->limit = ctl->limit;
416 red_end_of_idle_period(&q->parms);
418 red_set_parms(&q->parms,
419 ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog,
420 ctl->Scell_log, stab);
425 static int gred_change(struct Qdisc *sch, struct rtattr *opt)
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;
433 if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
436 if (tb[TCA_GRED_PARMS-1] == NULL && tb[TCA_GRED_STAB-1] == NULL)
437 return gred_change_table_def(sch, opt);
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)
445 ctl = RTA_DATA(tb[TCA_GRED_PARMS-1]);
446 stab = RTA_DATA(tb[TCA_GRED_STAB-1]);
448 if (ctl->DP >= table->DPs)
451 if (gred_rio_mode(table)) {
452 if (ctl->prio == 0) {
453 int def_prio = GRED_DEF_PRIO;
455 if (table->tab[table->def])
456 def_prio = table->tab[table->def]->prio;
458 printk(KERN_DEBUG "GRED: DP %u does not have a prio "
459 "setting default to %d\n", ctl->DP, def_prio);
468 err = gred_change_vq(sch, ctl->DP, ctl, prio, stab);
472 if (gred_rio_mode(table)) {
473 gred_disable_wred_mode(table);
474 if (gred_wred_mode_check(sch))
475 gred_enable_wred_mode(table);
481 sch_tree_unlock(sch);
486 static int gred_init(struct Qdisc *sch, struct rtattr *opt)
488 struct rtattr *tb[TCA_GRED_MAX];
490 if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
493 if (tb[TCA_GRED_PARMS-1] || tb[TCA_GRED_STAB-1])
496 return gred_change_table_def(sch, tb[TCA_GRED_DPS-1]);
499 static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
501 struct gred_sched *table = qdisc_priv(sch);
502 struct rtattr *parms, *opts = NULL;
504 struct tc_gred_sopt sopt = {
506 .def_DP = table->def,
507 .grio = gred_rio_mode(table),
508 .flags = table->red_flags,
511 opts = RTA_NEST(skb, TCA_OPTIONS);
512 RTA_PUT(skb, TCA_GRED_DPS, sizeof(sopt), &sopt);
513 parms = RTA_NEST(skb, TCA_GRED_PARMS);
515 for (i = 0; i < MAX_DPs; i++) {
516 struct gred_sched_data *q = table->tab[i];
517 struct tc_gred_qopt opt;
519 memset(&opt, 0, sizeof(opt));
522 /* hack -- fix at some point with proper message
523 This is how we indicate to tc that there is no VQ
526 opt.DP = MAX_DPs + i;
530 opt.limit = q->limit;
532 opt.backlog = q->backlog;
534 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;
543 opt.packets = q->packetsin;
544 opt.bytesin = q->bytesin;
546 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;
552 opt.qave = red_calc_qavg(&q->parms, q->parms.qavg);
555 RTA_APPEND(skb, sizeof(opt), &opt);
558 RTA_NEST_END(skb, parms);
560 return RTA_NEST_END(skb, opts);
563 return RTA_NEST_CANCEL(skb, opts);
566 static void gred_destroy(struct Qdisc *sch)
568 struct gred_sched *table = qdisc_priv(sch);
571 for (i = 0; i < table->DPs; i++) {
573 gred_destroy_vq(table->tab[i]);
577 static struct Qdisc_ops gred_qdisc_ops = {
579 .priv_size = sizeof(struct gred_sched),
580 .enqueue = gred_enqueue,
581 .dequeue = gred_dequeue,
582 .requeue = gred_requeue,
586 .destroy = gred_destroy,
587 .change = gred_change,
589 .owner = THIS_MODULE,
592 static int __init gred_module_init(void)
594 return register_qdisc(&gred_qdisc_ops);
597 static void __exit gred_module_exit(void)
599 unregister_qdisc(&gred_qdisc_ops);
602 module_init(gred_module_init)
603 module_exit(gred_module_exit)
605 MODULE_LICENSE("GPL");