xref: /openbmc/linux/net/sched/sch_mqprio.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b8970f0bSJohn Fastabend /*
3b8970f0bSJohn Fastabend  * net/sched/sch_mqprio.c
4b8970f0bSJohn Fastabend  *
5b8970f0bSJohn Fastabend  * Copyright (c) 2010 John Fastabend <john.r.fastabend@intel.com>
6b8970f0bSJohn Fastabend  */
7b8970f0bSJohn Fastabend 
8f62af20bSVladimir Oltean #include <linux/ethtool_netlink.h>
9b8970f0bSJohn Fastabend #include <linux/types.h>
10b8970f0bSJohn Fastabend #include <linux/slab.h>
11b8970f0bSJohn Fastabend #include <linux/kernel.h>
12b8970f0bSJohn Fastabend #include <linux/string.h>
13b8970f0bSJohn Fastabend #include <linux/errno.h>
14b8970f0bSJohn Fastabend #include <linux/skbuff.h>
153a9a231dSPaul Gortmaker #include <linux/module.h>
16b8970f0bSJohn Fastabend #include <net/netlink.h>
17b8970f0bSJohn Fastabend #include <net/pkt_sched.h>
18b8970f0bSJohn Fastabend #include <net/sch_generic.h>
194e8b86c0SAmritha Nambiar #include <net/pkt_cls.h>
20b8970f0bSJohn Fastabend 
211dfe086dSVladimir Oltean #include "sch_mqprio_lib.h"
221dfe086dSVladimir Oltean 
23b8970f0bSJohn Fastabend struct mqprio_sched {
24b8970f0bSJohn Fastabend 	struct Qdisc		**qdiscs;
254e8b86c0SAmritha Nambiar 	u16 mode;
264e8b86c0SAmritha Nambiar 	u16 shaper;
272026fecfSAlexander Duyck 	int hw_offload;
284e8b86c0SAmritha Nambiar 	u32 flags;
294e8b86c0SAmritha Nambiar 	u64 min_rate[TC_QOPT_MAX_QUEUE];
304e8b86c0SAmritha Nambiar 	u64 max_rate[TC_QOPT_MAX_QUEUE];
31f62af20bSVladimir Oltean 	u32 fp[TC_QOPT_MAX_QUEUE];
32b8970f0bSJohn Fastabend };
33b8970f0bSJohn Fastabend 
mqprio_enable_offload(struct Qdisc * sch,const struct tc_mqprio_qopt * qopt,struct netlink_ext_ack * extack)345cfb45e2SVladimir Oltean static int mqprio_enable_offload(struct Qdisc *sch,
35d404959fSVladimir Oltean 				 const struct tc_mqprio_qopt *qopt,
36d404959fSVladimir Oltean 				 struct netlink_ext_ack *extack)
375cfb45e2SVladimir Oltean {
385cfb45e2SVladimir Oltean 	struct mqprio_sched *priv = qdisc_priv(sch);
395cfb45e2SVladimir Oltean 	struct net_device *dev = qdisc_dev(sch);
40c54876cdSVladimir Oltean 	struct tc_mqprio_qopt_offload mqprio = {
41c54876cdSVladimir Oltean 		.qopt = *qopt,
42c54876cdSVladimir Oltean 		.extack = extack,
43c54876cdSVladimir Oltean 	};
445cfb45e2SVladimir Oltean 	int err, i;
455cfb45e2SVladimir Oltean 
465cfb45e2SVladimir Oltean 	switch (priv->mode) {
475cfb45e2SVladimir Oltean 	case TC_MQPRIO_MODE_DCB:
485cfb45e2SVladimir Oltean 		if (priv->shaper != TC_MQPRIO_SHAPER_DCB)
495cfb45e2SVladimir Oltean 			return -EINVAL;
505cfb45e2SVladimir Oltean 		break;
515cfb45e2SVladimir Oltean 	case TC_MQPRIO_MODE_CHANNEL:
525cfb45e2SVladimir Oltean 		mqprio.flags = priv->flags;
535cfb45e2SVladimir Oltean 		if (priv->flags & TC_MQPRIO_F_MODE)
545cfb45e2SVladimir Oltean 			mqprio.mode = priv->mode;
555cfb45e2SVladimir Oltean 		if (priv->flags & TC_MQPRIO_F_SHAPER)
565cfb45e2SVladimir Oltean 			mqprio.shaper = priv->shaper;
575cfb45e2SVladimir Oltean 		if (priv->flags & TC_MQPRIO_F_MIN_RATE)
585cfb45e2SVladimir Oltean 			for (i = 0; i < mqprio.qopt.num_tc; i++)
595cfb45e2SVladimir Oltean 				mqprio.min_rate[i] = priv->min_rate[i];
605cfb45e2SVladimir Oltean 		if (priv->flags & TC_MQPRIO_F_MAX_RATE)
615cfb45e2SVladimir Oltean 			for (i = 0; i < mqprio.qopt.num_tc; i++)
625cfb45e2SVladimir Oltean 				mqprio.max_rate[i] = priv->max_rate[i];
635cfb45e2SVladimir Oltean 		break;
645cfb45e2SVladimir Oltean 	default:
655cfb45e2SVladimir Oltean 		return -EINVAL;
665cfb45e2SVladimir Oltean 	}
675cfb45e2SVladimir Oltean 
68f62af20bSVladimir Oltean 	mqprio_fp_to_offload(priv->fp, &mqprio);
69f62af20bSVladimir Oltean 
705cfb45e2SVladimir Oltean 	err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_MQPRIO,
715cfb45e2SVladimir Oltean 					    &mqprio);
725cfb45e2SVladimir Oltean 	if (err)
735cfb45e2SVladimir Oltean 		return err;
745cfb45e2SVladimir Oltean 
755cfb45e2SVladimir Oltean 	priv->hw_offload = mqprio.qopt.hw;
765cfb45e2SVladimir Oltean 
775cfb45e2SVladimir Oltean 	return 0;
785cfb45e2SVladimir Oltean }
795cfb45e2SVladimir Oltean 
mqprio_disable_offload(struct Qdisc * sch)805cfb45e2SVladimir Oltean static void mqprio_disable_offload(struct Qdisc *sch)
815cfb45e2SVladimir Oltean {
825cfb45e2SVladimir Oltean 	struct tc_mqprio_qopt_offload mqprio = { { 0 } };
835cfb45e2SVladimir Oltean 	struct mqprio_sched *priv = qdisc_priv(sch);
845cfb45e2SVladimir Oltean 	struct net_device *dev = qdisc_dev(sch);
855cfb45e2SVladimir Oltean 
865cfb45e2SVladimir Oltean 	switch (priv->mode) {
875cfb45e2SVladimir Oltean 	case TC_MQPRIO_MODE_DCB:
885cfb45e2SVladimir Oltean 	case TC_MQPRIO_MODE_CHANNEL:
895cfb45e2SVladimir Oltean 		dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_MQPRIO,
905cfb45e2SVladimir Oltean 					      &mqprio);
915cfb45e2SVladimir Oltean 		break;
925cfb45e2SVladimir Oltean 	}
935cfb45e2SVladimir Oltean }
945cfb45e2SVladimir Oltean 
mqprio_destroy(struct Qdisc * sch)95b8970f0bSJohn Fastabend static void mqprio_destroy(struct Qdisc *sch)
96b8970f0bSJohn Fastabend {
97b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
98b8970f0bSJohn Fastabend 	struct mqprio_sched *priv = qdisc_priv(sch);
99b8970f0bSJohn Fastabend 	unsigned int ntx;
100b8970f0bSJohn Fastabend 
101ac7100baSBen Hutchings 	if (priv->qdiscs) {
102ac7100baSBen Hutchings 		for (ntx = 0;
103ac7100baSBen Hutchings 		     ntx < dev->num_tx_queues && priv->qdiscs[ntx];
104ac7100baSBen Hutchings 		     ntx++)
10586bd446bSVlad Buslov 			qdisc_put(priv->qdiscs[ntx]);
106ac7100baSBen Hutchings 		kfree(priv->qdiscs);
107ac7100baSBen Hutchings 	}
108b8970f0bSJohn Fastabend 
1095cfb45e2SVladimir Oltean 	if (priv->hw_offload && dev->netdev_ops->ndo_setup_tc)
1105cfb45e2SVladimir Oltean 		mqprio_disable_offload(sch);
1115cfb45e2SVladimir Oltean 	else
112b8970f0bSJohn Fastabend 		netdev_set_num_tc(dev, 0);
113b8970f0bSJohn Fastabend }
114b8970f0bSJohn Fastabend 
mqprio_parse_opt(struct net_device * dev,struct tc_mqprio_qopt * qopt,const struct tc_mqprio_caps * caps,struct netlink_ext_ack * extack)11519278d76SVladimir Oltean static int mqprio_parse_opt(struct net_device *dev, struct tc_mqprio_qopt *qopt,
116d404959fSVladimir Oltean 			    const struct tc_mqprio_caps *caps,
117d404959fSVladimir Oltean 			    struct netlink_ext_ack *extack)
118b8970f0bSJohn Fastabend {
1191dfe086dSVladimir Oltean 	int err;
120b8970f0bSJohn Fastabend 
1212026fecfSAlexander Duyck 	/* Limit qopt->hw to maximum supported offload value.  Drivers have
1222026fecfSAlexander Duyck 	 * the option of overriding this later if they don't support the a
1232026fecfSAlexander Duyck 	 * given offload type.
1242026fecfSAlexander Duyck 	 */
1252026fecfSAlexander Duyck 	if (qopt->hw > TC_MQPRIO_HW_OFFLOAD_MAX)
1262026fecfSAlexander Duyck 		qopt->hw = TC_MQPRIO_HW_OFFLOAD_MAX;
127b8970f0bSJohn Fastabend 
12819278d76SVladimir Oltean 	/* If hardware offload is requested, we will leave 3 options to the
12919278d76SVladimir Oltean 	 * device driver:
13019278d76SVladimir Oltean 	 * - populate the queue counts itself (and ignore what was requested)
13119278d76SVladimir Oltean 	 * - validate the provided queue counts by itself (and apply them)
13219278d76SVladimir Oltean 	 * - request queue count validation here (and apply them)
133b8970f0bSJohn Fastabend 	 */
1341dfe086dSVladimir Oltean 	err = mqprio_validate_qopt(dev, qopt,
1351dfe086dSVladimir Oltean 				   !qopt->hw || caps->validate_queue_counts,
1361dfe086dSVladimir Oltean 				   false, extack);
13719278d76SVladimir Oltean 	if (err)
13819278d76SVladimir Oltean 		return err;
139b8970f0bSJohn Fastabend 
14019278d76SVladimir Oltean 	/* If ndo_setup_tc is not present then hardware doesn't support offload
14119278d76SVladimir Oltean 	 * and we should return an error.
142b8970f0bSJohn Fastabend 	 */
143ab277d20SVladimir Oltean 	if (qopt->hw && !dev->netdev_ops->ndo_setup_tc) {
144ab277d20SVladimir Oltean 		NL_SET_ERR_MSG(extack,
145ab277d20SVladimir Oltean 			       "Device does not support hardware offload");
146b8970f0bSJohn Fastabend 		return -EINVAL;
147ab277d20SVladimir Oltean 	}
148b8970f0bSJohn Fastabend 
149b8970f0bSJohn Fastabend 	return 0;
150b8970f0bSJohn Fastabend }
151b8970f0bSJohn Fastabend 
152f62af20bSVladimir Oltean static const struct
153f62af20bSVladimir Oltean nla_policy mqprio_tc_entry_policy[TCA_MQPRIO_TC_ENTRY_MAX + 1] = {
154f62af20bSVladimir Oltean 	[TCA_MQPRIO_TC_ENTRY_INDEX]	= NLA_POLICY_MAX(NLA_U32,
155f62af20bSVladimir Oltean 							 TC_QOPT_MAX_QUEUE),
156f62af20bSVladimir Oltean 	[TCA_MQPRIO_TC_ENTRY_FP]	= NLA_POLICY_RANGE(NLA_U32,
157f62af20bSVladimir Oltean 							   TC_FP_EXPRESS,
158f62af20bSVladimir Oltean 							   TC_FP_PREEMPTIBLE),
159f62af20bSVladimir Oltean };
160f62af20bSVladimir Oltean 
1614e8b86c0SAmritha Nambiar static const struct nla_policy mqprio_policy[TCA_MQPRIO_MAX + 1] = {
1624e8b86c0SAmritha Nambiar 	[TCA_MQPRIO_MODE]	= { .len = sizeof(u16) },
1634e8b86c0SAmritha Nambiar 	[TCA_MQPRIO_SHAPER]	= { .len = sizeof(u16) },
1644e8b86c0SAmritha Nambiar 	[TCA_MQPRIO_MIN_RATE64]	= { .type = NLA_NESTED },
1654e8b86c0SAmritha Nambiar 	[TCA_MQPRIO_MAX_RATE64]	= { .type = NLA_NESTED },
166f62af20bSVladimir Oltean 	[TCA_MQPRIO_TC_ENTRY]	= { .type = NLA_NESTED },
1674e8b86c0SAmritha Nambiar };
1684e8b86c0SAmritha Nambiar 
mqprio_parse_tc_entry(u32 fp[TC_QOPT_MAX_QUEUE],struct nlattr * opt,unsigned long * seen_tcs,struct netlink_ext_ack * extack)169f62af20bSVladimir Oltean static int mqprio_parse_tc_entry(u32 fp[TC_QOPT_MAX_QUEUE],
170f62af20bSVladimir Oltean 				 struct nlattr *opt,
171f62af20bSVladimir Oltean 				 unsigned long *seen_tcs,
172f62af20bSVladimir Oltean 				 struct netlink_ext_ack *extack)
173f62af20bSVladimir Oltean {
174f62af20bSVladimir Oltean 	struct nlattr *tb[TCA_MQPRIO_TC_ENTRY_MAX + 1];
175f62af20bSVladimir Oltean 	int err, tc;
176f62af20bSVladimir Oltean 
177f62af20bSVladimir Oltean 	err = nla_parse_nested(tb, TCA_MQPRIO_TC_ENTRY_MAX, opt,
178f62af20bSVladimir Oltean 			       mqprio_tc_entry_policy, extack);
179f62af20bSVladimir Oltean 	if (err < 0)
180f62af20bSVladimir Oltean 		return err;
181f62af20bSVladimir Oltean 
182f62af20bSVladimir Oltean 	if (NL_REQ_ATTR_CHECK(extack, opt, tb, TCA_MQPRIO_TC_ENTRY_INDEX)) {
183f62af20bSVladimir Oltean 		NL_SET_ERR_MSG(extack, "TC entry index missing");
184f62af20bSVladimir Oltean 		return -EINVAL;
185f62af20bSVladimir Oltean 	}
186f62af20bSVladimir Oltean 
187f62af20bSVladimir Oltean 	tc = nla_get_u32(tb[TCA_MQPRIO_TC_ENTRY_INDEX]);
188f62af20bSVladimir Oltean 	if (*seen_tcs & BIT(tc)) {
189f62af20bSVladimir Oltean 		NL_SET_ERR_MSG_ATTR(extack, tb[TCA_MQPRIO_TC_ENTRY_INDEX],
190f62af20bSVladimir Oltean 				    "Duplicate tc entry");
191f62af20bSVladimir Oltean 		return -EINVAL;
192f62af20bSVladimir Oltean 	}
193f62af20bSVladimir Oltean 
194f62af20bSVladimir Oltean 	*seen_tcs |= BIT(tc);
195f62af20bSVladimir Oltean 
196f62af20bSVladimir Oltean 	if (tb[TCA_MQPRIO_TC_ENTRY_FP])
197f62af20bSVladimir Oltean 		fp[tc] = nla_get_u32(tb[TCA_MQPRIO_TC_ENTRY_FP]);
198f62af20bSVladimir Oltean 
199f62af20bSVladimir Oltean 	return 0;
200f62af20bSVladimir Oltean }
201f62af20bSVladimir Oltean 
mqprio_parse_tc_entries(struct Qdisc * sch,struct nlattr * nlattr_opt,int nlattr_opt_len,struct netlink_ext_ack * extack)202f62af20bSVladimir Oltean static int mqprio_parse_tc_entries(struct Qdisc *sch, struct nlattr *nlattr_opt,
203f62af20bSVladimir Oltean 				   int nlattr_opt_len,
204f62af20bSVladimir Oltean 				   struct netlink_ext_ack *extack)
205f62af20bSVladimir Oltean {
206f62af20bSVladimir Oltean 	struct mqprio_sched *priv = qdisc_priv(sch);
207f62af20bSVladimir Oltean 	struct net_device *dev = qdisc_dev(sch);
208f62af20bSVladimir Oltean 	bool have_preemption = false;
209f62af20bSVladimir Oltean 	unsigned long seen_tcs = 0;
210f62af20bSVladimir Oltean 	u32 fp[TC_QOPT_MAX_QUEUE];
211f62af20bSVladimir Oltean 	struct nlattr *n;
212f62af20bSVladimir Oltean 	int tc, rem;
213f62af20bSVladimir Oltean 	int err = 0;
214f62af20bSVladimir Oltean 
215f62af20bSVladimir Oltean 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++)
216f62af20bSVladimir Oltean 		fp[tc] = priv->fp[tc];
217f62af20bSVladimir Oltean 
218f62af20bSVladimir Oltean 	nla_for_each_attr(n, nlattr_opt, nlattr_opt_len, rem) {
219f62af20bSVladimir Oltean 		if (nla_type(n) != TCA_MQPRIO_TC_ENTRY)
220f62af20bSVladimir Oltean 			continue;
221f62af20bSVladimir Oltean 
222f62af20bSVladimir Oltean 		err = mqprio_parse_tc_entry(fp, n, &seen_tcs, extack);
223f62af20bSVladimir Oltean 		if (err)
224f62af20bSVladimir Oltean 			goto out;
225f62af20bSVladimir Oltean 	}
226f62af20bSVladimir Oltean 
227f62af20bSVladimir Oltean 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) {
228f62af20bSVladimir Oltean 		priv->fp[tc] = fp[tc];
229f62af20bSVladimir Oltean 		if (fp[tc] == TC_FP_PREEMPTIBLE)
230f62af20bSVladimir Oltean 			have_preemption = true;
231f62af20bSVladimir Oltean 	}
232f62af20bSVladimir Oltean 
233f62af20bSVladimir Oltean 	if (have_preemption && !ethtool_dev_mm_supported(dev)) {
234f62af20bSVladimir Oltean 		NL_SET_ERR_MSG(extack, "Device does not support preemption");
235f62af20bSVladimir Oltean 		return -EOPNOTSUPP;
236f62af20bSVladimir Oltean 	}
237f62af20bSVladimir Oltean out:
238f62af20bSVladimir Oltean 	return err;
239f62af20bSVladimir Oltean }
240f62af20bSVladimir Oltean 
2413dd0c16eSVladimir Oltean /* Parse the other netlink attributes that represent the payload of
2423dd0c16eSVladimir Oltean  * TCA_OPTIONS, which are appended right after struct tc_mqprio_qopt.
2433dd0c16eSVladimir Oltean  */
mqprio_parse_nlattr(struct Qdisc * sch,struct tc_mqprio_qopt * qopt,struct nlattr * opt,struct netlink_ext_ack * extack)244feb2cf3dSVladimir Oltean static int mqprio_parse_nlattr(struct Qdisc *sch, struct tc_mqprio_qopt *qopt,
24557f21bf8SVladimir Oltean 			       struct nlattr *opt,
24657f21bf8SVladimir Oltean 			       struct netlink_ext_ack *extack)
247b8970f0bSJohn Fastabend {
2483dd0c16eSVladimir Oltean 	struct nlattr *nlattr_opt = nla_data(opt) + NLA_ALIGN(sizeof(*qopt));
2493dd0c16eSVladimir Oltean 	int nlattr_opt_len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt));
250b8970f0bSJohn Fastabend 	struct mqprio_sched *priv = qdisc_priv(sch);
2513dd0c16eSVladimir Oltean 	struct nlattr *tb[TCA_MQPRIO_MAX + 1] = {};
2524e8b86c0SAmritha Nambiar 	struct nlattr *attr;
253feb2cf3dSVladimir Oltean 	int i, rem, err;
254b8970f0bSJohn Fastabend 
2553dd0c16eSVladimir Oltean 	if (nlattr_opt_len >= nla_attr_size(0)) {
2563dd0c16eSVladimir Oltean 		err = nla_parse_deprecated(tb, TCA_MQPRIO_MAX, nlattr_opt,
2573dd0c16eSVladimir Oltean 					   nlattr_opt_len, mqprio_policy,
2583dd0c16eSVladimir Oltean 					   NULL);
2594e8b86c0SAmritha Nambiar 		if (err < 0)
2604e8b86c0SAmritha Nambiar 			return err;
2613dd0c16eSVladimir Oltean 	}
2624e8b86c0SAmritha Nambiar 
26357f21bf8SVladimir Oltean 	if (!qopt->hw) {
26457f21bf8SVladimir Oltean 		NL_SET_ERR_MSG(extack,
26557f21bf8SVladimir Oltean 			       "mqprio TCA_OPTIONS can only contain netlink attributes in hardware mode");
2664e8b86c0SAmritha Nambiar 		return -EINVAL;
26757f21bf8SVladimir Oltean 	}
2684e8b86c0SAmritha Nambiar 
2694e8b86c0SAmritha Nambiar 	if (tb[TCA_MQPRIO_MODE]) {
2704e8b86c0SAmritha Nambiar 		priv->flags |= TC_MQPRIO_F_MODE;
2718b0f2565SPedro Tammela 		priv->mode = nla_get_u16(tb[TCA_MQPRIO_MODE]);
2724e8b86c0SAmritha Nambiar 	}
2734e8b86c0SAmritha Nambiar 
2744e8b86c0SAmritha Nambiar 	if (tb[TCA_MQPRIO_SHAPER]) {
2754e8b86c0SAmritha Nambiar 		priv->flags |= TC_MQPRIO_F_SHAPER;
2768b0f2565SPedro Tammela 		priv->shaper = nla_get_u16(tb[TCA_MQPRIO_SHAPER]);
2774e8b86c0SAmritha Nambiar 	}
2784e8b86c0SAmritha Nambiar 
2794e8b86c0SAmritha Nambiar 	if (tb[TCA_MQPRIO_MIN_RATE64]) {
28057f21bf8SVladimir Oltean 		if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) {
28157f21bf8SVladimir Oltean 			NL_SET_ERR_MSG_ATTR(extack, tb[TCA_MQPRIO_MIN_RATE64],
28257f21bf8SVladimir Oltean 					    "min_rate accepted only when shaper is in bw_rlimit mode");
2834e8b86c0SAmritha Nambiar 			return -EINVAL;
28457f21bf8SVladimir Oltean 		}
2854e8b86c0SAmritha Nambiar 		i = 0;
2864e8b86c0SAmritha Nambiar 		nla_for_each_nested(attr, tb[TCA_MQPRIO_MIN_RATE64],
2874e8b86c0SAmritha Nambiar 				    rem) {
28857f21bf8SVladimir Oltean 			if (nla_type(attr) != TCA_MQPRIO_MIN_RATE64) {
28957f21bf8SVladimir Oltean 				NL_SET_ERR_MSG_ATTR(extack, attr,
29057f21bf8SVladimir Oltean 						    "Attribute type expected to be TCA_MQPRIO_MIN_RATE64");
2914e8b86c0SAmritha Nambiar 				return -EINVAL;
29257f21bf8SVladimir Oltean 			}
293*6c58c881SLin Ma 
294*6c58c881SLin Ma 			if (nla_len(attr) != sizeof(u64)) {
295*6c58c881SLin Ma 				NL_SET_ERR_MSG_ATTR(extack, attr,
296*6c58c881SLin Ma 						    "Attribute TCA_MQPRIO_MIN_RATE64 expected to have 8 bytes length");
297*6c58c881SLin Ma 				return -EINVAL;
298*6c58c881SLin Ma 			}
299*6c58c881SLin Ma 
3004e8b86c0SAmritha Nambiar 			if (i >= qopt->num_tc)
3014e8b86c0SAmritha Nambiar 				break;
3028b0f2565SPedro Tammela 			priv->min_rate[i] = nla_get_u64(attr);
3034e8b86c0SAmritha Nambiar 			i++;
3044e8b86c0SAmritha Nambiar 		}
3054e8b86c0SAmritha Nambiar 		priv->flags |= TC_MQPRIO_F_MIN_RATE;
3064e8b86c0SAmritha Nambiar 	}
3074e8b86c0SAmritha Nambiar 
3084e8b86c0SAmritha Nambiar 	if (tb[TCA_MQPRIO_MAX_RATE64]) {
30957f21bf8SVladimir Oltean 		if (priv->shaper != TC_MQPRIO_SHAPER_BW_RATE) {
31057f21bf8SVladimir Oltean 			NL_SET_ERR_MSG_ATTR(extack, tb[TCA_MQPRIO_MAX_RATE64],
31157f21bf8SVladimir Oltean 					    "max_rate accepted only when shaper is in bw_rlimit mode");
3124e8b86c0SAmritha Nambiar 			return -EINVAL;
31357f21bf8SVladimir Oltean 		}
3144e8b86c0SAmritha Nambiar 		i = 0;
3154e8b86c0SAmritha Nambiar 		nla_for_each_nested(attr, tb[TCA_MQPRIO_MAX_RATE64],
3164e8b86c0SAmritha Nambiar 				    rem) {
31757f21bf8SVladimir Oltean 			if (nla_type(attr) != TCA_MQPRIO_MAX_RATE64) {
31857f21bf8SVladimir Oltean 				NL_SET_ERR_MSG_ATTR(extack, attr,
31957f21bf8SVladimir Oltean 						    "Attribute type expected to be TCA_MQPRIO_MAX_RATE64");
3204e8b86c0SAmritha Nambiar 				return -EINVAL;
32157f21bf8SVladimir Oltean 			}
322*6c58c881SLin Ma 
323*6c58c881SLin Ma 			if (nla_len(attr) != sizeof(u64)) {
324*6c58c881SLin Ma 				NL_SET_ERR_MSG_ATTR(extack, attr,
325*6c58c881SLin Ma 						    "Attribute TCA_MQPRIO_MAX_RATE64 expected to have 8 bytes length");
326*6c58c881SLin Ma 				return -EINVAL;
327*6c58c881SLin Ma 			}
328*6c58c881SLin Ma 
3294e8b86c0SAmritha Nambiar 			if (i >= qopt->num_tc)
3304e8b86c0SAmritha Nambiar 				break;
3318b0f2565SPedro Tammela 			priv->max_rate[i] = nla_get_u64(attr);
3324e8b86c0SAmritha Nambiar 			i++;
3334e8b86c0SAmritha Nambiar 		}
3344e8b86c0SAmritha Nambiar 		priv->flags |= TC_MQPRIO_F_MAX_RATE;
3354e8b86c0SAmritha Nambiar 	}
336feb2cf3dSVladimir Oltean 
337f62af20bSVladimir Oltean 	if (tb[TCA_MQPRIO_TC_ENTRY]) {
338f62af20bSVladimir Oltean 		err = mqprio_parse_tc_entries(sch, nlattr_opt, nlattr_opt_len,
339f62af20bSVladimir Oltean 					      extack);
340f62af20bSVladimir Oltean 		if (err)
341f62af20bSVladimir Oltean 			return err;
342f62af20bSVladimir Oltean 	}
343f62af20bSVladimir Oltean 
344feb2cf3dSVladimir Oltean 	return 0;
345feb2cf3dSVladimir Oltean }
346feb2cf3dSVladimir Oltean 
mqprio_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)347feb2cf3dSVladimir Oltean static int mqprio_init(struct Qdisc *sch, struct nlattr *opt,
348feb2cf3dSVladimir Oltean 		       struct netlink_ext_ack *extack)
349feb2cf3dSVladimir Oltean {
350feb2cf3dSVladimir Oltean 	struct net_device *dev = qdisc_dev(sch);
351feb2cf3dSVladimir Oltean 	struct mqprio_sched *priv = qdisc_priv(sch);
352feb2cf3dSVladimir Oltean 	struct netdev_queue *dev_queue;
353feb2cf3dSVladimir Oltean 	struct Qdisc *qdisc;
354feb2cf3dSVladimir Oltean 	int i, err = -EOPNOTSUPP;
355feb2cf3dSVladimir Oltean 	struct tc_mqprio_qopt *qopt = NULL;
35619278d76SVladimir Oltean 	struct tc_mqprio_caps caps;
357f62af20bSVladimir Oltean 	int len, tc;
358feb2cf3dSVladimir Oltean 
359feb2cf3dSVladimir Oltean 	BUILD_BUG_ON(TC_MAX_QUEUE != TC_QOPT_MAX_QUEUE);
360feb2cf3dSVladimir Oltean 	BUILD_BUG_ON(TC_BITMASK != TC_QOPT_BITMASK);
361feb2cf3dSVladimir Oltean 
362feb2cf3dSVladimir Oltean 	if (sch->parent != TC_H_ROOT)
363feb2cf3dSVladimir Oltean 		return -EOPNOTSUPP;
364feb2cf3dSVladimir Oltean 
365feb2cf3dSVladimir Oltean 	if (!netif_is_multiqueue(dev))
366feb2cf3dSVladimir Oltean 		return -EOPNOTSUPP;
367feb2cf3dSVladimir Oltean 
368feb2cf3dSVladimir Oltean 	/* make certain can allocate enough classids to handle queues */
369feb2cf3dSVladimir Oltean 	if (dev->num_tx_queues >= TC_H_MIN_PRIORITY)
370feb2cf3dSVladimir Oltean 		return -ENOMEM;
371feb2cf3dSVladimir Oltean 
372feb2cf3dSVladimir Oltean 	if (!opt || nla_len(opt) < sizeof(*qopt))
373feb2cf3dSVladimir Oltean 		return -EINVAL;
374feb2cf3dSVladimir Oltean 
375f62af20bSVladimir Oltean 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++)
376f62af20bSVladimir Oltean 		priv->fp[tc] = TC_FP_EXPRESS;
377f62af20bSVladimir Oltean 
37819278d76SVladimir Oltean 	qdisc_offload_query_caps(dev, TC_SETUP_QDISC_MQPRIO,
37919278d76SVladimir Oltean 				 &caps, sizeof(caps));
38019278d76SVladimir Oltean 
381feb2cf3dSVladimir Oltean 	qopt = nla_data(opt);
382d404959fSVladimir Oltean 	if (mqprio_parse_opt(dev, qopt, &caps, extack))
383feb2cf3dSVladimir Oltean 		return -EINVAL;
384feb2cf3dSVladimir Oltean 
385feb2cf3dSVladimir Oltean 	len = nla_len(opt) - NLA_ALIGN(sizeof(*qopt));
386feb2cf3dSVladimir Oltean 	if (len > 0) {
38757f21bf8SVladimir Oltean 		err = mqprio_parse_nlattr(sch, qopt, opt, extack);
388feb2cf3dSVladimir Oltean 		if (err)
389feb2cf3dSVladimir Oltean 			return err;
3904e8b86c0SAmritha Nambiar 	}
3914e8b86c0SAmritha Nambiar 
392b8970f0bSJohn Fastabend 	/* pre-allocate qdisc, attachment can't fail */
393b8970f0bSJohn Fastabend 	priv->qdiscs = kcalloc(dev->num_tx_queues, sizeof(priv->qdiscs[0]),
394b8970f0bSJohn Fastabend 			       GFP_KERNEL);
39587b60cfaSEric Dumazet 	if (!priv->qdiscs)
39687b60cfaSEric Dumazet 		return -ENOMEM;
397b8970f0bSJohn Fastabend 
398b8970f0bSJohn Fastabend 	for (i = 0; i < dev->num_tx_queues; i++) {
399b8970f0bSJohn Fastabend 		dev_queue = netdev_get_tx_queue(dev, i);
4001f27cde3SEric Dumazet 		qdisc = qdisc_create_dflt(dev_queue,
4011f27cde3SEric Dumazet 					  get_default_qdisc_ops(dev, i),
402b8970f0bSJohn Fastabend 					  TC_H_MAKE(TC_H_MAJ(sch->handle),
403a38a9882SAlexander Aring 						    TC_H_MIN(i + 1)), extack);
40487b60cfaSEric Dumazet 		if (!qdisc)
40587b60cfaSEric Dumazet 			return -ENOMEM;
40687b60cfaSEric Dumazet 
407b8970f0bSJohn Fastabend 		priv->qdiscs[i] = qdisc;
4084eaf3b84SEric Dumazet 		qdisc->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
409b8970f0bSJohn Fastabend 	}
410b8970f0bSJohn Fastabend 
411b8970f0bSJohn Fastabend 	/* If the mqprio options indicate that hardware should own
412b8970f0bSJohn Fastabend 	 * the queue mapping then run ndo_setup_tc otherwise use the
413b8970f0bSJohn Fastabend 	 * supplied and verified mapping
414b8970f0bSJohn Fastabend 	 */
415b8970f0bSJohn Fastabend 	if (qopt->hw) {
416d404959fSVladimir Oltean 		err = mqprio_enable_offload(sch, qopt, extack);
417b8970f0bSJohn Fastabend 		if (err)
41887b60cfaSEric Dumazet 			return err;
419b8970f0bSJohn Fastabend 	} else {
420b8970f0bSJohn Fastabend 		netdev_set_num_tc(dev, qopt->num_tc);
421b8970f0bSJohn Fastabend 		for (i = 0; i < qopt->num_tc; i++)
422b8970f0bSJohn Fastabend 			netdev_set_tc_queue(dev, i,
423b8970f0bSJohn Fastabend 					    qopt->count[i], qopt->offset[i]);
424b8970f0bSJohn Fastabend 	}
425b8970f0bSJohn Fastabend 
426b8970f0bSJohn Fastabend 	/* Always use supplied priority mappings */
427b8970f0bSJohn Fastabend 	for (i = 0; i < TC_BITMASK + 1; i++)
428b8970f0bSJohn Fastabend 		netdev_set_prio_tc_map(dev, i, qopt->prio_tc_map[i]);
429b8970f0bSJohn Fastabend 
430b8970f0bSJohn Fastabend 	sch->flags |= TCQ_F_MQROOT;
431b8970f0bSJohn Fastabend 	return 0;
432b8970f0bSJohn Fastabend }
433b8970f0bSJohn Fastabend 
mqprio_attach(struct Qdisc * sch)434b8970f0bSJohn Fastabend static void mqprio_attach(struct Qdisc *sch)
435b8970f0bSJohn Fastabend {
436b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
437b8970f0bSJohn Fastabend 	struct mqprio_sched *priv = qdisc_priv(sch);
43895dc1929SEric Dumazet 	struct Qdisc *qdisc, *old;
439b8970f0bSJohn Fastabend 	unsigned int ntx;
440b8970f0bSJohn Fastabend 
441b8970f0bSJohn Fastabend 	/* Attach underlying qdisc */
442b8970f0bSJohn Fastabend 	for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
443b8970f0bSJohn Fastabend 		qdisc = priv->qdiscs[ntx];
44495dc1929SEric Dumazet 		old = dev_graft_qdisc(qdisc->dev_queue, qdisc);
44595dc1929SEric Dumazet 		if (old)
44686bd446bSVlad Buslov 			qdisc_put(old);
44795dc1929SEric Dumazet 		if (ntx < dev->real_num_tx_queues)
44849b49971SJiri Kosina 			qdisc_hash_add(qdisc, false);
449b8970f0bSJohn Fastabend 	}
450b8970f0bSJohn Fastabend 	kfree(priv->qdiscs);
451b8970f0bSJohn Fastabend 	priv->qdiscs = NULL;
452b8970f0bSJohn Fastabend }
453b8970f0bSJohn Fastabend 
mqprio_queue_get(struct Qdisc * sch,unsigned long cl)454b8970f0bSJohn Fastabend static struct netdev_queue *mqprio_queue_get(struct Qdisc *sch,
455b8970f0bSJohn Fastabend 					     unsigned long cl)
456b8970f0bSJohn Fastabend {
457b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
45832302902SAlexander Duyck 	unsigned long ntx = cl - 1;
459b8970f0bSJohn Fastabend 
460b8970f0bSJohn Fastabend 	if (ntx >= dev->num_tx_queues)
461b8970f0bSJohn Fastabend 		return NULL;
462b8970f0bSJohn Fastabend 	return netdev_get_tx_queue(dev, ntx);
463b8970f0bSJohn Fastabend }
464b8970f0bSJohn Fastabend 
mqprio_graft(struct Qdisc * sch,unsigned long cl,struct Qdisc * new,struct Qdisc ** old,struct netlink_ext_ack * extack)465b8970f0bSJohn Fastabend static int mqprio_graft(struct Qdisc *sch, unsigned long cl, struct Qdisc *new,
466653d6fd6SAlexander Aring 			struct Qdisc **old, struct netlink_ext_ack *extack)
467b8970f0bSJohn Fastabend {
468b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
469b8970f0bSJohn Fastabend 	struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
470b8970f0bSJohn Fastabend 
471b8970f0bSJohn Fastabend 	if (!dev_queue)
472b8970f0bSJohn Fastabend 		return -EINVAL;
473b8970f0bSJohn Fastabend 
474b8970f0bSJohn Fastabend 	if (dev->flags & IFF_UP)
475b8970f0bSJohn Fastabend 		dev_deactivate(dev);
476b8970f0bSJohn Fastabend 
477b8970f0bSJohn Fastabend 	*old = dev_graft_qdisc(dev_queue, new);
478b8970f0bSJohn Fastabend 
4791abbe139SEric Dumazet 	if (new)
4804eaf3b84SEric Dumazet 		new->flags |= TCQ_F_ONETXQUEUE | TCQ_F_NOPARENT;
4811abbe139SEric Dumazet 
482b8970f0bSJohn Fastabend 	if (dev->flags & IFF_UP)
483b8970f0bSJohn Fastabend 		dev_activate(dev);
484b8970f0bSJohn Fastabend 
485b8970f0bSJohn Fastabend 	return 0;
486b8970f0bSJohn Fastabend }
487b8970f0bSJohn Fastabend 
dump_rates(struct mqprio_sched * priv,struct tc_mqprio_qopt * opt,struct sk_buff * skb)4884e8b86c0SAmritha Nambiar static int dump_rates(struct mqprio_sched *priv,
4894e8b86c0SAmritha Nambiar 		      struct tc_mqprio_qopt *opt, struct sk_buff *skb)
4904e8b86c0SAmritha Nambiar {
4914e8b86c0SAmritha Nambiar 	struct nlattr *nest;
4924e8b86c0SAmritha Nambiar 	int i;
4934e8b86c0SAmritha Nambiar 
4944e8b86c0SAmritha Nambiar 	if (priv->flags & TC_MQPRIO_F_MIN_RATE) {
495ae0be8deSMichal Kubecek 		nest = nla_nest_start_noflag(skb, TCA_MQPRIO_MIN_RATE64);
4964e8b86c0SAmritha Nambiar 		if (!nest)
4974e8b86c0SAmritha Nambiar 			goto nla_put_failure;
4984e8b86c0SAmritha Nambiar 
4994e8b86c0SAmritha Nambiar 		for (i = 0; i < opt->num_tc; i++) {
5004e8b86c0SAmritha Nambiar 			if (nla_put(skb, TCA_MQPRIO_MIN_RATE64,
5014e8b86c0SAmritha Nambiar 				    sizeof(priv->min_rate[i]),
5024e8b86c0SAmritha Nambiar 				    &priv->min_rate[i]))
5034e8b86c0SAmritha Nambiar 				goto nla_put_failure;
5044e8b86c0SAmritha Nambiar 		}
5054e8b86c0SAmritha Nambiar 		nla_nest_end(skb, nest);
5064e8b86c0SAmritha Nambiar 	}
5074e8b86c0SAmritha Nambiar 
5084e8b86c0SAmritha Nambiar 	if (priv->flags & TC_MQPRIO_F_MAX_RATE) {
509ae0be8deSMichal Kubecek 		nest = nla_nest_start_noflag(skb, TCA_MQPRIO_MAX_RATE64);
5104e8b86c0SAmritha Nambiar 		if (!nest)
5114e8b86c0SAmritha Nambiar 			goto nla_put_failure;
5124e8b86c0SAmritha Nambiar 
5134e8b86c0SAmritha Nambiar 		for (i = 0; i < opt->num_tc; i++) {
5144e8b86c0SAmritha Nambiar 			if (nla_put(skb, TCA_MQPRIO_MAX_RATE64,
5154e8b86c0SAmritha Nambiar 				    sizeof(priv->max_rate[i]),
5164e8b86c0SAmritha Nambiar 				    &priv->max_rate[i]))
5174e8b86c0SAmritha Nambiar 				goto nla_put_failure;
5184e8b86c0SAmritha Nambiar 		}
5194e8b86c0SAmritha Nambiar 		nla_nest_end(skb, nest);
5204e8b86c0SAmritha Nambiar 	}
5214e8b86c0SAmritha Nambiar 	return 0;
5224e8b86c0SAmritha Nambiar 
5234e8b86c0SAmritha Nambiar nla_put_failure:
5244e8b86c0SAmritha Nambiar 	nla_nest_cancel(skb, nest);
5254e8b86c0SAmritha Nambiar 	return -1;
5264e8b86c0SAmritha Nambiar }
5274e8b86c0SAmritha Nambiar 
mqprio_dump_tc_entries(struct mqprio_sched * priv,struct sk_buff * skb)528f62af20bSVladimir Oltean static int mqprio_dump_tc_entries(struct mqprio_sched *priv,
529f62af20bSVladimir Oltean 				  struct sk_buff *skb)
530f62af20bSVladimir Oltean {
531f62af20bSVladimir Oltean 	struct nlattr *n;
532f62af20bSVladimir Oltean 	int tc;
533f62af20bSVladimir Oltean 
534f62af20bSVladimir Oltean 	for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) {
535f62af20bSVladimir Oltean 		n = nla_nest_start(skb, TCA_MQPRIO_TC_ENTRY);
536f62af20bSVladimir Oltean 		if (!n)
537f62af20bSVladimir Oltean 			return -EMSGSIZE;
538f62af20bSVladimir Oltean 
539f62af20bSVladimir Oltean 		if (nla_put_u32(skb, TCA_MQPRIO_TC_ENTRY_INDEX, tc))
540f62af20bSVladimir Oltean 			goto nla_put_failure;
541f62af20bSVladimir Oltean 
542f62af20bSVladimir Oltean 		if (nla_put_u32(skb, TCA_MQPRIO_TC_ENTRY_FP, priv->fp[tc]))
543f62af20bSVladimir Oltean 			goto nla_put_failure;
544f62af20bSVladimir Oltean 
545f62af20bSVladimir Oltean 		nla_nest_end(skb, n);
546f62af20bSVladimir Oltean 	}
547f62af20bSVladimir Oltean 
548f62af20bSVladimir Oltean 	return 0;
549f62af20bSVladimir Oltean 
550f62af20bSVladimir Oltean nla_put_failure:
551f62af20bSVladimir Oltean 	nla_nest_cancel(skb, n);
552f62af20bSVladimir Oltean 	return -EMSGSIZE;
553f62af20bSVladimir Oltean }
554f62af20bSVladimir Oltean 
mqprio_dump(struct Qdisc * sch,struct sk_buff * skb)555b8970f0bSJohn Fastabend static int mqprio_dump(struct Qdisc *sch, struct sk_buff *skb)
556b8970f0bSJohn Fastabend {
557b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
558b8970f0bSJohn Fastabend 	struct mqprio_sched *priv = qdisc_priv(sch);
5594e8b86c0SAmritha Nambiar 	struct nlattr *nla = (struct nlattr *)skb_tail_pointer(skb);
560144ce879SEric Dumazet 	struct tc_mqprio_qopt opt = { 0 };
561b8970f0bSJohn Fastabend 	struct Qdisc *qdisc;
5629dd6ad67SVladimir Oltean 	unsigned int ntx;
563b8970f0bSJohn Fastabend 
564b8970f0bSJohn Fastabend 	sch->q.qlen = 0;
56550dc9a85SAhmed S. Darwish 	gnet_stats_basic_sync_init(&sch->bstats);
566b8970f0bSJohn Fastabend 	memset(&sch->qstats, 0, sizeof(sch->qstats));
567b8970f0bSJohn Fastabend 
568ce679e8dSJohn Fastabend 	/* MQ supports lockless qdiscs. However, statistics accounting needs
569ce679e8dSJohn Fastabend 	 * to account for all, none, or a mix of locked and unlocked child
570ce679e8dSJohn Fastabend 	 * qdiscs. Percpu stats are added to counters in-band and locking
571ce679e8dSJohn Fastabend 	 * qdisc totals are added at end.
572ce679e8dSJohn Fastabend 	 */
573ce679e8dSJohn Fastabend 	for (ntx = 0; ntx < dev->num_tx_queues; ntx++) {
574d636fc5dSEric Dumazet 		qdisc = rtnl_dereference(netdev_get_tx_queue(dev, ntx)->qdisc_sleeping);
575b8970f0bSJohn Fastabend 		spin_lock_bh(qdisc_lock(qdisc));
576ce679e8dSJohn Fastabend 
57729cbcd85SAhmed S. Darwish 		gnet_stats_add_basic(&sch->bstats, qdisc->cpu_bstats,
57829cbcd85SAhmed S. Darwish 				     &qdisc->bstats, false);
5797361df46SSebastian Andrzej Siewior 		gnet_stats_add_queue(&sch->qstats, qdisc->cpu_qstats,
5807361df46SSebastian Andrzej Siewior 				     &qdisc->qstats);
5817361df46SSebastian Andrzej Siewior 		sch->q.qlen += qdisc_qlen(qdisc);
582ce679e8dSJohn Fastabend 
583b8970f0bSJohn Fastabend 		spin_unlock_bh(qdisc_lock(qdisc));
584b8970f0bSJohn Fastabend 	}
585b8970f0bSJohn Fastabend 
5869dd6ad67SVladimir Oltean 	mqprio_qopt_reconstruct(dev, &opt);
5872026fecfSAlexander Duyck 	opt.hw = priv->hw_offload;
588b8970f0bSJohn Fastabend 
5899f104c77SVladyslav Tarasiuk 	if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
5901b34ec43SDavid S. Miller 		goto nla_put_failure;
591b8970f0bSJohn Fastabend 
5924e8b86c0SAmritha Nambiar 	if ((priv->flags & TC_MQPRIO_F_MODE) &&
5934e8b86c0SAmritha Nambiar 	    nla_put_u16(skb, TCA_MQPRIO_MODE, priv->mode))
5944e8b86c0SAmritha Nambiar 		goto nla_put_failure;
5954e8b86c0SAmritha Nambiar 
5964e8b86c0SAmritha Nambiar 	if ((priv->flags & TC_MQPRIO_F_SHAPER) &&
5974e8b86c0SAmritha Nambiar 	    nla_put_u16(skb, TCA_MQPRIO_SHAPER, priv->shaper))
5984e8b86c0SAmritha Nambiar 		goto nla_put_failure;
5994e8b86c0SAmritha Nambiar 
6004e8b86c0SAmritha Nambiar 	if ((priv->flags & TC_MQPRIO_F_MIN_RATE ||
6014e8b86c0SAmritha Nambiar 	     priv->flags & TC_MQPRIO_F_MAX_RATE) &&
6024e8b86c0SAmritha Nambiar 	    (dump_rates(priv, &opt, skb) != 0))
6034e8b86c0SAmritha Nambiar 		goto nla_put_failure;
6044e8b86c0SAmritha Nambiar 
605f62af20bSVladimir Oltean 	if (mqprio_dump_tc_entries(priv, skb))
606f62af20bSVladimir Oltean 		goto nla_put_failure;
607f62af20bSVladimir Oltean 
6084e8b86c0SAmritha Nambiar 	return nla_nest_end(skb, nla);
609b8970f0bSJohn Fastabend nla_put_failure:
6104e8b86c0SAmritha Nambiar 	nlmsg_trim(skb, nla);
611b8970f0bSJohn Fastabend 	return -1;
612b8970f0bSJohn Fastabend }
613b8970f0bSJohn Fastabend 
mqprio_leaf(struct Qdisc * sch,unsigned long cl)614b8970f0bSJohn Fastabend static struct Qdisc *mqprio_leaf(struct Qdisc *sch, unsigned long cl)
615b8970f0bSJohn Fastabend {
616b8970f0bSJohn Fastabend 	struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
617b8970f0bSJohn Fastabend 
618b8970f0bSJohn Fastabend 	if (!dev_queue)
619b8970f0bSJohn Fastabend 		return NULL;
620b8970f0bSJohn Fastabend 
621d636fc5dSEric Dumazet 	return rtnl_dereference(dev_queue->qdisc_sleeping);
622b8970f0bSJohn Fastabend }
623b8970f0bSJohn Fastabend 
mqprio_find(struct Qdisc * sch,u32 classid)624143976ceSWANG Cong static unsigned long mqprio_find(struct Qdisc *sch, u32 classid)
625b8970f0bSJohn Fastabend {
626b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
627b8970f0bSJohn Fastabend 	unsigned int ntx = TC_H_MIN(classid);
628b8970f0bSJohn Fastabend 
62932302902SAlexander Duyck 	/* There are essentially two regions here that have valid classid
63032302902SAlexander Duyck 	 * values. The first region will have a classid value of 1 through
63132302902SAlexander Duyck 	 * num_tx_queues. All of these are backed by actual Qdiscs.
63232302902SAlexander Duyck 	 */
63332302902SAlexander Duyck 	if (ntx < TC_H_MIN_PRIORITY)
63432302902SAlexander Duyck 		return (ntx <= dev->num_tx_queues) ? ntx : 0;
63532302902SAlexander Duyck 
63632302902SAlexander Duyck 	/* The second region represents the hardware traffic classes. These
63732302902SAlexander Duyck 	 * are represented by classid values of TC_H_MIN_PRIORITY through
63832302902SAlexander Duyck 	 * TC_H_MIN_PRIORITY + netdev_get_num_tc - 1
63932302902SAlexander Duyck 	 */
64032302902SAlexander Duyck 	return ((ntx - TC_H_MIN_PRIORITY) < netdev_get_num_tc(dev)) ? ntx : 0;
641b8970f0bSJohn Fastabend }
642b8970f0bSJohn Fastabend 
mqprio_dump_class(struct Qdisc * sch,unsigned long cl,struct sk_buff * skb,struct tcmsg * tcm)643b8970f0bSJohn Fastabend static int mqprio_dump_class(struct Qdisc *sch, unsigned long cl,
644b8970f0bSJohn Fastabend 			 struct sk_buff *skb, struct tcmsg *tcm)
645b8970f0bSJohn Fastabend {
64632302902SAlexander Duyck 	if (cl < TC_H_MIN_PRIORITY) {
64732302902SAlexander Duyck 		struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
648b8970f0bSJohn Fastabend 		struct net_device *dev = qdisc_dev(sch);
64932302902SAlexander Duyck 		int tc = netdev_txq_to_tc(dev, cl - 1);
650b8970f0bSJohn Fastabend 
65132302902SAlexander Duyck 		tcm->tcm_parent = (tc < 0) ? 0 :
65232302902SAlexander Duyck 			TC_H_MAKE(TC_H_MAJ(sch->handle),
65332302902SAlexander Duyck 				  TC_H_MIN(tc + TC_H_MIN_PRIORITY));
654d636fc5dSEric Dumazet 		tcm->tcm_info = rtnl_dereference(dev_queue->qdisc_sleeping)->handle;
65532302902SAlexander Duyck 	} else {
656b8970f0bSJohn Fastabend 		tcm->tcm_parent = TC_H_ROOT;
657b8970f0bSJohn Fastabend 		tcm->tcm_info = 0;
658b8970f0bSJohn Fastabend 	}
659b8970f0bSJohn Fastabend 	tcm->tcm_handle |= TC_H_MIN(cl);
660b8970f0bSJohn Fastabend 	return 0;
661b8970f0bSJohn Fastabend }
662b8970f0bSJohn Fastabend 
mqprio_dump_class_stats(struct Qdisc * sch,unsigned long cl,struct gnet_dump * d)663b8970f0bSJohn Fastabend static int mqprio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
664b8970f0bSJohn Fastabend 				   struct gnet_dump *d)
665ea18fd95Sstephen hemminger 	__releases(d->lock)
666ea18fd95Sstephen hemminger 	__acquires(d->lock)
667b8970f0bSJohn Fastabend {
66832302902SAlexander Duyck 	if (cl >= TC_H_MIN_PRIORITY) {
669b8970f0bSJohn Fastabend 		int i;
6707361df46SSebastian Andrzej Siewior 		__u32 qlen;
671b8970f0bSJohn Fastabend 		struct gnet_stats_queue qstats = {0};
67250dc9a85SAhmed S. Darwish 		struct gnet_stats_basic_sync bstats;
67332302902SAlexander Duyck 		struct net_device *dev = qdisc_dev(sch);
67432302902SAlexander Duyck 		struct netdev_tc_txq tc = dev->tc_to_txq[cl & TC_BITMASK];
675b8970f0bSJohn Fastabend 
67650dc9a85SAhmed S. Darwish 		gnet_stats_basic_sync_init(&bstats);
677b8970f0bSJohn Fastabend 		/* Drop lock here it will be reclaimed before touching
678b8970f0bSJohn Fastabend 		 * statistics this is required because the d->lock we
679b8970f0bSJohn Fastabend 		 * hold here is the look on dev_queue->qdisc_sleeping
680b8970f0bSJohn Fastabend 		 * also acquired below.
681b8970f0bSJohn Fastabend 		 */
682edb09eb1SEric Dumazet 		if (d->lock)
683b8970f0bSJohn Fastabend 			spin_unlock_bh(d->lock);
684b8970f0bSJohn Fastabend 
685b8970f0bSJohn Fastabend 		for (i = tc.offset; i < tc.offset + tc.count; i++) {
68646e5da40SJohn Fastabend 			struct netdev_queue *q = netdev_get_tx_queue(dev, i);
687ce679e8dSJohn Fastabend 			struct Qdisc *qdisc = rtnl_dereference(q->qdisc);
68846e5da40SJohn Fastabend 
689b8970f0bSJohn Fastabend 			spin_lock_bh(qdisc_lock(qdisc));
690ce679e8dSJohn Fastabend 
69129cbcd85SAhmed S. Darwish 			gnet_stats_add_basic(&bstats, qdisc->cpu_bstats,
69229cbcd85SAhmed S. Darwish 					     &qdisc->bstats, false);
6937361df46SSebastian Andrzej Siewior 			gnet_stats_add_queue(&qstats, qdisc->cpu_qstats,
6947361df46SSebastian Andrzej Siewior 					     &qdisc->qstats);
6957361df46SSebastian Andrzej Siewior 			sch->q.qlen += qdisc_qlen(qdisc);
6967361df46SSebastian Andrzej Siewior 
697b8970f0bSJohn Fastabend 			spin_unlock_bh(qdisc_lock(qdisc));
698b8970f0bSJohn Fastabend 		}
6997361df46SSebastian Andrzej Siewior 		qlen = qdisc_qlen(sch) + qstats.qlen;
700ce679e8dSJohn Fastabend 
701b8970f0bSJohn Fastabend 		/* Reclaim root sleeping lock before completing stats */
702edb09eb1SEric Dumazet 		if (d->lock)
703b8970f0bSJohn Fastabend 			spin_lock_bh(d->lock);
70429cbcd85SAhmed S. Darwish 		if (gnet_stats_copy_basic(d, NULL, &bstats, false) < 0 ||
705b0ab6f92SJohn Fastabend 		    gnet_stats_copy_queue(d, NULL, &qstats, qlen) < 0)
706b8970f0bSJohn Fastabend 			return -1;
707b8970f0bSJohn Fastabend 	} else {
708b8970f0bSJohn Fastabend 		struct netdev_queue *dev_queue = mqprio_queue_get(sch, cl);
709b8970f0bSJohn Fastabend 
710d636fc5dSEric Dumazet 		sch = rtnl_dereference(dev_queue->qdisc_sleeping);
71129cbcd85SAhmed S. Darwish 		if (gnet_stats_copy_basic(d, sch->cpu_bstats,
71229cbcd85SAhmed S. Darwish 					  &sch->bstats, true) < 0 ||
7135dd431b6SPaolo Abeni 		    qdisc_qstats_copy(d, sch) < 0)
714b8970f0bSJohn Fastabend 			return -1;
715b8970f0bSJohn Fastabend 	}
716b8970f0bSJohn Fastabend 	return 0;
717b8970f0bSJohn Fastabend }
718b8970f0bSJohn Fastabend 
mqprio_walk(struct Qdisc * sch,struct qdisc_walker * arg)719b8970f0bSJohn Fastabend static void mqprio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
720b8970f0bSJohn Fastabend {
721b8970f0bSJohn Fastabend 	struct net_device *dev = qdisc_dev(sch);
722b8970f0bSJohn Fastabend 	unsigned long ntx;
723b8970f0bSJohn Fastabend 
724b8970f0bSJohn Fastabend 	if (arg->stop)
725b8970f0bSJohn Fastabend 		return;
726b8970f0bSJohn Fastabend 
727b8970f0bSJohn Fastabend 	/* Walk hierarchy with a virtual class per tc */
728b8970f0bSJohn Fastabend 	arg->count = arg->skip;
72932302902SAlexander Duyck 	for (ntx = arg->skip; ntx < netdev_get_num_tc(dev); ntx++) {
730e046fa89SZhengchao Shao 		if (!tc_qdisc_stats_dump(sch, ntx + TC_H_MIN_PRIORITY, arg))
73132302902SAlexander Duyck 			return;
73232302902SAlexander Duyck 	}
73332302902SAlexander Duyck 
73432302902SAlexander Duyck 	/* Pad the values and skip over unused traffic classes */
73532302902SAlexander Duyck 	if (ntx < TC_MAX_QUEUE) {
73632302902SAlexander Duyck 		arg->count = TC_MAX_QUEUE;
73732302902SAlexander Duyck 		ntx = TC_MAX_QUEUE;
73832302902SAlexander Duyck 	}
73932302902SAlexander Duyck 
74032302902SAlexander Duyck 	/* Reset offset, sort out remaining per-queue qdiscs */
74132302902SAlexander Duyck 	for (ntx -= TC_MAX_QUEUE; ntx < dev->num_tx_queues; ntx++) {
742b8970f0bSJohn Fastabend 		if (arg->fn(sch, ntx + 1, arg) < 0) {
743b8970f0bSJohn Fastabend 			arg->stop = 1;
74432302902SAlexander Duyck 			return;
745b8970f0bSJohn Fastabend 		}
746b8970f0bSJohn Fastabend 		arg->count++;
747b8970f0bSJohn Fastabend 	}
748b8970f0bSJohn Fastabend }
749b8970f0bSJohn Fastabend 
mqprio_select_queue(struct Qdisc * sch,struct tcmsg * tcm)7500f7787b4SJesus Sanchez-Palencia static struct netdev_queue *mqprio_select_queue(struct Qdisc *sch,
7510f7787b4SJesus Sanchez-Palencia 						struct tcmsg *tcm)
7520f7787b4SJesus Sanchez-Palencia {
7530f7787b4SJesus Sanchez-Palencia 	return mqprio_queue_get(sch, TC_H_MIN(tcm->tcm_parent));
7540f7787b4SJesus Sanchez-Palencia }
7550f7787b4SJesus Sanchez-Palencia 
756b8970f0bSJohn Fastabend static const struct Qdisc_class_ops mqprio_class_ops = {
757b8970f0bSJohn Fastabend 	.graft		= mqprio_graft,
758b8970f0bSJohn Fastabend 	.leaf		= mqprio_leaf,
759143976ceSWANG Cong 	.find		= mqprio_find,
760b8970f0bSJohn Fastabend 	.walk		= mqprio_walk,
761b8970f0bSJohn Fastabend 	.dump		= mqprio_dump_class,
762b8970f0bSJohn Fastabend 	.dump_stats	= mqprio_dump_class_stats,
7630f7787b4SJesus Sanchez-Palencia 	.select_queue	= mqprio_select_queue,
764b8970f0bSJohn Fastabend };
765b8970f0bSJohn Fastabend 
766ea18fd95Sstephen hemminger static struct Qdisc_ops mqprio_qdisc_ops __read_mostly = {
767b8970f0bSJohn Fastabend 	.cl_ops		= &mqprio_class_ops,
768b8970f0bSJohn Fastabend 	.id		= "mqprio",
769b8970f0bSJohn Fastabend 	.priv_size	= sizeof(struct mqprio_sched),
770b8970f0bSJohn Fastabend 	.init		= mqprio_init,
771b8970f0bSJohn Fastabend 	.destroy	= mqprio_destroy,
772b8970f0bSJohn Fastabend 	.attach		= mqprio_attach,
773f7116fb4SJakub Kicinski 	.change_real_num_tx = mq_change_real_num_tx,
774b8970f0bSJohn Fastabend 	.dump		= mqprio_dump,
775b8970f0bSJohn Fastabend 	.owner		= THIS_MODULE,
776b8970f0bSJohn Fastabend };
777b8970f0bSJohn Fastabend 
mqprio_module_init(void)778b8970f0bSJohn Fastabend static int __init mqprio_module_init(void)
779b8970f0bSJohn Fastabend {
780b8970f0bSJohn Fastabend 	return register_qdisc(&mqprio_qdisc_ops);
781b8970f0bSJohn Fastabend }
782b8970f0bSJohn Fastabend 
mqprio_module_exit(void)783b8970f0bSJohn Fastabend static void __exit mqprio_module_exit(void)
784b8970f0bSJohn Fastabend {
785b8970f0bSJohn Fastabend 	unregister_qdisc(&mqprio_qdisc_ops);
786b8970f0bSJohn Fastabend }
787b8970f0bSJohn Fastabend 
788b8970f0bSJohn Fastabend module_init(mqprio_module_init);
789b8970f0bSJohn Fastabend module_exit(mqprio_module_exit);
790b8970f0bSJohn Fastabend 
791b8970f0bSJohn Fastabend MODULE_LICENSE("GPL");
792