xref: /linux/net/sched/sch_prio.c (revision 7b8e0b6e659983154c8d7e756cdb833d89a3d4d7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * net/sched/sch_prio.c	Simple 3-band priority "scheduler".
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
51da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
61da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
71da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
101da177e4SLinus Torvalds  * Fixes:       19990609: J Hadi Salim <hadi@nortelnetworks.com>:
111da177e4SLinus Torvalds  *              Init --  EINVAL when opt undefined
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/module.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
161da177e4SLinus Torvalds #include <linux/types.h>
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/string.h>
191da177e4SLinus Torvalds #include <linux/errno.h>
201da177e4SLinus Torvalds #include <linux/skbuff.h>
21dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
221da177e4SLinus Torvalds #include <net/pkt_sched.h>
23cf1facdaSJiri Pirko #include <net/pkt_cls.h>
241da177e4SLinus Torvalds 
25cc7ec456SEric Dumazet struct prio_sched_data {
261da177e4SLinus Torvalds 	int bands;
2725d8c0d5SJohn Fastabend 	struct tcf_proto __rcu *filter_list;
286529eabaSJiri Pirko 	struct tcf_block *block;
291da177e4SLinus Torvalds 	u8  prio2band[TC_PRIO_MAX+1];
301da177e4SLinus Torvalds 	struct Qdisc *queues[TCQ_PRIO_BANDS];
311da177e4SLinus Torvalds };
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds static struct Qdisc *
351da177e4SLinus Torvalds prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
361da177e4SLinus Torvalds {
371da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
381da177e4SLinus Torvalds 	u32 band = skb->priority;
391da177e4SLinus Torvalds 	struct tcf_result res;
4025d8c0d5SJohn Fastabend 	struct tcf_proto *fl;
41bdba91ecSPatrick McHardy 	int err;
421da177e4SLinus Torvalds 
43c27f339aSJarek Poplawski 	*qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
441da177e4SLinus Torvalds 	if (TC_H_MAJ(skb->priority) != sch->handle) {
4525d8c0d5SJohn Fastabend 		fl = rcu_dereference_bh(q->filter_list);
4687d83093SJiri Pirko 		err = tcf_classify(skb, fl, &res, false);
471da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
48dbaaa07aSLucas Nussbaum 		switch (err) {
491da177e4SLinus Torvalds 		case TC_ACT_STOLEN:
501da177e4SLinus Torvalds 		case TC_ACT_QUEUED:
51e25ea21fSJiri Pirko 		case TC_ACT_TRAP:
52378a2f09SJarek Poplawski 			*qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
53f3ae608eSGustavo A. R. Silva 			/* fall through */
541da177e4SLinus Torvalds 		case TC_ACT_SHOT:
551da177e4SLinus Torvalds 			return NULL;
563ff50b79SStephen Hemminger 		}
571da177e4SLinus Torvalds #endif
5825d8c0d5SJohn Fastabend 		if (!fl || err < 0) {
591da177e4SLinus Torvalds 			if (TC_H_MAJ(band))
601da177e4SLinus Torvalds 				band = 0;
611d8ae3fdSDavid S. Miller 			return q->queues[q->prio2band[band & TC_PRIO_MAX]];
621da177e4SLinus Torvalds 		}
631da177e4SLinus Torvalds 		band = res.classid;
641da177e4SLinus Torvalds 	}
651da177e4SLinus Torvalds 	band = TC_H_MIN(band) - 1;
663e5c2d3bSJamal Hadi Salim 	if (band >= q->bands)
671d8ae3fdSDavid S. Miller 		return q->queues[q->prio2band[0]];
681d8ae3fdSDavid S. Miller 
691da177e4SLinus Torvalds 	return q->queues[band];
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static int
73520ac30fSEric Dumazet prio_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	struct Qdisc *qdisc;
761da177e4SLinus Torvalds 	int ret;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	qdisc = prio_classify(skb, sch, &ret);
791da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
801da177e4SLinus Torvalds 	if (qdisc == NULL) {
8129f1df6cSJamal Hadi Salim 
82c27f339aSJarek Poplawski 		if (ret & __NET_XMIT_BYPASS)
8325331d6cSJohn Fastabend 			qdisc_qstats_drop(sch);
8439ad1297SGao Feng 		__qdisc_drop(skb, to_free);
851da177e4SLinus Torvalds 		return ret;
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds #endif
881da177e4SLinus Torvalds 
89520ac30fSEric Dumazet 	ret = qdisc_enqueue(skb, qdisc, to_free);
905f86173bSJussi Kivilinna 	if (ret == NET_XMIT_SUCCESS) {
916529d75aSWANG Cong 		qdisc_qstats_backlog_inc(sch, skb);
921da177e4SLinus Torvalds 		sch->q.qlen++;
931da177e4SLinus Torvalds 		return NET_XMIT_SUCCESS;
941da177e4SLinus Torvalds 	}
95378a2f09SJarek Poplawski 	if (net_xmit_drop_count(ret))
9625331d6cSJohn Fastabend 		qdisc_qstats_drop(sch);
971da177e4SLinus Torvalds 	return ret;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
10048a8f519SPatrick McHardy static struct sk_buff *prio_peek(struct Qdisc *sch)
10148a8f519SPatrick McHardy {
10248a8f519SPatrick McHardy 	struct prio_sched_data *q = qdisc_priv(sch);
10348a8f519SPatrick McHardy 	int prio;
10448a8f519SPatrick McHardy 
10548a8f519SPatrick McHardy 	for (prio = 0; prio < q->bands; prio++) {
10648a8f519SPatrick McHardy 		struct Qdisc *qdisc = q->queues[prio];
10748a8f519SPatrick McHardy 		struct sk_buff *skb = qdisc->ops->peek(qdisc);
10848a8f519SPatrick McHardy 		if (skb)
10948a8f519SPatrick McHardy 			return skb;
11048a8f519SPatrick McHardy 	}
11148a8f519SPatrick McHardy 	return NULL;
11248a8f519SPatrick McHardy }
1131da177e4SLinus Torvalds 
1141d8ae3fdSDavid S. Miller static struct sk_buff *prio_dequeue(struct Qdisc *sch)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
1171da177e4SLinus Torvalds 	int prio;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	for (prio = 0; prio < q->bands; prio++) {
1201d8ae3fdSDavid S. Miller 		struct Qdisc *qdisc = q->queues[prio];
1213557619fSFlorian Westphal 		struct sk_buff *skb = qdisc_dequeue_peeked(qdisc);
1221da177e4SLinus Torvalds 		if (skb) {
1239190b3b3SEric Dumazet 			qdisc_bstats_update(sch, skb);
1246529d75aSWANG Cong 			qdisc_qstats_backlog_dec(sch, skb);
1251da177e4SLinus Torvalds 			sch->q.qlen--;
1261da177e4SLinus Torvalds 			return skb;
1271da177e4SLinus Torvalds 		}
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 	return NULL;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static void
1341da177e4SLinus Torvalds prio_reset(struct Qdisc *sch)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	int prio;
1371da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	for (prio = 0; prio < q->bands; prio++)
1401da177e4SLinus Torvalds 		qdisc_reset(q->queues[prio]);
1416529d75aSWANG Cong 	sch->qstats.backlog = 0;
1421da177e4SLinus Torvalds 	sch->q.qlen = 0;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
14598ceb7b6SNogah Frankel static int prio_offload(struct Qdisc *sch, struct tc_prio_qopt *qopt)
1467fdb61b4SNogah Frankel {
1477fdb61b4SNogah Frankel 	struct net_device *dev = qdisc_dev(sch);
1487fdb61b4SNogah Frankel 	struct tc_prio_qopt_offload opt = {
1497fdb61b4SNogah Frankel 		.handle = sch->handle,
1507fdb61b4SNogah Frankel 		.parent = sch->parent,
1517fdb61b4SNogah Frankel 	};
1527fdb61b4SNogah Frankel 
1537fdb61b4SNogah Frankel 	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
1547fdb61b4SNogah Frankel 		return -EOPNOTSUPP;
1557fdb61b4SNogah Frankel 
15698ceb7b6SNogah Frankel 	if (qopt) {
1577fdb61b4SNogah Frankel 		opt.command = TC_PRIO_REPLACE;
15898ceb7b6SNogah Frankel 		opt.replace_params.bands = qopt->bands;
15998ceb7b6SNogah Frankel 		memcpy(&opt.replace_params.priomap, qopt->priomap,
1607fdb61b4SNogah Frankel 		       TC_PRIO_MAX + 1);
1617fdb61b4SNogah Frankel 		opt.replace_params.qstats = &sch->qstats;
1627fdb61b4SNogah Frankel 	} else {
1637fdb61b4SNogah Frankel 		opt.command = TC_PRIO_DESTROY;
1647fdb61b4SNogah Frankel 	}
1657fdb61b4SNogah Frankel 
1667fdb61b4SNogah Frankel 	return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_PRIO, &opt);
1677fdb61b4SNogah Frankel }
1687fdb61b4SNogah Frankel 
1691da177e4SLinus Torvalds static void
1701da177e4SLinus Torvalds prio_destroy(struct Qdisc *sch)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	int prio;
1731da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
1741da177e4SLinus Torvalds 
1756529eabaSJiri Pirko 	tcf_block_put(q->block);
17698ceb7b6SNogah Frankel 	prio_offload(sch, NULL);
1771da177e4SLinus Torvalds 	for (prio = 0; prio < q->bands; prio++)
17886bd446bSVlad Buslov 		qdisc_put(q->queues[prio]);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1812030721cSAlexander Aring static int prio_tune(struct Qdisc *sch, struct nlattr *opt,
1822030721cSAlexander Aring 		     struct netlink_ext_ack *extack)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
1853d7c8257SEric Dumazet 	struct Qdisc *queues[TCQ_PRIO_BANDS];
1863d7c8257SEric Dumazet 	int oldbands = q->bands, i;
187d62733c8SPeter P Waskiewicz Jr 	struct tc_prio_qopt *qopt;
1881da177e4SLinus Torvalds 
1891d8ae3fdSDavid S. Miller 	if (nla_len(opt) < sizeof(*qopt))
190d62733c8SPeter P Waskiewicz Jr 		return -EINVAL;
1911d8ae3fdSDavid S. Miller 	qopt = nla_data(opt);
192d62733c8SPeter P Waskiewicz Jr 
1931d8ae3fdSDavid S. Miller 	if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
1941da177e4SLinus Torvalds 		return -EINVAL;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	for (i = 0; i <= TC_PRIO_MAX; i++) {
1971d8ae3fdSDavid S. Miller 		if (qopt->priomap[i] >= qopt->bands)
1981da177e4SLinus Torvalds 			return -EINVAL;
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds 
2013d7c8257SEric Dumazet 	/* Before commit, make sure we can allocate all new qdiscs */
2023d7c8257SEric Dumazet 	for (i = oldbands; i < qopt->bands; i++) {
2033d7c8257SEric Dumazet 		queues[i] = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
204a38a9882SAlexander Aring 					      TC_H_MAKE(sch->handle, i + 1),
205a38a9882SAlexander Aring 					      extack);
2063d7c8257SEric Dumazet 		if (!queues[i]) {
2073d7c8257SEric Dumazet 			while (i > oldbands)
20886bd446bSVlad Buslov 				qdisc_put(queues[--i]);
2093d7c8257SEric Dumazet 			return -ENOMEM;
2103d7c8257SEric Dumazet 		}
2113d7c8257SEric Dumazet 	}
2123d7c8257SEric Dumazet 
21398ceb7b6SNogah Frankel 	prio_offload(sch, qopt);
2141da177e4SLinus Torvalds 	sch_tree_lock(sch);
2151d8ae3fdSDavid S. Miller 	q->bands = qopt->bands;
2161da177e4SLinus Torvalds 	memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
2171da177e4SLinus Torvalds 
2183d7c8257SEric Dumazet 	for (i = q->bands; i < oldbands; i++) {
219b94c8afcSPatrick McHardy 		struct Qdisc *child = q->queues[i];
2203d7c8257SEric Dumazet 
2213d7c8257SEric Dumazet 		qdisc_tree_reduce_backlog(child, child->q.qlen,
2223d7c8257SEric Dumazet 					  child->qstats.backlog);
2231da177e4SLinus Torvalds 	}
2243d7c8257SEric Dumazet 
22549b49971SJiri Kosina 	for (i = oldbands; i < q->bands; i++) {
2263d7c8257SEric Dumazet 		q->queues[i] = queues[i];
22749b49971SJiri Kosina 		if (q->queues[i] != &noop_qdisc)
22849b49971SJiri Kosina 			qdisc_hash_add(q->queues[i], true);
22949b49971SJiri Kosina 	}
2303d7c8257SEric Dumazet 
2311da177e4SLinus Torvalds 	sch_tree_unlock(sch);
232*7b8e0b6eSJakub Kicinski 
233*7b8e0b6eSJakub Kicinski 	for (i = q->bands; i < oldbands; i++)
234*7b8e0b6eSJakub Kicinski 		qdisc_put(q->queues[i]);
2351da177e4SLinus Torvalds 	return 0;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
238e63d7dfdSAlexander Aring static int prio_init(struct Qdisc *sch, struct nlattr *opt,
239e63d7dfdSAlexander Aring 		     struct netlink_ext_ack *extack)
2401da177e4SLinus Torvalds {
2416529eabaSJiri Pirko 	struct prio_sched_data *q = qdisc_priv(sch);
2426529eabaSJiri Pirko 	int err;
2436529eabaSJiri Pirko 
2443d7c8257SEric Dumazet 	if (!opt)
2451da177e4SLinus Torvalds 		return -EINVAL;
2461da177e4SLinus Torvalds 
2478d1a77f9SAlexander Aring 	err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
2486529eabaSJiri Pirko 	if (err)
2496529eabaSJiri Pirko 		return err;
2506529eabaSJiri Pirko 
2512030721cSAlexander Aring 	return prio_tune(sch, opt, extack);
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds 
2547fdb61b4SNogah Frankel static int prio_dump_offload(struct Qdisc *sch)
2557fdb61b4SNogah Frankel {
2567fdb61b4SNogah Frankel 	struct tc_prio_qopt_offload hw_stats = {
257ef58ca38SAndrew Morton 		.command = TC_PRIO_STATS,
2587fdb61b4SNogah Frankel 		.handle = sch->handle,
2597fdb61b4SNogah Frankel 		.parent = sch->parent,
260ef58ca38SAndrew Morton 		{
261ef58ca38SAndrew Morton 			.stats = {
262ef58ca38SAndrew Morton 				.bstats = &sch->bstats,
263ef58ca38SAndrew Morton 				.qstats = &sch->qstats,
264ef58ca38SAndrew Morton 			},
265ef58ca38SAndrew Morton 		},
2667fdb61b4SNogah Frankel 	};
2677fdb61b4SNogah Frankel 
268b592843cSJakub Kicinski 	return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_PRIO, &hw_stats);
2697fdb61b4SNogah Frankel }
2707fdb61b4SNogah Frankel 
2711da177e4SLinus Torvalds static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
27427a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
2751da177e4SLinus Torvalds 	struct tc_prio_qopt opt;
2767fdb61b4SNogah Frankel 	int err;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	opt.bands = q->bands;
2791da177e4SLinus Torvalds 	memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1);
280d62733c8SPeter P Waskiewicz Jr 
2817fdb61b4SNogah Frankel 	err = prio_dump_offload(sch);
2827fdb61b4SNogah Frankel 	if (err)
2837fdb61b4SNogah Frankel 		goto nla_put_failure;
2847fdb61b4SNogah Frankel 
2851b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
2861b34ec43SDavid S. Miller 		goto nla_put_failure;
287d62733c8SPeter P Waskiewicz Jr 
2881da177e4SLinus Torvalds 	return skb->len;
2891da177e4SLinus Torvalds 
2901e90474cSPatrick McHardy nla_put_failure:
291dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
2921da177e4SLinus Torvalds 	return -1;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
296653d6fd6SAlexander Aring 		      struct Qdisc **old, struct netlink_ext_ack *extack)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
299b9c7a7acSNogah Frankel 	struct tc_prio_qopt_offload graft_offload;
3001da177e4SLinus Torvalds 	unsigned long band = arg - 1;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	if (new == NULL)
3031da177e4SLinus Torvalds 		new = &noop_qdisc;
3041da177e4SLinus Torvalds 
30586a7996cSWANG Cong 	*old = qdisc_replace(sch, new, &q->queues[band]);
306b9c7a7acSNogah Frankel 
307b9c7a7acSNogah Frankel 	graft_offload.handle = sch->handle;
308b9c7a7acSNogah Frankel 	graft_offload.parent = sch->parent;
309b9c7a7acSNogah Frankel 	graft_offload.graft_params.band = band;
310b9c7a7acSNogah Frankel 	graft_offload.graft_params.child_handle = new->handle;
311b9c7a7acSNogah Frankel 	graft_offload.command = TC_PRIO_GRAFT;
312b9c7a7acSNogah Frankel 
313bfaee911SJakub Kicinski 	qdisc_offload_graft_helper(qdisc_dev(sch), sch, new, *old,
314bfaee911SJakub Kicinski 				   TC_SETUP_QDISC_PRIO, &graft_offload,
315bfaee911SJakub Kicinski 				   extack);
3161da177e4SLinus Torvalds 	return 0;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds static struct Qdisc *
3201da177e4SLinus Torvalds prio_leaf(struct Qdisc *sch, unsigned long arg)
3211da177e4SLinus Torvalds {
3221da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3231da177e4SLinus Torvalds 	unsigned long band = arg - 1;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	return q->queues[band];
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
328143976ceSWANG Cong static unsigned long prio_find(struct Qdisc *sch, u32 classid)
3291da177e4SLinus Torvalds {
3301da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3311da177e4SLinus Torvalds 	unsigned long band = TC_H_MIN(classid);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	if (band - 1 >= q->bands)
3341da177e4SLinus Torvalds 		return 0;
3351da177e4SLinus Torvalds 	return band;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
3391da177e4SLinus Torvalds {
340143976ceSWANG Cong 	return prio_find(sch, classid);
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 
344143976ceSWANG Cong static void prio_unbind(struct Qdisc *q, unsigned long cl)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
3491da177e4SLinus Torvalds 			   struct tcmsg *tcm)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	tcm->tcm_handle |= TC_H_MIN(cl);
3541da177e4SLinus Torvalds 	tcm->tcm_info = q->queues[cl-1]->handle;
3551da177e4SLinus Torvalds 	return 0;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
3582cf6c36cSJarek Poplawski static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
3592cf6c36cSJarek Poplawski 				 struct gnet_dump *d)
3602cf6c36cSJarek Poplawski {
3612cf6c36cSJarek Poplawski 	struct prio_sched_data *q = qdisc_priv(sch);
3622cf6c36cSJarek Poplawski 	struct Qdisc *cl_q;
3632cf6c36cSJarek Poplawski 
3642cf6c36cSJarek Poplawski 	cl_q = q->queues[cl - 1];
365edb09eb1SEric Dumazet 	if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
366edb09eb1SEric Dumazet 				  d, NULL, &cl_q->bstats) < 0 ||
367b0ab6f92SJohn Fastabend 	    gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0)
3682cf6c36cSJarek Poplawski 		return -1;
3692cf6c36cSJarek Poplawski 
3702cf6c36cSJarek Poplawski 	return 0;
3712cf6c36cSJarek Poplawski }
3722cf6c36cSJarek Poplawski 
3731da177e4SLinus Torvalds static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3761da177e4SLinus Torvalds 	int prio;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	if (arg->stop)
3791da177e4SLinus Torvalds 		return;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	for (prio = 0; prio < q->bands; prio++) {
3821da177e4SLinus Torvalds 		if (arg->count < arg->skip) {
3831da177e4SLinus Torvalds 			arg->count++;
3841da177e4SLinus Torvalds 			continue;
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 		if (arg->fn(sch, prio + 1, arg) < 0) {
3871da177e4SLinus Torvalds 			arg->stop = 1;
3881da177e4SLinus Torvalds 			break;
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 		arg->count++;
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
394cbaacc4eSAlexander Aring static struct tcf_block *prio_tcf_block(struct Qdisc *sch, unsigned long cl,
395cbaacc4eSAlexander Aring 					struct netlink_ext_ack *extack)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	if (cl)
4001da177e4SLinus Torvalds 		return NULL;
4016529eabaSJiri Pirko 	return q->block;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
40420fea08bSEric Dumazet static const struct Qdisc_class_ops prio_class_ops = {
4051da177e4SLinus Torvalds 	.graft		=	prio_graft,
4061da177e4SLinus Torvalds 	.leaf		=	prio_leaf,
407143976ceSWANG Cong 	.find		=	prio_find,
4081da177e4SLinus Torvalds 	.walk		=	prio_walk,
4096529eabaSJiri Pirko 	.tcf_block	=	prio_tcf_block,
4101da177e4SLinus Torvalds 	.bind_tcf	=	prio_bind,
411143976ceSWANG Cong 	.unbind_tcf	=	prio_unbind,
4121da177e4SLinus Torvalds 	.dump		=	prio_dump_class,
4132cf6c36cSJarek Poplawski 	.dump_stats	=	prio_dump_class_stats,
4141da177e4SLinus Torvalds };
4151da177e4SLinus Torvalds 
41620fea08bSEric Dumazet static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
4171da177e4SLinus Torvalds 	.next		=	NULL,
4181da177e4SLinus Torvalds 	.cl_ops		=	&prio_class_ops,
4191da177e4SLinus Torvalds 	.id		=	"prio",
4201da177e4SLinus Torvalds 	.priv_size	=	sizeof(struct prio_sched_data),
4211da177e4SLinus Torvalds 	.enqueue	=	prio_enqueue,
4221da177e4SLinus Torvalds 	.dequeue	=	prio_dequeue,
42348a8f519SPatrick McHardy 	.peek		=	prio_peek,
4241da177e4SLinus Torvalds 	.init		=	prio_init,
4251da177e4SLinus Torvalds 	.reset		=	prio_reset,
4261da177e4SLinus Torvalds 	.destroy	=	prio_destroy,
4271da177e4SLinus Torvalds 	.change		=	prio_tune,
4281da177e4SLinus Torvalds 	.dump		=	prio_dump,
4291da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
4301da177e4SLinus Torvalds };
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds static int __init prio_module_init(void)
4331da177e4SLinus Torvalds {
4341d8ae3fdSDavid S. Miller 	return register_qdisc(&prio_qdisc_ops);
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds static void __exit prio_module_exit(void)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	unregister_qdisc(&prio_qdisc_ops);
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds module_init(prio_module_init)
4431da177e4SLinus Torvalds module_exit(prio_module_exit)
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds MODULE_LICENSE("GPL");
446