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 int curband
; /* for round-robin */
44 struct tcf_proto
*filter_list
;
45 u8 prio2band
[TC_PRIO_MAX
+1];
46 struct Qdisc
*queues
[TCQ_PRIO_BANDS
];
52 prio_classify(struct sk_buff
*skb
, struct Qdisc
*sch
, int *qerr
)
54 struct prio_sched_data
*q
= qdisc_priv(sch
);
55 u32 band
= skb
->priority
;
56 struct tcf_result res
;
58 *qerr
= NET_XMIT_BYPASS
;
59 if (TC_H_MAJ(skb
->priority
) != sch
->handle
) {
60 #ifdef CONFIG_NET_CLS_ACT
61 switch (tc_classify(skb
, q
->filter_list
, &res
)) {
64 *qerr
= NET_XMIT_SUCCESS
;
69 if (!q
->filter_list
) {
71 if (!q
->filter_list
|| tc_classify(skb
, q
->filter_list
, &res
)) {
75 band
= q
->prio2band
[band
&TC_PRIO_MAX
];
80 band
= TC_H_MIN(band
) - 1;
82 band
= q
->prio2band
[0];
85 skb_set_queue_mapping(skb
, band
);
86 return q
->queues
[band
];
90 prio_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
)
95 qdisc
= prio_classify(skb
, sch
, &ret
);
96 #ifdef CONFIG_NET_CLS_ACT
99 if (ret
== NET_XMIT_BYPASS
)
106 if ((ret
= qdisc
->enqueue(skb
, qdisc
)) == NET_XMIT_SUCCESS
) {
107 sch
->bstats
.bytes
+= skb
->len
;
108 sch
->bstats
.packets
++;
110 return NET_XMIT_SUCCESS
;
118 prio_requeue(struct sk_buff
*skb
, struct Qdisc
* sch
)
123 qdisc
= prio_classify(skb
, sch
, &ret
);
124 #ifdef CONFIG_NET_CLS_ACT
126 if (ret
== NET_XMIT_BYPASS
)
133 if ((ret
= qdisc
->ops
->requeue(skb
, qdisc
)) == NET_XMIT_SUCCESS
) {
135 sch
->qstats
.requeues
++;
139 return NET_XMIT_DROP
;
143 static struct sk_buff
*
144 prio_dequeue(struct Qdisc
* sch
)
147 struct prio_sched_data
*q
= qdisc_priv(sch
);
151 for (prio
= 0; prio
< q
->bands
; prio
++) {
152 /* Check if the target subqueue is available before
153 * pulling an skb. This way we avoid excessive requeues
156 if (!netif_subqueue_stopped(sch
->dev
, (q
->mq
? prio
: 0))) {
157 qdisc
= q
->queues
[prio
];
158 skb
= qdisc
->dequeue(qdisc
);
169 static struct sk_buff
*rr_dequeue(struct Qdisc
* sch
)
172 struct prio_sched_data
*q
= qdisc_priv(sch
);
176 /* Only take one pass through the queues. If nothing is available,
179 for (bandcount
= 0; bandcount
< q
->bands
; bandcount
++) {
180 /* Check if the target subqueue is available before
181 * pulling an skb. This way we avoid excessive requeues
182 * for slower queues. If the queue is stopped, try the
185 if (!netif_subqueue_stopped(sch
->dev
,
186 (q
->mq
? q
->curband
: 0))) {
187 qdisc
= q
->queues
[q
->curband
];
188 skb
= qdisc
->dequeue(qdisc
);
192 if (q
->curband
>= q
->bands
)
198 if (q
->curband
>= q
->bands
)
204 static unsigned int prio_drop(struct Qdisc
* sch
)
206 struct prio_sched_data
*q
= qdisc_priv(sch
);
211 for (prio
= q
->bands
-1; prio
>= 0; prio
--) {
212 qdisc
= q
->queues
[prio
];
213 if (qdisc
->ops
->drop
&& (len
= qdisc
->ops
->drop(qdisc
)) != 0) {
223 prio_reset(struct Qdisc
* sch
)
226 struct prio_sched_data
*q
= qdisc_priv(sch
);
228 for (prio
=0; prio
<q
->bands
; prio
++)
229 qdisc_reset(q
->queues
[prio
]);
234 prio_destroy(struct Qdisc
* sch
)
237 struct prio_sched_data
*q
= qdisc_priv(sch
);
239 tcf_destroy_chain(q
->filter_list
);
240 for (prio
=0; prio
<q
->bands
; prio
++)
241 qdisc_destroy(q
->queues
[prio
]);
244 static int prio_tune(struct Qdisc
*sch
, struct rtattr
*opt
)
246 struct prio_sched_data
*q
= qdisc_priv(sch
);
247 struct tc_prio_qopt
*qopt
;
248 struct rtattr
*tb
[TCA_PRIO_MAX
];
251 if (rtattr_parse_nested_compat(tb
, TCA_PRIO_MAX
, opt
, qopt
,
254 q
->bands
= qopt
->bands
;
255 /* If we're multiqueue, make sure the number of incoming bands
256 * matches the number of queues on the device we're associating with.
257 * If the number of bands requested is zero, then set q->bands to
258 * dev->egress_subqueue_count.
260 q
->mq
= RTA_GET_FLAG(tb
[TCA_PRIO_MQ
- 1]);
262 if (sch
->handle
!= TC_H_ROOT
)
264 if (netif_is_multiqueue(sch
->dev
)) {
266 q
->bands
= sch
->dev
->egress_subqueue_count
;
267 else if (q
->bands
!= sch
->dev
->egress_subqueue_count
)
273 if (q
->bands
> TCQ_PRIO_BANDS
|| q
->bands
< 2)
276 for (i
=0; i
<=TC_PRIO_MAX
; i
++) {
277 if (qopt
->priomap
[i
] >= q
->bands
)
282 memcpy(q
->prio2band
, qopt
->priomap
, TC_PRIO_MAX
+1);
284 for (i
=q
->bands
; i
<TCQ_PRIO_BANDS
; i
++) {
285 struct Qdisc
*child
= xchg(&q
->queues
[i
], &noop_qdisc
);
286 if (child
!= &noop_qdisc
) {
287 qdisc_tree_decrease_qlen(child
, child
->q
.qlen
);
288 qdisc_destroy(child
);
291 sch_tree_unlock(sch
);
293 for (i
=0; i
<q
->bands
; i
++) {
294 if (q
->queues
[i
] == &noop_qdisc
) {
296 child
= qdisc_create_dflt(sch
->dev
, &pfifo_qdisc_ops
,
297 TC_H_MAKE(sch
->handle
, i
+ 1));
300 child
= xchg(&q
->queues
[i
], child
);
302 if (child
!= &noop_qdisc
) {
303 qdisc_tree_decrease_qlen(child
,
305 qdisc_destroy(child
);
307 sch_tree_unlock(sch
);
314 static int prio_init(struct Qdisc
*sch
, struct rtattr
*opt
)
316 struct prio_sched_data
*q
= qdisc_priv(sch
);
319 for (i
=0; i
<TCQ_PRIO_BANDS
; i
++)
320 q
->queues
[i
] = &noop_qdisc
;
327 if ((err
= prio_tune(sch
, opt
)) != 0)
333 static int prio_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
335 struct prio_sched_data
*q
= qdisc_priv(sch
);
336 unsigned char *b
= skb_tail_pointer(skb
);
338 struct tc_prio_qopt opt
;
340 opt
.bands
= q
->bands
;
341 memcpy(&opt
.priomap
, q
->prio2band
, TC_PRIO_MAX
+1);
343 nest
= RTA_NEST_COMPAT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
345 RTA_PUT_FLAG(skb
, TCA_PRIO_MQ
);
346 RTA_NEST_COMPAT_END(skb
, nest
);
355 static int prio_graft(struct Qdisc
*sch
, unsigned long arg
, struct Qdisc
*new,
358 struct prio_sched_data
*q
= qdisc_priv(sch
);
359 unsigned long band
= arg
- 1;
361 if (band
>= q
->bands
)
368 *old
= q
->queues
[band
];
369 q
->queues
[band
] = new;
370 qdisc_tree_decrease_qlen(*old
, (*old
)->q
.qlen
);
372 sch_tree_unlock(sch
);
377 static struct Qdisc
*
378 prio_leaf(struct Qdisc
*sch
, unsigned long arg
)
380 struct prio_sched_data
*q
= qdisc_priv(sch
);
381 unsigned long band
= arg
- 1;
383 if (band
>= q
->bands
)
386 return q
->queues
[band
];
389 static unsigned long prio_get(struct Qdisc
*sch
, u32 classid
)
391 struct prio_sched_data
*q
= qdisc_priv(sch
);
392 unsigned long band
= TC_H_MIN(classid
);
394 if (band
- 1 >= q
->bands
)
399 static unsigned long prio_bind(struct Qdisc
*sch
, unsigned long parent
, u32 classid
)
401 return prio_get(sch
, classid
);
405 static void prio_put(struct Qdisc
*q
, unsigned long cl
)
410 static int prio_change(struct Qdisc
*sch
, u32 handle
, u32 parent
, struct rtattr
**tca
, unsigned long *arg
)
412 unsigned long cl
= *arg
;
413 struct prio_sched_data
*q
= qdisc_priv(sch
);
415 if (cl
- 1 > q
->bands
)
420 static int prio_delete(struct Qdisc
*sch
, unsigned long cl
)
422 struct prio_sched_data
*q
= qdisc_priv(sch
);
423 if (cl
- 1 > q
->bands
)
429 static int prio_dump_class(struct Qdisc
*sch
, unsigned long cl
, struct sk_buff
*skb
,
432 struct prio_sched_data
*q
= qdisc_priv(sch
);
434 if (cl
- 1 > q
->bands
)
436 tcm
->tcm_handle
|= TC_H_MIN(cl
);
438 tcm
->tcm_info
= q
->queues
[cl
-1]->handle
;
442 static int prio_dump_class_stats(struct Qdisc
*sch
, unsigned long cl
,
445 struct prio_sched_data
*q
= qdisc_priv(sch
);
448 cl_q
= q
->queues
[cl
- 1];
449 if (gnet_stats_copy_basic(d
, &cl_q
->bstats
) < 0 ||
450 gnet_stats_copy_queue(d
, &cl_q
->qstats
) < 0)
456 static void prio_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
458 struct prio_sched_data
*q
= qdisc_priv(sch
);
464 for (prio
= 0; prio
< q
->bands
; prio
++) {
465 if (arg
->count
< arg
->skip
) {
469 if (arg
->fn(sch
, prio
+1, arg
) < 0) {
477 static struct tcf_proto
** prio_find_tcf(struct Qdisc
*sch
, unsigned long cl
)
479 struct prio_sched_data
*q
= qdisc_priv(sch
);
483 return &q
->filter_list
;
486 static struct Qdisc_class_ops prio_class_ops
= {
491 .change
= prio_change
,
492 .delete = prio_delete
,
494 .tcf_chain
= prio_find_tcf
,
495 .bind_tcf
= prio_bind
,
496 .unbind_tcf
= prio_put
,
497 .dump
= prio_dump_class
,
498 .dump_stats
= prio_dump_class_stats
,
501 static struct Qdisc_ops prio_qdisc_ops
= {
503 .cl_ops
= &prio_class_ops
,
505 .priv_size
= sizeof(struct prio_sched_data
),
506 .enqueue
= prio_enqueue
,
507 .dequeue
= prio_dequeue
,
508 .requeue
= prio_requeue
,
512 .destroy
= prio_destroy
,
515 .owner
= THIS_MODULE
,
518 static struct Qdisc_ops rr_qdisc_ops
= {
520 .cl_ops
= &prio_class_ops
,
522 .priv_size
= sizeof(struct prio_sched_data
),
523 .enqueue
= prio_enqueue
,
524 .dequeue
= rr_dequeue
,
525 .requeue
= prio_requeue
,
529 .destroy
= prio_destroy
,
532 .owner
= THIS_MODULE
,
535 static int __init
prio_module_init(void)
539 err
= register_qdisc(&prio_qdisc_ops
);
542 err
= register_qdisc(&rr_qdisc_ops
);
544 unregister_qdisc(&prio_qdisc_ops
);
548 static void __exit
prio_module_exit(void)
550 unregister_qdisc(&prio_qdisc_ops
);
551 unregister_qdisc(&rr_qdisc_ops
);
554 module_init(prio_module_init
)
555 module_exit(prio_module_exit
)
557 MODULE_LICENSE("GPL");
558 MODULE_ALIAS("sch_rr");