xref: /linux/net/sched/sch_cbs.c (revision 86bd446b5cebd783187ea3772ff258210de77d99)
1585d763aSVinicius Costa Gomes /*
2585d763aSVinicius Costa Gomes  * net/sched/sch_cbs.c	Credit Based Shaper
3585d763aSVinicius Costa Gomes  *
4585d763aSVinicius Costa Gomes  *		This program is free software; you can redistribute it and/or
5585d763aSVinicius Costa Gomes  *		modify it under the terms of the GNU General Public License
6585d763aSVinicius Costa Gomes  *		as published by the Free Software Foundation; either version
7585d763aSVinicius Costa Gomes  *		2 of the License, or (at your option) any later version.
8585d763aSVinicius Costa Gomes  *
9585d763aSVinicius Costa Gomes  * Authors:	Vinicius Costa Gomes <vinicius.gomes@intel.com>
10585d763aSVinicius Costa Gomes  *
11585d763aSVinicius Costa Gomes  */
12585d763aSVinicius Costa Gomes 
13585d763aSVinicius Costa Gomes /* Credit Based Shaper (CBS)
14585d763aSVinicius Costa Gomes  * =========================
15585d763aSVinicius Costa Gomes  *
16585d763aSVinicius Costa Gomes  * This is a simple rate-limiting shaper aimed at TSN applications on
17585d763aSVinicius Costa Gomes  * systems with known traffic workloads.
18585d763aSVinicius Costa Gomes  *
19585d763aSVinicius Costa Gomes  * Its algorithm is defined by the IEEE 802.1Q-2014 Specification,
20585d763aSVinicius Costa Gomes  * Section 8.6.8.2, and explained in more detail in the Annex L of the
21585d763aSVinicius Costa Gomes  * same specification.
22585d763aSVinicius Costa Gomes  *
23585d763aSVinicius Costa Gomes  * There are four tunables to be considered:
24585d763aSVinicius Costa Gomes  *
25585d763aSVinicius Costa Gomes  *	'idleslope': Idleslope is the rate of credits that is
26585d763aSVinicius Costa Gomes  *	accumulated (in kilobits per second) when there is at least
27585d763aSVinicius Costa Gomes  *	one packet waiting for transmission. Packets are transmitted
28585d763aSVinicius Costa Gomes  *	when the current value of credits is equal or greater than
29585d763aSVinicius Costa Gomes  *	zero. When there is no packet to be transmitted the amount of
30585d763aSVinicius Costa Gomes  *	credits is set to zero. This is the main tunable of the CBS
31585d763aSVinicius Costa Gomes  *	algorithm.
32585d763aSVinicius Costa Gomes  *
33585d763aSVinicius Costa Gomes  *	'sendslope':
34585d763aSVinicius Costa Gomes  *	Sendslope is the rate of credits that is depleted (it should be a
35585d763aSVinicius Costa Gomes  *	negative number of kilobits per second) when a transmission is
36585d763aSVinicius Costa Gomes  *	ocurring. It can be calculated as follows, (IEEE 802.1Q-2014 Section
37585d763aSVinicius Costa Gomes  *	8.6.8.2 item g):
38585d763aSVinicius Costa Gomes  *
39585d763aSVinicius Costa Gomes  *	sendslope = idleslope - port_transmit_rate
40585d763aSVinicius Costa Gomes  *
41585d763aSVinicius Costa Gomes  *	'hicredit': Hicredit defines the maximum amount of credits (in
42585d763aSVinicius Costa Gomes  *	bytes) that can be accumulated. Hicredit depends on the
43585d763aSVinicius Costa Gomes  *	characteristics of interfering traffic,
44585d763aSVinicius Costa Gomes  *	'max_interference_size' is the maximum size of any burst of
45585d763aSVinicius Costa Gomes  *	traffic that can delay the transmission of a frame that is
46585d763aSVinicius Costa Gomes  *	available for transmission for this traffic class, (IEEE
47585d763aSVinicius Costa Gomes  *	802.1Q-2014 Annex L, Equation L-3):
48585d763aSVinicius Costa Gomes  *
49585d763aSVinicius Costa Gomes  *	hicredit = max_interference_size * (idleslope / port_transmit_rate)
50585d763aSVinicius Costa Gomes  *
51585d763aSVinicius Costa Gomes  *	'locredit': Locredit is the minimum amount of credits that can
52585d763aSVinicius Costa Gomes  *	be reached. It is a function of the traffic flowing through
53585d763aSVinicius Costa Gomes  *	this qdisc (IEEE 802.1Q-2014 Annex L, Equation L-2):
54585d763aSVinicius Costa Gomes  *
55585d763aSVinicius Costa Gomes  *	locredit = max_frame_size * (sendslope / port_transmit_rate)
56585d763aSVinicius Costa Gomes  */
57585d763aSVinicius Costa Gomes 
58585d763aSVinicius Costa Gomes #include <linux/module.h>
59585d763aSVinicius Costa Gomes #include <linux/types.h>
60585d763aSVinicius Costa Gomes #include <linux/kernel.h>
61585d763aSVinicius Costa Gomes #include <linux/string.h>
62585d763aSVinicius Costa Gomes #include <linux/errno.h>
63585d763aSVinicius Costa Gomes #include <linux/skbuff.h>
64585d763aSVinicius Costa Gomes #include <net/netlink.h>
65585d763aSVinicius Costa Gomes #include <net/sch_generic.h>
66585d763aSVinicius Costa Gomes #include <net/pkt_sched.h>
67585d763aSVinicius Costa Gomes 
68585d763aSVinicius Costa Gomes #define BYTES_PER_KBIT (1000LL / 8)
69585d763aSVinicius Costa Gomes 
70585d763aSVinicius Costa Gomes struct cbs_sched_data {
713d0bd028SVinicius Costa Gomes 	bool offload;
723d0bd028SVinicius Costa Gomes 	int queue;
73585d763aSVinicius Costa Gomes 	s64 port_rate; /* in bytes/s */
74585d763aSVinicius Costa Gomes 	s64 last; /* timestamp in ns */
75585d763aSVinicius Costa Gomes 	s64 credits; /* in bytes */
76585d763aSVinicius Costa Gomes 	s32 locredit; /* in bytes */
77585d763aSVinicius Costa Gomes 	s32 hicredit; /* in bytes */
78585d763aSVinicius Costa Gomes 	s64 sendslope; /* in bytes/s */
79585d763aSVinicius Costa Gomes 	s64 idleslope; /* in bytes/s */
80585d763aSVinicius Costa Gomes 	struct qdisc_watchdog watchdog;
81990e35ecSVinicius Costa Gomes 	int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch,
82990e35ecSVinicius Costa Gomes 		       struct sk_buff **to_free);
83585d763aSVinicius Costa Gomes 	struct sk_buff *(*dequeue)(struct Qdisc *sch);
84990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc;
85585d763aSVinicius Costa Gomes };
86585d763aSVinicius Costa Gomes 
87990e35ecSVinicius Costa Gomes static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch,
88990e35ecSVinicius Costa Gomes 			     struct Qdisc *child,
89990e35ecSVinicius Costa Gomes 			     struct sk_buff **to_free)
903d0bd028SVinicius Costa Gomes {
91990e35ecSVinicius Costa Gomes 	int err;
92990e35ecSVinicius Costa Gomes 
93990e35ecSVinicius Costa Gomes 	err = child->ops->enqueue(skb, child, to_free);
94990e35ecSVinicius Costa Gomes 	if (err != NET_XMIT_SUCCESS)
95990e35ecSVinicius Costa Gomes 		return err;
96990e35ecSVinicius Costa Gomes 
97990e35ecSVinicius Costa Gomes 	qdisc_qstats_backlog_inc(sch, skb);
98990e35ecSVinicius Costa Gomes 	sch->q.qlen++;
99990e35ecSVinicius Costa Gomes 
100990e35ecSVinicius Costa Gomes 	return NET_XMIT_SUCCESS;
1013d0bd028SVinicius Costa Gomes }
1023d0bd028SVinicius Costa Gomes 
103990e35ecSVinicius Costa Gomes static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch,
104990e35ecSVinicius Costa Gomes 			       struct sk_buff **to_free)
105585d763aSVinicius Costa Gomes {
106585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
107990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
108990e35ecSVinicius Costa Gomes 
109990e35ecSVinicius Costa Gomes 	return cbs_child_enqueue(skb, sch, qdisc, to_free);
110990e35ecSVinicius Costa Gomes }
111990e35ecSVinicius Costa Gomes 
112990e35ecSVinicius Costa Gomes static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch,
113990e35ecSVinicius Costa Gomes 			    struct sk_buff **to_free)
114990e35ecSVinicius Costa Gomes {
115990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
116990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
117585d763aSVinicius Costa Gomes 
118585d763aSVinicius Costa Gomes 	if (sch->q.qlen == 0 && q->credits > 0) {
119585d763aSVinicius Costa Gomes 		/* We need to stop accumulating credits when there's
120585d763aSVinicius Costa Gomes 		 * no enqueued packets and q->credits is positive.
121585d763aSVinicius Costa Gomes 		 */
122585d763aSVinicius Costa Gomes 		q->credits = 0;
123585d763aSVinicius Costa Gomes 		q->last = ktime_get_ns();
124585d763aSVinicius Costa Gomes 	}
125585d763aSVinicius Costa Gomes 
126990e35ecSVinicius Costa Gomes 	return cbs_child_enqueue(skb, sch, qdisc, to_free);
127585d763aSVinicius Costa Gomes }
128585d763aSVinicius Costa Gomes 
129585d763aSVinicius Costa Gomes static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
130585d763aSVinicius Costa Gomes 		       struct sk_buff **to_free)
131585d763aSVinicius Costa Gomes {
132585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
133585d763aSVinicius Costa Gomes 
134990e35ecSVinicius Costa Gomes 	return q->enqueue(skb, sch, to_free);
135585d763aSVinicius Costa Gomes }
136585d763aSVinicius Costa Gomes 
137585d763aSVinicius Costa Gomes /* timediff is in ns, slope is in bytes/s */
138585d763aSVinicius Costa Gomes static s64 timediff_to_credits(s64 timediff, s64 slope)
139585d763aSVinicius Costa Gomes {
140585d763aSVinicius Costa Gomes 	return div64_s64(timediff * slope, NSEC_PER_SEC);
141585d763aSVinicius Costa Gomes }
142585d763aSVinicius Costa Gomes 
143585d763aSVinicius Costa Gomes static s64 delay_from_credits(s64 credits, s64 slope)
144585d763aSVinicius Costa Gomes {
145585d763aSVinicius Costa Gomes 	if (unlikely(slope == 0))
146585d763aSVinicius Costa Gomes 		return S64_MAX;
147585d763aSVinicius Costa Gomes 
148585d763aSVinicius Costa Gomes 	return div64_s64(-credits * NSEC_PER_SEC, slope);
149585d763aSVinicius Costa Gomes }
150585d763aSVinicius Costa Gomes 
151585d763aSVinicius Costa Gomes static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
152585d763aSVinicius Costa Gomes {
153585d763aSVinicius Costa Gomes 	if (unlikely(port_rate == 0))
154585d763aSVinicius Costa Gomes 		return S64_MAX;
155585d763aSVinicius Costa Gomes 
156585d763aSVinicius Costa Gomes 	return div64_s64(len * slope, port_rate);
157585d763aSVinicius Costa Gomes }
158585d763aSVinicius Costa Gomes 
159990e35ecSVinicius Costa Gomes static struct sk_buff *cbs_child_dequeue(struct Qdisc *sch, struct Qdisc *child)
160990e35ecSVinicius Costa Gomes {
161990e35ecSVinicius Costa Gomes 	struct sk_buff *skb;
162990e35ecSVinicius Costa Gomes 
163990e35ecSVinicius Costa Gomes 	skb = child->ops->dequeue(child);
164990e35ecSVinicius Costa Gomes 	if (!skb)
165990e35ecSVinicius Costa Gomes 		return NULL;
166990e35ecSVinicius Costa Gomes 
167990e35ecSVinicius Costa Gomes 	qdisc_qstats_backlog_dec(sch, skb);
168990e35ecSVinicius Costa Gomes 	qdisc_bstats_update(sch, skb);
169990e35ecSVinicius Costa Gomes 	sch->q.qlen--;
170990e35ecSVinicius Costa Gomes 
171990e35ecSVinicius Costa Gomes 	return skb;
172990e35ecSVinicius Costa Gomes }
173990e35ecSVinicius Costa Gomes 
174585d763aSVinicius Costa Gomes static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
175585d763aSVinicius Costa Gomes {
176585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
177990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
178585d763aSVinicius Costa Gomes 	s64 now = ktime_get_ns();
179585d763aSVinicius Costa Gomes 	struct sk_buff *skb;
180585d763aSVinicius Costa Gomes 	s64 credits;
181585d763aSVinicius Costa Gomes 	int len;
182585d763aSVinicius Costa Gomes 
183585d763aSVinicius Costa Gomes 	if (q->credits < 0) {
184585d763aSVinicius Costa Gomes 		credits = timediff_to_credits(now - q->last, q->idleslope);
185585d763aSVinicius Costa Gomes 
186585d763aSVinicius Costa Gomes 		credits = q->credits + credits;
187585d763aSVinicius Costa Gomes 		q->credits = min_t(s64, credits, q->hicredit);
188585d763aSVinicius Costa Gomes 
189585d763aSVinicius Costa Gomes 		if (q->credits < 0) {
190585d763aSVinicius Costa Gomes 			s64 delay;
191585d763aSVinicius Costa Gomes 
192585d763aSVinicius Costa Gomes 			delay = delay_from_credits(q->credits, q->idleslope);
193585d763aSVinicius Costa Gomes 			qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
194585d763aSVinicius Costa Gomes 
195585d763aSVinicius Costa Gomes 			q->last = now;
196585d763aSVinicius Costa Gomes 
197585d763aSVinicius Costa Gomes 			return NULL;
198585d763aSVinicius Costa Gomes 		}
199585d763aSVinicius Costa Gomes 	}
200990e35ecSVinicius Costa Gomes 	skb = cbs_child_dequeue(sch, qdisc);
201585d763aSVinicius Costa Gomes 	if (!skb)
202585d763aSVinicius Costa Gomes 		return NULL;
203585d763aSVinicius Costa Gomes 
204585d763aSVinicius Costa Gomes 	len = qdisc_pkt_len(skb);
205585d763aSVinicius Costa Gomes 
206585d763aSVinicius Costa Gomes 	/* As sendslope is a negative number, this will decrease the
207585d763aSVinicius Costa Gomes 	 * amount of q->credits.
208585d763aSVinicius Costa Gomes 	 */
209585d763aSVinicius Costa Gomes 	credits = credits_from_len(len, q->sendslope, q->port_rate);
210585d763aSVinicius Costa Gomes 	credits += q->credits;
211585d763aSVinicius Costa Gomes 
212585d763aSVinicius Costa Gomes 	q->credits = max_t(s64, credits, q->locredit);
213585d763aSVinicius Costa Gomes 	q->last = now;
214585d763aSVinicius Costa Gomes 
215585d763aSVinicius Costa Gomes 	return skb;
216585d763aSVinicius Costa Gomes }
217585d763aSVinicius Costa Gomes 
2183d0bd028SVinicius Costa Gomes static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
2193d0bd028SVinicius Costa Gomes {
220990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
221990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
222990e35ecSVinicius Costa Gomes 
223990e35ecSVinicius Costa Gomes 	return cbs_child_dequeue(sch, qdisc);
2243d0bd028SVinicius Costa Gomes }
2253d0bd028SVinicius Costa Gomes 
226585d763aSVinicius Costa Gomes static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
227585d763aSVinicius Costa Gomes {
228585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
229585d763aSVinicius Costa Gomes 
230585d763aSVinicius Costa Gomes 	return q->dequeue(sch);
231585d763aSVinicius Costa Gomes }
232585d763aSVinicius Costa Gomes 
233585d763aSVinicius Costa Gomes static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
234585d763aSVinicius Costa Gomes 	[TCA_CBS_PARMS]	= { .len = sizeof(struct tc_cbs_qopt) },
235585d763aSVinicius Costa Gomes };
236585d763aSVinicius Costa Gomes 
2373d0bd028SVinicius Costa Gomes static void cbs_disable_offload(struct net_device *dev,
2383d0bd028SVinicius Costa Gomes 				struct cbs_sched_data *q)
2393d0bd028SVinicius Costa Gomes {
2403d0bd028SVinicius Costa Gomes 	struct tc_cbs_qopt_offload cbs = { };
2413d0bd028SVinicius Costa Gomes 	const struct net_device_ops *ops;
2423d0bd028SVinicius Costa Gomes 	int err;
2433d0bd028SVinicius Costa Gomes 
2443d0bd028SVinicius Costa Gomes 	if (!q->offload)
2453d0bd028SVinicius Costa Gomes 		return;
2463d0bd028SVinicius Costa Gomes 
2473d0bd028SVinicius Costa Gomes 	q->enqueue = cbs_enqueue_soft;
2483d0bd028SVinicius Costa Gomes 	q->dequeue = cbs_dequeue_soft;
2493d0bd028SVinicius Costa Gomes 
2503d0bd028SVinicius Costa Gomes 	ops = dev->netdev_ops;
2513d0bd028SVinicius Costa Gomes 	if (!ops->ndo_setup_tc)
2523d0bd028SVinicius Costa Gomes 		return;
2533d0bd028SVinicius Costa Gomes 
2543d0bd028SVinicius Costa Gomes 	cbs.queue = q->queue;
2553d0bd028SVinicius Costa Gomes 	cbs.enable = 0;
2563d0bd028SVinicius Costa Gomes 
2578521db4cSNogah Frankel 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
2583d0bd028SVinicius Costa Gomes 	if (err < 0)
2593d0bd028SVinicius Costa Gomes 		pr_warn("Couldn't disable CBS offload for queue %d\n",
2603d0bd028SVinicius Costa Gomes 			cbs.queue);
2613d0bd028SVinicius Costa Gomes }
2623d0bd028SVinicius Costa Gomes 
2633d0bd028SVinicius Costa Gomes static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
264710fb396SAlexander Aring 			      const struct tc_cbs_qopt *opt,
265710fb396SAlexander Aring 			      struct netlink_ext_ack *extack)
2663d0bd028SVinicius Costa Gomes {
2673d0bd028SVinicius Costa Gomes 	const struct net_device_ops *ops = dev->netdev_ops;
2683d0bd028SVinicius Costa Gomes 	struct tc_cbs_qopt_offload cbs = { };
2693d0bd028SVinicius Costa Gomes 	int err;
2703d0bd028SVinicius Costa Gomes 
271710fb396SAlexander Aring 	if (!ops->ndo_setup_tc) {
272710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
2733d0bd028SVinicius Costa Gomes 		return -EOPNOTSUPP;
274710fb396SAlexander Aring 	}
2753d0bd028SVinicius Costa Gomes 
2763d0bd028SVinicius Costa Gomes 	cbs.queue = q->queue;
2773d0bd028SVinicius Costa Gomes 
2783d0bd028SVinicius Costa Gomes 	cbs.enable = 1;
2793d0bd028SVinicius Costa Gomes 	cbs.hicredit = opt->hicredit;
2803d0bd028SVinicius Costa Gomes 	cbs.locredit = opt->locredit;
2813d0bd028SVinicius Costa Gomes 	cbs.idleslope = opt->idleslope;
2823d0bd028SVinicius Costa Gomes 	cbs.sendslope = opt->sendslope;
2833d0bd028SVinicius Costa Gomes 
2848521db4cSNogah Frankel 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
285710fb396SAlexander Aring 	if (err < 0) {
286710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
2873d0bd028SVinicius Costa Gomes 		return err;
288710fb396SAlexander Aring 	}
2893d0bd028SVinicius Costa Gomes 
2903d0bd028SVinicius Costa Gomes 	q->enqueue = cbs_enqueue_offload;
2913d0bd028SVinicius Costa Gomes 	q->dequeue = cbs_dequeue_offload;
2923d0bd028SVinicius Costa Gomes 
2933d0bd028SVinicius Costa Gomes 	return 0;
2943d0bd028SVinicius Costa Gomes }
2953d0bd028SVinicius Costa Gomes 
2962030721cSAlexander Aring static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
2972030721cSAlexander Aring 		      struct netlink_ext_ack *extack)
298585d763aSVinicius Costa Gomes {
299585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
300585d763aSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
301585d763aSVinicius Costa Gomes 	struct nlattr *tb[TCA_CBS_MAX + 1];
302585d763aSVinicius Costa Gomes 	struct tc_cbs_qopt *qopt;
303585d763aSVinicius Costa Gomes 	int err;
304585d763aSVinicius Costa Gomes 
305710fb396SAlexander Aring 	err = nla_parse_nested(tb, TCA_CBS_MAX, opt, cbs_policy, extack);
306585d763aSVinicius Costa Gomes 	if (err < 0)
307585d763aSVinicius Costa Gomes 		return err;
308585d763aSVinicius Costa Gomes 
309710fb396SAlexander Aring 	if (!tb[TCA_CBS_PARMS]) {
310710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
311585d763aSVinicius Costa Gomes 		return -EINVAL;
312710fb396SAlexander Aring 	}
313585d763aSVinicius Costa Gomes 
314585d763aSVinicius Costa Gomes 	qopt = nla_data(tb[TCA_CBS_PARMS]);
315585d763aSVinicius Costa Gomes 
3163d0bd028SVinicius Costa Gomes 	if (!qopt->offload) {
3173d0bd028SVinicius Costa Gomes 		struct ethtool_link_ksettings ecmd;
3183d0bd028SVinicius Costa Gomes 		s64 link_speed;
319585d763aSVinicius Costa Gomes 
320585d763aSVinicius Costa Gomes 		if (!__ethtool_get_link_ksettings(dev, &ecmd))
321585d763aSVinicius Costa Gomes 			link_speed = ecmd.base.speed;
322585d763aSVinicius Costa Gomes 		else
323585d763aSVinicius Costa Gomes 			link_speed = SPEED_1000;
324585d763aSVinicius Costa Gomes 
325585d763aSVinicius Costa Gomes 		q->port_rate = link_speed * 1000 * BYTES_PER_KBIT;
326585d763aSVinicius Costa Gomes 
3273d0bd028SVinicius Costa Gomes 		cbs_disable_offload(dev, q);
3283d0bd028SVinicius Costa Gomes 	} else {
329710fb396SAlexander Aring 		err = cbs_enable_offload(dev, q, qopt, extack);
3303d0bd028SVinicius Costa Gomes 		if (err < 0)
3313d0bd028SVinicius Costa Gomes 			return err;
3323d0bd028SVinicius Costa Gomes 	}
333585d763aSVinicius Costa Gomes 
3343d0bd028SVinicius Costa Gomes 	/* Everything went OK, save the parameters used. */
335585d763aSVinicius Costa Gomes 	q->hicredit = qopt->hicredit;
336585d763aSVinicius Costa Gomes 	q->locredit = qopt->locredit;
337585d763aSVinicius Costa Gomes 	q->idleslope = qopt->idleslope * BYTES_PER_KBIT;
338585d763aSVinicius Costa Gomes 	q->sendslope = qopt->sendslope * BYTES_PER_KBIT;
3393d0bd028SVinicius Costa Gomes 	q->offload = qopt->offload;
340585d763aSVinicius Costa Gomes 
341585d763aSVinicius Costa Gomes 	return 0;
342585d763aSVinicius Costa Gomes }
343585d763aSVinicius Costa Gomes 
344e63d7dfdSAlexander Aring static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
345e63d7dfdSAlexander Aring 		    struct netlink_ext_ack *extack)
346585d763aSVinicius Costa Gomes {
347585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
3483d0bd028SVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
349585d763aSVinicius Costa Gomes 
350710fb396SAlexander Aring 	if (!opt) {
351710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Missing CBS qdisc options  which are mandatory");
352585d763aSVinicius Costa Gomes 		return -EINVAL;
353710fb396SAlexander Aring 	}
354585d763aSVinicius Costa Gomes 
355990e35ecSVinicius Costa Gomes 	q->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
356990e35ecSVinicius Costa Gomes 				     sch->handle, extack);
357990e35ecSVinicius Costa Gomes 	if (!q->qdisc)
358990e35ecSVinicius Costa Gomes 		return -ENOMEM;
359990e35ecSVinicius Costa Gomes 
360990e35ecSVinicius Costa Gomes 	qdisc_hash_add(q->qdisc, false);
361990e35ecSVinicius Costa Gomes 
3623d0bd028SVinicius Costa Gomes 	q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
3633d0bd028SVinicius Costa Gomes 
3643d0bd028SVinicius Costa Gomes 	q->enqueue = cbs_enqueue_soft;
3653d0bd028SVinicius Costa Gomes 	q->dequeue = cbs_dequeue_soft;
3663d0bd028SVinicius Costa Gomes 
367585d763aSVinicius Costa Gomes 	qdisc_watchdog_init(&q->watchdog, sch);
368585d763aSVinicius Costa Gomes 
3692030721cSAlexander Aring 	return cbs_change(sch, opt, extack);
370585d763aSVinicius Costa Gomes }
371585d763aSVinicius Costa Gomes 
372585d763aSVinicius Costa Gomes static void cbs_destroy(struct Qdisc *sch)
373585d763aSVinicius Costa Gomes {
374585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
3753d0bd028SVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
376585d763aSVinicius Costa Gomes 
377585d763aSVinicius Costa Gomes 	qdisc_watchdog_cancel(&q->watchdog);
3783d0bd028SVinicius Costa Gomes 
3793d0bd028SVinicius Costa Gomes 	cbs_disable_offload(dev, q);
380990e35ecSVinicius Costa Gomes 
381990e35ecSVinicius Costa Gomes 	if (q->qdisc)
382*86bd446bSVlad Buslov 		qdisc_put(q->qdisc);
383585d763aSVinicius Costa Gomes }
384585d763aSVinicius Costa Gomes 
385585d763aSVinicius Costa Gomes static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
386585d763aSVinicius Costa Gomes {
387585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
388585d763aSVinicius Costa Gomes 	struct tc_cbs_qopt opt = { };
389585d763aSVinicius Costa Gomes 	struct nlattr *nest;
390585d763aSVinicius Costa Gomes 
391585d763aSVinicius Costa Gomes 	nest = nla_nest_start(skb, TCA_OPTIONS);
392585d763aSVinicius Costa Gomes 	if (!nest)
393585d763aSVinicius Costa Gomes 		goto nla_put_failure;
394585d763aSVinicius Costa Gomes 
395585d763aSVinicius Costa Gomes 	opt.hicredit = q->hicredit;
396585d763aSVinicius Costa Gomes 	opt.locredit = q->locredit;
397585d763aSVinicius Costa Gomes 	opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT);
398585d763aSVinicius Costa Gomes 	opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT);
3993d0bd028SVinicius Costa Gomes 	opt.offload = q->offload;
400585d763aSVinicius Costa Gomes 
401585d763aSVinicius Costa Gomes 	if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
402585d763aSVinicius Costa Gomes 		goto nla_put_failure;
403585d763aSVinicius Costa Gomes 
404585d763aSVinicius Costa Gomes 	return nla_nest_end(skb, nest);
405585d763aSVinicius Costa Gomes 
406585d763aSVinicius Costa Gomes nla_put_failure:
407585d763aSVinicius Costa Gomes 	nla_nest_cancel(skb, nest);
408585d763aSVinicius Costa Gomes 	return -1;
409585d763aSVinicius Costa Gomes }
410585d763aSVinicius Costa Gomes 
411990e35ecSVinicius Costa Gomes static int cbs_dump_class(struct Qdisc *sch, unsigned long cl,
412990e35ecSVinicius Costa Gomes 			  struct sk_buff *skb, struct tcmsg *tcm)
413990e35ecSVinicius Costa Gomes {
414990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
415990e35ecSVinicius Costa Gomes 
416990e35ecSVinicius Costa Gomes 	if (cl != 1 || !q->qdisc)	/* only one class */
417990e35ecSVinicius Costa Gomes 		return -ENOENT;
418990e35ecSVinicius Costa Gomes 
419990e35ecSVinicius Costa Gomes 	tcm->tcm_handle |= TC_H_MIN(1);
420990e35ecSVinicius Costa Gomes 	tcm->tcm_info = q->qdisc->handle;
421990e35ecSVinicius Costa Gomes 
422990e35ecSVinicius Costa Gomes 	return 0;
423990e35ecSVinicius Costa Gomes }
424990e35ecSVinicius Costa Gomes 
425990e35ecSVinicius Costa Gomes static int cbs_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
426990e35ecSVinicius Costa Gomes 		     struct Qdisc **old, struct netlink_ext_ack *extack)
427990e35ecSVinicius Costa Gomes {
428990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
429990e35ecSVinicius Costa Gomes 
430990e35ecSVinicius Costa Gomes 	if (!new) {
431990e35ecSVinicius Costa Gomes 		new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
432990e35ecSVinicius Costa Gomes 					sch->handle, NULL);
433990e35ecSVinicius Costa Gomes 		if (!new)
434990e35ecSVinicius Costa Gomes 			new = &noop_qdisc;
435990e35ecSVinicius Costa Gomes 	}
436990e35ecSVinicius Costa Gomes 
437990e35ecSVinicius Costa Gomes 	*old = qdisc_replace(sch, new, &q->qdisc);
438990e35ecSVinicius Costa Gomes 	return 0;
439990e35ecSVinicius Costa Gomes }
440990e35ecSVinicius Costa Gomes 
441990e35ecSVinicius Costa Gomes static struct Qdisc *cbs_leaf(struct Qdisc *sch, unsigned long arg)
442990e35ecSVinicius Costa Gomes {
443990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
444990e35ecSVinicius Costa Gomes 
445990e35ecSVinicius Costa Gomes 	return q->qdisc;
446990e35ecSVinicius Costa Gomes }
447990e35ecSVinicius Costa Gomes 
448990e35ecSVinicius Costa Gomes static unsigned long cbs_find(struct Qdisc *sch, u32 classid)
449990e35ecSVinicius Costa Gomes {
450990e35ecSVinicius Costa Gomes 	return 1;
451990e35ecSVinicius Costa Gomes }
452990e35ecSVinicius Costa Gomes 
453990e35ecSVinicius Costa Gomes static void cbs_walk(struct Qdisc *sch, struct qdisc_walker *walker)
454990e35ecSVinicius Costa Gomes {
455990e35ecSVinicius Costa Gomes 	if (!walker->stop) {
456990e35ecSVinicius Costa Gomes 		if (walker->count >= walker->skip) {
457990e35ecSVinicius Costa Gomes 			if (walker->fn(sch, 1, walker) < 0) {
458990e35ecSVinicius Costa Gomes 				walker->stop = 1;
459990e35ecSVinicius Costa Gomes 				return;
460990e35ecSVinicius Costa Gomes 			}
461990e35ecSVinicius Costa Gomes 		}
462990e35ecSVinicius Costa Gomes 		walker->count++;
463990e35ecSVinicius Costa Gomes 	}
464990e35ecSVinicius Costa Gomes }
465990e35ecSVinicius Costa Gomes 
466990e35ecSVinicius Costa Gomes static const struct Qdisc_class_ops cbs_class_ops = {
467990e35ecSVinicius Costa Gomes 	.graft		=	cbs_graft,
468990e35ecSVinicius Costa Gomes 	.leaf		=	cbs_leaf,
469990e35ecSVinicius Costa Gomes 	.find		=	cbs_find,
470990e35ecSVinicius Costa Gomes 	.walk		=	cbs_walk,
471990e35ecSVinicius Costa Gomes 	.dump		=	cbs_dump_class,
472990e35ecSVinicius Costa Gomes };
473990e35ecSVinicius Costa Gomes 
474585d763aSVinicius Costa Gomes static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
475585d763aSVinicius Costa Gomes 	.id		=	"cbs",
476990e35ecSVinicius Costa Gomes 	.cl_ops		=	&cbs_class_ops,
477585d763aSVinicius Costa Gomes 	.priv_size	=	sizeof(struct cbs_sched_data),
478585d763aSVinicius Costa Gomes 	.enqueue	=	cbs_enqueue,
479585d763aSVinicius Costa Gomes 	.dequeue	=	cbs_dequeue,
480585d763aSVinicius Costa Gomes 	.peek		=	qdisc_peek_dequeued,
481585d763aSVinicius Costa Gomes 	.init		=	cbs_init,
482585d763aSVinicius Costa Gomes 	.reset		=	qdisc_reset_queue,
483585d763aSVinicius Costa Gomes 	.destroy	=	cbs_destroy,
484585d763aSVinicius Costa Gomes 	.change		=	cbs_change,
485585d763aSVinicius Costa Gomes 	.dump		=	cbs_dump,
486585d763aSVinicius Costa Gomes 	.owner		=	THIS_MODULE,
487585d763aSVinicius Costa Gomes };
488585d763aSVinicius Costa Gomes 
489585d763aSVinicius Costa Gomes static int __init cbs_module_init(void)
490585d763aSVinicius Costa Gomes {
491585d763aSVinicius Costa Gomes 	return register_qdisc(&cbs_qdisc_ops);
492585d763aSVinicius Costa Gomes }
493585d763aSVinicius Costa Gomes 
494585d763aSVinicius Costa Gomes static void __exit cbs_module_exit(void)
495585d763aSVinicius Costa Gomes {
496585d763aSVinicius Costa Gomes 	unregister_qdisc(&cbs_qdisc_ops);
497585d763aSVinicius Costa Gomes }
498585d763aSVinicius Costa Gomes module_init(cbs_module_init)
499585d763aSVinicius Costa Gomes module_exit(cbs_module_exit)
500585d763aSVinicius Costa Gomes MODULE_LICENSE("GPL");
501