2 * net/sched/sch_prio.c Simple 3-band priority "scheduler".
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * Fixes: 19990609: J Hadi Salim <hadi@nortelnetworks.com>:
11 * Init -- EINVAL when opt undefined
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>
24 struct prio_sched_data
27 struct tcf_proto *filter_list;
28 u8 prio2band[TC_PRIO_MAX+1];
29 struct Qdisc *queues[TCQ_PRIO_BANDS];
34 prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
36 struct prio_sched_data *q = qdisc_priv(sch);
37 u32 band = skb->priority;
38 struct tcf_result res;
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
48 *qerr = NET_XMIT_SUCCESS;
53 if (!q->filter_list || err < 0) {
56 return q->queues[q->prio2band[band&TC_PRIO_MAX]];
60 band = TC_H_MIN(band) - 1;
62 return q->queues[q->prio2band[0]];
64 return q->queues[band];
68 prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
73 qdisc = prio_classify(skb, sch, &ret);
74 #ifdef CONFIG_NET_CLS_ACT
77 if (ret == NET_XMIT_BYPASS)
84 if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
85 sch->bstats.bytes += skb->len;
86 sch->bstats.packets++;
88 return NET_XMIT_SUCCESS;
96 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
101 qdisc = prio_classify(skb, sch, &ret);
102 #ifdef CONFIG_NET_CLS_ACT
104 if (ret == NET_XMIT_BYPASS)
111 if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
113 sch->qstats.requeues++;
117 return NET_XMIT_DROP;
121 static struct sk_buff *prio_dequeue(struct Qdisc* sch)
123 struct prio_sched_data *q = qdisc_priv(sch);
126 for (prio = 0; prio < q->bands; prio++) {
127 struct Qdisc *qdisc = q->queues[prio];
128 struct sk_buff *skb = qdisc->dequeue(qdisc);
138 static unsigned int prio_drop(struct Qdisc* sch)
140 struct prio_sched_data *q = qdisc_priv(sch);
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) {
157 prio_reset(struct Qdisc* sch)
160 struct prio_sched_data *q = qdisc_priv(sch);
162 for (prio=0; prio<q->bands; prio++)
163 qdisc_reset(q->queues[prio]);
168 prio_destroy(struct Qdisc* sch)
171 struct prio_sched_data *q = qdisc_priv(sch);
173 tcf_destroy_chain(&q->filter_list);
174 for (prio=0; prio<q->bands; prio++)
175 qdisc_destroy(q->queues[prio]);
178 static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
180 struct prio_sched_data *q = qdisc_priv(sch);
181 struct tc_prio_qopt *qopt;
184 if (nla_len(opt) < sizeof(*qopt))
186 qopt = nla_data(opt);
188 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
191 for (i=0; i<=TC_PRIO_MAX; i++) {
192 if (qopt->priomap[i] >= qopt->bands)
197 q->bands = qopt->bands;
198 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
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);
207 sch_tree_unlock(sch);
209 for (i=0; i<q->bands; i++) {
210 if (q->queues[i] == &noop_qdisc) {
212 child = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
214 TC_H_MAKE(sch->handle, i + 1));
217 child = xchg(&q->queues[i], child);
219 if (child != &noop_qdisc) {
220 qdisc_tree_decrease_qlen(child,
222 qdisc_destroy(child);
224 sch_tree_unlock(sch);
231 static int prio_init(struct Qdisc *sch, struct nlattr *opt)
233 struct prio_sched_data *q = qdisc_priv(sch);
236 for (i=0; i<TCQ_PRIO_BANDS; i++)
237 q->queues[i] = &noop_qdisc;
244 if ((err= prio_tune(sch, opt)) != 0)
250 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
252 struct prio_sched_data *q = qdisc_priv(sch);
253 unsigned char *b = skb_tail_pointer(skb);
255 struct tc_prio_qopt opt;
257 opt.bands = q->bands;
258 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
260 nest = nla_nest_compat_start(skb, TCA_OPTIONS, sizeof(opt), &opt);
262 goto nla_put_failure;
263 nla_nest_compat_end(skb, nest);
272 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
275 struct prio_sched_data *q = qdisc_priv(sch);
276 unsigned long band = arg - 1;
278 if (band >= q->bands)
285 *old = q->queues[band];
286 q->queues[band] = new;
287 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
289 sch_tree_unlock(sch);
294 static struct Qdisc *
295 prio_leaf(struct Qdisc *sch, unsigned long arg)
297 struct prio_sched_data *q = qdisc_priv(sch);
298 unsigned long band = arg - 1;
300 if (band >= q->bands)
303 return q->queues[band];
306 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
308 struct prio_sched_data *q = qdisc_priv(sch);
309 unsigned long band = TC_H_MIN(classid);
311 if (band - 1 >= q->bands)
316 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
318 return prio_get(sch, classid);
322 static void prio_put(struct Qdisc *q, unsigned long cl)
327 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct nlattr **tca, unsigned long *arg)
329 unsigned long cl = *arg;
330 struct prio_sched_data *q = qdisc_priv(sch);
332 if (cl - 1 > q->bands)
337 static int prio_delete(struct Qdisc *sch, unsigned long cl)
339 struct prio_sched_data *q = qdisc_priv(sch);
340 if (cl - 1 > q->bands)
346 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
349 struct prio_sched_data *q = qdisc_priv(sch);
351 if (cl - 1 > q->bands)
353 tcm->tcm_handle |= TC_H_MIN(cl);
355 tcm->tcm_info = q->queues[cl-1]->handle;
359 static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
362 struct prio_sched_data *q = qdisc_priv(sch);
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)
373 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
375 struct prio_sched_data *q = qdisc_priv(sch);
381 for (prio = 0; prio < q->bands; prio++) {
382 if (arg->count < arg->skip) {
386 if (arg->fn(sch, prio+1, arg) < 0) {
394 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
396 struct prio_sched_data *q = qdisc_priv(sch);
400 return &q->filter_list;
403 static const struct Qdisc_class_ops prio_class_ops = {
408 .change = prio_change,
409 .delete = prio_delete,
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,
418 static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
420 .cl_ops = &prio_class_ops,
422 .priv_size = sizeof(struct prio_sched_data),
423 .enqueue = prio_enqueue,
424 .dequeue = prio_dequeue,
425 .requeue = prio_requeue,
429 .destroy = prio_destroy,
432 .owner = THIS_MODULE,
435 static int __init prio_module_init(void)
437 return register_qdisc(&prio_qdisc_ops);
440 static void __exit prio_module_exit(void)
442 unregister_qdisc(&prio_qdisc_ops);
445 module_init(prio_module_init)
446 module_exit(prio_module_exit)
448 MODULE_LICENSE("GPL");