xref: /linux/net/sched/sch_prio.c (revision 86bd446b5cebd783187ea3772ff258210de77d99)
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++)
178*86bd446bSVlad 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)
208*86bd446bSVlad 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);
223*86bd446bSVlad Buslov 		qdisc_put(child);
2241da177e4SLinus Torvalds 	}
2253d7c8257SEric Dumazet 
22649b49971SJiri Kosina 	for (i = oldbands; i < q->bands; i++) {
2273d7c8257SEric Dumazet 		q->queues[i] = queues[i];
22849b49971SJiri Kosina 		if (q->queues[i] != &noop_qdisc)
22949b49971SJiri Kosina 			qdisc_hash_add(q->queues[i], true);
23049b49971SJiri Kosina 	}
2313d7c8257SEric Dumazet 
2321da177e4SLinus Torvalds 	sch_tree_unlock(sch);
2331da177e4SLinus Torvalds 	return 0;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
236e63d7dfdSAlexander Aring static int prio_init(struct Qdisc *sch, struct nlattr *opt,
237e63d7dfdSAlexander Aring 		     struct netlink_ext_ack *extack)
2381da177e4SLinus Torvalds {
2396529eabaSJiri Pirko 	struct prio_sched_data *q = qdisc_priv(sch);
2406529eabaSJiri Pirko 	int err;
2416529eabaSJiri Pirko 
2423d7c8257SEric Dumazet 	if (!opt)
2431da177e4SLinus Torvalds 		return -EINVAL;
2441da177e4SLinus Torvalds 
2458d1a77f9SAlexander Aring 	err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
2466529eabaSJiri Pirko 	if (err)
2476529eabaSJiri Pirko 		return err;
2486529eabaSJiri Pirko 
2492030721cSAlexander Aring 	return prio_tune(sch, opt, extack);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2527fdb61b4SNogah Frankel static int prio_dump_offload(struct Qdisc *sch)
2537fdb61b4SNogah Frankel {
2547fdb61b4SNogah Frankel 	struct net_device *dev = qdisc_dev(sch);
2557fdb61b4SNogah Frankel 	struct tc_prio_qopt_offload hw_stats = {
256ef58ca38SAndrew Morton 		.command = TC_PRIO_STATS,
2577fdb61b4SNogah Frankel 		.handle = sch->handle,
2587fdb61b4SNogah Frankel 		.parent = sch->parent,
259ef58ca38SAndrew Morton 		{
260ef58ca38SAndrew Morton 			.stats = {
261ef58ca38SAndrew Morton 				.bstats = &sch->bstats,
262ef58ca38SAndrew Morton 				.qstats = &sch->qstats,
263ef58ca38SAndrew Morton 			},
264ef58ca38SAndrew Morton 		},
2657fdb61b4SNogah Frankel 	};
2667fdb61b4SNogah Frankel 	int err;
2677fdb61b4SNogah Frankel 
2687fdb61b4SNogah Frankel 	sch->flags &= ~TCQ_F_OFFLOADED;
2697fdb61b4SNogah Frankel 	if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
2707fdb61b4SNogah Frankel 		return 0;
2717fdb61b4SNogah Frankel 
2727fdb61b4SNogah Frankel 	err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_PRIO,
2737fdb61b4SNogah Frankel 					    &hw_stats);
2747fdb61b4SNogah Frankel 	if (err == -EOPNOTSUPP)
2757fdb61b4SNogah Frankel 		return 0;
2767fdb61b4SNogah Frankel 
2777fdb61b4SNogah Frankel 	if (!err)
2787fdb61b4SNogah Frankel 		sch->flags |= TCQ_F_OFFLOADED;
2797fdb61b4SNogah Frankel 
2807fdb61b4SNogah Frankel 	return err;
2817fdb61b4SNogah Frankel }
2827fdb61b4SNogah Frankel 
2831da177e4SLinus Torvalds static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
28627a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
2871da177e4SLinus Torvalds 	struct tc_prio_qopt opt;
2887fdb61b4SNogah Frankel 	int err;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	opt.bands = q->bands;
2911da177e4SLinus Torvalds 	memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1);
292d62733c8SPeter P Waskiewicz Jr 
2937fdb61b4SNogah Frankel 	err = prio_dump_offload(sch);
2947fdb61b4SNogah Frankel 	if (err)
2957fdb61b4SNogah Frankel 		goto nla_put_failure;
2967fdb61b4SNogah Frankel 
2971b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
2981b34ec43SDavid S. Miller 		goto nla_put_failure;
299d62733c8SPeter P Waskiewicz Jr 
3001da177e4SLinus Torvalds 	return skb->len;
3011da177e4SLinus Torvalds 
3021e90474cSPatrick McHardy nla_put_failure:
303dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
3041da177e4SLinus Torvalds 	return -1;
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
308653d6fd6SAlexander Aring 		      struct Qdisc **old, struct netlink_ext_ack *extack)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
311b9c7a7acSNogah Frankel 	struct tc_prio_qopt_offload graft_offload;
312b9c7a7acSNogah Frankel 	struct net_device *dev = qdisc_dev(sch);
3131da177e4SLinus Torvalds 	unsigned long band = arg - 1;
314b9c7a7acSNogah Frankel 	bool any_qdisc_is_offloaded;
315b9c7a7acSNogah Frankel 	int err;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	if (new == NULL)
3181da177e4SLinus Torvalds 		new = &noop_qdisc;
3191da177e4SLinus Torvalds 
32086a7996cSWANG Cong 	*old = qdisc_replace(sch, new, &q->queues[band]);
321b9c7a7acSNogah Frankel 
322b9c7a7acSNogah Frankel 	if (!tc_can_offload(dev))
323b9c7a7acSNogah Frankel 		return 0;
324b9c7a7acSNogah Frankel 
325b9c7a7acSNogah Frankel 	graft_offload.handle = sch->handle;
326b9c7a7acSNogah Frankel 	graft_offload.parent = sch->parent;
327b9c7a7acSNogah Frankel 	graft_offload.graft_params.band = band;
328b9c7a7acSNogah Frankel 	graft_offload.graft_params.child_handle = new->handle;
329b9c7a7acSNogah Frankel 	graft_offload.command = TC_PRIO_GRAFT;
330b9c7a7acSNogah Frankel 
331b9c7a7acSNogah Frankel 	err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_PRIO,
332b9c7a7acSNogah Frankel 					    &graft_offload);
333b9c7a7acSNogah Frankel 
334b9c7a7acSNogah Frankel 	/* Don't report error if the graft is part of destroy operation. */
335b9c7a7acSNogah Frankel 	if (err && new != &noop_qdisc) {
336b9c7a7acSNogah Frankel 		/* Don't report error if the parent, the old child and the new
337b9c7a7acSNogah Frankel 		 * one are not offloaded.
338b9c7a7acSNogah Frankel 		 */
339b9c7a7acSNogah Frankel 		any_qdisc_is_offloaded = sch->flags & TCQ_F_OFFLOADED;
340b9c7a7acSNogah Frankel 		any_qdisc_is_offloaded |= new->flags & TCQ_F_OFFLOADED;
341b9c7a7acSNogah Frankel 		if (*old)
342b9c7a7acSNogah Frankel 			any_qdisc_is_offloaded |= (*old)->flags &
343b9c7a7acSNogah Frankel 						   TCQ_F_OFFLOADED;
344b9c7a7acSNogah Frankel 
345b9c7a7acSNogah Frankel 		if (any_qdisc_is_offloaded)
346b9c7a7acSNogah Frankel 			NL_SET_ERR_MSG(extack, "Offloading graft operation failed.");
347b9c7a7acSNogah Frankel 	}
348b9c7a7acSNogah Frankel 
3491da177e4SLinus Torvalds 	return 0;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static struct Qdisc *
3531da177e4SLinus Torvalds prio_leaf(struct Qdisc *sch, unsigned long arg)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3561da177e4SLinus Torvalds 	unsigned long band = arg - 1;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	return q->queues[band];
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
361143976ceSWANG Cong static unsigned long prio_find(struct Qdisc *sch, u32 classid)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3641da177e4SLinus Torvalds 	unsigned long band = TC_H_MIN(classid);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	if (band - 1 >= q->bands)
3671da177e4SLinus Torvalds 		return 0;
3681da177e4SLinus Torvalds 	return band;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
3721da177e4SLinus Torvalds {
373143976ceSWANG Cong 	return prio_find(sch, classid);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 
377143976ceSWANG Cong static void prio_unbind(struct Qdisc *q, unsigned long cl)
3781da177e4SLinus Torvalds {
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
3821da177e4SLinus Torvalds 			   struct tcmsg *tcm)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	tcm->tcm_handle |= TC_H_MIN(cl);
3871da177e4SLinus Torvalds 	tcm->tcm_info = q->queues[cl-1]->handle;
3881da177e4SLinus Torvalds 	return 0;
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3912cf6c36cSJarek Poplawski static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
3922cf6c36cSJarek Poplawski 				 struct gnet_dump *d)
3932cf6c36cSJarek Poplawski {
3942cf6c36cSJarek Poplawski 	struct prio_sched_data *q = qdisc_priv(sch);
3952cf6c36cSJarek Poplawski 	struct Qdisc *cl_q;
3962cf6c36cSJarek Poplawski 
3972cf6c36cSJarek Poplawski 	cl_q = q->queues[cl - 1];
398edb09eb1SEric Dumazet 	if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
399edb09eb1SEric Dumazet 				  d, NULL, &cl_q->bstats) < 0 ||
400b0ab6f92SJohn Fastabend 	    gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0)
4012cf6c36cSJarek Poplawski 		return -1;
4022cf6c36cSJarek Poplawski 
4032cf6c36cSJarek Poplawski 	return 0;
4042cf6c36cSJarek Poplawski }
4052cf6c36cSJarek Poplawski 
4061da177e4SLinus Torvalds static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
4091da177e4SLinus Torvalds 	int prio;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	if (arg->stop)
4121da177e4SLinus Torvalds 		return;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	for (prio = 0; prio < q->bands; prio++) {
4151da177e4SLinus Torvalds 		if (arg->count < arg->skip) {
4161da177e4SLinus Torvalds 			arg->count++;
4171da177e4SLinus Torvalds 			continue;
4181da177e4SLinus Torvalds 		}
4191da177e4SLinus Torvalds 		if (arg->fn(sch, prio + 1, arg) < 0) {
4201da177e4SLinus Torvalds 			arg->stop = 1;
4211da177e4SLinus Torvalds 			break;
4221da177e4SLinus Torvalds 		}
4231da177e4SLinus Torvalds 		arg->count++;
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
427cbaacc4eSAlexander Aring static struct tcf_block *prio_tcf_block(struct Qdisc *sch, unsigned long cl,
428cbaacc4eSAlexander Aring 					struct netlink_ext_ack *extack)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	if (cl)
4331da177e4SLinus Torvalds 		return NULL;
4346529eabaSJiri Pirko 	return q->block;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
43720fea08bSEric Dumazet static const struct Qdisc_class_ops prio_class_ops = {
4381da177e4SLinus Torvalds 	.graft		=	prio_graft,
4391da177e4SLinus Torvalds 	.leaf		=	prio_leaf,
440143976ceSWANG Cong 	.find		=	prio_find,
4411da177e4SLinus Torvalds 	.walk		=	prio_walk,
4426529eabaSJiri Pirko 	.tcf_block	=	prio_tcf_block,
4431da177e4SLinus Torvalds 	.bind_tcf	=	prio_bind,
444143976ceSWANG Cong 	.unbind_tcf	=	prio_unbind,
4451da177e4SLinus Torvalds 	.dump		=	prio_dump_class,
4462cf6c36cSJarek Poplawski 	.dump_stats	=	prio_dump_class_stats,
4471da177e4SLinus Torvalds };
4481da177e4SLinus Torvalds 
44920fea08bSEric Dumazet static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
4501da177e4SLinus Torvalds 	.next		=	NULL,
4511da177e4SLinus Torvalds 	.cl_ops		=	&prio_class_ops,
4521da177e4SLinus Torvalds 	.id		=	"prio",
4531da177e4SLinus Torvalds 	.priv_size	=	sizeof(struct prio_sched_data),
4541da177e4SLinus Torvalds 	.enqueue	=	prio_enqueue,
4551da177e4SLinus Torvalds 	.dequeue	=	prio_dequeue,
45648a8f519SPatrick McHardy 	.peek		=	prio_peek,
4571da177e4SLinus Torvalds 	.init		=	prio_init,
4581da177e4SLinus Torvalds 	.reset		=	prio_reset,
4591da177e4SLinus Torvalds 	.destroy	=	prio_destroy,
4601da177e4SLinus Torvalds 	.change		=	prio_tune,
4611da177e4SLinus Torvalds 	.dump		=	prio_dump,
4621da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
4631da177e4SLinus Torvalds };
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds static int __init prio_module_init(void)
4661da177e4SLinus Torvalds {
4671d8ae3fdSDavid S. Miller 	return register_qdisc(&prio_qdisc_ops);
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds static void __exit prio_module_exit(void)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	unregister_qdisc(&prio_qdisc_ops);
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds module_init(prio_module_init)
4761da177e4SLinus Torvalds module_exit(prio_module_exit)
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds MODULE_LICENSE("GPL");
479