1 /*
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@nortelnetworks.com) 1998,1999
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 <asm/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>
48
49 #if 1 /* control */
50 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
51 #else
52 #define DPRINTK(format,args...)
53 #endif
54
55 #if 0 /* data */
56 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
57 #else
58 #define D2PRINTK(format,args...)
59 #endif
60
61 struct gred_sched_data;
62 struct gred_sched;
63
64 struct gred_sched_data
65 {
66 /* Parameters */
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 */
73 u32 Scell_max;
74 u32 Rmask;
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 */
82 char Scell_log;
83 u8 Stab[256];
84 u8 prio; /* the prio of this vq */
85
86 /* Variables */
87 unsigned long qave; /* Average queue length: A scaled */
88 int qcount; /* Packets since last random number generation */
89 u32 qR; /* Cached random number */
90
91 psched_time_t qidlestart; /* Start of idle period */
92 };
93
94 struct gred_sched
95 {
96 struct gred_sched_data *tab[MAX_DPs];
97 u32 DPs;
98 u32 def;
99 u8 initd;
100 u8 grio;
101 u8 eqp;
102 };
103
104 static int
105 gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
106 {
107 psched_time_t now;
108 struct gred_sched_data *q=NULL;
109 struct gred_sched *t= (struct gred_sched *)sch->data;
110 unsigned long qave=0;
111 int i=0;
112
113 if (!t->initd) {
114 DPRINTK("NO GRED Queues setup yet! Enqueued anyway\n");
115 if (q->backlog <= q->limit) {
116 __skb_queue_tail(&sch->q, skb);
117 return NET_XMIT_DROP; /* @@@@ */
118 }
119 }
120
121
122 if ( ((skb->tc_index&0xf) > t->DPs) || !(q=t->tab[skb->tc_index&0xf])) {
123 printk("GRED: setting to default (%d)\n ",t->def);
124 if (!(q=t->tab[t->def])) {
125 DPRINTK("GRED: setting to default FAILED! dropping!! "
126 "(%d)\n ", t->def);
127 goto drop;
128 }
129 /* fix tc_index? --could be controvesial but needed for
130 requeueing */
131 skb->tc_index=(skb->tc_index&0xfffffff0) | t->def;
132 }
133
134 D2PRINTK("gred_enqueue virtualQ 0x%x classid %x backlog %d "
135 "general backlog %d\n",skb->tc_index&0xf,sch->handle,q->backlog,
136 sch->stats.backlog);
137 /* sum up all the qaves of prios <= to ours to get the new qave*/
138 if (!t->eqp && t->grio) {
139 for (i=0;i<t->DPs;i++) {
140 if ((!t->tab[i]) || (i==q->DP))
141 continue;
142
143 if ((t->tab[i]->prio < q->prio) && (PSCHED_IS_PASTPERFECT(t->tab[i]->qidlestart)))
144 qave +=t->tab[i]->qave;
145 }
146
147 }
148
149 q->packetsin++;
150 q->bytesin+=skb->len;
151
152 if (t->eqp && t->grio) {
153 qave=0;
154 q->qave=t->tab[t->def]->qave;
155 q->qidlestart=t->tab[t->def]->qidlestart;
156 }
157
158 if (!PSCHED_IS_PASTPERFECT(q->qidlestart)) {
159 long us_idle;
160 PSCHED_GET_TIME(now);
161 us_idle = PSCHED_TDIFF_SAFE(now, q->qidlestart, q->Scell_max, 0);
162 PSCHED_SET_PASTPERFECT(q->qidlestart);
163
164 q->qave >>= q->Stab[(us_idle>>q->Scell_log)&0xFF];
165 } else {
166 if (t->eqp) {
167 q->qave += sch->stats.backlog - (q->qave >> q->Wlog);
168 } else {
169 q->qave += q->backlog - (q->qave >> q->Wlog);
170 }
171
172 }
173
174
175 if (t->eqp && t->grio)
176 t->tab[t->def]->qave=q->qave;
177
178 if ((q->qave+qave) < q->qth_min) {
179 q->qcount = -1;
180 enqueue:
181 if (q->backlog <= q->limit) {
182 __skb_queue_tail(&sch->q, skb);
183 sch->stats.backlog += skb->len;
184 sch->stats.bytes += skb->len;
185 sch->stats.packets++;
186 q->backlog += skb->len;
187 return 0;
188 } else {
189 q->pdrop++;
190 }
191
192 drop:
193 kfree_skb(skb);
194 sch->stats.drops++;
195 return NET_XMIT_DROP;
196 }
197 if ((q->qave+qave) >= q->qth_max) {
198 q->qcount = -1;
199 sch->stats.overlimits++;
200 q->forced++;
201 goto drop;
202 }
203 if (++q->qcount) {
204 if ((((qave+q->qave) - q->qth_min)>>q->Wlog)*q->qcount < q->qR)
205 goto enqueue;
206 q->qcount = 0;
207 q->qR = net_random()&q->Rmask;
208 sch->stats.overlimits++;
209 q->early++;
210 goto drop;
211 }
212 q->qR = net_random()&q->Rmask;
213 goto enqueue;
214 }
215
216 static int
217 gred_requeue(struct sk_buff *skb, struct Qdisc* sch)
218 {
219 struct gred_sched_data *q;
220 struct gred_sched *t= (struct gred_sched *)sch->data;
221 q= t->tab[(skb->tc_index&0xf)];
222 /* error checking here -- probably unnecessary */
223 PSCHED_SET_PASTPERFECT(q->qidlestart);
224
225 __skb_queue_head(&sch->q, skb);
226 sch->stats.backlog += skb->len;
227 q->backlog += skb->len;
228 return 0;
229 }
230
231 static struct sk_buff *
232 gred_dequeue(struct Qdisc* sch)
233 {
234 struct sk_buff *skb;
235 struct gred_sched_data *q;
236 struct gred_sched *t= (struct gred_sched *)sch->data;
237
238 skb = __skb_dequeue(&sch->q);
239 if (skb) {
240 sch->stats.backlog -= skb->len;
241 q= t->tab[(skb->tc_index&0xf)];
242 if (q) {
243 q->backlog -= skb->len;
244 if (!q->backlog && !t->eqp)
245 PSCHED_GET_TIME(q->qidlestart);
246 } else {
247 D2PRINTK("gred_dequeue: skb has bad tcindex %x\n",skb->tc_index&0xf);
248 }
249 return skb;
250 }
251
252 if (t->eqp) {
253 q= t->tab[t->def];
254 if (!q)
255 D2PRINTK("no default VQ set: Results will be "
256 "screwed up\n");
257 else
258 PSCHED_GET_TIME(q->qidlestart);
259 }
260
261 return NULL;
262 }
263
264 static int
265 gred_drop(struct Qdisc* sch)
266 {
267 struct sk_buff *skb;
268
269 struct gred_sched_data *q;
270 struct gred_sched *t= (struct gred_sched *)sch->data;
271
272 skb = __skb_dequeue_tail(&sch->q);
273 if (skb) {
274 sch->stats.backlog -= skb->len;
275 sch->stats.drops++;
276 q= t->tab[(skb->tc_index&0xf)];
277 if (q) {
278 q->backlog -= skb->len;
279 q->other++;
280 if (!q->backlog && !t->eqp)
281 PSCHED_GET_TIME(q->qidlestart);
282 } else {
283 D2PRINTK("gred_dequeue: skb has bad tcindex %x\n",skb->tc_index&0xf);
284 }
285
286 kfree_skb(skb);
287 return 1;
288 }
289
290 q=t->tab[t->def];
291 if (!q) {
292 D2PRINTK("no default VQ set: Results might be screwed up\n");
293 return 0;
294 }
295
296 PSCHED_GET_TIME(q->qidlestart);
297 return 0;
298
299 }
300
301 static void gred_reset(struct Qdisc* sch)
302 {
303 struct sk_buff *skb;
304 int i;
305
306 struct gred_sched_data *q;
307 struct gred_sched *t= (struct gred_sched *)sch->data;
308
309 while((skb=__skb_dequeue(&sch->q))!=NULL)
310 kfree_skb(skb);
311
312 sch->stats.backlog = 0;
313
314 for (i=0;i<t->DPs;i++) {
315 q= t->tab[i];
316 if (!q)
317 continue;
318 PSCHED_SET_PASTPERFECT(q->qidlestart);
319 q->qave = 0;
320 q->qcount = -1;
321 q->backlog = 0;
322 q->other=0;
323 q->forced=0;
324 q->pdrop=0;
325 q->early=0;
326 }
327 }
328
329 static int gred_change(struct Qdisc *sch, struct rtattr *opt)
330 {
331 struct gred_sched *table = (struct gred_sched *)sch->data;
332 struct gred_sched_data *q;
333 struct tc_gred_qopt *ctl;
334 struct tc_gred_sopt *sopt;
335 struct rtattr *tb[TCA_GRED_STAB];
336 struct rtattr *tb2[TCA_GRED_STAB];
337 int i;
338
339 if (opt == NULL ||
340 rtattr_parse(tb, TCA_GRED_STAB, RTA_DATA(opt), RTA_PAYLOAD(opt)) )
341 return -EINVAL;
342
343 if (tb[TCA_GRED_PARMS-1] == 0 && tb[TCA_GRED_STAB-1] == 0 &&
344 tb[TCA_GRED_DPS-1] != 0) {
345 rtattr_parse(tb2, TCA_GRED_DPS, RTA_DATA(opt),
346 RTA_PAYLOAD(opt));
347
348 sopt = RTA_DATA(tb2[TCA_GRED_DPS-1]);
349 table->DPs=sopt->DPs;
350 table->def=sopt->def_DP;
351 table->grio=sopt->grio;
352 table->initd=0;
353 /* probably need to clear all the table DP entries as well */
354 MOD_INC_USE_COUNT;
355 return 0;
356 }
357
358
359 if (!table->DPs || tb[TCA_GRED_PARMS-1] == 0 || tb[TCA_GRED_STAB-1] == 0 ||
360 RTA_PAYLOAD(tb[TCA_GRED_PARMS-1]) < sizeof(*ctl) ||
361 RTA_PAYLOAD(tb[TCA_GRED_STAB-1]) < 256)
362 return -EINVAL;
363
364 ctl = RTA_DATA(tb[TCA_GRED_PARMS-1]);
365 if (ctl->DP > MAX_DPs-1 ) {
366 /* misbehaving is punished! Put in the default drop probability */
367 DPRINTK("\nGRED: DP %u not in the proper range fixed. New DP "
368 "set to default at %d\n",ctl->DP,table->def);
369 ctl->DP=table->def;
370 }
371
372 if (table->tab[ctl->DP] == NULL) {
373 table->tab[ctl->DP]=kmalloc(sizeof(struct gred_sched_data),
374 GFP_KERNEL);
375 if (NULL == table->tab[ctl->DP])
376 return -ENOMEM;
377 memset(table->tab[ctl->DP], 0, (sizeof(struct gred_sched_data)));
378 }
379 q= table->tab[ctl->DP];
380
381 if (table->grio) {
382 if (ctl->prio <=0) {
383 if (table->def && table->tab[table->def]) {
384 DPRINTK("\nGRED: DP %u does not have a prio"
385 "setting default to %d\n",ctl->DP,
386 table->tab[table->def]->prio);
387 q->prio=table->tab[table->def]->prio;
388 } else {
389 DPRINTK("\nGRED: DP %u does not have a prio"
390 " setting default to 8\n",ctl->DP);
391 q->prio=8;
392 }
393 } else {
394 q->prio=ctl->prio;
395 }
396 } else {
397 q->prio=8;
398 }
399
400
401 q->DP=ctl->DP;
402 q->Wlog = ctl->Wlog;
403 q->Plog = ctl->Plog;
404 q->limit = ctl->limit;
405 q->Scell_log = ctl->Scell_log;
406 q->Rmask = ctl->Plog < 32 ? ((1<<ctl->Plog) - 1) : ~0UL;
407 q->Scell_max = (255<<q->Scell_log);
408 q->qth_min = ctl->qth_min<<ctl->Wlog;
409 q->qth_max = ctl->qth_max<<ctl->Wlog;
410 q->qave=0;
411 q->backlog=0;
412 q->qcount = -1;
413 q->other=0;
414 q->forced=0;
415 q->pdrop=0;
416 q->early=0;
417
418 PSCHED_SET_PASTPERFECT(q->qidlestart);
419 memcpy(q->Stab, RTA_DATA(tb[TCA_GRED_STAB-1]), 256);
420
421 if ( table->initd && table->grio) {
422 /* this looks ugly but its not in the fast path */
423 for (i=0;i<table->DPs;i++) {
424 if ((!table->tab[i]) || (i==q->DP) )
425 continue;
426 if (table->tab[i]->prio == q->prio ){
427 /* WRED mode detected */
428 table->eqp=1;
429 break;
430 }
431 }
432 }
433
434 if (!table->initd) {
435 table->initd=1;
436 /*
437 the first entry also goes into the default until
438 over-written
439 */
440
441 if (table->tab[table->def] == NULL) {
442 table->tab[table->def]=
443 kmalloc(sizeof(struct gred_sched_data), GFP_KERNEL);
444 if (NULL == table->tab[ctl->DP])
445 return -ENOMEM;
446
447 memset(table->tab[table->def], 0,
448 (sizeof(struct gred_sched_data)));
449 }
450 q= table->tab[table->def];
451 q->DP=table->def;
452 q->Wlog = ctl->Wlog;
453 q->Plog = ctl->Plog;
454 q->limit = ctl->limit;
455 q->Scell_log = ctl->Scell_log;
456 q->Rmask = ctl->Plog < 32 ? ((1<<ctl->Plog) - 1) : ~0UL;
457 q->Scell_max = (255<<q->Scell_log);
458 q->qth_min = ctl->qth_min<<ctl->Wlog;
459 q->qth_max = ctl->qth_max<<ctl->Wlog;
460
461 if (table->grio)
462 q->prio=table->tab[ctl->DP]->prio;
463 else
464 q->prio=8;
465
466 q->qcount = -1;
467 PSCHED_SET_PASTPERFECT(q->qidlestart);
468 memcpy(q->Stab, RTA_DATA(tb[TCA_GRED_STAB-1]), 256);
469 }
470 return 0;
471
472 }
473
474 static int gred_init(struct Qdisc *sch, struct rtattr *opt)
475 {
476 struct gred_sched *table = (struct gred_sched *)sch->data;
477 struct tc_gred_sopt *sopt;
478 struct rtattr *tb[TCA_GRED_STAB];
479 struct rtattr *tb2[TCA_GRED_STAB];
480
481 if (opt == NULL ||
482 rtattr_parse(tb, TCA_GRED_STAB, RTA_DATA(opt), RTA_PAYLOAD(opt)) )
483 return -EINVAL;
484
485 if (tb[TCA_GRED_PARMS-1] == 0 && tb[TCA_GRED_STAB-1] == 0 &&
486 tb[TCA_GRED_DPS-1] != 0) {
487 rtattr_parse(tb2, TCA_GRED_DPS, RTA_DATA(opt),RTA_PAYLOAD(opt));
488
489 sopt = RTA_DATA(tb2[TCA_GRED_DPS-1]);
490 table->DPs=sopt->DPs;
491 table->def=sopt->def_DP;
492 table->grio=sopt->grio;
493 table->initd=0;
494 MOD_INC_USE_COUNT;
495 return 0;
496 }
497
498 DPRINTK("\n GRED_INIT error!\n");
499 return -EINVAL;
500 }
501
502 #ifdef CONFIG_RTNETLINK
503 static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
504 {
505 unsigned long qave;
506 struct rtattr *rta;
507 struct tc_gred_qopt *opt;
508 struct tc_gred_qopt *dst;
509 struct gred_sched *table = (struct gred_sched *)sch->data;
510 struct gred_sched_data *q;
511 int i;
512 unsigned char *b = skb->tail;
513
514 rta = (struct rtattr*)b;
515 RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
516
517 opt=kmalloc(sizeof(struct tc_gred_qopt)*MAX_DPs, GFP_KERNEL);
518
519 if (opt == NULL) {
520 DPRINTK("gred_dump:failed to malloc for %Zd\n",
521 sizeof(struct tc_gred_qopt)*MAX_DPs);
522 goto rtattr_failure;
523 }
524
525 memset(opt, 0, (sizeof(struct tc_gred_qopt))*table->DPs);
526
527 if (!table->initd) {
528 DPRINTK("NO GRED Queues setup!\n");
529 return -1;
530 }
531
532 for (i=0;i<MAX_DPs;i++) {
533 dst= &opt[i];
534 q= table->tab[i];
535
536 if (!q) {
537 /* hack -- fix at some point with proper message
538 This is how we indicate to tc that there is no VQ
539 at this DP */
540
541 dst->DP=MAX_DPs+i;
542 continue;
543 }
544
545 dst->limit=q->limit;
546 dst->qth_min=q->qth_min>>q->Wlog;
547 dst->qth_max=q->qth_max>>q->Wlog;
548 dst->DP=q->DP;
549 dst->backlog=q->backlog;
550 if (q->qave) {
551 if (table->eqp && table->grio) {
552 q->qidlestart=table->tab[table->def]->qidlestart;
553 q->qave=table->tab[table->def]->qave;
554 }
555 if (!PSCHED_IS_PASTPERFECT(q->qidlestart)) {
556 long idle;
557 psched_time_t now;
558 PSCHED_GET_TIME(now);
559 idle = PSCHED_TDIFF_SAFE(now, q->qidlestart, q->Scell_max, 0);
560 qave = q->qave >> q->Stab[(idle>>q->Scell_log)&0xFF];
561 dst->qave = qave >> q->Wlog;
562
563 } else {
564 dst->qave = q->qave >> q->Wlog;
565 }
566 } else {
567 dst->qave = 0;
568 }
569
570
571 dst->Wlog = q->Wlog;
572 dst->Plog = q->Plog;
573 dst->Scell_log = q->Scell_log;
574 dst->other = q->other;
575 dst->forced = q->forced;
576 dst->early = q->early;
577 dst->pdrop = q->pdrop;
578 dst->prio = q->prio;
579 dst->packets=q->packetsin;
580 dst->bytesin=q->bytesin;
581 }
582
583 RTA_PUT(skb, TCA_GRED_PARMS, sizeof(struct tc_gred_qopt)*MAX_DPs, opt);
584 rta->rta_len = skb->tail - b;
585
586 return skb->len;
587
588 rtattr_failure:
589 DPRINTK("gred_dump: FAILURE!!!!\n");
590
591 /* also free the opt struct here */
592 skb_trim(skb, b - skb->data);
593 return -1;
594 }
595 #endif
596
597 static void gred_destroy(struct Qdisc *sch)
598 {
599 struct gred_sched *table = (struct gred_sched *)sch->data;
600 int i;
601
602 for (i = 0;i < table->DPs; i++) {
603 if (table->tab[i])
604 kfree(table->tab[i]);
605 }
606 MOD_DEC_USE_COUNT;
607 }
608
609 struct Qdisc_ops gred_qdisc_ops =
610 {
611 NULL,
612 NULL,
613 "gred",
614 sizeof(struct gred_sched),
615 gred_enqueue,
616 gred_dequeue,
617 gred_requeue,
618 gred_drop,
619 gred_init,
620 gred_reset,
621 gred_destroy,
622 gred_change, /* change */
623 #ifdef CONFIG_RTNETLINK
624 gred_dump,
625 #endif
626 };
627
628
629 #ifdef MODULE
630 int init_module(void)
631 {
632 return register_qdisc(&gred_qdisc_ops);
633 }
634
635 void cleanup_module(void)
636 {
637 unregister_qdisc(&gred_qdisc_ops);
638 }
639 #endif
640
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.