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
20 * For all the glorious comments look at Alexey's sch_red.c
23 #include <linux/config.h>
24 #include <linux/module.h>
25 #include <asm/uaccess.h>
26 #include <asm/system.h>
27 #include <asm/bitops.h>
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/string.h>
33 #include <linux/socket.h>
34 #include <linux/sockios.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>
44 #include <net/route.h>
45 #include <linux/skbuff.h>
47 #include <net/pkt_sched.h>
50 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
52 #define DPRINTK(format,args...)
56 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
58 #define D2PRINTK(format,args...)
61 struct gred_sched_data;
64 struct gred_sched_data
67 u32 limit; /* HARD maximal queue length */
68 u32 qth_min; /* Min average length threshold: A scaled */
69 u32 qth_max; /* Max average length threshold: A scaled */
70 u32 DP; /* the drop pramaters */
71 char Wlog; /* log(W) */
72 char Plog; /* random number bits */
75 u32 bytesin; /* bytes seen on virtualQ so far*/
76 u32 packetsin; /* packets seen on virtualQ so far*/
77 u32 backlog; /* bytes on the virtualQ */
78 u32 forced; /* packets dropped for exceeding limits */
79 u32 early; /* packets dropped as a warning */
80 u32 other; /* packets dropped by invoking drop() */
81 u32 pdrop; /* packets dropped because we exceeded physical queue limits */
84 u8 prio; /* the prio of this vq */
87 unsigned long qave; /* Average queue length: A scaled */
88 int qcount; /* Packets since last random number generation */
89 u32 qR; /* Cached random number */
91 psched_time_t qidlestart; /* Start of idle period */
96 struct gred_sched_data *tab[MAX_DPs];
105 gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
108 struct gred_sched_data *q=NULL;
109 struct gred_sched *t= (struct gred_sched *)sch->data;
110 unsigned long qave=0;
113 if (!t->initd && skb_queue_len(&sch->q) < (sch->dev->tx_queue_len ? : 1)) {
114 D2PRINTK("NO GRED Queues setup yet! Enqueued anyway\n");
119 if ( ((skb->tc_index&0xf) > (t->DPs -1)) || !(q=t->tab[skb->tc_index&0xf])) {
120 printk("GRED: setting to default (%d)\n ",t->def);
121 if (!(q=t->tab[t->def])) {
122 DPRINTK("GRED: setting to default FAILED! dropping!! "
126 /* fix tc_index? --could be controvesial but needed for
128 skb->tc_index=(skb->tc_index&0xfffffff0) | t->def;
131 D2PRINTK("gred_enqueue virtualQ 0x%x classid %x backlog %d "
132 "general backlog %d\n",skb->tc_index&0xf,sch->handle,q->backlog,
134 /* sum up all the qaves of prios <= to ours to get the new qave*/
135 if (!t->eqp && t->grio) {
136 for (i=0;i<t->DPs;i++) {
137 if ((!t->tab[i]) || (i==q->DP))
140 if ((t->tab[i]->prio < q->prio) && (PSCHED_IS_PASTPERFECT(t->tab[i]->qidlestart)))
141 qave +=t->tab[i]->qave;
147 q->bytesin+=skb->len;
149 if (t->eqp && t->grio) {
151 q->qave=t->tab[t->def]->qave;
152 q->qidlestart=t->tab[t->def]->qidlestart;
155 if (!PSCHED_IS_PASTPERFECT(q->qidlestart)) {
157 PSCHED_GET_TIME(now);
158 us_idle = PSCHED_TDIFF_SAFE(now, q->qidlestart, q->Scell_max, 0);
159 PSCHED_SET_PASTPERFECT(q->qidlestart);
161 q->qave >>= q->Stab[(us_idle>>q->Scell_log)&0xFF];
164 q->qave += sch->stats.backlog - (q->qave >> q->Wlog);
166 q->qave += q->backlog - (q->qave >> q->Wlog);
172 if (t->eqp && t->grio)
173 t->tab[t->def]->qave=q->qave;
175 if ((q->qave+qave) < q->qth_min) {
178 if (q->backlog + skb->len <= q->limit) {
179 q->backlog += skb->len;
181 __skb_queue_tail(&sch->q, skb);
182 sch->stats.backlog += skb->len;
183 sch->stats.bytes += skb->len;
184 sch->stats.packets++;
193 return NET_XMIT_DROP;
195 if ((q->qave+qave) >= q->qth_max) {
197 sch->stats.overlimits++;
202 if ((((qave+q->qave) - q->qth_min)>>q->Wlog)*q->qcount < q->qR)
205 q->qR = net_random()&q->Rmask;
206 sch->stats.overlimits++;
210 q->qR = net_random()&q->Rmask;
215 gred_requeue(struct sk_buff *skb, struct Qdisc* sch)
217 struct gred_sched_data *q;
218 struct gred_sched *t= (struct gred_sched *)sch->data;
219 q= t->tab[(skb->tc_index&0xf)];
220 /* error checking here -- probably unnecessary */
221 PSCHED_SET_PASTPERFECT(q->qidlestart);
223 __skb_queue_head(&sch->q, skb);
224 sch->stats.backlog += skb->len;
225 q->backlog += skb->len;
229 static struct sk_buff *
230 gred_dequeue(struct Qdisc* sch)
233 struct gred_sched_data *q;
234 struct gred_sched *t= (struct gred_sched *)sch->data;
236 skb = __skb_dequeue(&sch->q);
238 sch->stats.backlog -= skb->len;
239 q= t->tab[(skb->tc_index&0xf)];
241 q->backlog -= skb->len;
242 if (!q->backlog && !t->eqp)
243 PSCHED_GET_TIME(q->qidlestart);
245 D2PRINTK("gred_dequeue: skb has bad tcindex %x\n",skb->tc_index&0xf);
253 D2PRINTK("no default VQ set: Results will be "
256 PSCHED_GET_TIME(q->qidlestart);
262 static unsigned int gred_drop(struct Qdisc* sch)
266 struct gred_sched_data *q;
267 struct gred_sched *t= (struct gred_sched *)sch->data;
269 skb = __skb_dequeue_tail(&sch->q);
271 unsigned int len = skb->len;
272 sch->stats.backlog -= len;
274 q= t->tab[(skb->tc_index&0xf)];
278 if (!q->backlog && !t->eqp)
279 PSCHED_GET_TIME(q->qidlestart);
281 D2PRINTK("gred_dequeue: skb has bad tcindex %x\n",skb->tc_index&0xf);
290 D2PRINTK("no default VQ set: Results might be screwed up\n");
294 PSCHED_GET_TIME(q->qidlestart);
299 static void gred_reset(struct Qdisc* sch)
302 struct gred_sched_data *q;
303 struct gred_sched *t= (struct gred_sched *)sch->data;
305 __skb_queue_purge(&sch->q);
307 sch->stats.backlog = 0;
309 for (i=0;i<t->DPs;i++) {
313 PSCHED_SET_PASTPERFECT(q->qidlestart);
324 static int gred_change(struct Qdisc *sch, struct rtattr *opt)
326 struct gred_sched *table = (struct gred_sched *)sch->data;
327 struct gred_sched_data *q;
328 struct tc_gred_qopt *ctl;
329 struct tc_gred_sopt *sopt;
330 struct rtattr *tb[TCA_GRED_STAB];
331 struct rtattr *tb2[TCA_GRED_DPS];
335 rtattr_parse(tb, TCA_GRED_STAB, RTA_DATA(opt), RTA_PAYLOAD(opt)) )
338 if (tb[TCA_GRED_PARMS-1] == 0 && tb[TCA_GRED_STAB-1] == 0) {
339 rtattr_parse(tb2, TCA_GRED_DPS, RTA_DATA(opt),
342 if (tb2[TCA_GRED_DPS-1] == 0)
345 sopt = RTA_DATA(tb2[TCA_GRED_DPS-1]);
346 table->DPs=sopt->DPs;
347 table->def=sopt->def_DP;
348 table->grio=sopt->grio;
350 /* probably need to clear all the table DP entries as well */
356 if (!table->DPs || tb[TCA_GRED_PARMS-1] == 0 || tb[TCA_GRED_STAB-1] == 0 ||
357 RTA_PAYLOAD(tb[TCA_GRED_PARMS-1]) < sizeof(*ctl) ||
358 RTA_PAYLOAD(tb[TCA_GRED_STAB-1]) < 256)
361 ctl = RTA_DATA(tb[TCA_GRED_PARMS-1]);
362 if (ctl->DP > MAX_DPs-1 ) {
363 /* misbehaving is punished! Put in the default drop probability */
364 DPRINTK("\nGRED: DP %u not in the proper range fixed. New DP "
365 "set to default at %d\n",ctl->DP,table->def);
369 if (table->tab[ctl->DP] == NULL) {
370 table->tab[ctl->DP]=kmalloc(sizeof(struct gred_sched_data),
372 if (NULL == table->tab[ctl->DP])
374 memset(table->tab[ctl->DP], 0, (sizeof(struct gred_sched_data)));
376 q= table->tab[ctl->DP];
380 if (table->def && table->tab[table->def]) {
381 DPRINTK("\nGRED: DP %u does not have a prio"
382 "setting default to %d\n",ctl->DP,
383 table->tab[table->def]->prio);
384 q->prio=table->tab[table->def]->prio;
386 DPRINTK("\nGRED: DP %u does not have a prio"
387 " setting default to 8\n",ctl->DP);
401 q->limit = ctl->limit;
402 q->Scell_log = ctl->Scell_log;
403 q->Rmask = ctl->Plog < 32 ? ((1<<ctl->Plog) - 1) : ~0UL;
404 q->Scell_max = (255<<q->Scell_log);
405 q->qth_min = ctl->qth_min<<ctl->Wlog;
406 q->qth_max = ctl->qth_max<<ctl->Wlog;
415 PSCHED_SET_PASTPERFECT(q->qidlestart);
416 memcpy(q->Stab, RTA_DATA(tb[TCA_GRED_STAB-1]), 256);
418 if ( table->initd && table->grio) {
419 /* this looks ugly but its not in the fast path */
420 for (i=0;i<table->DPs;i++) {
421 if ((!table->tab[i]) || (i==q->DP) )
423 if (table->tab[i]->prio == q->prio ){
424 /* WRED mode detected */
434 the first entry also goes into the default until
438 if (table->tab[table->def] == NULL) {
439 table->tab[table->def]=
440 kmalloc(sizeof(struct gred_sched_data), GFP_KERNEL);
441 if (NULL == table->tab[table->def])
444 memset(table->tab[table->def], 0,
445 (sizeof(struct gred_sched_data)));
447 q= table->tab[table->def];
451 q->limit = ctl->limit;
452 q->Scell_log = ctl->Scell_log;
453 q->Rmask = ctl->Plog < 32 ? ((1<<ctl->Plog) - 1) : ~0UL;
454 q->Scell_max = (255<<q->Scell_log);
455 q->qth_min = ctl->qth_min<<ctl->Wlog;
456 q->qth_max = ctl->qth_max<<ctl->Wlog;
459 q->prio=table->tab[ctl->DP]->prio;
464 PSCHED_SET_PASTPERFECT(q->qidlestart);
465 memcpy(q->Stab, RTA_DATA(tb[TCA_GRED_STAB-1]), 256);
471 static int gred_init(struct Qdisc *sch, struct rtattr *opt)
473 struct gred_sched *table = (struct gred_sched *)sch->data;
474 struct tc_gred_sopt *sopt;
475 struct rtattr *tb[TCA_GRED_STAB];
476 struct rtattr *tb2[TCA_GRED_DPS];
479 rtattr_parse(tb, TCA_GRED_STAB, RTA_DATA(opt), RTA_PAYLOAD(opt)) )
482 if (tb[TCA_GRED_PARMS-1] == 0 && tb[TCA_GRED_STAB-1] == 0 ) {
483 rtattr_parse(tb2, TCA_GRED_DPS, RTA_DATA(opt),RTA_PAYLOAD(opt));
485 if (tb2[TCA_GRED_DPS-1] == 0)
488 sopt = RTA_DATA(tb2[TCA_GRED_DPS-1]);
489 table->DPs=sopt->DPs;
490 table->def=sopt->def_DP;
491 table->grio=sopt->grio;
497 DPRINTK("\n GRED_INIT error!\n");
501 static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
505 struct tc_gred_qopt *opt = NULL ;
506 struct tc_gred_qopt *dst;
507 struct gred_sched *table = (struct gred_sched *)sch->data;
508 struct gred_sched_data *q;
510 unsigned char *b = skb->tail;
512 rta = (struct rtattr*)b;
513 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
515 opt=kmalloc(sizeof(struct tc_gred_qopt)*MAX_DPs, GFP_KERNEL);
518 DPRINTK("gred_dump:failed to malloc for %Zd\n",
519 sizeof(struct tc_gred_qopt)*MAX_DPs);
523 memset(opt, 0, (sizeof(struct tc_gred_qopt))*table->DPs);
526 DPRINTK("NO GRED Queues setup!\n");
529 for (i=0;i<MAX_DPs;i++) {
534 /* hack -- fix at some point with proper message
535 This is how we indicate to tc that there is no VQ
543 dst->qth_min=q->qth_min>>q->Wlog;
544 dst->qth_max=q->qth_max>>q->Wlog;
546 dst->backlog=q->backlog;
548 if (table->eqp && table->grio) {
549 q->qidlestart=table->tab[table->def]->qidlestart;
550 q->qave=table->tab[table->def]->qave;
552 if (!PSCHED_IS_PASTPERFECT(q->qidlestart)) {
555 PSCHED_GET_TIME(now);
556 idle = PSCHED_TDIFF_SAFE(now, q->qidlestart, q->Scell_max, 0);
557 qave = q->qave >> q->Stab[(idle>>q->Scell_log)&0xFF];
558 dst->qave = qave >> q->Wlog;
561 dst->qave = q->qave >> q->Wlog;
570 dst->Scell_log = q->Scell_log;
571 dst->other = q->other;
572 dst->forced = q->forced;
573 dst->early = q->early;
574 dst->pdrop = q->pdrop;
576 dst->packets=q->packetsin;
577 dst->bytesin=q->bytesin;
580 RTA_PUT(skb, TCA_GRED_PARMS, sizeof(struct tc_gred_qopt)*MAX_DPs, opt);
581 rta->rta_len = skb->tail - b;
589 DPRINTK("gred_dump: FAILURE!!!!\n");
591 /* also free the opt struct here */
592 skb_trim(skb, b - skb->data);
596 static void gred_destroy(struct Qdisc *sch)
598 struct gred_sched *table = (struct gred_sched *)sch->data;
601 for (i = 0;i < table->DPs; i++) {
603 kfree(table->tab[i]);
608 struct Qdisc_ops gred_qdisc_ops =
613 sizeof(struct gred_sched),
621 gred_change, /* change */
627 int init_module(void)
629 return register_qdisc(&gred_qdisc_ops);
632 void cleanup_module(void)
634 unregister_qdisc(&gred_qdisc_ops);
637 MODULE_LICENSE("GPL");