upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / net / sched / sch_prio.c
1 /*
2  * net/sched/sch_prio.c Simple 3-band priority "scheduler".
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  * Fixes:       19990609: J Hadi Salim <hadi@nortelnetworks.com>: 
11  *              Init --  EINVAL when opt undefined
12  */
13
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
18 #include <linux/bitops.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
23 #include <linux/mm.h>
24 #include <linux/socket.h>
25 #include <linux/sockios.h>
26 #include <linux/in.h>
27 #include <linux/errno.h>
28 #include <linux/interrupt.h>
29 #include <linux/if_ether.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/notifier.h>
34 #include <net/ip.h>
35 #include <net/route.h>
36 #include <linux/skbuff.h>
37 #include <net/sock.h>
38 #include <net/pkt_sched.h>
39
40
41 struct prio_sched_data
42 {
43         int bands;
44         struct tcf_proto *filter_list;
45         u8  prio2band[TC_PRIO_MAX+1];
46         struct Qdisc *queues[TCQ_PRIO_BANDS];
47 };
48
49
50 struct Qdisc *prio_classify(struct sk_buff *skb, struct Qdisc *sch,int *r)
51 {
52         struct prio_sched_data *q = qdisc_priv(sch);
53         u32 band = skb->priority;
54         struct tcf_result res;
55
56         if (TC_H_MAJ(skb->priority) != sch->handle) {
57 #ifdef CONFIG_NET_CLS_ACT
58                 int result = 0, terminal = 0;
59                 result = tc_classify(skb, q->filter_list, &res);
60
61                 switch (result) {
62                         case TC_ACT_SHOT:
63                                 *r = NET_XMIT_DROP;
64                                 terminal = 1;
65                                 break;
66                         case TC_ACT_STOLEN:
67                         case TC_ACT_QUEUED:
68                                 terminal = 1;
69                                 break;
70                         case TC_ACT_RECLASSIFY:
71                         case TC_ACT_OK:
72                         case TC_ACT_UNSPEC:
73                         default:
74                         break;
75                 };
76                 if (terminal) {
77                         kfree_skb(skb);
78                         return NULL;
79                 } 
80
81                 if (!q->filter_list ) {
82 #else
83                 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
84 #endif
85                         if (TC_H_MAJ(band))
86                                 band = 0;
87                         return q->queues[q->prio2band[band&TC_PRIO_MAX]];
88                 }
89                 band = res.classid;
90         }
91         band = TC_H_MIN(band) - 1;
92         if (band > q->bands)
93                 return q->queues[q->prio2band[0]];
94
95         return q->queues[band];
96 }
97
98 static int
99 prio_enqueue(struct sk_buff *skb, struct Qdisc* sch)
100 {
101         struct Qdisc *qdisc;
102         int ret = NET_XMIT_SUCCESS;
103
104         qdisc = prio_classify(skb, sch, &ret);
105
106         if (NULL == qdisc)
107                 goto dropped;
108
109         if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
110                 sch->bstats.bytes += skb->len;
111                 sch->bstats.packets++;
112                 sch->q.qlen++;
113                 return NET_XMIT_SUCCESS;
114         }
115
116 dropped:
117 #ifdef CONFIG_NET_CLS_ACT
118         if (NET_XMIT_DROP == ret) {
119 #endif
120                 sch->qstats.drops++;
121 #ifdef CONFIG_NET_CLS_ACT
122         } else {
123                 sch->qstats.overlimits++; /* abuse, but noone uses it */
124         }
125 #endif
126         return ret; 
127 }
128
129
130 static int
131 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
132 {
133         struct Qdisc *qdisc;
134         int ret = NET_XMIT_DROP;
135
136         qdisc = prio_classify(skb, sch, &ret);
137         if (qdisc == NULL)
138                 goto dropped;
139
140         if ((ret = qdisc->ops->requeue(skb, qdisc)) == 0) {
141                 sch->q.qlen++;
142                 sch->qstats.requeues++;
143                 return 0;
144         }
145 dropped:
146         sch->qstats.drops++;
147         return NET_XMIT_DROP;
148 }
149
150
151 static struct sk_buff *
152 prio_dequeue(struct Qdisc* sch)
153 {
154         struct sk_buff *skb;
155         struct prio_sched_data *q = qdisc_priv(sch);
156         int prio;
157         struct Qdisc *qdisc;
158
159         for (prio = 0; prio < q->bands; prio++) {
160                 qdisc = q->queues[prio];
161                 skb = qdisc->dequeue(qdisc);
162                 if (skb) {
163                         sch->q.qlen--;
164                         return skb;
165                 }
166         }
167         return NULL;
168
169 }
170
171 static unsigned int prio_drop(struct Qdisc* sch)
172 {
173         struct prio_sched_data *q = qdisc_priv(sch);
174         int prio;
175         unsigned int len;
176         struct Qdisc *qdisc;
177
178         for (prio = q->bands-1; prio >= 0; prio--) {
179                 qdisc = q->queues[prio];
180                 if ((len = qdisc->ops->drop(qdisc)) != 0) {
181                         sch->q.qlen--;
182                         return len;
183                 }
184         }
185         return 0;
186 }
187
188
189 static void
190 prio_reset(struct Qdisc* sch)
191 {
192         int prio;
193         struct prio_sched_data *q = qdisc_priv(sch);
194
195         for (prio=0; prio<q->bands; prio++)
196                 qdisc_reset(q->queues[prio]);
197         sch->q.qlen = 0;
198 }
199
200 static void
201 prio_destroy(struct Qdisc* sch)
202 {
203         int prio;
204         struct prio_sched_data *q = qdisc_priv(sch);
205         struct tcf_proto *tp;
206
207         while ((tp = q->filter_list) != NULL) {
208                 q->filter_list = tp->next;
209                 tcf_destroy(tp);
210         }
211
212         for (prio=0; prio<q->bands; prio++)
213                 qdisc_destroy(q->queues[prio]);
214 }
215
216 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
217 {
218         struct prio_sched_data *q = qdisc_priv(sch);
219         struct tc_prio_qopt *qopt = RTA_DATA(opt);
220         int i;
221
222         if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
223                 return -EINVAL;
224         if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
225                 return -EINVAL;
226
227         for (i=0; i<=TC_PRIO_MAX; i++) {
228                 if (qopt->priomap[i] >= qopt->bands)
229                         return -EINVAL;
230         }
231
232         sch_tree_lock(sch);
233         q->bands = qopt->bands;
234         memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
235
236         for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
237                 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
238                 if (child != &noop_qdisc)
239                         qdisc_destroy(child);
240         }
241         sch_tree_unlock(sch);
242
243         for (i=0; i<=TC_PRIO_MAX; i++) {
244                 int band = q->prio2band[i];
245                 if (q->queues[band] == &noop_qdisc) {
246                         struct Qdisc *child;
247                         child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
248                         if (child) {
249                                 sch_tree_lock(sch);
250                                 child = xchg(&q->queues[band], child);
251
252                                 if (child != &noop_qdisc)
253                                         qdisc_destroy(child);
254                                 sch_tree_unlock(sch);
255                         }
256                 }
257         }
258         return 0;
259 }
260
261 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
262 {
263         struct prio_sched_data *q = qdisc_priv(sch);
264         int i;
265
266         for (i=0; i<TCQ_PRIO_BANDS; i++)
267                 q->queues[i] = &noop_qdisc;
268
269         if (opt == NULL) {
270                 return -EINVAL;
271         } else {
272                 int err;
273
274                 if ((err= prio_tune(sch, opt)) != 0)
275                         return err;
276         }
277         return 0;
278 }
279
280 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
281 {
282         struct prio_sched_data *q = qdisc_priv(sch);
283         unsigned char    *b = skb->tail;
284         struct tc_prio_qopt opt;
285
286         opt.bands = q->bands;
287         memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
288         RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
289         return skb->len;
290
291 rtattr_failure:
292         skb_trim(skb, b - skb->data);
293         return -1;
294 }
295
296 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
297                       struct Qdisc **old)
298 {
299         struct prio_sched_data *q = qdisc_priv(sch);
300         unsigned long band = arg - 1;
301
302         if (band >= q->bands)
303                 return -EINVAL;
304
305         if (new == NULL)
306                 new = &noop_qdisc;
307
308         sch_tree_lock(sch);
309         *old = q->queues[band];
310         q->queues[band] = new;
311         sch->q.qlen -= (*old)->q.qlen;
312         qdisc_reset(*old);
313         sch_tree_unlock(sch);
314
315         return 0;
316 }
317
318 static struct Qdisc *
319 prio_leaf(struct Qdisc *sch, unsigned long arg)
320 {
321         struct prio_sched_data *q = qdisc_priv(sch);
322         unsigned long band = arg - 1;
323
324         if (band >= q->bands)
325                 return NULL;
326
327         return q->queues[band];
328 }
329
330 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
331 {
332         struct prio_sched_data *q = qdisc_priv(sch);
333         unsigned long band = TC_H_MIN(classid);
334
335         if (band - 1 >= q->bands)
336                 return 0;
337         return band;
338 }
339
340 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
341 {
342         return prio_get(sch, classid);
343 }
344
345
346 static void prio_put(struct Qdisc *q, unsigned long cl)
347 {
348         return;
349 }
350
351 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
352 {
353         unsigned long cl = *arg;
354         struct prio_sched_data *q = qdisc_priv(sch);
355
356         if (cl - 1 > q->bands)
357                 return -ENOENT;
358         return 0;
359 }
360
361 static int prio_delete(struct Qdisc *sch, unsigned long cl)
362 {
363         struct prio_sched_data *q = qdisc_priv(sch);
364         if (cl - 1 > q->bands)
365                 return -ENOENT;
366         return 0;
367 }
368
369
370 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
371                            struct tcmsg *tcm)
372 {
373         struct prio_sched_data *q = qdisc_priv(sch);
374
375         if (cl - 1 > q->bands)
376                 return -ENOENT;
377         tcm->tcm_handle |= TC_H_MIN(cl);
378         if (q->queues[cl-1])
379                 tcm->tcm_info = q->queues[cl-1]->handle;
380         return 0;
381 }
382
383 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
384 {
385         struct prio_sched_data *q = qdisc_priv(sch);
386         int prio;
387
388         if (arg->stop)
389                 return;
390
391         for (prio = 0; prio < q->bands; prio++) {
392                 if (arg->count < arg->skip) {
393                         arg->count++;
394                         continue;
395                 }
396                 if (arg->fn(sch, prio+1, arg) < 0) {
397                         arg->stop = 1;
398                         break;
399                 }
400                 arg->count++;
401         }
402 }
403
404 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
405 {
406         struct prio_sched_data *q = qdisc_priv(sch);
407
408         if (cl)
409                 return NULL;
410         return &q->filter_list;
411 }
412
413 static struct Qdisc_class_ops prio_class_ops = {
414         .graft          =       prio_graft,
415         .leaf           =       prio_leaf,
416         .get            =       prio_get,
417         .put            =       prio_put,
418         .change         =       prio_change,
419         .delete         =       prio_delete,
420         .walk           =       prio_walk,
421         .tcf_chain      =       prio_find_tcf,
422         .bind_tcf       =       prio_bind,
423         .unbind_tcf     =       prio_put,
424         .dump           =       prio_dump_class,
425 };
426
427 static struct Qdisc_ops prio_qdisc_ops = {
428         .next           =       NULL,
429         .cl_ops         =       &prio_class_ops,
430         .id             =       "prio",
431         .priv_size      =       sizeof(struct prio_sched_data),
432         .enqueue        =       prio_enqueue,
433         .dequeue        =       prio_dequeue,
434         .requeue        =       prio_requeue,
435         .drop           =       prio_drop,
436         .init           =       prio_init,
437         .reset          =       prio_reset,
438         .destroy        =       prio_destroy,
439         .change         =       prio_tune,
440         .dump           =       prio_dump,
441         .owner          =       THIS_MODULE,
442 };
443
444 static int __init prio_module_init(void)
445 {
446         return register_qdisc(&prio_qdisc_ops);
447 }
448
449 static void __exit prio_module_exit(void) 
450 {
451         unregister_qdisc(&prio_qdisc_ops);
452 }
453
454 module_init(prio_module_init)
455 module_exit(prio_module_exit)
456
457 MODULE_LICENSE("GPL");