pkt_sched: Schedule qdiscs instead of netdev_queue.
[linux-2.6] / 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/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/skbuff.h>
20 #include <net/netlink.h>
21 #include <net/pkt_sched.h>
22
23
24 struct prio_sched_data
25 {
26         int bands;
27         struct tcf_proto *filter_list;
28         u8  prio2band[TC_PRIO_MAX+1];
29         struct Qdisc *queues[TCQ_PRIO_BANDS];
30 };
31
32
33 static struct Qdisc *
34 prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
35 {
36         struct prio_sched_data *q = qdisc_priv(sch);
37         u32 band = skb->priority;
38         struct tcf_result res;
39         int err;
40
41         *qerr = NET_XMIT_BYPASS;
42         if (TC_H_MAJ(skb->priority) != sch->handle) {
43                 err = tc_classify(skb, q->filter_list, &res);
44 #ifdef CONFIG_NET_CLS_ACT
45                 switch (err) {
46                 case TC_ACT_STOLEN:
47                 case TC_ACT_QUEUED:
48                         *qerr = NET_XMIT_SUCCESS;
49                 case TC_ACT_SHOT:
50                         return NULL;
51                 }
52 #endif
53                 if (!q->filter_list || err < 0) {
54                         if (TC_H_MAJ(band))
55                                 band = 0;
56                         return q->queues[q->prio2band[band&TC_PRIO_MAX]];
57                 }
58                 band = res.classid;
59         }
60         band = TC_H_MIN(band) - 1;
61         if (band >= q->bands)
62                 return q->queues[q->prio2band[0]];
63
64         return q->queues[band];
65 }
66
67 static int
68 prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
69 {
70         struct Qdisc *qdisc;
71         int ret;
72
73         qdisc = prio_classify(skb, sch, &ret);
74 #ifdef CONFIG_NET_CLS_ACT
75         if (qdisc == NULL) {
76
77                 if (ret == NET_XMIT_BYPASS)
78                         sch->qstats.drops++;
79                 kfree_skb(skb);
80                 return ret;
81         }
82 #endif
83
84         if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
85                 sch->bstats.bytes += skb->len;
86                 sch->bstats.packets++;
87                 sch->q.qlen++;
88                 return NET_XMIT_SUCCESS;
89         }
90         sch->qstats.drops++;
91         return ret;
92 }
93
94
95 static int
96 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
97 {
98         struct Qdisc *qdisc;
99         int ret;
100
101         qdisc = prio_classify(skb, sch, &ret);
102 #ifdef CONFIG_NET_CLS_ACT
103         if (qdisc == NULL) {
104                 if (ret == NET_XMIT_BYPASS)
105                         sch->qstats.drops++;
106                 kfree_skb(skb);
107                 return ret;
108         }
109 #endif
110
111         if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
112                 sch->q.qlen++;
113                 sch->qstats.requeues++;
114                 return 0;
115         }
116         sch->qstats.drops++;
117         return NET_XMIT_DROP;
118 }
119
120
121 static struct sk_buff *prio_dequeue(struct Qdisc* sch)
122 {
123         struct prio_sched_data *q = qdisc_priv(sch);
124         int prio;
125
126         for (prio = 0; prio < q->bands; prio++) {
127                 struct Qdisc *qdisc = q->queues[prio];
128                 struct sk_buff *skb = qdisc->dequeue(qdisc);
129                 if (skb) {
130                         sch->q.qlen--;
131                         return skb;
132                 }
133         }
134         return NULL;
135
136 }
137
138 static unsigned int prio_drop(struct Qdisc* sch)
139 {
140         struct prio_sched_data *q = qdisc_priv(sch);
141         int prio;
142         unsigned int len;
143         struct Qdisc *qdisc;
144
145         for (prio = q->bands-1; prio >= 0; prio--) {
146                 qdisc = q->queues[prio];
147                 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
148                         sch->q.qlen--;
149                         return len;
150                 }
151         }
152         return 0;
153 }
154
155
156 static void
157 prio_reset(struct Qdisc* sch)
158 {
159         int prio;
160         struct prio_sched_data *q = qdisc_priv(sch);
161
162         for (prio=0; prio<q->bands; prio++)
163                 qdisc_reset(q->queues[prio]);
164         sch->q.qlen = 0;
165 }
166
167 static void
168 prio_destroy(struct Qdisc* sch)
169 {
170         int prio;
171         struct prio_sched_data *q = qdisc_priv(sch);
172
173         tcf_destroy_chain(&q->filter_list);
174         for (prio=0; prio<q->bands; prio++)
175                 qdisc_destroy(q->queues[prio]);
176 }
177
178 static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
179 {
180         struct prio_sched_data *q = qdisc_priv(sch);
181         struct tc_prio_qopt *qopt;
182         int i;
183
184         if (nla_len(opt) < sizeof(*qopt))
185                 return -EINVAL;
186         qopt = nla_data(opt);
187
188         if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
189                 return -EINVAL;
190
191         for (i=0; i<=TC_PRIO_MAX; i++) {
192                 if (qopt->priomap[i] >= qopt->bands)
193                         return -EINVAL;
194         }
195
196         sch_tree_lock(sch);
197         q->bands = qopt->bands;
198         memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
199
200         for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
201                 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
202                 if (child != &noop_qdisc) {
203                         qdisc_tree_decrease_qlen(child, child->q.qlen);
204                         qdisc_destroy(child);
205                 }
206         }
207         sch_tree_unlock(sch);
208
209         for (i=0; i<q->bands; i++) {
210                 if (q->queues[i] == &noop_qdisc) {
211                         struct Qdisc *child;
212                         child = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
213                                                   &pfifo_qdisc_ops,
214                                                   TC_H_MAKE(sch->handle, i + 1));
215                         if (child) {
216                                 sch_tree_lock(sch);
217                                 child = xchg(&q->queues[i], child);
218
219                                 if (child != &noop_qdisc) {
220                                         qdisc_tree_decrease_qlen(child,
221                                                                  child->q.qlen);
222                                         qdisc_destroy(child);
223                                 }
224                                 sch_tree_unlock(sch);
225                         }
226                 }
227         }
228         return 0;
229 }
230
231 static int prio_init(struct Qdisc *sch, struct nlattr *opt)
232 {
233         struct prio_sched_data *q = qdisc_priv(sch);
234         int i;
235
236         for (i=0; i<TCQ_PRIO_BANDS; i++)
237                 q->queues[i] = &noop_qdisc;
238
239         if (opt == NULL) {
240                 return -EINVAL;
241         } else {
242                 int err;
243
244                 if ((err= prio_tune(sch, opt)) != 0)
245                         return err;
246         }
247         return 0;
248 }
249
250 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
251 {
252         struct prio_sched_data *q = qdisc_priv(sch);
253         unsigned char *b = skb_tail_pointer(skb);
254         struct nlattr *nest;
255         struct tc_prio_qopt opt;
256
257         opt.bands = q->bands;
258         memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
259
260         nest = nla_nest_compat_start(skb, TCA_OPTIONS, sizeof(opt), &opt);
261         if (nest == NULL)
262                 goto nla_put_failure;
263         nla_nest_compat_end(skb, nest);
264
265         return skb->len;
266
267 nla_put_failure:
268         nlmsg_trim(skb, b);
269         return -1;
270 }
271
272 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
273                       struct Qdisc **old)
274 {
275         struct prio_sched_data *q = qdisc_priv(sch);
276         unsigned long band = arg - 1;
277
278         if (band >= q->bands)
279                 return -EINVAL;
280
281         if (new == NULL)
282                 new = &noop_qdisc;
283
284         sch_tree_lock(sch);
285         *old = q->queues[band];
286         q->queues[band] = new;
287         qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
288         qdisc_reset(*old);
289         sch_tree_unlock(sch);
290
291         return 0;
292 }
293
294 static struct Qdisc *
295 prio_leaf(struct Qdisc *sch, unsigned long arg)
296 {
297         struct prio_sched_data *q = qdisc_priv(sch);
298         unsigned long band = arg - 1;
299
300         if (band >= q->bands)
301                 return NULL;
302
303         return q->queues[band];
304 }
305
306 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
307 {
308         struct prio_sched_data *q = qdisc_priv(sch);
309         unsigned long band = TC_H_MIN(classid);
310
311         if (band - 1 >= q->bands)
312                 return 0;
313         return band;
314 }
315
316 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
317 {
318         return prio_get(sch, classid);
319 }
320
321
322 static void prio_put(struct Qdisc *q, unsigned long cl)
323 {
324         return;
325 }
326
327 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct nlattr **tca, unsigned long *arg)
328 {
329         unsigned long cl = *arg;
330         struct prio_sched_data *q = qdisc_priv(sch);
331
332         if (cl - 1 > q->bands)
333                 return -ENOENT;
334         return 0;
335 }
336
337 static int prio_delete(struct Qdisc *sch, unsigned long cl)
338 {
339         struct prio_sched_data *q = qdisc_priv(sch);
340         if (cl - 1 > q->bands)
341                 return -ENOENT;
342         return 0;
343 }
344
345
346 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
347                            struct tcmsg *tcm)
348 {
349         struct prio_sched_data *q = qdisc_priv(sch);
350
351         if (cl - 1 > q->bands)
352                 return -ENOENT;
353         tcm->tcm_handle |= TC_H_MIN(cl);
354         if (q->queues[cl-1])
355                 tcm->tcm_info = q->queues[cl-1]->handle;
356         return 0;
357 }
358
359 static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
360                                  struct gnet_dump *d)
361 {
362         struct prio_sched_data *q = qdisc_priv(sch);
363         struct Qdisc *cl_q;
364
365         cl_q = q->queues[cl - 1];
366         if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
367             gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
368                 return -1;
369
370         return 0;
371 }
372
373 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
374 {
375         struct prio_sched_data *q = qdisc_priv(sch);
376         int prio;
377
378         if (arg->stop)
379                 return;
380
381         for (prio = 0; prio < q->bands; prio++) {
382                 if (arg->count < arg->skip) {
383                         arg->count++;
384                         continue;
385                 }
386                 if (arg->fn(sch, prio+1, arg) < 0) {
387                         arg->stop = 1;
388                         break;
389                 }
390                 arg->count++;
391         }
392 }
393
394 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
395 {
396         struct prio_sched_data *q = qdisc_priv(sch);
397
398         if (cl)
399                 return NULL;
400         return &q->filter_list;
401 }
402
403 static const struct Qdisc_class_ops prio_class_ops = {
404         .graft          =       prio_graft,
405         .leaf           =       prio_leaf,
406         .get            =       prio_get,
407         .put            =       prio_put,
408         .change         =       prio_change,
409         .delete         =       prio_delete,
410         .walk           =       prio_walk,
411         .tcf_chain      =       prio_find_tcf,
412         .bind_tcf       =       prio_bind,
413         .unbind_tcf     =       prio_put,
414         .dump           =       prio_dump_class,
415         .dump_stats     =       prio_dump_class_stats,
416 };
417
418 static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
419         .next           =       NULL,
420         .cl_ops         =       &prio_class_ops,
421         .id             =       "prio",
422         .priv_size      =       sizeof(struct prio_sched_data),
423         .enqueue        =       prio_enqueue,
424         .dequeue        =       prio_dequeue,
425         .requeue        =       prio_requeue,
426         .drop           =       prio_drop,
427         .init           =       prio_init,
428         .reset          =       prio_reset,
429         .destroy        =       prio_destroy,
430         .change         =       prio_tune,
431         .dump           =       prio_dump,
432         .owner          =       THIS_MODULE,
433 };
434
435 static int __init prio_module_init(void)
436 {
437         return register_qdisc(&prio_qdisc_ops);
438 }
439
440 static void __exit prio_module_exit(void)
441 {
442         unregister_qdisc(&prio_qdisc_ops);
443 }
444
445 module_init(prio_module_init)
446 module_exit(prio_module_exit)
447
448 MODULE_LICENSE("GPL");