VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 <asm/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->stats.bytes += skb->len;
111                 sch->stats.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->stats.drops++;
121 #ifdef CONFIG_NET_CLS_ACT
122         } else {
123                 sch->stats.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                 return 0;
143         }
144 dropped:
145         sch->stats.drops++;
146         return NET_XMIT_DROP;
147 }
148
149
150 static struct sk_buff *
151 prio_dequeue(struct Qdisc* sch)
152 {
153         struct sk_buff *skb;
154         struct prio_sched_data *q = qdisc_priv(sch);
155         int prio;
156         struct Qdisc *qdisc;
157
158         for (prio = 0; prio < q->bands; prio++) {
159                 qdisc = q->queues[prio];
160                 skb = qdisc->dequeue(qdisc);
161                 if (skb) {
162                         sch->q.qlen--;
163                         return skb;
164                 }
165         }
166         return NULL;
167
168 }
169
170 static unsigned int prio_drop(struct Qdisc* sch)
171 {
172         struct prio_sched_data *q = qdisc_priv(sch);
173         int prio;
174         unsigned int len;
175         struct Qdisc *qdisc;
176
177         for (prio = q->bands-1; prio >= 0; prio--) {
178                 qdisc = q->queues[prio];
179                 if ((len = qdisc->ops->drop(qdisc)) != 0) {
180                         sch->q.qlen--;
181                         return len;
182                 }
183         }
184         return 0;
185 }
186
187
188 static void
189 prio_reset(struct Qdisc* sch)
190 {
191         int prio;
192         struct prio_sched_data *q = qdisc_priv(sch);
193
194         for (prio=0; prio<q->bands; prio++)
195                 qdisc_reset(q->queues[prio]);
196         sch->q.qlen = 0;
197 }
198
199 static void
200 prio_destroy(struct Qdisc* sch)
201 {
202         int prio;
203         struct prio_sched_data *q = qdisc_priv(sch);
204         struct tcf_proto *tp;
205
206         while ((tp = q->filter_list) != NULL) {
207                 q->filter_list = tp->next;
208                 tcf_destroy(tp);
209         }
210
211         for (prio=0; prio<q->bands; prio++)
212                 qdisc_destroy(q->queues[prio]);
213 }
214
215 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
216 {
217         struct prio_sched_data *q = qdisc_priv(sch);
218         struct tc_prio_qopt *qopt = RTA_DATA(opt);
219         int i;
220
221         if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
222                 return -EINVAL;
223         if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
224                 return -EINVAL;
225
226         for (i=0; i<=TC_PRIO_MAX; i++) {
227                 if (qopt->priomap[i] >= qopt->bands)
228                         return -EINVAL;
229         }
230
231         sch_tree_lock(sch);
232         q->bands = qopt->bands;
233         memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
234
235         for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
236                 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
237                 if (child != &noop_qdisc)
238                         qdisc_destroy(child);
239         }
240         sch_tree_unlock(sch);
241
242         for (i=0; i<=TC_PRIO_MAX; i++) {
243                 int band = q->prio2band[i];
244                 if (q->queues[band] == &noop_qdisc) {
245                         struct Qdisc *child;
246                         child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
247                         if (child) {
248                                 sch_tree_lock(sch);
249                                 child = xchg(&q->queues[band], child);
250
251                                 if (child != &noop_qdisc)
252                                         qdisc_destroy(child);
253                                 sch_tree_unlock(sch);
254                         }
255                 }
256         }
257         return 0;
258 }
259
260 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
261 {
262         struct prio_sched_data *q = qdisc_priv(sch);
263         int i;
264
265         for (i=0; i<TCQ_PRIO_BANDS; i++)
266                 q->queues[i] = &noop_qdisc;
267
268         if (opt == NULL) {
269                 return -EINVAL;
270         } else {
271                 int err;
272
273                 if ((err= prio_tune(sch, opt)) != 0)
274                         return err;
275         }
276         return 0;
277 }
278
279 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
280 {
281         struct prio_sched_data *q = qdisc_priv(sch);
282         unsigned char    *b = skb->tail;
283         struct tc_prio_qopt opt;
284
285         opt.bands = q->bands;
286         memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
287         RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
288         return skb->len;
289
290 rtattr_failure:
291         skb_trim(skb, b - skb->data);
292         return -1;
293 }
294
295 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
296                       struct Qdisc **old)
297 {
298         struct prio_sched_data *q = qdisc_priv(sch);
299         unsigned long band = arg - 1;
300
301         if (band >= q->bands)
302                 return -EINVAL;
303
304         if (new == NULL)
305                 new = &noop_qdisc;
306
307         sch_tree_lock(sch);
308         *old = q->queues[band];
309         q->queues[band] = new;
310         sch->q.qlen -= (*old)->q.qlen;
311         qdisc_reset(*old);
312         sch_tree_unlock(sch);
313
314         return 0;
315 }
316
317 static struct Qdisc *
318 prio_leaf(struct Qdisc *sch, unsigned long arg)
319 {
320         struct prio_sched_data *q = qdisc_priv(sch);
321         unsigned long band = arg - 1;
322
323         if (band >= q->bands)
324                 return NULL;
325
326         return q->queues[band];
327 }
328
329 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
330 {
331         struct prio_sched_data *q = qdisc_priv(sch);
332         unsigned long band = TC_H_MIN(classid);
333
334         if (band - 1 >= q->bands)
335                 return 0;
336         return band;
337 }
338
339 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
340 {
341         return prio_get(sch, classid);
342 }
343
344
345 static void prio_put(struct Qdisc *q, unsigned long cl)
346 {
347         return;
348 }
349
350 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
351 {
352         unsigned long cl = *arg;
353         struct prio_sched_data *q = qdisc_priv(sch);
354
355         if (cl - 1 > q->bands)
356                 return -ENOENT;
357         return 0;
358 }
359
360 static int prio_delete(struct Qdisc *sch, unsigned long cl)
361 {
362         struct prio_sched_data *q = qdisc_priv(sch);
363         if (cl - 1 > q->bands)
364                 return -ENOENT;
365         return 0;
366 }
367
368
369 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
370                            struct tcmsg *tcm)
371 {
372         struct prio_sched_data *q = qdisc_priv(sch);
373
374         if (cl - 1 > q->bands)
375                 return -ENOENT;
376         tcm->tcm_handle |= TC_H_MIN(cl);
377         if (q->queues[cl-1])
378                 tcm->tcm_info = q->queues[cl-1]->handle;
379         return 0;
380 }
381
382 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
383 {
384         struct prio_sched_data *q = qdisc_priv(sch);
385         int prio;
386
387         if (arg->stop)
388                 return;
389
390         for (prio = 0; prio < q->bands; prio++) {
391                 if (arg->count < arg->skip) {
392                         arg->count++;
393                         continue;
394                 }
395                 if (arg->fn(sch, prio+1, arg) < 0) {
396                         arg->stop = 1;
397                         break;
398                 }
399                 arg->count++;
400         }
401 }
402
403 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
404 {
405         struct prio_sched_data *q = qdisc_priv(sch);
406
407         if (cl)
408                 return NULL;
409         return &q->filter_list;
410 }
411
412 static struct Qdisc_class_ops prio_class_ops = {
413         .graft          =       prio_graft,
414         .leaf           =       prio_leaf,
415         .get            =       prio_get,
416         .put            =       prio_put,
417         .change         =       prio_change,
418         .delete         =       prio_delete,
419         .walk           =       prio_walk,
420         .tcf_chain      =       prio_find_tcf,
421         .bind_tcf       =       prio_bind,
422         .unbind_tcf     =       prio_put,
423         .dump           =       prio_dump_class,
424 };
425
426 static struct Qdisc_ops prio_qdisc_ops = {
427         .next           =       NULL,
428         .cl_ops         =       &prio_class_ops,
429         .id             =       "prio",
430         .priv_size      =       sizeof(struct prio_sched_data),
431         .enqueue        =       prio_enqueue,
432         .dequeue        =       prio_dequeue,
433         .requeue        =       prio_requeue,
434         .drop           =       prio_drop,
435         .init           =       prio_init,
436         .reset          =       prio_reset,
437         .destroy        =       prio_destroy,
438         .change         =       prio_tune,
439         .dump           =       prio_dump,
440         .owner          =       THIS_MODULE,
441 };
442
443 static int __init prio_module_init(void)
444 {
445         return register_qdisc(&prio_qdisc_ops);
446 }
447
448 static void __exit prio_module_exit(void) 
449 {
450         unregister_qdisc(&prio_qdisc_ops);
451 }
452
453 module_init(prio_module_init)
454 module_exit(prio_module_exit)
455
456 MODULE_LICENSE("GPL");