blob: dfb06d5bfacc37c8f4d6703113aa984a9f9c85fa [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>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070021#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/pkt_sched.h>
Jiri Pirkocf1facd2017-02-09 14:38:56 +010023#include <net/pkt_cls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Eric Dumazetcc7ec452011-01-19 19:26:56 +000025struct prio_sched_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int bands;
John Fastabend25d8c0d2014-09-12 20:05:27 -070027 struct tcf_proto __rcu *filter_list;
Jiri Pirko6529eab2017-05-17 11:07:55 +020028 struct tcf_block *block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 u8 prio2band[TC_PRIO_MAX+1];
30 struct Qdisc *queues[TCQ_PRIO_BANDS];
31};
32
33
34static struct Qdisc *
35prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
36{
37 struct prio_sched_data *q = qdisc_priv(sch);
38 u32 band = skb->priority;
39 struct tcf_result res;
John Fastabend25d8c0d2014-09-12 20:05:27 -070040 struct tcf_proto *fl;
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) {
John Fastabend25d8c0d2014-09-12 20:05:27 -070045 fl = rcu_dereference_bh(q->filter_list);
Jiri Pirko87d83092017-05-17 11:07:54 +020046 err = tcf_classify(skb, fl, &res, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#ifdef CONFIG_NET_CLS_ACT
Lucas Nussbaumdbaaa072007-08-30 22:35:46 -070048 switch (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 case TC_ACT_STOLEN:
50 case TC_ACT_QUEUED:
Jiri Pirkoe25ea212017-06-06 14:12:02 +020051 case TC_ACT_TRAP:
Jarek Poplawski378a2f02008-08-04 22:31:03 -070052 *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
Gustavo A. R. Silvaf3ae6082017-10-19 16:28:24 -050053 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 case TC_ACT_SHOT:
55 return NULL;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
John Fastabend25d8c0d2014-09-12 20:05:27 -070058 if (!fl || err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (TC_H_MAJ(band))
60 band = 0;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000061 return q->queues[q->prio2band[band & TC_PRIO_MAX]];
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63 band = res.classid;
64 }
65 band = TC_H_MIN(band) - 1;
Jamal Hadi Salim3e5c2d32007-05-14 02:57:19 -070066 if (band >= q->bands)
David S. Miller1d8ae3f2008-07-15 02:52:19 -070067 return q->queues[q->prio2band[0]];
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return q->queues[band];
70}
71
72static int
Eric Dumazet520ac302016-06-21 23:16:49 -070073prio_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Toke Høiland-Jørgensenf6bab192019-01-09 17:09:42 +010075 unsigned int len = qdisc_pkt_len(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct Qdisc *qdisc;
77 int ret;
78
79 qdisc = prio_classify(skb, sch, &ret);
80#ifdef CONFIG_NET_CLS_ACT
81 if (qdisc == NULL) {
Jamal Hadi Salim29f1df62006-01-08 22:35:55 -080082
Jarek Poplawskic27f3392008-08-04 22:39:11 -070083 if (ret & __NET_XMIT_BYPASS)
John Fastabend25331d62014-09-28 11:53:29 -070084 qdisc_qstats_drop(sch);
Gao Feng39ad1292017-09-04 14:21:12 +080085 __qdisc_drop(skb, to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return ret;
87 }
88#endif
89
Eric Dumazet520ac302016-06-21 23:16:49 -070090 ret = qdisc_enqueue(skb, qdisc, to_free);
Jussi Kivilinna5f861732008-07-20 00:08:04 -070091 if (ret == NET_XMIT_SUCCESS) {
Toke Høiland-Jørgensenf6bab192019-01-09 17:09:42 +010092 sch->qstats.backlog += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 sch->q.qlen++;
94 return NET_XMIT_SUCCESS;
95 }
Jarek Poplawski378a2f02008-08-04 22:31:03 -070096 if (net_xmit_drop_count(ret))
John Fastabend25331d62014-09-28 11:53:29 -070097 qdisc_qstats_drop(sch);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090098 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Patrick McHardy48a8f512008-10-31 00:44:18 -0700101static struct sk_buff *prio_peek(struct Qdisc *sch)
102{
103 struct prio_sched_data *q = qdisc_priv(sch);
104 int prio;
105
106 for (prio = 0; prio < q->bands; prio++) {
107 struct Qdisc *qdisc = q->queues[prio];
108 struct sk_buff *skb = qdisc->ops->peek(qdisc);
109 if (skb)
110 return skb;
111 }
112 return NULL;
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000115static struct sk_buff *prio_dequeue(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct prio_sched_data *q = qdisc_priv(sch);
118 int prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 for (prio = 0; prio < q->bands; prio++) {
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700121 struct Qdisc *qdisc = q->queues[prio];
Florian Westphal35576192011-08-09 02:04:43 +0000122 struct sk_buff *skb = qdisc_dequeue_peeked(qdisc);
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700123 if (skb) {
Eric Dumazet9190b3b2011-01-20 23:31:33 -0800124 qdisc_bstats_update(sch, skb);
WANG Cong6529d752016-06-01 16:15:16 -0700125 qdisc_qstats_backlog_dec(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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static void
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000135prio_reset(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 int prio;
138 struct prio_sched_data *q = qdisc_priv(sch);
139
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000140 for (prio = 0; prio < q->bands; prio++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 qdisc_reset(q->queues[prio]);
WANG Cong6529d752016-06-01 16:15:16 -0700142 sch->qstats.backlog = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 sch->q.qlen = 0;
144}
145
Nogah Frankel98ceb7b2018-02-28 10:45:05 +0100146static int prio_offload(struct Qdisc *sch, struct tc_prio_qopt *qopt)
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100147{
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100148 struct net_device *dev = qdisc_dev(sch);
149 struct tc_prio_qopt_offload opt = {
150 .handle = sch->handle,
151 .parent = sch->parent,
152 };
153
154 if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
155 return -EOPNOTSUPP;
156
Nogah Frankel98ceb7b2018-02-28 10:45:05 +0100157 if (qopt) {
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100158 opt.command = TC_PRIO_REPLACE;
Nogah Frankel98ceb7b2018-02-28 10:45:05 +0100159 opt.replace_params.bands = qopt->bands;
160 memcpy(&opt.replace_params.priomap, qopt->priomap,
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100161 TC_PRIO_MAX + 1);
162 opt.replace_params.qstats = &sch->qstats;
163 } else {
164 opt.command = TC_PRIO_DESTROY;
165 }
166
167 return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_PRIO, &opt);
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static void
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000171prio_destroy(struct Qdisc *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 int prio;
174 struct prio_sched_data *q = qdisc_priv(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jiri Pirko6529eab2017-05-17 11:07:55 +0200176 tcf_block_put(q->block);
Nogah Frankel98ceb7b2018-02-28 10:45:05 +0100177 prio_offload(sch, NULL);
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000178 for (prio = 0; prio < q->bands; prio++)
Vlad Buslov86bd4462018-09-24 19:22:50 +0300179 qdisc_put(q->queues[prio]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Alexander Aring20307212017-12-20 12:35:14 -0500182static int prio_tune(struct Qdisc *sch, struct nlattr *opt,
183 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct prio_sched_data *q = qdisc_priv(sch);
Eric Dumazet3d7c8252016-06-13 11:33:32 -0700186 struct Qdisc *queues[TCQ_PRIO_BANDS];
187 int oldbands = q->bands, i;
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700188 struct tc_prio_qopt *qopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700190 if (nla_len(opt) < sizeof(*qopt))
191 return -EINVAL;
192 qopt = nla_data(opt);
Patrick McHardycee63722008-01-23 20:33:32 -0800193
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700194 if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -EINVAL;
196
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000197 for (i = 0; i <= TC_PRIO_MAX; i++) {
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700198 if (qopt->priomap[i] >= qopt->bands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -EINVAL;
200 }
201
Eric Dumazet3d7c8252016-06-13 11:33:32 -0700202 /* Before commit, make sure we can allocate all new qdiscs */
203 for (i = oldbands; i < qopt->bands; i++) {
204 queues[i] = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
Alexander Aringa38a98822017-12-20 12:35:21 -0500205 TC_H_MAKE(sch->handle, i + 1),
206 extack);
Eric Dumazet3d7c8252016-06-13 11:33:32 -0700207 if (!queues[i]) {
208 while (i > oldbands)
Vlad Buslov86bd4462018-09-24 19:22:50 +0300209 qdisc_put(queues[--i]);
Eric Dumazet3d7c8252016-06-13 11:33:32 -0700210 return -ENOMEM;
211 }
212 }
213
Nogah Frankel98ceb7b2018-02-28 10:45:05 +0100214 prio_offload(sch, qopt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 sch_tree_lock(sch);
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700216 q->bands = qopt->bands;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
218
Eric Dumazet3d7c8252016-06-13 11:33:32 -0700219 for (i = q->bands; i < oldbands; i++) {
Patrick McHardyb94c8af2008-11-20 04:11:36 -0800220 struct Qdisc *child = q->queues[i];
Eric Dumazet3d7c8252016-06-13 11:33:32 -0700221
222 qdisc_tree_reduce_backlog(child, child->q.qlen,
223 child->qstats.backlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Eric Dumazet3d7c8252016-06-13 11:33:32 -0700225
Jiri Kosina49b49972017-03-08 16:03:32 +0100226 for (i = oldbands; i < q->bands; i++) {
Eric Dumazet3d7c8252016-06-13 11:33:32 -0700227 q->queues[i] = queues[i];
Jiri Kosina49b49972017-03-08 16:03:32 +0100228 if (q->queues[i] != &noop_qdisc)
229 qdisc_hash_add(q->queues[i], true);
230 }
Eric Dumazet3d7c8252016-06-13 11:33:32 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 sch_tree_unlock(sch);
Jakub Kicinski7b8e0b62018-11-07 17:33:40 -0800233
234 for (i = q->bands; i < oldbands; i++)
235 qdisc_put(q->queues[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return 0;
237}
238
Alexander Aringe63d7df2017-12-20 12:35:13 -0500239static int prio_init(struct Qdisc *sch, struct nlattr *opt,
240 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Jiri Pirko6529eab2017-05-17 11:07:55 +0200242 struct prio_sched_data *q = qdisc_priv(sch);
243 int err;
244
Eric Dumazet3d7c8252016-06-13 11:33:32 -0700245 if (!opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Alexander Aring8d1a77f2017-12-20 12:35:19 -0500248 err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
Jiri Pirko6529eab2017-05-17 11:07:55 +0200249 if (err)
250 return err;
251
Alexander Aring20307212017-12-20 12:35:14 -0500252 return prio_tune(sch, opt, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100255static int prio_dump_offload(struct Qdisc *sch)
256{
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100257 struct tc_prio_qopt_offload hw_stats = {
Andrew Mortonef58ca32018-01-18 16:30:49 -0800258 .command = TC_PRIO_STATS,
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100259 .handle = sch->handle,
260 .parent = sch->parent,
Andrew Mortonef58ca32018-01-18 16:30:49 -0800261 {
262 .stats = {
263 .bstats = &sch->bstats,
264 .qstats = &sch->qstats,
265 },
266 },
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100267 };
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100268
Jakub Kicinskib5928432018-11-07 17:33:34 -0800269 return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_PRIO, &hw_stats);
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
273{
274 struct prio_sched_data *q = qdisc_priv(sch);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700275 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct tc_prio_qopt opt;
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100277 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 opt.bands = q->bands;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000280 memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1);
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700281
Nogah Frankel7fdb61b2018-01-14 12:33:15 +0100282 err = prio_dump_offload(sch);
283 if (err)
284 goto nla_put_failure;
285
David S. Miller1b34ec42012-03-29 05:11:39 -0400286 if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
287 goto nla_put_failure;
Peter P Waskiewicz Jrd62733c2007-06-28 21:04:31 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return skb->len;
290
Patrick McHardy1e904742008-01-22 22:11:17 -0800291nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700292 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -1;
294}
295
296static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
Alexander Aring653d6fd2017-12-20 12:35:17 -0500297 struct Qdisc **old, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct prio_sched_data *q = qdisc_priv(sch);
Nogah Frankelb9c7a7a2018-02-28 10:45:06 +0100300 struct tc_prio_qopt_offload graft_offload;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned long band = arg - 1;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (new == NULL)
304 new = &noop_qdisc;
305
WANG Cong86a79962016-02-25 14:55:00 -0800306 *old = qdisc_replace(sch, new, &q->queues[band]);
Nogah Frankelb9c7a7a2018-02-28 10:45:06 +0100307
Nogah Frankelb9c7a7a2018-02-28 10:45:06 +0100308 graft_offload.handle = sch->handle;
309 graft_offload.parent = sch->parent;
310 graft_offload.graft_params.band = band;
311 graft_offload.graft_params.child_handle = new->handle;
312 graft_offload.command = TC_PRIO_GRAFT;
313
Jakub Kicinskibfaee912018-11-07 17:33:37 -0800314 qdisc_offload_graft_helper(qdisc_dev(sch), sch, new, *old,
315 TC_SETUP_QDISC_PRIO, &graft_offload,
316 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return 0;
318}
319
320static struct Qdisc *
321prio_leaf(struct Qdisc *sch, unsigned long arg)
322{
323 struct prio_sched_data *q = qdisc_priv(sch);
324 unsigned long band = arg - 1;
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return q->queues[band];
327}
328
WANG Cong143976c2017-08-24 16:51:29 -0700329static unsigned long prio_find(struct Qdisc *sch, u32 classid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 struct prio_sched_data *q = qdisc_priv(sch);
332 unsigned long band = TC_H_MIN(classid);
333
334 if (band - 1 >= q->bands)
335 return 0;
336 return band;
337}
338
339static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
340{
WANG Cong143976c2017-08-24 16:51:29 -0700341 return prio_find(sch, classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344
WANG Cong143976c2017-08-24 16:51:29 -0700345static void prio_unbind(struct Qdisc *q, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
350 struct tcmsg *tcm)
351{
352 struct prio_sched_data *q = qdisc_priv(sch);
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 tcm->tcm_handle |= TC_H_MIN(cl);
Patrick McHardy5b9a9cc2009-09-04 06:41:17 +0000355 tcm->tcm_info = q->queues[cl-1]->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357}
358
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800359static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
360 struct gnet_dump *d)
361{
362 struct prio_sched_data *q = qdisc_priv(sch);
363 struct Qdisc *cl_q;
364
365 cl_q = q->queues[cl - 1];
Eric Dumazetedb09eb2016-06-06 09:37:16 -0700366 if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
367 d, NULL, &cl_q->bstats) < 0 ||
Paolo Abeni5dd431b2019-03-28 16:53:12 +0100368 qdisc_qstats_copy(d, cl_q) < 0)
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800369 return -1;
370
371 return 0;
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
375{
376 struct prio_sched_data *q = qdisc_priv(sch);
377 int prio;
378
379 if (arg->stop)
380 return;
381
382 for (prio = 0; prio < q->bands; prio++) {
383 if (arg->count < arg->skip) {
384 arg->count++;
385 continue;
386 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000387 if (arg->fn(sch, prio + 1, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 arg->stop = 1;
389 break;
390 }
391 arg->count++;
392 }
393}
394
Alexander Aringcbaacc42017-12-20 12:35:16 -0500395static struct tcf_block *prio_tcf_block(struct Qdisc *sch, unsigned long cl,
396 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct prio_sched_data *q = qdisc_priv(sch);
399
400 if (cl)
401 return NULL;
Jiri Pirko6529eab2017-05-17 11:07:55 +0200402 return q->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Eric Dumazet20fea082007-11-14 01:44:41 -0800405static const struct Qdisc_class_ops prio_class_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 .graft = prio_graft,
407 .leaf = prio_leaf,
WANG Cong143976c2017-08-24 16:51:29 -0700408 .find = prio_find,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 .walk = prio_walk,
Jiri Pirko6529eab2017-05-17 11:07:55 +0200410 .tcf_block = prio_tcf_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 .bind_tcf = prio_bind,
WANG Cong143976c2017-08-24 16:51:29 -0700412 .unbind_tcf = prio_unbind,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 .dump = prio_dump_class,
Jarek Poplawski2cf6c362007-01-31 12:21:24 -0800414 .dump_stats = prio_dump_class_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415};
416
Eric Dumazet20fea082007-11-14 01:44:41 -0800417static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 .next = NULL,
419 .cl_ops = &prio_class_ops,
420 .id = "prio",
421 .priv_size = sizeof(struct prio_sched_data),
422 .enqueue = prio_enqueue,
423 .dequeue = prio_dequeue,
Patrick McHardy48a8f512008-10-31 00:44:18 -0700424 .peek = prio_peek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 .init = prio_init,
426 .reset = prio_reset,
427 .destroy = prio_destroy,
428 .change = prio_tune,
429 .dump = prio_dump,
430 .owner = THIS_MODULE,
431};
432
433static int __init prio_module_init(void)
434{
David S. Miller1d8ae3f2008-07-15 02:52:19 -0700435 return register_qdisc(&prio_qdisc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900438static void __exit prio_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 unregister_qdisc(&prio_qdisc_ops);
441}
442
443module_init(prio_module_init)
444module_exit(prio_module_exit)
445
446MODULE_LICENSE("GPL");