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/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>
24 #include <linux/socket.h>
25 #include <linux/sockios.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>
35 #include <net/route.h>
36 #include <linux/skbuff.h>
38 #include <net/pkt_sched.h>
41 struct prio_sched_data
44 struct tcf_proto *filter_list;
45 u8 prio2band[TC_PRIO_MAX+1];
46 struct Qdisc *queues[TCQ_PRIO_BANDS];
50 static __inline__ unsigned prio_classify(struct sk_buff *skb, struct Qdisc *sch)
52 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
53 struct tcf_result res;
57 if (TC_H_MAJ(skb->priority) != sch->handle) {
58 if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
61 return q->prio2band[band&TC_PRIO_MAX];
65 band = TC_H_MIN(band) - 1;
66 return band < q->bands ? band : q->prio2band[0];
70 prio_enqueue(struct sk_buff *skb, struct Qdisc* sch)
72 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
76 qdisc = q->queues[prio_classify(skb, sch)];
78 if ((ret = qdisc->enqueue(skb, qdisc)) == 0) {
79 sch->stats.bytes += skb->len;
90 prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
92 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
96 qdisc = q->queues[prio_classify(skb, sch)];
98 if ((ret = qdisc->ops->requeue(skb, qdisc)) == 0) {
107 static struct sk_buff *
108 prio_dequeue(struct Qdisc* sch)
111 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
115 for (prio = 0; prio < q->bands; prio++) {
116 qdisc = q->queues[prio];
117 skb = qdisc->dequeue(qdisc);
127 static unsigned int prio_drop(struct Qdisc* sch)
129 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
134 for (prio = q->bands-1; prio >= 0; prio--) {
135 qdisc = q->queues[prio];
136 if ((len = qdisc->ops->drop(qdisc)) != 0) {
146 prio_reset(struct Qdisc* sch)
149 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
151 for (prio=0; prio<q->bands; prio++)
152 qdisc_reset(q->queues[prio]);
157 prio_destroy(struct Qdisc* sch)
160 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
161 struct tcf_proto *tp;
163 while ((tp = q->filter_list) != NULL) {
164 q->filter_list = tp->next;
168 for (prio=0; prio<q->bands; prio++) {
169 qdisc_destroy(q->queues[prio]);
170 q->queues[prio] = &noop_qdisc;
174 static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
176 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
177 struct tc_prio_qopt *qopt = RTA_DATA(opt);
180 if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
182 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
185 for (i=0; i<=TC_PRIO_MAX; i++) {
186 if (qopt->priomap[i] >= qopt->bands)
191 q->bands = qopt->bands;
192 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
194 for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
195 struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
196 if (child != &noop_qdisc)
197 qdisc_destroy(child);
199 sch_tree_unlock(sch);
201 for (i=0; i<=TC_PRIO_MAX; i++) {
202 int band = q->prio2band[i];
203 if (q->queues[band] == &noop_qdisc) {
205 child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
208 child = xchg(&q->queues[band], child);
210 if (child != &noop_qdisc)
211 qdisc_destroy(child);
212 sch_tree_unlock(sch);
219 static int prio_init(struct Qdisc *sch, struct rtattr *opt)
221 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
224 for (i=0; i<TCQ_PRIO_BANDS; i++)
225 q->queues[i] = &noop_qdisc;
232 if ((err= prio_tune(sch, opt)) != 0)
238 static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
240 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
241 unsigned char *b = skb->tail;
242 struct tc_prio_qopt opt;
244 opt.bands = q->bands;
245 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
246 RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
250 skb_trim(skb, b - skb->data);
254 static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
257 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
258 unsigned long band = arg - 1;
260 if (band >= q->bands)
267 *old = q->queues[band];
268 q->queues[band] = new;
269 sch->q.qlen -= (*old)->q.qlen;
271 sch_tree_unlock(sch);
276 static struct Qdisc *
277 prio_leaf(struct Qdisc *sch, unsigned long arg)
279 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
280 unsigned long band = arg - 1;
282 if (band >= q->bands)
285 return q->queues[band];
288 static unsigned long prio_get(struct Qdisc *sch, u32 classid)
290 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
291 unsigned long band = TC_H_MIN(classid);
293 if (band - 1 >= q->bands)
298 static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
300 return prio_get(sch, classid);
304 static void prio_put(struct Qdisc *q, unsigned long cl)
309 static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
311 unsigned long cl = *arg;
312 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
314 if (cl - 1 > q->bands)
319 static int prio_delete(struct Qdisc *sch, unsigned long cl)
321 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
322 if (cl - 1 > q->bands)
328 static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
331 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
333 if (cl - 1 > q->bands)
335 tcm->tcm_handle |= TC_H_MIN(cl);
337 tcm->tcm_info = q->queues[cl-1]->handle;
341 static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
343 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
349 for (prio = 0; prio < q->bands; prio++) {
350 if (arg->count < arg->skip) {
354 if (arg->fn(sch, prio+1, arg) < 0) {
362 static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
364 struct prio_sched_data *q = (struct prio_sched_data *)sch->data;
368 return &q->filter_list;
371 static struct Qdisc_class_ops prio_class_ops = {
376 .change = prio_change,
377 .delete = prio_delete,
379 .tcf_chain = prio_find_tcf,
380 .bind_tcf = prio_bind,
381 .unbind_tcf = prio_put,
382 .dump = prio_dump_class,
385 static struct Qdisc_ops prio_qdisc_ops = {
387 .cl_ops = &prio_class_ops,
389 .priv_size = sizeof(struct prio_sched_data),
390 .enqueue = prio_enqueue,
391 .dequeue = prio_dequeue,
392 .requeue = prio_requeue,
396 .destroy = prio_destroy,
399 .owner = THIS_MODULE,
402 static int __init prio_module_init(void)
404 return register_qdisc(&prio_qdisc_ops);
407 static void __exit prio_module_exit(void)
409 unregister_qdisc(&prio_qdisc_ops);
412 module_init(prio_module_init)
413 module_exit(prio_module_exit)
415 MODULE_LICENSE("GPL");