[PKT_SCHED]: GRED: Improve error handling and messages
[linux-2.6.git] / net / sched / sch_gred.c
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@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>
48 #include <net/red.h>
49
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
62 #define GRED_DEF_PRIO (MAX_DPs / 2)
63 #define GRED_VQ_MASK (MAX_DPs - 1)
64
65 struct gred_sched_data;
66 struct gred_sched;
67
68 struct gred_sched_data
69 {
70         u32             limit;          /* HARD maximal queue length    */
71         u32             DP;             /* the drop pramaters */
72         u32             bytesin;        /* bytes seen on virtualQ so far*/
73         u32             packetsin;      /* packets seen on virtualQ so far*/
74         u32             backlog;        /* bytes on the virtualQ */
75         u8              prio;        /* the prio of this vq */
76
77         struct red_parms parms;
78         struct red_stats stats;
79 };
80
81 enum {
82         GRED_WRED_MODE = 1,
83         GRED_RIO_MODE,
84 };
85
86 struct gred_sched
87 {
88         struct gred_sched_data *tab[MAX_DPs];
89         unsigned long   flags;
90         u32             DPs;   
91         u32             def; 
92         u8              initd; 
93 };
94
95 static inline int gred_wred_mode(struct gred_sched *table)
96 {
97         return test_bit(GRED_WRED_MODE, &table->flags);
98 }
99
100 static inline void gred_enable_wred_mode(struct gred_sched *table)
101 {
102         __set_bit(GRED_WRED_MODE, &table->flags);
103 }
104
105 static inline void gred_disable_wred_mode(struct gred_sched *table)
106 {
107         __clear_bit(GRED_WRED_MODE, &table->flags);
108 }
109
110 static inline int gred_rio_mode(struct gred_sched *table)
111 {
112         return test_bit(GRED_RIO_MODE, &table->flags);
113 }
114
115 static inline void gred_enable_rio_mode(struct gred_sched *table)
116 {
117         __set_bit(GRED_RIO_MODE, &table->flags);
118 }
119
120 static inline void gred_disable_rio_mode(struct gred_sched *table)
121 {
122         __clear_bit(GRED_RIO_MODE, &table->flags);
123 }
124
125 static 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
147 static 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
157 static inline u16 tc_index_to_dp(struct sk_buff *skb)
158 {
159         return skb->tc_index & GRED_VQ_MASK;
160 }
161
162 static int
163 gred_enqueue(struct sk_buff *skb, struct Qdisc* sch)
164 {
165         struct gred_sched_data *q=NULL;
166         struct gred_sched *t= qdisc_priv(sch);
167         unsigned long qavg = 0;
168         int i=0;
169         u16 dp;
170
171         if (!t->initd && skb_queue_len(&sch->q) < (sch->dev->tx_queue_len ? : 1)) {
172                 D2PRINTK("NO GRED Queues setup yet! Enqueued anyway\n");
173                 goto do_enqueue;
174         }
175
176         dp = tc_index_to_dp(skb);
177
178         if (dp >= t->DPs  || (q = t->tab[dp]) == NULL) {
179                 dp = t->def;
180
181                 if ((q = t->tab[dp]) == NULL) {
182                         /* Pass through packets not assigned to a DP
183                          * if no default DP has been configured. This
184                          * allows for DP flows to be left untouched.
185                          */
186                         if (skb_queue_len(&sch->q) < sch->dev->tx_queue_len)
187                                 return qdisc_enqueue_tail(skb, sch);
188                         else
189                                 goto drop;
190                 }
191
192                 /* fix tc_index? --could be controvesial but needed for
193                    requeueing */
194                 skb->tc_index = (skb->tc_index & ~GRED_VQ_MASK) | dp;
195         }
196
197         /* sum up all the qaves of prios <= to ours to get the new qave*/
198         if (!gred_wred_mode(t) && gred_rio_mode(t)) {
199                 for (i=0;i<t->DPs;i++) {
200                         if ((!t->tab[i]) || (i==q->DP)) 
201                                 continue; 
202                                 
203                         if (t->tab[i]->prio < q->prio &&
204                             !red_is_idling(&t->tab[i]->parms))
205                                 qavg +=t->tab[i]->parms.qavg;
206                 }
207                         
208         }
209
210         q->packetsin++;
211         q->bytesin+=skb->len;
212
213         if (gred_wred_mode(t)) {
214                 qavg = 0;
215                 q->parms.qavg = t->tab[t->def]->parms.qavg;
216                 q->parms.qidlestart = t->tab[t->def]->parms.qidlestart;
217         }
218
219         q->parms.qavg = red_calc_qavg(&q->parms, gred_backlog(t, q, sch));
220
221         if (red_is_idling(&q->parms))
222                 red_end_of_idle_period(&q->parms);
223
224         if (gred_wred_mode(t))
225                 t->tab[t->def]->parms.qavg = q->parms.qavg;
226
227         switch (red_action(&q->parms, q->parms.qavg + qavg)) {
228                 case RED_DONT_MARK:
229                         break;
230
231                 case RED_PROB_MARK:
232                         sch->qstats.overlimits++;
233                         q->stats.prob_drop++;
234                         goto congestion_drop;
235
236                 case RED_HARD_MARK:
237                         sch->qstats.overlimits++;
238                         q->stats.forced_drop++;
239                         goto congestion_drop;
240         }
241
242         if (q->backlog + skb->len <= q->limit) {
243                 q->backlog += skb->len;
244 do_enqueue:
245                 return qdisc_enqueue_tail(skb, sch);
246         }
247
248         q->stats.pdrop++;
249 drop:
250         return qdisc_drop(skb, sch);
251
252 congestion_drop:
253         qdisc_drop(skb, sch);
254         return NET_XMIT_CN;
255 }
256
257 static int
258 gred_requeue(struct sk_buff *skb, struct Qdisc* sch)
259 {
260         struct gred_sched *t = qdisc_priv(sch);
261         struct gred_sched_data *q;
262         u16 dp = tc_index_to_dp(skb);
263
264         if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
265                 if (net_ratelimit())
266                         printk(KERN_WARNING "GRED: Unable to relocate VQ 0x%x "
267                                "for requeue, screwing up backlog.\n",
268                                tc_index_to_dp(skb));
269         } else {
270                 if (red_is_idling(&q->parms))
271                         red_end_of_idle_period(&q->parms);
272                 q->backlog += skb->len;
273         }
274
275         return qdisc_requeue(skb, sch);
276 }
277
278 static struct sk_buff *
279 gred_dequeue(struct Qdisc* sch)
280 {
281         struct sk_buff *skb;
282         struct gred_sched_data *q;
283         struct gred_sched *t= qdisc_priv(sch);
284
285         skb = qdisc_dequeue_head(sch);
286
287         if (skb) {
288                 u16 dp = tc_index_to_dp(skb);
289
290                 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
291                         if (net_ratelimit())
292                                 printk(KERN_WARNING "GRED: Unable to relocate "
293                                        "VQ 0x%x after dequeue, screwing up "
294                                        "backlog.\n", tc_index_to_dp(skb));
295                 } else {
296                         q->backlog -= skb->len;
297
298                         if (!q->backlog && !gred_wred_mode(t))
299                                 red_start_of_idle_period(&q->parms);
300                 }
301
302                 return skb;
303         }
304
305         if (gred_wred_mode(t)) {
306                         q= t->tab[t->def];
307                         if (!q) 
308                                 D2PRINTK("no default VQ set: Results will be "
309                                        "screwed up\n");
310                         else
311                                 red_start_of_idle_period(&q->parms);
312         }
313
314         return NULL;
315 }
316
317 static unsigned int gred_drop(struct Qdisc* sch)
318 {
319         struct sk_buff *skb;
320
321         struct gred_sched_data *q;
322         struct gred_sched *t= qdisc_priv(sch);
323
324         skb = qdisc_dequeue_tail(sch);
325         if (skb) {
326                 unsigned int len = skb->len;
327                 u16 dp = tc_index_to_dp(skb);
328
329                 if (dp >= t->DPs || (q = t->tab[dp]) == NULL) {
330                         if (net_ratelimit())
331                                 printk(KERN_WARNING "GRED: Unable to relocate "
332                                        "VQ 0x%x while dropping, screwing up "
333                                        "backlog.\n", tc_index_to_dp(skb));
334                 } else {
335                         q->backlog -= len;
336                         q->stats.other++;
337
338                         if (!q->backlog && !gred_wred_mode(t))
339                                 red_start_of_idle_period(&q->parms);
340                 }
341
342                 qdisc_drop(skb, sch);
343                 return len;
344         }
345
346         q=t->tab[t->def];
347         if (!q) {
348                 D2PRINTK("no default VQ set: Results might be screwed up\n");
349                 return 0;
350         }
351
352         red_start_of_idle_period(&q->parms);
353         return 0;
354
355 }
356
357 static void gred_reset(struct Qdisc* sch)
358 {
359         int i;
360         struct gred_sched_data *q;
361         struct gred_sched *t= qdisc_priv(sch);
362
363         qdisc_reset_queue(sch);
364
365         for (i=0;i<t->DPs;i++) {
366                 q= t->tab[i];
367                 if (!q) 
368                         continue; 
369                 red_restart(&q->parms);
370                 q->backlog = 0;
371         }
372 }
373
374 static inline void gred_destroy_vq(struct gred_sched_data *q)
375 {
376         kfree(q);
377 }
378
379 static inline int gred_change_table_def(struct Qdisc *sch, struct rtattr *dps)
380 {
381         struct gred_sched *table = qdisc_priv(sch);
382         struct tc_gred_sopt *sopt;
383         int i;
384
385         if (dps == NULL || RTA_PAYLOAD(dps) < sizeof(*sopt))
386                 return -EINVAL;
387
388         sopt = RTA_DATA(dps);
389
390         if (sopt->DPs > MAX_DPs || sopt->DPs == 0 || sopt->def_DP >= sopt->DPs)
391                 return -EINVAL;
392
393         sch_tree_lock(sch);
394         table->DPs = sopt->DPs;
395         table->def = sopt->def_DP;
396
397         /*
398          * Every entry point to GRED is synchronized with the above code
399          * and the DP is checked against DPs, i.e. shadowed VQs can no
400          * longer be found so we can unlock right here.
401          */
402         sch_tree_unlock(sch);
403
404         if (sopt->grio) {
405                 gred_enable_rio_mode(table);
406                 gred_disable_wred_mode(table);
407                 if (gred_wred_mode_check(sch))
408                         gred_enable_wred_mode(table);
409         } else {
410                 gred_disable_rio_mode(table);
411                 gred_disable_wred_mode(table);
412         }
413
414         for (i = table->DPs; i < MAX_DPs; i++) {
415                 if (table->tab[i]) {
416                         printk(KERN_WARNING "GRED: Warning: Destroying "
417                                "shadowed VQ 0x%x\n", i);
418                         gred_destroy_vq(table->tab[i]);
419                         table->tab[i] = NULL;
420                 }
421         }
422
423         table->initd = 0;
424
425         return 0;
426 }
427
428 static inline int gred_change_vq(struct Qdisc *sch, int dp,
429                                  struct tc_gred_qopt *ctl, int prio, u8 *stab)
430 {
431         struct gred_sched *table = qdisc_priv(sch);
432         struct gred_sched_data *q;
433
434         if (table->tab[dp] == NULL) {
435                 table->tab[dp] = kmalloc(sizeof(*q), GFP_KERNEL);
436                 if (table->tab[dp] == NULL)
437                         return -ENOMEM;
438                 memset(table->tab[dp], 0, sizeof(*q));
439         }
440
441         q = table->tab[dp];
442         q->DP = dp;
443         q->prio = prio;
444         q->limit = ctl->limit;
445
446         if (q->backlog == 0)
447                 red_end_of_idle_period(&q->parms);
448
449         red_set_parms(&q->parms,
450                       ctl->qth_min, ctl->qth_max, ctl->Wlog, ctl->Plog,
451                       ctl->Scell_log, stab);
452
453         return 0;
454 }
455
456 static int gred_change(struct Qdisc *sch, struct rtattr *opt)
457 {
458         struct gred_sched *table = qdisc_priv(sch);
459         struct tc_gred_qopt *ctl;
460         struct rtattr *tb[TCA_GRED_MAX];
461         int err = -EINVAL, prio = GRED_DEF_PRIO;
462         u8 *stab;
463
464         if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
465                 return -EINVAL;
466
467         if (tb[TCA_GRED_PARMS-1] == NULL && tb[TCA_GRED_STAB-1] == NULL)
468                 return gred_change_table_def(sch, opt);
469
470         if (tb[TCA_GRED_PARMS-1] == NULL ||
471             RTA_PAYLOAD(tb[TCA_GRED_PARMS-1]) < sizeof(*ctl) ||
472             tb[TCA_GRED_STAB-1] == NULL ||
473             RTA_PAYLOAD(tb[TCA_GRED_STAB-1]) < 256)
474                 return -EINVAL;
475
476         ctl = RTA_DATA(tb[TCA_GRED_PARMS-1]);
477         stab = RTA_DATA(tb[TCA_GRED_STAB-1]);
478
479         if (ctl->DP >= table->DPs)
480                 goto errout;
481
482         if (gred_rio_mode(table)) {
483                 if (ctl->prio == 0) {
484                         int def_prio = GRED_DEF_PRIO;
485
486                         if (table->tab[table->def])
487                                 def_prio = table->tab[table->def]->prio;
488
489                         printk(KERN_DEBUG "GRED: DP %u does not have a prio "
490                                "setting default to %d\n", ctl->DP, def_prio);
491
492                         prio = def_prio;
493                 } else
494                         prio = ctl->prio;
495         }
496
497         sch_tree_lock(sch);
498
499         err = gred_change_vq(sch, ctl->DP, ctl, prio, stab);
500         if (err < 0)
501                 goto errout_locked;
502
503         if (table->tab[table->def] == NULL) {
504                 if (gred_rio_mode(table))
505                         prio = table->tab[ctl->DP]->prio;
506
507                 err = gred_change_vq(sch, table->def, ctl, prio, stab);
508                 if (err < 0)
509                         goto errout_locked;
510         }
511
512         table->initd = 1;
513
514         if (gred_rio_mode(table)) {
515                 gred_disable_wred_mode(table);
516                 if (gred_wred_mode_check(sch))
517                         gred_enable_wred_mode(table);
518         }
519
520         err = 0;
521
522 errout_locked:
523         sch_tree_unlock(sch);
524 errout:
525         return err;
526 }
527
528 static int gred_init(struct Qdisc *sch, struct rtattr *opt)
529 {
530         struct rtattr *tb[TCA_GRED_MAX];
531
532         if (opt == NULL || rtattr_parse_nested(tb, TCA_GRED_MAX, opt))
533                 return -EINVAL;
534
535         if (tb[TCA_GRED_PARMS-1] || tb[TCA_GRED_STAB-1])
536                 return -EINVAL;
537
538         return gred_change_table_def(sch, tb[TCA_GRED_DPS-1]);
539 }
540
541 static int gred_dump(struct Qdisc *sch, struct sk_buff *skb)
542 {
543         struct gred_sched *table = qdisc_priv(sch);
544         struct rtattr *parms, *opts = NULL;
545         int i;
546         struct tc_gred_sopt sopt = {
547                 .DPs    = table->DPs,
548                 .def_DP = table->def,
549                 .grio   = gred_rio_mode(table),
550         };
551
552         opts = RTA_NEST(skb, TCA_OPTIONS);
553         RTA_PUT(skb, TCA_GRED_DPS, sizeof(sopt), &sopt);
554         parms = RTA_NEST(skb, TCA_GRED_PARMS);
555
556         for (i = 0; i < MAX_DPs; i++) {
557                 struct gred_sched_data *q = table->tab[i];
558                 struct tc_gred_qopt opt;
559
560                 memset(&opt, 0, sizeof(opt));
561
562                 if (!q) {
563                         /* hack -- fix at some point with proper message
564                            This is how we indicate to tc that there is no VQ
565                            at this DP */
566
567                         opt.DP = MAX_DPs + i;
568                         goto append_opt;
569                 }
570
571                 opt.limit       = q->limit;
572                 opt.DP          = q->DP;
573                 opt.backlog     = q->backlog;
574                 opt.prio        = q->prio;
575                 opt.qth_min     = q->parms.qth_min >> q->parms.Wlog;
576                 opt.qth_max     = q->parms.qth_max >> q->parms.Wlog;
577                 opt.Wlog        = q->parms.Wlog;
578                 opt.Plog        = q->parms.Plog;
579                 opt.Scell_log   = q->parms.Scell_log;
580                 opt.other       = q->stats.other;
581                 opt.early       = q->stats.prob_drop;
582                 opt.forced      = q->stats.forced_drop;
583                 opt.pdrop       = q->stats.pdrop;
584                 opt.packets     = q->packetsin;
585                 opt.bytesin     = q->bytesin;
586
587                 if (gred_wred_mode(table)) {
588                         q->parms.qidlestart =
589                                 table->tab[table->def]->parms.qidlestart;
590                         q->parms.qavg = table->tab[table->def]->parms.qavg;
591                 }
592
593                 opt.qave = red_calc_qavg(&q->parms, q->parms.qavg);
594
595 append_opt:
596                 RTA_APPEND(skb, sizeof(opt), &opt);
597         }
598
599         RTA_NEST_END(skb, parms);
600
601         return RTA_NEST_END(skb, opts);
602
603 rtattr_failure:
604         return RTA_NEST_CANCEL(skb, opts);
605 }
606
607 static void gred_destroy(struct Qdisc *sch)
608 {
609         struct gred_sched *table = qdisc_priv(sch);
610         int i;
611
612         for (i = 0;i < table->DPs; i++) {
613                 if (table->tab[i])
614                         gred_destroy_vq(table->tab[i]);
615         }
616 }
617
618 static struct Qdisc_ops gred_qdisc_ops = {
619         .next           =       NULL,
620         .cl_ops         =       NULL,
621         .id             =       "gred",
622         .priv_size      =       sizeof(struct gred_sched),
623         .enqueue        =       gred_enqueue,
624         .dequeue        =       gred_dequeue,
625         .requeue        =       gred_requeue,
626         .drop           =       gred_drop,
627         .init           =       gred_init,
628         .reset          =       gred_reset,
629         .destroy        =       gred_destroy,
630         .change         =       gred_change,
631         .dump           =       gred_dump,
632         .owner          =       THIS_MODULE,
633 };
634
635 static int __init gred_module_init(void)
636 {
637         return register_qdisc(&gred_qdisc_ops);
638 }
639 static void __exit gred_module_exit(void) 
640 {
641         unregister_qdisc(&gred_qdisc_ops);
642 }
643 module_init(gred_module_init)
644 module_exit(gred_module_exit)
645 MODULE_LICENSE("GPL");