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 <asm/uaccess.h>
16 #include <asm/system.h>
17 #include <linux/bitops.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/if_ether.h>
28 #include <linux/inet.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/notifier.h>
33 #include <net/route.h>
34 #include <linux/skbuff.h>
35 #include <net/netlink.h>
37 #include <net/pkt_sched.h>
40 struct prio_sched_data
43 struct tcf_proto
*filter_list
;
44 u8 prio2band
[TC_PRIO_MAX
+1];
45 struct Qdisc
*queues
[TCQ_PRIO_BANDS
];
50 prio_classify(struct sk_buff
*skb
, struct Qdisc
*sch
, int *qerr
)
52 struct prio_sched_data
*q
= qdisc_priv(sch
);
53 u32 band
= skb
->priority
;
54 struct tcf_result res
;
56 *qerr
= NET_XMIT_BYPASS
;
57 if (TC_H_MAJ(skb
->priority
) != sch
->handle
) {
58 #ifdef CONFIG_NET_CLS_ACT
59 switch (tc_classify(skb
, q
->filter_list
, &res
)) {
62 *qerr
= NET_XMIT_SUCCESS
;
67 if (!q
->filter_list
) {
69 if (!q
->filter_list
|| tc_classify(skb
, q
->filter_list
, &res
)) {
73 return q
->queues
[q
->prio2band
[band
&TC_PRIO_MAX
]];
77 band
= TC_H_MIN(band
) - 1;
79 return q
->queues
[q
->prio2band
[0]];
81 return q
->queues
[band
];
85 prio_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
)
90 qdisc
= prio_classify(skb
, sch
, &ret
);
91 #ifdef CONFIG_NET_CLS_ACT
94 if (ret
== NET_XMIT_BYPASS
)
101 if ((ret
= qdisc
->enqueue(skb
, qdisc
)) == NET_XMIT_SUCCESS
) {
102 sch
->bstats
.bytes
+= skb
->len
;
103 sch
->bstats
.packets
++;
105 return NET_XMIT_SUCCESS
;
113 prio_requeue(struct sk_buff
*skb
, struct Qdisc
* sch
)
118 qdisc
= prio_classify(skb
, sch
, &ret
);
119 #ifdef CONFIG_NET_CLS_ACT
121 if (ret
== NET_XMIT_BYPASS
)
128 if ((ret
= qdisc
->ops
->requeue(skb
, qdisc
)) == NET_XMIT_SUCCESS
) {
130 sch
->qstats
.requeues
++;
134 return NET_XMIT_DROP
;
138 static struct sk_buff
*
139 prio_dequeue(struct Qdisc
* sch
)
142 struct prio_sched_data
*q
= qdisc_priv(sch
);
146 for (prio
= 0; prio
< q
->bands
; prio
++) {
147 qdisc
= q
->queues
[prio
];
148 skb
= qdisc
->dequeue(qdisc
);
158 static unsigned int prio_drop(struct Qdisc
* sch
)
160 struct prio_sched_data
*q
= qdisc_priv(sch
);
165 for (prio
= q
->bands
-1; prio
>= 0; prio
--) {
166 qdisc
= q
->queues
[prio
];
167 if (qdisc
->ops
->drop
&& (len
= qdisc
->ops
->drop(qdisc
)) != 0) {
177 prio_reset(struct Qdisc
* sch
)
180 struct prio_sched_data
*q
= qdisc_priv(sch
);
182 for (prio
=0; prio
<q
->bands
; prio
++)
183 qdisc_reset(q
->queues
[prio
]);
188 prio_destroy(struct Qdisc
* sch
)
191 struct prio_sched_data
*q
= qdisc_priv(sch
);
193 tcf_destroy_chain(q
->filter_list
);
194 for (prio
=0; prio
<q
->bands
; prio
++)
195 qdisc_destroy(q
->queues
[prio
]);
198 static int prio_tune(struct Qdisc
*sch
, struct rtattr
*opt
)
200 struct prio_sched_data
*q
= qdisc_priv(sch
);
201 struct tc_prio_qopt
*qopt
= RTA_DATA(opt
);
204 if (opt
->rta_len
< RTA_LENGTH(sizeof(*qopt
)))
206 if (qopt
->bands
> TCQ_PRIO_BANDS
|| qopt
->bands
< 2)
209 for (i
=0; i
<=TC_PRIO_MAX
; i
++) {
210 if (qopt
->priomap
[i
] >= qopt
->bands
)
215 q
->bands
= qopt
->bands
;
216 memcpy(q
->prio2band
, qopt
->priomap
, TC_PRIO_MAX
+1);
218 for (i
=q
->bands
; i
<TCQ_PRIO_BANDS
; i
++) {
219 struct Qdisc
*child
= xchg(&q
->queues
[i
], &noop_qdisc
);
220 if (child
!= &noop_qdisc
) {
221 qdisc_tree_decrease_qlen(child
, child
->q
.qlen
);
222 qdisc_destroy(child
);
225 sch_tree_unlock(sch
);
227 for (i
=0; i
<q
->bands
; i
++) {
228 if (q
->queues
[i
] == &noop_qdisc
) {
230 child
= qdisc_create_dflt(sch
->dev
, &pfifo_qdisc_ops
,
231 TC_H_MAKE(sch
->handle
, i
+ 1));
234 child
= xchg(&q
->queues
[i
], child
);
236 if (child
!= &noop_qdisc
) {
237 qdisc_tree_decrease_qlen(child
,
239 qdisc_destroy(child
);
241 sch_tree_unlock(sch
);
248 static int prio_init(struct Qdisc
*sch
, struct rtattr
*opt
)
250 struct prio_sched_data
*q
= qdisc_priv(sch
);
253 for (i
=0; i
<TCQ_PRIO_BANDS
; i
++)
254 q
->queues
[i
] = &noop_qdisc
;
261 if ((err
= prio_tune(sch
, opt
)) != 0)
267 static int prio_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
269 struct prio_sched_data
*q
= qdisc_priv(sch
);
270 unsigned char *b
= skb_tail_pointer(skb
);
271 struct tc_prio_qopt opt
;
273 opt
.bands
= q
->bands
;
274 memcpy(&opt
.priomap
, q
->prio2band
, TC_PRIO_MAX
+1);
275 RTA_PUT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
283 static int prio_graft(struct Qdisc
*sch
, unsigned long arg
, struct Qdisc
*new,
286 struct prio_sched_data
*q
= qdisc_priv(sch
);
287 unsigned long band
= arg
- 1;
289 if (band
>= q
->bands
)
296 *old
= q
->queues
[band
];
297 q
->queues
[band
] = new;
298 qdisc_tree_decrease_qlen(*old
, (*old
)->q
.qlen
);
300 sch_tree_unlock(sch
);
305 static struct Qdisc
*
306 prio_leaf(struct Qdisc
*sch
, unsigned long arg
)
308 struct prio_sched_data
*q
= qdisc_priv(sch
);
309 unsigned long band
= arg
- 1;
311 if (band
>= q
->bands
)
314 return q
->queues
[band
];
317 static unsigned long prio_get(struct Qdisc
*sch
, u32 classid
)
319 struct prio_sched_data
*q
= qdisc_priv(sch
);
320 unsigned long band
= TC_H_MIN(classid
);
322 if (band
- 1 >= q
->bands
)
327 static unsigned long prio_bind(struct Qdisc
*sch
, unsigned long parent
, u32 classid
)
329 return prio_get(sch
, classid
);
333 static void prio_put(struct Qdisc
*q
, unsigned long cl
)
338 static int prio_change(struct Qdisc
*sch
, u32 handle
, u32 parent
, struct rtattr
**tca
, unsigned long *arg
)
340 unsigned long cl
= *arg
;
341 struct prio_sched_data
*q
= qdisc_priv(sch
);
343 if (cl
- 1 > q
->bands
)
348 static int prio_delete(struct Qdisc
*sch
, unsigned long cl
)
350 struct prio_sched_data
*q
= qdisc_priv(sch
);
351 if (cl
- 1 > q
->bands
)
357 static int prio_dump_class(struct Qdisc
*sch
, unsigned long cl
, struct sk_buff
*skb
,
360 struct prio_sched_data
*q
= qdisc_priv(sch
);
362 if (cl
- 1 > q
->bands
)
364 tcm
->tcm_handle
|= TC_H_MIN(cl
);
366 tcm
->tcm_info
= q
->queues
[cl
-1]->handle
;
370 static int prio_dump_class_stats(struct Qdisc
*sch
, unsigned long cl
,
373 struct prio_sched_data
*q
= qdisc_priv(sch
);
376 cl_q
= q
->queues
[cl
- 1];
377 if (gnet_stats_copy_basic(d
, &cl_q
->bstats
) < 0 ||
378 gnet_stats_copy_queue(d
, &cl_q
->qstats
) < 0)
384 static void prio_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
386 struct prio_sched_data
*q
= qdisc_priv(sch
);
392 for (prio
= 0; prio
< q
->bands
; prio
++) {
393 if (arg
->count
< arg
->skip
) {
397 if (arg
->fn(sch
, prio
+1, arg
) < 0) {
405 static struct tcf_proto
** prio_find_tcf(struct Qdisc
*sch
, unsigned long cl
)
407 struct prio_sched_data
*q
= qdisc_priv(sch
);
411 return &q
->filter_list
;
414 static struct Qdisc_class_ops prio_class_ops
= {
419 .change
= prio_change
,
420 .delete = prio_delete
,
422 .tcf_chain
= prio_find_tcf
,
423 .bind_tcf
= prio_bind
,
424 .unbind_tcf
= prio_put
,
425 .dump
= prio_dump_class
,
426 .dump_stats
= prio_dump_class_stats
,
429 static struct Qdisc_ops prio_qdisc_ops
= {
431 .cl_ops
= &prio_class_ops
,
433 .priv_size
= sizeof(struct prio_sched_data
),
434 .enqueue
= prio_enqueue
,
435 .dequeue
= prio_dequeue
,
436 .requeue
= prio_requeue
,
440 .destroy
= prio_destroy
,
443 .owner
= THIS_MODULE
,
446 static int __init
prio_module_init(void)
448 return register_qdisc(&prio_qdisc_ops
);
451 static void __exit
prio_module_exit(void)
453 unregister_qdisc(&prio_qdisc_ops
);
456 module_init(prio_module_init
)
457 module_exit(prio_module_exit
)
459 MODULE_LICENSE("GPL");