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 <net/netlink.h>
22 #include <net/pkt_sched.h>
25 struct prio_sched_data
{
27 struct tcf_proto __rcu
*filter_list
;
28 u8 prio2band
[TC_PRIO_MAX
+1];
29 struct Qdisc
*queues
[TCQ_PRIO_BANDS
];
34 prio_classify(struct sk_buff
*skb
, struct Qdisc
*sch
, int *qerr
)
36 struct prio_sched_data
*q
= qdisc_priv(sch
);
37 u32 band
= skb
->priority
;
38 struct tcf_result res
;
42 *qerr
= NET_XMIT_SUCCESS
| __NET_XMIT_BYPASS
;
43 if (TC_H_MAJ(skb
->priority
) != sch
->handle
) {
44 fl
= rcu_dereference_bh(q
->filter_list
);
45 err
= tc_classify(skb
, fl
, &res
, false);
46 #ifdef CONFIG_NET_CLS_ACT
50 *qerr
= NET_XMIT_SUCCESS
| __NET_XMIT_STOLEN
;
58 return q
->queues
[q
->prio2band
[band
& TC_PRIO_MAX
]];
62 band
= TC_H_MIN(band
) - 1;
64 return q
->queues
[q
->prio2band
[0]];
66 return q
->queues
[band
];
70 prio_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
)
75 qdisc
= prio_classify(skb
, sch
, &ret
);
76 #ifdef CONFIG_NET_CLS_ACT
79 if (ret
& __NET_XMIT_BYPASS
)
80 qdisc_qstats_drop(sch
);
86 ret
= qdisc_enqueue(skb
, qdisc
);
87 if (ret
== NET_XMIT_SUCCESS
) {
88 qdisc_qstats_backlog_inc(sch
, skb
);
90 return NET_XMIT_SUCCESS
;
92 if (net_xmit_drop_count(ret
))
93 qdisc_qstats_drop(sch
);
97 static struct sk_buff
*prio_peek(struct Qdisc
*sch
)
99 struct prio_sched_data
*q
= qdisc_priv(sch
);
102 for (prio
= 0; prio
< q
->bands
; prio
++) {
103 struct Qdisc
*qdisc
= q
->queues
[prio
];
104 struct sk_buff
*skb
= qdisc
->ops
->peek(qdisc
);
111 static struct sk_buff
*prio_dequeue(struct Qdisc
*sch
)
113 struct prio_sched_data
*q
= qdisc_priv(sch
);
116 for (prio
= 0; prio
< q
->bands
; prio
++) {
117 struct Qdisc
*qdisc
= q
->queues
[prio
];
118 struct sk_buff
*skb
= qdisc_dequeue_peeked(qdisc
);
120 qdisc_bstats_update(sch
, skb
);
121 qdisc_qstats_backlog_dec(sch
, skb
);
131 prio_reset(struct Qdisc
*sch
)
134 struct prio_sched_data
*q
= qdisc_priv(sch
);
136 for (prio
= 0; prio
< q
->bands
; prio
++)
137 qdisc_reset(q
->queues
[prio
]);
138 sch
->qstats
.backlog
= 0;
143 prio_destroy(struct Qdisc
*sch
)
146 struct prio_sched_data
*q
= qdisc_priv(sch
);
148 tcf_destroy_chain(&q
->filter_list
);
149 for (prio
= 0; prio
< q
->bands
; prio
++)
150 qdisc_destroy(q
->queues
[prio
]);
153 static int prio_tune(struct Qdisc
*sch
, struct nlattr
*opt
)
155 struct prio_sched_data
*q
= qdisc_priv(sch
);
156 struct tc_prio_qopt
*qopt
;
159 if (nla_len(opt
) < sizeof(*qopt
))
161 qopt
= nla_data(opt
);
163 if (qopt
->bands
> TCQ_PRIO_BANDS
|| qopt
->bands
< 2)
166 for (i
= 0; i
<= TC_PRIO_MAX
; i
++) {
167 if (qopt
->priomap
[i
] >= qopt
->bands
)
172 q
->bands
= qopt
->bands
;
173 memcpy(q
->prio2band
, qopt
->priomap
, TC_PRIO_MAX
+1);
175 for (i
= q
->bands
; i
< TCQ_PRIO_BANDS
; i
++) {
176 struct Qdisc
*child
= q
->queues
[i
];
177 q
->queues
[i
] = &noop_qdisc
;
178 if (child
!= &noop_qdisc
) {
179 qdisc_tree_reduce_backlog(child
, child
->q
.qlen
, child
->qstats
.backlog
);
180 qdisc_destroy(child
);
183 sch_tree_unlock(sch
);
185 for (i
= 0; i
< q
->bands
; i
++) {
186 if (q
->queues
[i
] == &noop_qdisc
) {
187 struct Qdisc
*child
, *old
;
189 child
= qdisc_create_dflt(sch
->dev_queue
,
191 TC_H_MAKE(sch
->handle
, i
+ 1));
195 q
->queues
[i
] = child
;
197 if (old
!= &noop_qdisc
) {
198 qdisc_tree_reduce_backlog(old
,
200 old
->qstats
.backlog
);
203 sch_tree_unlock(sch
);
210 static int prio_init(struct Qdisc
*sch
, struct nlattr
*opt
)
212 struct prio_sched_data
*q
= qdisc_priv(sch
);
215 for (i
= 0; i
< TCQ_PRIO_BANDS
; i
++)
216 q
->queues
[i
] = &noop_qdisc
;
223 if ((err
= prio_tune(sch
, opt
)) != 0)
229 static int prio_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
231 struct prio_sched_data
*q
= qdisc_priv(sch
);
232 unsigned char *b
= skb_tail_pointer(skb
);
233 struct tc_prio_qopt opt
;
235 opt
.bands
= q
->bands
;
236 memcpy(&opt
.priomap
, q
->prio2band
, TC_PRIO_MAX
+ 1);
238 if (nla_put(skb
, TCA_OPTIONS
, sizeof(opt
), &opt
))
239 goto nla_put_failure
;
248 static int prio_graft(struct Qdisc
*sch
, unsigned long arg
, struct Qdisc
*new,
251 struct prio_sched_data
*q
= qdisc_priv(sch
);
252 unsigned long band
= arg
- 1;
257 *old
= qdisc_replace(sch
, new, &q
->queues
[band
]);
261 static struct Qdisc
*
262 prio_leaf(struct Qdisc
*sch
, unsigned long arg
)
264 struct prio_sched_data
*q
= qdisc_priv(sch
);
265 unsigned long band
= arg
- 1;
267 return q
->queues
[band
];
270 static unsigned long prio_get(struct Qdisc
*sch
, u32 classid
)
272 struct prio_sched_data
*q
= qdisc_priv(sch
);
273 unsigned long band
= TC_H_MIN(classid
);
275 if (band
- 1 >= q
->bands
)
280 static unsigned long prio_bind(struct Qdisc
*sch
, unsigned long parent
, u32 classid
)
282 return prio_get(sch
, classid
);
286 static void prio_put(struct Qdisc
*q
, unsigned long cl
)
290 static int prio_dump_class(struct Qdisc
*sch
, unsigned long cl
, struct sk_buff
*skb
,
293 struct prio_sched_data
*q
= qdisc_priv(sch
);
295 tcm
->tcm_handle
|= TC_H_MIN(cl
);
296 tcm
->tcm_info
= q
->queues
[cl
-1]->handle
;
300 static int prio_dump_class_stats(struct Qdisc
*sch
, unsigned long cl
,
303 struct prio_sched_data
*q
= qdisc_priv(sch
);
306 cl_q
= q
->queues
[cl
- 1];
307 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch
),
308 d
, NULL
, &cl_q
->bstats
) < 0 ||
309 gnet_stats_copy_queue(d
, NULL
, &cl_q
->qstats
, cl_q
->q
.qlen
) < 0)
315 static void prio_walk(struct Qdisc
*sch
, struct qdisc_walker
*arg
)
317 struct prio_sched_data
*q
= qdisc_priv(sch
);
323 for (prio
= 0; prio
< q
->bands
; prio
++) {
324 if (arg
->count
< arg
->skip
) {
328 if (arg
->fn(sch
, prio
+ 1, arg
) < 0) {
336 static struct tcf_proto __rcu
**prio_find_tcf(struct Qdisc
*sch
,
339 struct prio_sched_data
*q
= qdisc_priv(sch
);
343 return &q
->filter_list
;
346 static const struct Qdisc_class_ops prio_class_ops
= {
352 .tcf_chain
= prio_find_tcf
,
353 .bind_tcf
= prio_bind
,
354 .unbind_tcf
= prio_put
,
355 .dump
= prio_dump_class
,
356 .dump_stats
= prio_dump_class_stats
,
359 static struct Qdisc_ops prio_qdisc_ops __read_mostly
= {
361 .cl_ops
= &prio_class_ops
,
363 .priv_size
= sizeof(struct prio_sched_data
),
364 .enqueue
= prio_enqueue
,
365 .dequeue
= prio_dequeue
,
369 .destroy
= prio_destroy
,
372 .owner
= THIS_MODULE
,
375 static int __init
prio_module_init(void)
377 return register_qdisc(&prio_qdisc_ops
);
380 static void __exit
prio_module_exit(void)
382 unregister_qdisc(&prio_qdisc_ops
);
385 module_init(prio_module_init
)
386 module_exit(prio_module_exit
)
388 MODULE_LICENSE("GPL");