xref: /linux/net/sched/sch_prio.c (revision bb949fbd1878973c3539d9aecff52f284482a937)
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>
151da177e4SLinus Torvalds #include <linux/types.h>
161da177e4SLinus Torvalds #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/string.h>
181da177e4SLinus Torvalds #include <linux/errno.h>
191da177e4SLinus Torvalds #include <linux/skbuff.h>
20dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
211da177e4SLinus Torvalds #include <net/pkt_sched.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds struct prio_sched_data
251da177e4SLinus Torvalds {
261da177e4SLinus Torvalds 	int bands;
27d62733c8SPeter P Waskiewicz Jr 	int curband; /* for round-robin */
281da177e4SLinus Torvalds 	struct tcf_proto *filter_list;
291da177e4SLinus Torvalds 	u8  prio2band[TC_PRIO_MAX+1];
301da177e4SLinus Torvalds 	struct Qdisc *queues[TCQ_PRIO_BANDS];
31d62733c8SPeter P Waskiewicz Jr 	int mq;
321da177e4SLinus Torvalds };
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static struct Qdisc *
361da177e4SLinus Torvalds prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
371da177e4SLinus Torvalds {
381da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
391da177e4SLinus Torvalds 	u32 band = skb->priority;
401da177e4SLinus Torvalds 	struct tcf_result res;
41bdba91ecSPatrick McHardy 	int err;
421da177e4SLinus Torvalds 
4329f1df6cSJamal Hadi Salim 	*qerr = NET_XMIT_BYPASS;
441da177e4SLinus Torvalds 	if (TC_H_MAJ(skb->priority) != sch->handle) {
45bdba91ecSPatrick McHardy 		err = tc_classify(skb, q->filter_list, &res);
461da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
47dbaaa07aSLucas Nussbaum 		switch (err) {
481da177e4SLinus Torvalds 		case TC_ACT_STOLEN:
491da177e4SLinus Torvalds 		case TC_ACT_QUEUED:
501da177e4SLinus Torvalds 			*qerr = NET_XMIT_SUCCESS;
511da177e4SLinus Torvalds 		case TC_ACT_SHOT:
521da177e4SLinus Torvalds 			return NULL;
533ff50b79SStephen Hemminger 		}
541da177e4SLinus Torvalds #endif
55bdba91ecSPatrick McHardy 		if (!q->filter_list || err < 0) {
561da177e4SLinus Torvalds 			if (TC_H_MAJ(band))
571da177e4SLinus Torvalds 				band = 0;
58d62733c8SPeter P Waskiewicz Jr 			band = q->prio2band[band&TC_PRIO_MAX];
59d62733c8SPeter P Waskiewicz Jr 			goto out;
601da177e4SLinus Torvalds 		}
611da177e4SLinus Torvalds 		band = res.classid;
621da177e4SLinus Torvalds 	}
631da177e4SLinus Torvalds 	band = TC_H_MIN(band) - 1;
643e5c2d3bSJamal Hadi Salim 	if (band >= q->bands)
65d62733c8SPeter P Waskiewicz Jr 		band = q->prio2band[0];
66d62733c8SPeter P Waskiewicz Jr out:
67d62733c8SPeter P Waskiewicz Jr 	if (q->mq)
68d62733c8SPeter P Waskiewicz Jr 		skb_set_queue_mapping(skb, band);
691da177e4SLinus Torvalds 	return q->queues[band];
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static int
731da177e4SLinus Torvalds prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
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 
8229f1df6cSJamal Hadi Salim 		if (ret == NET_XMIT_BYPASS)
831da177e4SLinus Torvalds 			sch->qstats.drops++;
841da177e4SLinus Torvalds 		kfree_skb(skb);
851da177e4SLinus Torvalds 		return ret;
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds #endif
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
901da177e4SLinus Torvalds 		sch->bstats.bytes += skb->len;
911da177e4SLinus Torvalds 		sch->bstats.packets++;
921da177e4SLinus Torvalds 		sch->q.qlen++;
931da177e4SLinus Torvalds 		return NET_XMIT_SUCCESS;
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds 	sch->qstats.drops++;
961da177e4SLinus Torvalds 	return ret;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static int
1011da177e4SLinus Torvalds prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	struct Qdisc *qdisc;
1041da177e4SLinus Torvalds 	int ret;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	qdisc = prio_classify(skb, sch, &ret);
1071da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
1081da177e4SLinus Torvalds 	if (qdisc == NULL) {
10929f1df6cSJamal Hadi Salim 		if (ret == NET_XMIT_BYPASS)
1101da177e4SLinus Torvalds 			sch->qstats.drops++;
1111da177e4SLinus Torvalds 		kfree_skb(skb);
1121da177e4SLinus Torvalds 		return ret;
1131da177e4SLinus Torvalds 	}
1141da177e4SLinus Torvalds #endif
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
1171da177e4SLinus Torvalds 		sch->q.qlen++;
1181da177e4SLinus Torvalds 		sch->qstats.requeues++;
1191da177e4SLinus Torvalds 		return 0;
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 	sch->qstats.drops++;
1221da177e4SLinus Torvalds 	return NET_XMIT_DROP;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds static struct sk_buff *
1271da177e4SLinus Torvalds prio_dequeue(struct Qdisc* sch)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	struct sk_buff *skb;
1301da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
1311da177e4SLinus Torvalds 	int prio;
1321da177e4SLinus Torvalds 	struct Qdisc *qdisc;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	for (prio = 0; prio < q->bands; prio++) {
135d62733c8SPeter P Waskiewicz Jr 		/* Check if the target subqueue is available before
136d62733c8SPeter P Waskiewicz Jr 		 * pulling an skb.  This way we avoid excessive requeues
137d62733c8SPeter P Waskiewicz Jr 		 * for slower queues.
138d62733c8SPeter P Waskiewicz Jr 		 */
13900346226SPavel Emelyanov 		if (!__netif_subqueue_stopped(sch->dev, (q->mq ? prio : 0))) {
1401da177e4SLinus Torvalds 			qdisc = q->queues[prio];
1411da177e4SLinus Torvalds 			skb = qdisc->dequeue(qdisc);
1421da177e4SLinus Torvalds 			if (skb) {
1431da177e4SLinus Torvalds 				sch->q.qlen--;
1441da177e4SLinus Torvalds 				return skb;
1451da177e4SLinus Torvalds 			}
1461da177e4SLinus Torvalds 		}
147d62733c8SPeter P Waskiewicz Jr 	}
1481da177e4SLinus Torvalds 	return NULL;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
152d62733c8SPeter P Waskiewicz Jr static struct sk_buff *rr_dequeue(struct Qdisc* sch)
153d62733c8SPeter P Waskiewicz Jr {
154d62733c8SPeter P Waskiewicz Jr 	struct sk_buff *skb;
155d62733c8SPeter P Waskiewicz Jr 	struct prio_sched_data *q = qdisc_priv(sch);
156d62733c8SPeter P Waskiewicz Jr 	struct Qdisc *qdisc;
157d62733c8SPeter P Waskiewicz Jr 	int bandcount;
158d62733c8SPeter P Waskiewicz Jr 
159d62733c8SPeter P Waskiewicz Jr 	/* Only take one pass through the queues.  If nothing is available,
160d62733c8SPeter P Waskiewicz Jr 	 * return nothing.
161d62733c8SPeter P Waskiewicz Jr 	 */
162d62733c8SPeter P Waskiewicz Jr 	for (bandcount = 0; bandcount < q->bands; bandcount++) {
163d62733c8SPeter P Waskiewicz Jr 		/* Check if the target subqueue is available before
164d62733c8SPeter P Waskiewicz Jr 		 * pulling an skb.  This way we avoid excessive requeues
165d62733c8SPeter P Waskiewicz Jr 		 * for slower queues.  If the queue is stopped, try the
166d62733c8SPeter P Waskiewicz Jr 		 * next queue.
167d62733c8SPeter P Waskiewicz Jr 		 */
16800346226SPavel Emelyanov 		if (!__netif_subqueue_stopped(sch->dev,
169d62733c8SPeter P Waskiewicz Jr 					    (q->mq ? q->curband : 0))) {
170d62733c8SPeter P Waskiewicz Jr 			qdisc = q->queues[q->curband];
171d62733c8SPeter P Waskiewicz Jr 			skb = qdisc->dequeue(qdisc);
172d62733c8SPeter P Waskiewicz Jr 			if (skb) {
173d62733c8SPeter P Waskiewicz Jr 				sch->q.qlen--;
174d62733c8SPeter P Waskiewicz Jr 				q->curband++;
175d62733c8SPeter P Waskiewicz Jr 				if (q->curband >= q->bands)
176d62733c8SPeter P Waskiewicz Jr 					q->curband = 0;
177d62733c8SPeter P Waskiewicz Jr 				return skb;
178d62733c8SPeter P Waskiewicz Jr 			}
179d62733c8SPeter P Waskiewicz Jr 		}
180d62733c8SPeter P Waskiewicz Jr 		q->curband++;
181d62733c8SPeter P Waskiewicz Jr 		if (q->curband >= q->bands)
182d62733c8SPeter P Waskiewicz Jr 			q->curband = 0;
183d62733c8SPeter P Waskiewicz Jr 	}
184d62733c8SPeter P Waskiewicz Jr 	return NULL;
185d62733c8SPeter P Waskiewicz Jr }
186d62733c8SPeter P Waskiewicz Jr 
1871da177e4SLinus Torvalds static unsigned int prio_drop(struct Qdisc* sch)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
1901da177e4SLinus Torvalds 	int prio;
1911da177e4SLinus Torvalds 	unsigned int len;
1921da177e4SLinus Torvalds 	struct Qdisc *qdisc;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	for (prio = q->bands-1; prio >= 0; prio--) {
1951da177e4SLinus Torvalds 		qdisc = q->queues[prio];
1966d037a26SPatrick McHardy 		if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
1971da177e4SLinus Torvalds 			sch->q.qlen--;
1981da177e4SLinus Torvalds 			return len;
1991da177e4SLinus Torvalds 		}
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds 	return 0;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds static void
2061da177e4SLinus Torvalds prio_reset(struct Qdisc* sch)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	int prio;
2091da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	for (prio=0; prio<q->bands; prio++)
2121da177e4SLinus Torvalds 		qdisc_reset(q->queues[prio]);
2131da177e4SLinus Torvalds 	sch->q.qlen = 0;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds static void
2171da177e4SLinus Torvalds prio_destroy(struct Qdisc* sch)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	int prio;
2201da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
2211da177e4SLinus Torvalds 
222ff31ab56SPatrick McHardy 	tcf_destroy_chain(&q->filter_list);
2231da177e4SLinus Torvalds 	for (prio=0; prio<q->bands; prio++)
2241da177e4SLinus Torvalds 		qdisc_destroy(q->queues[prio]);
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
2271e90474cSPatrick McHardy static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
230d62733c8SPeter P Waskiewicz Jr 	struct tc_prio_qopt *qopt;
2311e90474cSPatrick McHardy 	struct nlattr *tb[TCA_PRIO_MAX + 1];
232cee63723SPatrick McHardy 	int err;
2331da177e4SLinus Torvalds 	int i;
2341da177e4SLinus Torvalds 
235cee63723SPatrick McHardy 	err = nla_parse_nested_compat(tb, TCA_PRIO_MAX, opt, NULL, qopt,
236cee63723SPatrick McHardy 				      sizeof(*qopt));
237cee63723SPatrick McHardy 	if (err < 0)
238cee63723SPatrick McHardy 		return err;
239cee63723SPatrick McHardy 
240d62733c8SPeter P Waskiewicz Jr 	q->bands = qopt->bands;
241d62733c8SPeter P Waskiewicz Jr 	/* If we're multiqueue, make sure the number of incoming bands
242d62733c8SPeter P Waskiewicz Jr 	 * matches the number of queues on the device we're associating with.
243d62733c8SPeter P Waskiewicz Jr 	 * If the number of bands requested is zero, then set q->bands to
2440773192bSPeter P Waskiewicz Jr 	 * dev->egress_subqueue_count.  Also, the root qdisc must be the
2450773192bSPeter P Waskiewicz Jr 	 * only one that is enabled for multiqueue, since it's the only one
2460773192bSPeter P Waskiewicz Jr 	 * that interacts with the underlying device.
247d62733c8SPeter P Waskiewicz Jr 	 */
2481e90474cSPatrick McHardy 	q->mq = nla_get_flag(tb[TCA_PRIO_MQ]);
249d62733c8SPeter P Waskiewicz Jr 	if (q->mq) {
2500773192bSPeter P Waskiewicz Jr 		if (sch->parent != TC_H_ROOT)
251d62733c8SPeter P Waskiewicz Jr 			return -EINVAL;
252d62733c8SPeter P Waskiewicz Jr 		if (netif_is_multiqueue(sch->dev)) {
253d62733c8SPeter P Waskiewicz Jr 			if (q->bands == 0)
254d62733c8SPeter P Waskiewicz Jr 				q->bands = sch->dev->egress_subqueue_count;
255d62733c8SPeter P Waskiewicz Jr 			else if (q->bands != sch->dev->egress_subqueue_count)
256d62733c8SPeter P Waskiewicz Jr 				return -EINVAL;
257d62733c8SPeter P Waskiewicz Jr 		} else
258d62733c8SPeter P Waskiewicz Jr 			return -EOPNOTSUPP;
259d62733c8SPeter P Waskiewicz Jr 	}
260d62733c8SPeter P Waskiewicz Jr 
261d62733c8SPeter P Waskiewicz Jr 	if (q->bands > TCQ_PRIO_BANDS || q->bands < 2)
2621da177e4SLinus Torvalds 		return -EINVAL;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	for (i=0; i<=TC_PRIO_MAX; i++) {
265d62733c8SPeter P Waskiewicz Jr 		if (qopt->priomap[i] >= q->bands)
2661da177e4SLinus Torvalds 			return -EINVAL;
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	sch_tree_lock(sch);
2701da177e4SLinus Torvalds 	memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
2731da177e4SLinus Torvalds 		struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
2745e50da01SPatrick McHardy 		if (child != &noop_qdisc) {
2755e50da01SPatrick McHardy 			qdisc_tree_decrease_qlen(child, child->q.qlen);
2761da177e4SLinus Torvalds 			qdisc_destroy(child);
2771da177e4SLinus Torvalds 		}
2785e50da01SPatrick McHardy 	}
2791da177e4SLinus Torvalds 	sch_tree_unlock(sch);
2801da177e4SLinus Torvalds 
281dd914b40SAmnon Aaronsohn 	for (i=0; i<q->bands; i++) {
282dd914b40SAmnon Aaronsohn 		if (q->queues[i] == &noop_qdisc) {
2831da177e4SLinus Torvalds 			struct Qdisc *child;
284*bb949fbdSDavid S. Miller 			child = qdisc_create_dflt(sch->dev, sch->dev_queue,
285*bb949fbdSDavid S. Miller 						  &pfifo_qdisc_ops,
2869f9afec4SPatrick McHardy 						  TC_H_MAKE(sch->handle, i + 1));
2871da177e4SLinus Torvalds 			if (child) {
2881da177e4SLinus Torvalds 				sch_tree_lock(sch);
289dd914b40SAmnon Aaronsohn 				child = xchg(&q->queues[i], child);
2901da177e4SLinus Torvalds 
2915e50da01SPatrick McHardy 				if (child != &noop_qdisc) {
2925e50da01SPatrick McHardy 					qdisc_tree_decrease_qlen(child,
2935e50da01SPatrick McHardy 								 child->q.qlen);
2941da177e4SLinus Torvalds 					qdisc_destroy(child);
2955e50da01SPatrick McHardy 				}
2961da177e4SLinus Torvalds 				sch_tree_unlock(sch);
2971da177e4SLinus Torvalds 			}
2981da177e4SLinus Torvalds 		}
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 	return 0;
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031e90474cSPatrick McHardy static int prio_init(struct Qdisc *sch, struct nlattr *opt)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3061da177e4SLinus Torvalds 	int i;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	for (i=0; i<TCQ_PRIO_BANDS; i++)
3091da177e4SLinus Torvalds 		q->queues[i] = &noop_qdisc;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	if (opt == NULL) {
3121da177e4SLinus Torvalds 		return -EINVAL;
3131da177e4SLinus Torvalds 	} else {
3141da177e4SLinus Torvalds 		int err;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 		if ((err= prio_tune(sch, opt)) != 0)
3171da177e4SLinus Torvalds 			return err;
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 	return 0;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
32527a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
3261e90474cSPatrick McHardy 	struct nlattr *nest;
3271da177e4SLinus Torvalds 	struct tc_prio_qopt opt;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	opt.bands = q->bands;
3301da177e4SLinus Torvalds 	memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
331d62733c8SPeter P Waskiewicz Jr 
3321e90474cSPatrick McHardy 	nest = nla_nest_compat_start(skb, TCA_OPTIONS, sizeof(opt), &opt);
3331e90474cSPatrick McHardy 	if (nest == NULL)
3341e90474cSPatrick McHardy 		goto nla_put_failure;
3351e90474cSPatrick McHardy 	if (q->mq) {
3361e90474cSPatrick McHardy 		if (nla_put_flag(skb, TCA_PRIO_MQ) < 0)
3371e90474cSPatrick McHardy 			goto nla_put_failure;
3381e90474cSPatrick McHardy 	}
3391e90474cSPatrick McHardy 	nla_nest_compat_end(skb, nest);
340d62733c8SPeter P Waskiewicz Jr 
3411da177e4SLinus Torvalds 	return skb->len;
3421da177e4SLinus Torvalds 
3431e90474cSPatrick McHardy nla_put_failure:
344dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
3451da177e4SLinus Torvalds 	return -1;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
3491da177e4SLinus Torvalds 		      struct Qdisc **old)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3521da177e4SLinus Torvalds 	unsigned long band = arg - 1;
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	if (band >= q->bands)
3551da177e4SLinus Torvalds 		return -EINVAL;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	if (new == NULL)
3581da177e4SLinus Torvalds 		new = &noop_qdisc;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	sch_tree_lock(sch);
3611da177e4SLinus Torvalds 	*old = q->queues[band];
3621da177e4SLinus Torvalds 	q->queues[band] = new;
3635e50da01SPatrick McHardy 	qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
3641da177e4SLinus Torvalds 	qdisc_reset(*old);
3651da177e4SLinus Torvalds 	sch_tree_unlock(sch);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	return 0;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds static struct Qdisc *
3711da177e4SLinus Torvalds prio_leaf(struct Qdisc *sch, unsigned long arg)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3741da177e4SLinus Torvalds 	unsigned long band = arg - 1;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	if (band >= q->bands)
3771da177e4SLinus Torvalds 		return NULL;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	return q->queues[band];
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds static unsigned long prio_get(struct Qdisc *sch, u32 classid)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
3851da177e4SLinus Torvalds 	unsigned long band = TC_H_MIN(classid);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	if (band - 1 >= q->bands)
3881da177e4SLinus Torvalds 		return 0;
3891da177e4SLinus Torvalds 	return band;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
3931da177e4SLinus Torvalds {
3941da177e4SLinus Torvalds 	return prio_get(sch, classid);
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds static void prio_put(struct Qdisc *q, unsigned long cl)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	return;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031e90474cSPatrick McHardy static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct nlattr **tca, unsigned long *arg)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	unsigned long cl = *arg;
4061da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	if (cl - 1 > q->bands)
4091da177e4SLinus Torvalds 		return -ENOENT;
4101da177e4SLinus Torvalds 	return 0;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds static int prio_delete(struct Qdisc *sch, unsigned long cl)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
4161da177e4SLinus Torvalds 	if (cl - 1 > q->bands)
4171da177e4SLinus Torvalds 		return -ENOENT;
4181da177e4SLinus Torvalds 	return 0;
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
4231da177e4SLinus Torvalds 			   struct tcmsg *tcm)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	if (cl - 1 > q->bands)
4281da177e4SLinus Torvalds 		return -ENOENT;
4291da177e4SLinus Torvalds 	tcm->tcm_handle |= TC_H_MIN(cl);
4301da177e4SLinus Torvalds 	if (q->queues[cl-1])
4311da177e4SLinus Torvalds 		tcm->tcm_info = q->queues[cl-1]->handle;
4321da177e4SLinus Torvalds 	return 0;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4352cf6c36cSJarek Poplawski static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
4362cf6c36cSJarek Poplawski 				 struct gnet_dump *d)
4372cf6c36cSJarek Poplawski {
4382cf6c36cSJarek Poplawski 	struct prio_sched_data *q = qdisc_priv(sch);
4392cf6c36cSJarek Poplawski 	struct Qdisc *cl_q;
4402cf6c36cSJarek Poplawski 
4412cf6c36cSJarek Poplawski 	cl_q = q->queues[cl - 1];
4422cf6c36cSJarek Poplawski 	if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
4432cf6c36cSJarek Poplawski 	    gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
4442cf6c36cSJarek Poplawski 		return -1;
4452cf6c36cSJarek Poplawski 
4462cf6c36cSJarek Poplawski 	return 0;
4472cf6c36cSJarek Poplawski }
4482cf6c36cSJarek Poplawski 
4491da177e4SLinus Torvalds static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
4521da177e4SLinus Torvalds 	int prio;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	if (arg->stop)
4551da177e4SLinus Torvalds 		return;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	for (prio = 0; prio < q->bands; prio++) {
4581da177e4SLinus Torvalds 		if (arg->count < arg->skip) {
4591da177e4SLinus Torvalds 			arg->count++;
4601da177e4SLinus Torvalds 			continue;
4611da177e4SLinus Torvalds 		}
4621da177e4SLinus Torvalds 		if (arg->fn(sch, prio+1, arg) < 0) {
4631da177e4SLinus Torvalds 			arg->stop = 1;
4641da177e4SLinus Torvalds 			break;
4651da177e4SLinus Torvalds 		}
4661da177e4SLinus Torvalds 		arg->count++;
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	struct prio_sched_data *q = qdisc_priv(sch);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	if (cl)
4751da177e4SLinus Torvalds 		return NULL;
4761da177e4SLinus Torvalds 	return &q->filter_list;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
47920fea08bSEric Dumazet static const struct Qdisc_class_ops prio_class_ops = {
4801da177e4SLinus Torvalds 	.graft		=	prio_graft,
4811da177e4SLinus Torvalds 	.leaf		=	prio_leaf,
4821da177e4SLinus Torvalds 	.get		=	prio_get,
4831da177e4SLinus Torvalds 	.put		=	prio_put,
4841da177e4SLinus Torvalds 	.change		=	prio_change,
4851da177e4SLinus Torvalds 	.delete		=	prio_delete,
4861da177e4SLinus Torvalds 	.walk		=	prio_walk,
4871da177e4SLinus Torvalds 	.tcf_chain	=	prio_find_tcf,
4881da177e4SLinus Torvalds 	.bind_tcf	=	prio_bind,
4891da177e4SLinus Torvalds 	.unbind_tcf	=	prio_put,
4901da177e4SLinus Torvalds 	.dump		=	prio_dump_class,
4912cf6c36cSJarek Poplawski 	.dump_stats	=	prio_dump_class_stats,
4921da177e4SLinus Torvalds };
4931da177e4SLinus Torvalds 
49420fea08bSEric Dumazet static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
4951da177e4SLinus Torvalds 	.next		=	NULL,
4961da177e4SLinus Torvalds 	.cl_ops		=	&prio_class_ops,
4971da177e4SLinus Torvalds 	.id		=	"prio",
4981da177e4SLinus Torvalds 	.priv_size	=	sizeof(struct prio_sched_data),
4991da177e4SLinus Torvalds 	.enqueue	=	prio_enqueue,
5001da177e4SLinus Torvalds 	.dequeue	=	prio_dequeue,
5011da177e4SLinus Torvalds 	.requeue	=	prio_requeue,
5021da177e4SLinus Torvalds 	.drop		=	prio_drop,
5031da177e4SLinus Torvalds 	.init		=	prio_init,
5041da177e4SLinus Torvalds 	.reset		=	prio_reset,
5051da177e4SLinus Torvalds 	.destroy	=	prio_destroy,
5061da177e4SLinus Torvalds 	.change		=	prio_tune,
5071da177e4SLinus Torvalds 	.dump		=	prio_dump,
5081da177e4SLinus Torvalds 	.owner		=	THIS_MODULE,
5091da177e4SLinus Torvalds };
5101da177e4SLinus Torvalds 
51120fea08bSEric Dumazet static struct Qdisc_ops rr_qdisc_ops __read_mostly = {
512d62733c8SPeter P Waskiewicz Jr 	.next		=	NULL,
513d62733c8SPeter P Waskiewicz Jr 	.cl_ops		=	&prio_class_ops,
514d62733c8SPeter P Waskiewicz Jr 	.id		=	"rr",
515d62733c8SPeter P Waskiewicz Jr 	.priv_size	=	sizeof(struct prio_sched_data),
516d62733c8SPeter P Waskiewicz Jr 	.enqueue	=	prio_enqueue,
517d62733c8SPeter P Waskiewicz Jr 	.dequeue	=	rr_dequeue,
518d62733c8SPeter P Waskiewicz Jr 	.requeue	=	prio_requeue,
519d62733c8SPeter P Waskiewicz Jr 	.drop		=	prio_drop,
520d62733c8SPeter P Waskiewicz Jr 	.init		=	prio_init,
521d62733c8SPeter P Waskiewicz Jr 	.reset		=	prio_reset,
522d62733c8SPeter P Waskiewicz Jr 	.destroy	=	prio_destroy,
523d62733c8SPeter P Waskiewicz Jr 	.change		=	prio_tune,
524d62733c8SPeter P Waskiewicz Jr 	.dump		=	prio_dump,
525d62733c8SPeter P Waskiewicz Jr 	.owner		=	THIS_MODULE,
526d62733c8SPeter P Waskiewicz Jr };
527d62733c8SPeter P Waskiewicz Jr 
5281da177e4SLinus Torvalds static int __init prio_module_init(void)
5291da177e4SLinus Torvalds {
530d62733c8SPeter P Waskiewicz Jr 	int err;
531d62733c8SPeter P Waskiewicz Jr 
532d62733c8SPeter P Waskiewicz Jr 	err = register_qdisc(&prio_qdisc_ops);
533d62733c8SPeter P Waskiewicz Jr 	if (err < 0)
534d62733c8SPeter P Waskiewicz Jr 		return err;
535d62733c8SPeter P Waskiewicz Jr 	err = register_qdisc(&rr_qdisc_ops);
536d62733c8SPeter P Waskiewicz Jr 	if (err < 0)
537d62733c8SPeter P Waskiewicz Jr 		unregister_qdisc(&prio_qdisc_ops);
538d62733c8SPeter P Waskiewicz Jr 	return err;
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds static void __exit prio_module_exit(void)
5421da177e4SLinus Torvalds {
5431da177e4SLinus Torvalds 	unregister_qdisc(&prio_qdisc_ops);
544d62733c8SPeter P Waskiewicz Jr 	unregister_qdisc(&rr_qdisc_ops);
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds module_init(prio_module_init)
5481da177e4SLinus Torvalds module_exit(prio_module_exit)
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds MODULE_LICENSE("GPL");
551d62733c8SPeter P Waskiewicz Jr MODULE_ALIAS("sch_rr");
552