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 int curband
; /* for round-robin */
28 struct tcf_proto
*filter_list
;
29 u8 prio2band
[TC_PRIO_MAX
+1];
30 struct Qdisc
*queues
[TCQ_PRIO_BANDS
];
36 prio_classify(struct sk_buff
*skb
, struct Qdisc
*sch
, int *qerr
)
38 struct prio_sched_data
*q
= qdisc_priv(sch
);
39 u32 band
= skb
->priority
;
40 struct tcf_result res
;
42 *qerr
= NET_XMIT_BYPASS
;
43 if (TC_H_MAJ(skb
->priority
) != sch
->handle
) {
44 #ifdef CONFIG_NET_CLS_ACT
45 switch (tc_classify(skb
, q
->filter_list
, &res
)) {
48 *qerr
= NET_XMIT_SUCCESS
;
53 if (!q
->filter_list
) {
55 if (!q
->filter_list
|| tc_classify(skb
, q
->filter_list
, &res
)) {
59 band
= q
->prio2band
[band
&TC_PRIO_MAX
];
64 band
= TC_H_MIN(band
) - 1;
66 band
= q
->prio2band
[0];
69 skb_set_queue_mapping(skb
, band
);
70 return q
->queues
[band
];
74 prio_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
)
79 qdisc
= prio_classify(skb
, sch
, &ret
);
80 #ifdef CONFIG_NET_CLS_ACT
83 if (ret
== NET_XMIT_BYPASS
)
90 if ((ret
= qdisc
->enqueue(skb
, qdisc
)) == NET_XMIT_SUCCESS
) {
91 sch
->bstats
.bytes
+= skb
->len
;
92 sch
->bstats
.packets
++;
94 return NET_XMIT_SUCCESS
;
102 prio_requeue(struct sk_buff
*skb
, struct Qdisc
* sch
)
107 qdisc
= prio_classify(skb
, sch
, &ret
);
108 #ifdef CONFIG_NET_CLS_ACT
110 if (ret
== NET_XMIT_BYPASS
)
117 if ((ret
= qdisc
->ops
->requeue(skb
, qdisc
)) == NET_XMIT_SUCCESS
) {
119 sch
->qstats
.requeues
++;
123 return NET_XMIT_DROP
;
127 static struct sk_buff
*
128 prio_dequeue(struct Qdisc
* sch
)
131 struct prio_sched_data
*q
= qdisc_priv(sch
);
135 for (prio
= 0; prio
< q
->bands
; prio
++) {
136 /* Check if the target subqueue is available before
137 * pulling an skb. This way we avoid excessive requeues
140 if (!netif_subqueue_stopped(sch
->dev
, (q
->mq
? prio
: 0))) {
141 qdisc
= q
->queues
[prio
];
142 skb
= qdisc
->dequeue(qdisc
);
153 static struct sk_buff
*rr_dequeue(struct Qdisc
* sch
)
156 struct prio_sched_data
*q
= qdisc_priv(sch
);
160 /* Only take one pass through the queues. If nothing is available,
163 for (bandcount
= 0; bandcount
< q
->bands
; bandcount
++) {
164 /* Check if the target subqueue is available before
165 * pulling an skb. This way we avoid excessive requeues
166 * for slower queues. If the queue is stopped, try the
169 if (!netif_subqueue_stopped(sch
->dev
,
170 (q
->mq
? q
->curband
: 0))) {
171 qdisc
= q
->queues
[q
->curband
];
172 skb
= qdisc
->dequeue(qdisc
);
176 if (q
->curband
>= q
->bands
)
182 if (q
->curband
>= q
->bands
)
188 static unsigned int prio_drop(struct Qdisc
* sch
)
190 struct prio_sched_data
*q
= qdisc_priv(sch
);
195 for (prio
= q
->bands
-1; prio
>= 0; prio
--) {
196 qdisc
= q
->queues
[prio
];
197 if (qdisc
->ops
->drop
&& (len
= qdisc
->ops
->drop(qdisc
)) != 0) {
207 prio_reset(struct Qdisc
* sch
)
210 struct prio_sched_data
*q
= qdisc_priv(sch
);
212 for (prio
=0; prio
<q
->bands
; prio
++)
213 qdisc_reset(q
->queues
[prio
]);
218 prio_destroy(struct Qdisc
* sch
)
221 struct prio_sched_data
*q
= qdisc_priv(sch
);
223 tcf_destroy_chain(q
->filter_list
);
224 for (prio
=0; prio
<q
->bands
; prio
++)
225 qdisc_destroy(q
->queues
[prio
]);
228 static int prio_tune(struct Qdisc
*sch
, struct rtattr
*opt
)
230 struct prio_sched_data
*q
= qdisc_priv(sch
);
231 struct tc_prio_qopt
*qopt
;
232 struct rtattr
*tb
[TCA_PRIO_MAX
];
235 if (rtattr_parse_nested_compat(tb
, TCA_PRIO_MAX
, opt
, qopt
,
238 q
->bands
= qopt
->bands
;
239 /* If we're multiqueue, make sure the number of incoming bands
240 * matches the number of queues on the device we're associating with.
241 * If the number of bands requested is zero, then set q->bands to
242 * dev->egress_subqueue_count.
244 q
->mq
= RTA_GET_FLAG(tb
[TCA_PRIO_MQ
- 1]);
246 if (sch
->handle
!= TC_H_ROOT
)
248 if (netif_is_multiqueue(sch
->dev
)) {
250 q
->bands
= sch
->dev
->egress_subqueue_count
;
251 else if (q
->bands
!= sch
->dev
->egress_subqueue_count
)
257 if (q
->bands
> TCQ_PRIO_BANDS
|| q
->bands
< 2)
260 for (i
=0; i
<=TC_PRIO_MAX
; i
++) {
261 if (qopt
->priomap
[i
] >= q
->bands
)
266 memcpy(q
->prio2band
, qopt
->priomap
, TC_PRIO_MAX
+1);
268 for (i
=q
->bands
; i
<TCQ_PRIO_BANDS
; i
++) {
269 struct Qdisc
*child
= xchg(&q
->queues
[i
], &noop_qdisc
);
270 if (child
!= &noop_qdisc
) {
271 qdisc_tree_decrease_qlen(child
, child
->q
.qlen
);
272 qdisc_destroy(child
);
275 sch_tree_unlock(sch
);
277 for (i
=0; i
<q
->bands
; i
++) {
278 if (q
->queues
[i
] == &noop_qdisc
) {
280 child
= qdisc_create_dflt(sch
->dev
, &pfifo_qdisc_ops
,
281 TC_H_MAKE(sch
->handle
, i
+ 1));
284 child
= xchg(&q
->queues
[i
], child
);
286 if (child
!= &noop_qdisc
) {
287 qdisc_tree_decrease_qlen(child
,
289 qdisc_destroy(child
);
291 sch_tree_unlock(sch
);
298 static int prio_init(struct Qdisc
*sch
, struct rtattr
*opt
)
300 struct prio_sched_data
*q
= qdisc_priv(sch
);
303 for (i
=0; i
<TCQ_PRIO_BANDS
; i
++)
304 q
->queues
[i
] = &noop_qdisc
;
311 if ((err
= prio_tune(sch
, opt
)) != 0)
317 static int prio_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
319 struct prio_sched_data
*q
= qdisc_priv(sch
);
320 unsigned char *b
= skb_tail_pointer(skb
);
322 struct tc_prio_qopt opt
;
324 opt
.bands
= q
->bands
;
325 memcpy(&opt
.priomap
, q
->prio2band
, TC_PRIO_MAX
+1);
327 nest
= RTA_NEST_COMPAT(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
);
329 RTA_PUT_FLAG(skb
, TCA_PRIO_MQ
);
330 RTA_NEST_COMPAT_END(skb
, nest
);
339 static int prio_graft(struct Qdisc
*sch
, unsigned long arg
, struct Qdisc
*new,
342 struct prio_sched_data
*q
= qdisc_priv(sch
);
343 unsigned long band
= arg
- 1;
345 if (band
>= q
->bands
)
352 *old
= q
->queues
[band
];
353 q
->queues
[band
] = new;
354 qdisc_tree_decrease_qlen(*old
, (*old
)->q
.qlen
);
356 sch_tree_unlock(sch
);
361 static struct Qdisc
*
362 prio_leaf(struct Qdisc
*sch
, unsigned long arg
)
364 struct prio_sched_data
*q
= qdisc_priv(sch
);
365 unsigned long band
= arg
- 1;
367 if (band
>= q
->bands
)
370 return q
->queues
[band
];
373 static unsigned long prio_get(struct Qdisc
*sch
, u32 classid
)
375 struct prio_sched_data
*q
= qdisc_priv(sch
);
376 unsigned long band
= TC_H_MIN(classid
);
378 if (band
- 1 >= q
->bands
)
383 static unsigned long prio_bind(struct Qdisc
*sch
, unsigned long parent
, u32 classid
)
385 return prio_get(sch
, classid
);
389 static void prio_put(struct Qdisc
*q
, unsigned long cl
)
394 static int prio_change(struct Qdisc
*sch
, u32 handle
, u32 parent
, struct rtattr
**tca
, unsigned long *arg
)
396 unsigned long cl
= *arg
;
397 struct prio_sched_data
*q
= qdisc_priv(sch
);
399 if (cl
- 1 > q
->bands
)
404 static int prio_delete(struct Qdisc
*sch
, unsigned long cl
)
406 struct prio_sched_data
*q
= qdisc_priv(sch
);
407 if (cl
- 1 > q
->bands
)
413 static int prio_dump_class(struct Qdisc
*sch
, unsigned long cl
, struct sk_buff
*skb
,
416 struct prio_sched_data
*q
= qdisc_priv(sch
);
418 if (cl
- 1 > q
->bands
)
420 tcm
->tcm_handle
|= TC_H_MIN(cl
);
422 tcm
->tcm_info
= q
->queues
[cl
-1]->handle
;
426 static int prio_dump_class_stats(struct Qdisc
*sch
, unsigned long cl
,
429 struct prio_sched_data
*q
= qdisc_priv(sch
);
432 cl_q
= q
->queues
[cl
- 1];
433 if (gnet_stats_copy_basic(d
, &cl_q
->bstats
) < 0 ||
434 gnet_stats_copy_queue(d
, &cl_q
->qstats
) < 0)
440 static void prio_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
442 struct prio_sched_data
*q
= qdisc_priv(sch
);
448 for (prio
= 0; prio
< q
->bands
; prio
++) {
449 if (arg
->count
< arg
->skip
) {
453 if (arg
->fn(sch
, prio
+1, arg
) < 0) {
461 static struct tcf_proto
** prio_find_tcf(struct Qdisc
*sch
, unsigned long cl
)
463 struct prio_sched_data
*q
= qdisc_priv(sch
);
467 return &q
->filter_list
;
470 static struct Qdisc_class_ops prio_class_ops
= {
475 .change
= prio_change
,
476 .delete = prio_delete
,
478 .tcf_chain
= prio_find_tcf
,
479 .bind_tcf
= prio_bind
,
480 .unbind_tcf
= prio_put
,
481 .dump
= prio_dump_class
,
482 .dump_stats
= prio_dump_class_stats
,
485 static struct Qdisc_ops prio_qdisc_ops
= {
487 .cl_ops
= &prio_class_ops
,
489 .priv_size
= sizeof(struct prio_sched_data
),
490 .enqueue
= prio_enqueue
,
491 .dequeue
= prio_dequeue
,
492 .requeue
= prio_requeue
,
496 .destroy
= prio_destroy
,
499 .owner
= THIS_MODULE
,
502 static struct Qdisc_ops rr_qdisc_ops
= {
504 .cl_ops
= &prio_class_ops
,
506 .priv_size
= sizeof(struct prio_sched_data
),
507 .enqueue
= prio_enqueue
,
508 .dequeue
= rr_dequeue
,
509 .requeue
= prio_requeue
,
513 .destroy
= prio_destroy
,
516 .owner
= THIS_MODULE
,
519 static int __init
prio_module_init(void)
523 err
= register_qdisc(&prio_qdisc_ops
);
526 err
= register_qdisc(&rr_qdisc_ops
);
528 unregister_qdisc(&prio_qdisc_ops
);
532 static void __exit
prio_module_exit(void)
534 unregister_qdisc(&prio_qdisc_ops
);
535 unregister_qdisc(&rr_qdisc_ops
);
538 module_init(prio_module_init
)
539 module_exit(prio_module_exit
)
541 MODULE_LICENSE("GPL");
542 MODULE_ALIAS("sch_rr");