blob: 5e5ad91e02dd6a46cce4d535f46cedc2fc01bf54 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/sch_prio.c Simple 3-band priority "scheduler".
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090010 * Fixes: 19990609: J Hadi Salim <hadi@nortelnetworks.com>:
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Init -- EINVAL when opt undefined
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/types.h>
17#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/skbuff.h>
Tianyi Gou835e8e82012-06-26 10:11:05 -060021#include <linux/netdevice.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070022#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <net/pkt_sched.h>
24
25
Eric Dumazetcc7ec452011-01-19 19:26:56 +000026struct prio_sched_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 int bands;
28 struct tcf_proto *filter_list;
29 u8 prio2band[TC_PRIO_MAX+1];
30 struct Qdisc *queues[TCQ_PRIO_BANDS];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070031 u8 enable_flow;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
34
35static struct Qdisc *
36prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
37{
38 struct prio_sched_data *q = qdisc_priv(sch);
39 u32 band = skb->priority;
40 struct tcf_result res;
Patrick McHardybdba91e2007-07-30 17:07:14 -070041 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jarek Poplawskic27f3392008-08-04 22:39:11 -070043 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (TC_H_MAJ(skb->priority) != sch->handle) {
Patrick McHardybdba91e2007-07-30 17:07:14 -070045 err = tc_classify(skb, q->filter_list, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#ifdef CONFIG_NET_CLS_ACT
Lucas Nussbaumdbaaa072007-08-30 22:35:46 -070047 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 case TC_ACT_STOLEN:
49 case TC_ACT_QUEUED:
Jarek Poplawski378a2f02008-08-04 22:31:03 -070050 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 case TC_ACT_SHOT:
52 return NULL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#endif
Patrick McHardybdba91e2007-07-30 17:07:14 -070055 if (!q->filter_list || err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (TC_H_MAJ(band))
57 band = 0;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000058 return q->queues[q->prio2band[band & TC_PRIO_MAX]];
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60 band = res.classid;
61 }
62 band = TC_H_MIN(band) - 1;
Jamal Hadi Salim3e5c2d32007-05-14 02:57:19 -070063 if (band >= q->bands)
David S. Miller1d8ae3f2008-07-15 02:52:19 -070064 return q->queues[q->prio2band[0]];
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return q->queues[band];
67}
68
69static int
70prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
71{
72 struct Qdisc *qdisc;
73 int ret;
74
75 qdisc = prio_classify(skb, sch, &ret);
76#ifdef CONFIG_NET_CLS_ACT
77 if (qdisc == NULL) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -080078
Jarek Poplawskic27f3392008-08-04 22:39:11 -070079 if (ret & __NET_XMIT_BYPASS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 sch->qstats.drops++;
81 kfree_skb(skb);
82 return ret;
83 }
84#endif
85
Jussi Kivilinna5f861732008-07-20 00:08:04 -070086 ret = qdisc_enqueue(skb, qdisc);
87 if (ret == NET_XMIT_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 sch->q.qlen++;
89 return NET_XMIT_SUCCESS;
90 }
Jarek Poplawski378a2f02008-08-04 22:31:03 -070091 if (net_xmit_drop_count(ret))
92 sch->qstats.drops++;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090093 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Patrick McHardy48a8f512008-10-31 00:44:18 -070096static struct sk_buff *prio_peek(struct Qdisc *sch)
97{
98 struct prio_sched_data *q = qdisc_priv(sch);
99 int prio;
100
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101 if (!q->enable_flow)
102 return NULL;
103
Patrick McHardy48a8f512008-10-31 00:44:18 -0700104 for (prio = 0; prio < q->bands; prio++) {
105 struct Qdisc *qdisc = q->queues[prio];
106 struct sk_buff *skb = qdisc->ops->peek(qdisc);
107 if (skb)
108 return skb;
109 }
110 return NULL;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000113static struct sk_buff *prio_dequeue(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 struct prio_sched_data *q = qdisc_priv(sch);
116 int prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118 if (!q->enable_flow)
119 return NULL;
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 for (prio = 0; prio < q->bands; prio++) {
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700122 struct Qdisc *qdisc = q->queues[prio];
Florian Westphal621ad272011-08-09 02:04:43 +0000123 struct sk_buff *skb = qdisc_dequeue_peeked(qdisc);
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700124 if (skb) {
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800125 qdisc_bstats_update(sch, skb);
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700126 sch->q.qlen--;
127 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 }
130 return NULL;
131
132}
133
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000134static unsigned int prio_drop(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 struct prio_sched_data *q = qdisc_priv(sch);
137 int prio;
138 unsigned int len;
139 struct Qdisc *qdisc;
140
141 for (prio = q->bands-1; prio >= 0; prio--) {
142 qdisc = q->queues[prio];
Patrick McHardy6d037a22006-03-20 19:00:49 -0800143 if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 sch->q.qlen--;
145 return len;
146 }
147 }
148 return 0;
149}
150
151
152static void
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000153prio_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 int prio;
156 struct prio_sched_data *q = qdisc_priv(sch);
157
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000158 for (prio = 0; prio < q->bands; prio++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 qdisc_reset(q->queues[prio]);
160 sch->q.qlen = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700161 q->enable_flow = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164static void
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000165prio_destroy(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 int prio;
168 struct prio_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Patrick McHardyff31ab52008-07-01 19:52:38 -0700170 tcf_destroy_chain(&q->filter_list);
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000171 for (prio = 0; prio < q->bands; prio++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 qdisc_destroy(q->queues[prio]);
173}
174
Patrick McHardy1e904742008-01-22 22:11:17 -0800175static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct prio_sched_data *q = qdisc_priv(sch);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700178 struct tc_prio_qopt *qopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int i;
Tianyi Gou835e8e82012-06-26 10:11:05 -0600180 int flow_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700182 if (nla_len(opt) < sizeof(*qopt))
183 return -EINVAL;
184 qopt = nla_data(opt);
Patrick McHardycee63722008-01-23 20:33:32 -0800185
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700186 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
188
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000189 for (i = 0; i <= TC_PRIO_MAX; i++) {
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700190 if (qopt->priomap[i] >= qopt->bands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EINVAL;
192 }
193
194 sch_tree_lock(sch);
Tianyi Gou835e8e82012-06-26 10:11:05 -0600195 if (q->enable_flow != qopt->enable_flow) {
196 q->enable_flow = qopt->enable_flow;
197 flow_change = 1;
198 }
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700199 q->bands = qopt->bands;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
201
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000202 for (i = q->bands; i < TCQ_PRIO_BANDS; i++) {
Patrick McHardyb94c8af2008-11-20 04:11:36 -0800203 struct Qdisc *child = q->queues[i];
204 q->queues[i] = &noop_qdisc;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800205 if (child != &noop_qdisc) {
206 qdisc_tree_decrease_qlen(child, child->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 qdisc_destroy(child);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210 sch_tree_unlock(sch);
211
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000212 for (i = 0; i < q->bands; i++) {
Amnon Aaronsohndd914b42006-01-17 02:24:26 -0800213 if (q->queues[i] == &noop_qdisc) {
Patrick McHardyb94c8af2008-11-20 04:11:36 -0800214 struct Qdisc *child, *old;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000215
Changli Gao3511c912010-10-16 13:04:08 +0000216 child = qdisc_create_dflt(sch->dev_queue,
David S. Millerbb949fb2008-07-08 16:55:56 -0700217 &pfifo_qdisc_ops,
Patrick McHardy9f9afec2006-11-29 17:35:18 -0800218 TC_H_MAKE(sch->handle, i + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (child) {
220 sch_tree_lock(sch);
Patrick McHardyb94c8af2008-11-20 04:11:36 -0800221 old = q->queues[i];
222 q->queues[i] = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Patrick McHardyb94c8af2008-11-20 04:11:36 -0800224 if (old != &noop_qdisc) {
225 qdisc_tree_decrease_qlen(old,
226 old->q.qlen);
227 qdisc_destroy(old);
Patrick McHardy5e50da02006-11-29 17:36:20 -0800228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 sch_tree_unlock(sch);
230 }
231 }
232 }
Tianyi Gou835e8e82012-06-26 10:11:05 -0600233
234 /* Schedule qdisc when flow re-enabled */
235 if (flow_change && q->enable_flow) {
236 if (!test_bit(__QDISC_STATE_DEACTIVATED,
237 &sch->state))
238 __netif_schedule(qdisc_root(sch));
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 0;
241}
242
Patrick McHardy1e904742008-01-22 22:11:17 -0800243static int prio_init(struct Qdisc *sch, struct nlattr *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct prio_sched_data *q = qdisc_priv(sch);
246 int i;
247
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000248 for (i = 0; i < TCQ_PRIO_BANDS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 q->queues[i] = &noop_qdisc;
250
251 if (opt == NULL) {
252 return -EINVAL;
253 } else {
254 int err;
255
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000256 if ((err = prio_tune(sch, opt)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return err;
258 }
259 return 0;
260}
261
262static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
263{
264 struct prio_sched_data *q = qdisc_priv(sch);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700265 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct tc_prio_qopt opt;
267
268 opt.bands = q->bands;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269 opt.enable_flow = q->enable_flow;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000270 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700271
Thomas Graf2c10b322008-09-02 17:30:27 -0700272 NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return skb->len;
275
Patrick McHardy1e904742008-01-22 22:11:17 -0800276nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700277 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -1;
279}
280
281static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
282 struct Qdisc **old)
283{
284 struct prio_sched_data *q = qdisc_priv(sch);
285 unsigned long band = arg - 1;
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (new == NULL)
288 new = &noop_qdisc;
289
290 sch_tree_lock(sch);
291 *old = q->queues[band];
292 q->queues[band] = new;
Patrick McHardy5e50da02006-11-29 17:36:20 -0800293 qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 qdisc_reset(*old);
295 sch_tree_unlock(sch);
296
297 return 0;
298}
299
300static struct Qdisc *
301prio_leaf(struct Qdisc *sch, unsigned long arg)
302{
303 struct prio_sched_data *q = qdisc_priv(sch);
304 unsigned long band = arg - 1;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return q->queues[band];
307}
308
309static unsigned long prio_get(struct Qdisc *sch, u32 classid)
310{
311 struct prio_sched_data *q = qdisc_priv(sch);
312 unsigned long band = TC_H_MIN(classid);
313
314 if (band - 1 >= q->bands)
315 return 0;
316 return band;
317}
318
319static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
320{
321 return prio_get(sch, classid);
322}
323
324
325static void prio_put(struct Qdisc *q, unsigned long cl)
326{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
330 struct tcmsg *tcm)
331{
332 struct prio_sched_data *q = qdisc_priv(sch);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 tcm->tcm_handle |= TC_H_MIN(cl);
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +0000335 tcm->tcm_info = q->queues[cl-1]->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337}
338
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800339static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
340 struct gnet_dump *d)
341{
342 struct prio_sched_data *q = qdisc_priv(sch);
343 struct Qdisc *cl_q;
344
345 cl_q = q->queues[cl - 1];
Jarek Poplawskia19d2152009-09-17 10:26:07 -0700346 cl_q->qstats.qlen = cl_q->q.qlen;
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800347 if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
348 gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
349 return -1;
350
351 return 0;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
355{
356 struct prio_sched_data *q = qdisc_priv(sch);
357 int prio;
358
359 if (arg->stop)
360 return;
361
362 for (prio = 0; prio < q->bands; prio++) {
363 if (arg->count < arg->skip) {
364 arg->count++;
365 continue;
366 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000367 if (arg->fn(sch, prio + 1, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 arg->stop = 1;
369 break;
370 }
371 arg->count++;
372 }
373}
374
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000375static struct tcf_proto **prio_find_tcf(struct Qdisc *sch, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct prio_sched_data *q = qdisc_priv(sch);
378
379 if (cl)
380 return NULL;
381 return &q->filter_list;
382}
383
Eric Dumazet20fea082007-11-14 01:44:41 -0800384static const struct Qdisc_class_ops prio_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 .graft = prio_graft,
386 .leaf = prio_leaf,
387 .get = prio_get,
388 .put = prio_put,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 .walk = prio_walk,
390 .tcf_chain = prio_find_tcf,
391 .bind_tcf = prio_bind,
392 .unbind_tcf = prio_put,
393 .dump = prio_dump_class,
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800394 .dump_stats = prio_dump_class_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
Eric Dumazet20fea082007-11-14 01:44:41 -0800397static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 .next = NULL,
399 .cl_ops = &prio_class_ops,
400 .id = "prio",
401 .priv_size = sizeof(struct prio_sched_data),
402 .enqueue = prio_enqueue,
403 .dequeue = prio_dequeue,
Patrick McHardy48a8f512008-10-31 00:44:18 -0700404 .peek = prio_peek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 .drop = prio_drop,
406 .init = prio_init,
407 .reset = prio_reset,
408 .destroy = prio_destroy,
409 .change = prio_tune,
410 .dump = prio_dump,
411 .owner = THIS_MODULE,
412};
413
414static int __init prio_module_init(void)
415{
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700416 return register_qdisc(&prio_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900419static void __exit prio_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 unregister_qdisc(&prio_qdisc_ops);
422}
423
424module_init(prio_module_init)
425module_exit(prio_module_exit)
426
427MODULE_LICENSE("GPL");