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/slab.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/skbuff.h>
21 #include <linux/netdevice.h>
22 #include <net/netlink.h>
23 #include <net/pkt_sched.h>
26 struct prio_sched_data
{
28 struct tcf_proto __rcu
*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
;
44 *qerr
= NET_XMIT_SUCCESS
| __NET_XMIT_BYPASS
;
45 if (TC_H_MAJ(skb
->priority
) != sch
->handle
) {
46 fl
= rcu_dereference_bh(q
->filter_list
);
47 err
= tc_classify(skb
, fl
, &res
, false);
48 #ifdef CONFIG_NET_CLS_ACT
52 *qerr
= NET_XMIT_SUCCESS
| __NET_XMIT_STOLEN
;
60 return q
->queues
[q
->prio2band
[band
& TC_PRIO_MAX
]];
64 band
= TC_H_MIN(band
) - 1;
66 return q
->queues
[q
->prio2band
[0]];
68 return q
->queues
[band
];
72 prio_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
)
77 qdisc
= prio_classify(skb
, sch
, &ret
);
78 #ifdef CONFIG_NET_CLS_ACT
81 if (ret
& __NET_XMIT_BYPASS
)
82 qdisc_qstats_drop(sch
);
88 ret
= qdisc_enqueue(skb
, qdisc
);
89 if (ret
== NET_XMIT_SUCCESS
) {
91 return NET_XMIT_SUCCESS
;
93 if (net_xmit_drop_count(ret
))
94 qdisc_qstats_drop(sch
);
98 static struct sk_buff
*prio_peek(struct Qdisc
*sch
)
100 struct prio_sched_data
*q
= qdisc_priv(sch
);
104 for (prio
= 0; prio
< q
->bands
; prio
++) {
105 struct Qdisc
*qdisc
= q
->queues
[prio
];
106 struct sk_buff
*skb
= qdisc
->ops
->peek(qdisc
);
113 static struct sk_buff
*prio_dequeue(struct Qdisc
*sch
)
115 struct prio_sched_data
*q
= qdisc_priv(sch
);
120 for (prio
= 0; prio
< q
->bands
; prio
++) {
121 struct Qdisc
*qdisc
= q
->queues
[prio
];
122 struct sk_buff
*skb
= qdisc_dequeue_peeked(qdisc
);
124 qdisc_bstats_update(sch
, skb
);
133 static unsigned int prio_drop(struct Qdisc
*sch
)
135 struct prio_sched_data
*q
= qdisc_priv(sch
);
140 for (prio
= q
->bands
-1; prio
>= 0; prio
--) {
141 qdisc
= q
->queues
[prio
];
142 if (qdisc
->ops
->drop
&& (len
= qdisc
->ops
->drop(qdisc
)) != 0) {
152 prio_reset(struct Qdisc
*sch
)
155 struct prio_sched_data
*q
= qdisc_priv(sch
);
157 for (prio
= 0; prio
< q
->bands
; prio
++)
158 qdisc_reset(q
->queues
[prio
]);
164 prio_destroy(struct Qdisc
*sch
)
167 struct prio_sched_data
*q
= qdisc_priv(sch
);
169 tcf_destroy_chain(&q
->filter_list
);
170 for (prio
= 0; prio
< q
->bands
; prio
++)
171 qdisc_destroy(q
->queues
[prio
]);
174 static int prio_tune(struct Qdisc
*sch
, struct nlattr
*opt
)
176 struct prio_sched_data
*q
= qdisc_priv(sch
);
177 struct tc_prio_qopt
*qopt
;
181 if (nla_len(opt
) < sizeof(*qopt
))
183 qopt
= nla_data(opt
);
185 if (qopt
->bands
> TCQ_PRIO_BANDS
|| qopt
->bands
< 2)
188 for (i
= 0; i
<= TC_PRIO_MAX
; i
++) {
189 if (qopt
->priomap
[i
] >= qopt
->bands
)
194 if (q
->enable_flow
!= qopt
->enable_flow
) {
195 q
->enable_flow
= qopt
->enable_flow
;
198 q
->bands
= qopt
->bands
;
199 memcpy(q
->prio2band
, qopt
->priomap
, TC_PRIO_MAX
+1);
201 for (i
= q
->bands
; i
< TCQ_PRIO_BANDS
; i
++) {
202 struct Qdisc
*child
= q
->queues
[i
];
203 q
->queues
[i
] = &noop_qdisc
;
204 if (child
!= &noop_qdisc
) {
205 qdisc_tree_reduce_backlog(child
, child
->q
.qlen
, child
->qstats
.backlog
);
206 qdisc_destroy(child
);
209 sch_tree_unlock(sch
);
211 for (i
= 0; i
< q
->bands
; i
++) {
212 if (q
->queues
[i
] == &noop_qdisc
) {
213 struct Qdisc
*child
, *old
;
215 child
= qdisc_create_dflt(sch
->dev_queue
,
217 TC_H_MAKE(sch
->handle
, i
+ 1));
221 q
->queues
[i
] = child
;
223 if (old
!= &noop_qdisc
) {
224 qdisc_tree_reduce_backlog(old
,
226 old
->qstats
.backlog
);
229 sch_tree_unlock(sch
);
233 /* Schedule qdisc when flow re-enabled */
234 if (flow_change
&& q
->enable_flow
) {
235 if (!test_bit(__QDISC_STATE_DEACTIVATED
,
237 __netif_schedule(qdisc_root(sch
));
242 static int prio_init(struct Qdisc
*sch
, struct nlattr
*opt
)
244 struct prio_sched_data
*q
= qdisc_priv(sch
);
247 for (i
= 0; i
< TCQ_PRIO_BANDS
; i
++)
248 q
->queues
[i
] = &noop_qdisc
;
255 if ((err
= prio_tune(sch
, opt
)) != 0)
261 static int prio_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
263 struct prio_sched_data
*q
= qdisc_priv(sch
);
264 unsigned char *b
= skb_tail_pointer(skb
);
265 struct tc_prio_qopt opt
;
267 opt
.bands
= q
->bands
;
268 opt
.enable_flow
= q
->enable_flow
;
269 memcpy(&opt
.priomap
, q
->prio2band
, TC_PRIO_MAX
+ 1);
271 if (nla_put(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
))
272 goto nla_put_failure
;
281 static int prio_graft(struct Qdisc
*sch
, unsigned long arg
, struct Qdisc
*new,
284 struct prio_sched_data
*q
= qdisc_priv(sch
);
285 unsigned long band
= arg
- 1;
290 *old
= qdisc_replace(sch
, new, &q
->queues
[band
]);
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 return q
->queues
[band
];
303 static unsigned long prio_get(struct Qdisc
*sch
, u32 classid
)
305 struct prio_sched_data
*q
= qdisc_priv(sch
);
306 unsigned long band
= TC_H_MIN(classid
);
308 if (band
- 1 >= q
->bands
)
313 static unsigned long prio_bind(struct Qdisc
*sch
, unsigned long parent
, u32 classid
)
315 return prio_get(sch
, classid
);
319 static void prio_put(struct Qdisc
*q
, unsigned long cl
)
323 static int prio_dump_class(struct Qdisc
*sch
, unsigned long cl
, struct sk_buff
*skb
,
326 struct prio_sched_data
*q
= qdisc_priv(sch
);
328 tcm
->tcm_handle
|= TC_H_MIN(cl
);
329 tcm
->tcm_info
= q
->queues
[cl
-1]->handle
;
333 static int prio_dump_class_stats(struct Qdisc
*sch
, unsigned long cl
,
336 struct prio_sched_data
*q
= qdisc_priv(sch
);
339 cl_q
= q
->queues
[cl
- 1];
340 if (gnet_stats_copy_basic(d
, NULL
, &cl_q
->bstats
) < 0 ||
341 gnet_stats_copy_queue(d
, NULL
, &cl_q
->qstats
, cl_q
->q
.qlen
) < 0)
347 static void prio_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
349 struct prio_sched_data
*q
= qdisc_priv(sch
);
355 for (prio
= 0; prio
< q
->bands
; prio
++) {
356 if (arg
->count
< arg
->skip
) {
360 if (arg
->fn(sch
, prio
+ 1, arg
) < 0) {
368 static struct tcf_proto __rcu
**prio_find_tcf(struct Qdisc
*sch
,
371 struct prio_sched_data
*q
= qdisc_priv(sch
);
375 return &q
->filter_list
;
378 static const struct Qdisc_class_ops prio_class_ops
= {
384 .tcf_chain
= prio_find_tcf
,
385 .bind_tcf
= prio_bind
,
386 .unbind_tcf
= prio_put
,
387 .dump
= prio_dump_class
,
388 .dump_stats
= prio_dump_class_stats
,
391 static struct Qdisc_ops prio_qdisc_ops __read_mostly
= {
393 .cl_ops
= &prio_class_ops
,
395 .priv_size
= sizeof(struct prio_sched_data
),
396 .enqueue
= prio_enqueue
,
397 .dequeue
= prio_dequeue
,
402 .destroy
= prio_destroy
,
405 .owner
= THIS_MODULE
,
408 static int __init
prio_module_init(void)
410 return register_qdisc(&prio_qdisc_ops
);
413 static void __exit
prio_module_exit(void)
415 unregister_qdisc(&prio_qdisc_ops
);
418 module_init(prio_module_init
)
419 module_exit(prio_module_exit
)
421 MODULE_LICENSE("GPL");