xref: /linux/net/sched/sch_cbs.c (revision ae0be8de9a53cda3505865c11826d8ff0640237c)
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>
64e0a7683dSLeandro Dorileo #include <net/netevent.h>
65585d763aSVinicius Costa Gomes #include <net/netlink.h>
66585d763aSVinicius Costa Gomes #include <net/sch_generic.h>
67585d763aSVinicius Costa Gomes #include <net/pkt_sched.h>
68585d763aSVinicius Costa Gomes 
69e0a7683dSLeandro Dorileo static LIST_HEAD(cbs_list);
70e0a7683dSLeandro Dorileo static DEFINE_SPINLOCK(cbs_list_lock);
71e0a7683dSLeandro Dorileo 
72585d763aSVinicius Costa Gomes #define BYTES_PER_KBIT (1000LL / 8)
73585d763aSVinicius Costa Gomes 
74585d763aSVinicius Costa Gomes struct cbs_sched_data {
753d0bd028SVinicius Costa Gomes 	bool offload;
763d0bd028SVinicius Costa Gomes 	int queue;
77e0a7683dSLeandro Dorileo 	atomic64_t port_rate; /* in bytes/s */
78585d763aSVinicius Costa Gomes 	s64 last; /* timestamp in ns */
79585d763aSVinicius Costa Gomes 	s64 credits; /* in bytes */
80585d763aSVinicius Costa Gomes 	s32 locredit; /* in bytes */
81585d763aSVinicius Costa Gomes 	s32 hicredit; /* in bytes */
82585d763aSVinicius Costa Gomes 	s64 sendslope; /* in bytes/s */
83585d763aSVinicius Costa Gomes 	s64 idleslope; /* in bytes/s */
84585d763aSVinicius Costa Gomes 	struct qdisc_watchdog watchdog;
85990e35ecSVinicius Costa Gomes 	int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch,
86990e35ecSVinicius Costa Gomes 		       struct sk_buff **to_free);
87585d763aSVinicius Costa Gomes 	struct sk_buff *(*dequeue)(struct Qdisc *sch);
88990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc;
89e0a7683dSLeandro Dorileo 	struct list_head cbs_list;
90585d763aSVinicius Costa Gomes };
91585d763aSVinicius Costa Gomes 
92990e35ecSVinicius Costa Gomes static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch,
93990e35ecSVinicius Costa Gomes 			     struct Qdisc *child,
94990e35ecSVinicius Costa Gomes 			     struct sk_buff **to_free)
953d0bd028SVinicius Costa Gomes {
96f6bab199SToke Høiland-Jørgensen 	unsigned int len = qdisc_pkt_len(skb);
97990e35ecSVinicius Costa Gomes 	int err;
98990e35ecSVinicius Costa Gomes 
99990e35ecSVinicius Costa Gomes 	err = child->ops->enqueue(skb, child, to_free);
100990e35ecSVinicius Costa Gomes 	if (err != NET_XMIT_SUCCESS)
101990e35ecSVinicius Costa Gomes 		return err;
102990e35ecSVinicius Costa Gomes 
103f6bab199SToke Høiland-Jørgensen 	sch->qstats.backlog += len;
104990e35ecSVinicius Costa Gomes 	sch->q.qlen++;
105990e35ecSVinicius Costa Gomes 
106990e35ecSVinicius Costa Gomes 	return NET_XMIT_SUCCESS;
1073d0bd028SVinicius Costa Gomes }
1083d0bd028SVinicius Costa Gomes 
109990e35ecSVinicius Costa Gomes static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch,
110990e35ecSVinicius Costa Gomes 			       struct sk_buff **to_free)
111585d763aSVinicius Costa Gomes {
112585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
113990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
114990e35ecSVinicius Costa Gomes 
115990e35ecSVinicius Costa Gomes 	return cbs_child_enqueue(skb, sch, qdisc, to_free);
116990e35ecSVinicius Costa Gomes }
117990e35ecSVinicius Costa Gomes 
118990e35ecSVinicius Costa Gomes static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch,
119990e35ecSVinicius Costa Gomes 			    struct sk_buff **to_free)
120990e35ecSVinicius Costa Gomes {
121990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
122990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
123585d763aSVinicius Costa Gomes 
124585d763aSVinicius Costa Gomes 	if (sch->q.qlen == 0 && q->credits > 0) {
125585d763aSVinicius Costa Gomes 		/* We need to stop accumulating credits when there's
126585d763aSVinicius Costa Gomes 		 * no enqueued packets and q->credits is positive.
127585d763aSVinicius Costa Gomes 		 */
128585d763aSVinicius Costa Gomes 		q->credits = 0;
129585d763aSVinicius Costa Gomes 		q->last = ktime_get_ns();
130585d763aSVinicius Costa Gomes 	}
131585d763aSVinicius Costa Gomes 
132990e35ecSVinicius Costa Gomes 	return cbs_child_enqueue(skb, sch, qdisc, to_free);
133585d763aSVinicius Costa Gomes }
134585d763aSVinicius Costa Gomes 
135585d763aSVinicius Costa Gomes static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
136585d763aSVinicius Costa Gomes 		       struct sk_buff **to_free)
137585d763aSVinicius Costa Gomes {
138585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
139585d763aSVinicius Costa Gomes 
140990e35ecSVinicius Costa Gomes 	return q->enqueue(skb, sch, to_free);
141585d763aSVinicius Costa Gomes }
142585d763aSVinicius Costa Gomes 
143585d763aSVinicius Costa Gomes /* timediff is in ns, slope is in bytes/s */
144585d763aSVinicius Costa Gomes static s64 timediff_to_credits(s64 timediff, s64 slope)
145585d763aSVinicius Costa Gomes {
146585d763aSVinicius Costa Gomes 	return div64_s64(timediff * slope, NSEC_PER_SEC);
147585d763aSVinicius Costa Gomes }
148585d763aSVinicius Costa Gomes 
149585d763aSVinicius Costa Gomes static s64 delay_from_credits(s64 credits, s64 slope)
150585d763aSVinicius Costa Gomes {
151585d763aSVinicius Costa Gomes 	if (unlikely(slope == 0))
152585d763aSVinicius Costa Gomes 		return S64_MAX;
153585d763aSVinicius Costa Gomes 
154585d763aSVinicius Costa Gomes 	return div64_s64(-credits * NSEC_PER_SEC, slope);
155585d763aSVinicius Costa Gomes }
156585d763aSVinicius Costa Gomes 
157585d763aSVinicius Costa Gomes static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
158585d763aSVinicius Costa Gomes {
159585d763aSVinicius Costa Gomes 	if (unlikely(port_rate == 0))
160585d763aSVinicius Costa Gomes 		return S64_MAX;
161585d763aSVinicius Costa Gomes 
162585d763aSVinicius Costa Gomes 	return div64_s64(len * slope, port_rate);
163585d763aSVinicius Costa Gomes }
164585d763aSVinicius Costa Gomes 
165990e35ecSVinicius Costa Gomes static struct sk_buff *cbs_child_dequeue(struct Qdisc *sch, struct Qdisc *child)
166990e35ecSVinicius Costa Gomes {
167990e35ecSVinicius Costa Gomes 	struct sk_buff *skb;
168990e35ecSVinicius Costa Gomes 
169990e35ecSVinicius Costa Gomes 	skb = child->ops->dequeue(child);
170990e35ecSVinicius Costa Gomes 	if (!skb)
171990e35ecSVinicius Costa Gomes 		return NULL;
172990e35ecSVinicius Costa Gomes 
173990e35ecSVinicius Costa Gomes 	qdisc_qstats_backlog_dec(sch, skb);
174990e35ecSVinicius Costa Gomes 	qdisc_bstats_update(sch, skb);
175990e35ecSVinicius Costa Gomes 	sch->q.qlen--;
176990e35ecSVinicius Costa Gomes 
177990e35ecSVinicius Costa Gomes 	return skb;
178990e35ecSVinicius Costa Gomes }
179990e35ecSVinicius Costa Gomes 
180585d763aSVinicius Costa Gomes static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
181585d763aSVinicius Costa Gomes {
182585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
183990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
184585d763aSVinicius Costa Gomes 	s64 now = ktime_get_ns();
185585d763aSVinicius Costa Gomes 	struct sk_buff *skb;
186585d763aSVinicius Costa Gomes 	s64 credits;
187585d763aSVinicius Costa Gomes 	int len;
188585d763aSVinicius Costa Gomes 
189e0a7683dSLeandro Dorileo 	if (atomic64_read(&q->port_rate) == -1) {
190e0a7683dSLeandro Dorileo 		WARN_ONCE(1, "cbs: dequeue() called with unknown port rate.");
191e0a7683dSLeandro Dorileo 		return NULL;
192e0a7683dSLeandro Dorileo 	}
193e0a7683dSLeandro Dorileo 
194585d763aSVinicius Costa Gomes 	if (q->credits < 0) {
195585d763aSVinicius Costa Gomes 		credits = timediff_to_credits(now - q->last, q->idleslope);
196585d763aSVinicius Costa Gomes 
197585d763aSVinicius Costa Gomes 		credits = q->credits + credits;
198585d763aSVinicius Costa Gomes 		q->credits = min_t(s64, credits, q->hicredit);
199585d763aSVinicius Costa Gomes 
200585d763aSVinicius Costa Gomes 		if (q->credits < 0) {
201585d763aSVinicius Costa Gomes 			s64 delay;
202585d763aSVinicius Costa Gomes 
203585d763aSVinicius Costa Gomes 			delay = delay_from_credits(q->credits, q->idleslope);
204585d763aSVinicius Costa Gomes 			qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
205585d763aSVinicius Costa Gomes 
206585d763aSVinicius Costa Gomes 			q->last = now;
207585d763aSVinicius Costa Gomes 
208585d763aSVinicius Costa Gomes 			return NULL;
209585d763aSVinicius Costa Gomes 		}
210585d763aSVinicius Costa Gomes 	}
211990e35ecSVinicius Costa Gomes 	skb = cbs_child_dequeue(sch, qdisc);
212585d763aSVinicius Costa Gomes 	if (!skb)
213585d763aSVinicius Costa Gomes 		return NULL;
214585d763aSVinicius Costa Gomes 
215585d763aSVinicius Costa Gomes 	len = qdisc_pkt_len(skb);
216585d763aSVinicius Costa Gomes 
217585d763aSVinicius Costa Gomes 	/* As sendslope is a negative number, this will decrease the
218585d763aSVinicius Costa Gomes 	 * amount of q->credits.
219585d763aSVinicius Costa Gomes 	 */
220e0a7683dSLeandro Dorileo 	credits = credits_from_len(len, q->sendslope,
221e0a7683dSLeandro Dorileo 				   atomic64_read(&q->port_rate));
222585d763aSVinicius Costa Gomes 	credits += q->credits;
223585d763aSVinicius Costa Gomes 
224585d763aSVinicius Costa Gomes 	q->credits = max_t(s64, credits, q->locredit);
225585d763aSVinicius Costa Gomes 	q->last = now;
226585d763aSVinicius Costa Gomes 
227585d763aSVinicius Costa Gomes 	return skb;
228585d763aSVinicius Costa Gomes }
229585d763aSVinicius Costa Gomes 
2303d0bd028SVinicius Costa Gomes static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
2313d0bd028SVinicius Costa Gomes {
232990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
233990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
234990e35ecSVinicius Costa Gomes 
235990e35ecSVinicius Costa Gomes 	return cbs_child_dequeue(sch, qdisc);
2363d0bd028SVinicius Costa Gomes }
2373d0bd028SVinicius Costa Gomes 
238585d763aSVinicius Costa Gomes static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
239585d763aSVinicius Costa Gomes {
240585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
241585d763aSVinicius Costa Gomes 
242585d763aSVinicius Costa Gomes 	return q->dequeue(sch);
243585d763aSVinicius Costa Gomes }
244585d763aSVinicius Costa Gomes 
245585d763aSVinicius Costa Gomes static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
246585d763aSVinicius Costa Gomes 	[TCA_CBS_PARMS]	= { .len = sizeof(struct tc_cbs_qopt) },
247585d763aSVinicius Costa Gomes };
248585d763aSVinicius Costa Gomes 
2493d0bd028SVinicius Costa Gomes static void cbs_disable_offload(struct net_device *dev,
2503d0bd028SVinicius Costa Gomes 				struct cbs_sched_data *q)
2513d0bd028SVinicius Costa Gomes {
2523d0bd028SVinicius Costa Gomes 	struct tc_cbs_qopt_offload cbs = { };
2533d0bd028SVinicius Costa Gomes 	const struct net_device_ops *ops;
2543d0bd028SVinicius Costa Gomes 	int err;
2553d0bd028SVinicius Costa Gomes 
2563d0bd028SVinicius Costa Gomes 	if (!q->offload)
2573d0bd028SVinicius Costa Gomes 		return;
2583d0bd028SVinicius Costa Gomes 
2593d0bd028SVinicius Costa Gomes 	q->enqueue = cbs_enqueue_soft;
2603d0bd028SVinicius Costa Gomes 	q->dequeue = cbs_dequeue_soft;
2613d0bd028SVinicius Costa Gomes 
2623d0bd028SVinicius Costa Gomes 	ops = dev->netdev_ops;
2633d0bd028SVinicius Costa Gomes 	if (!ops->ndo_setup_tc)
2643d0bd028SVinicius Costa Gomes 		return;
2653d0bd028SVinicius Costa Gomes 
2663d0bd028SVinicius Costa Gomes 	cbs.queue = q->queue;
2673d0bd028SVinicius Costa Gomes 	cbs.enable = 0;
2683d0bd028SVinicius Costa Gomes 
2698521db4cSNogah Frankel 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
2703d0bd028SVinicius Costa Gomes 	if (err < 0)
2713d0bd028SVinicius Costa Gomes 		pr_warn("Couldn't disable CBS offload for queue %d\n",
2723d0bd028SVinicius Costa Gomes 			cbs.queue);
2733d0bd028SVinicius Costa Gomes }
2743d0bd028SVinicius Costa Gomes 
2753d0bd028SVinicius Costa Gomes static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
276710fb396SAlexander Aring 			      const struct tc_cbs_qopt *opt,
277710fb396SAlexander Aring 			      struct netlink_ext_ack *extack)
2783d0bd028SVinicius Costa Gomes {
2793d0bd028SVinicius Costa Gomes 	const struct net_device_ops *ops = dev->netdev_ops;
2803d0bd028SVinicius Costa Gomes 	struct tc_cbs_qopt_offload cbs = { };
2813d0bd028SVinicius Costa Gomes 	int err;
2823d0bd028SVinicius Costa Gomes 
283710fb396SAlexander Aring 	if (!ops->ndo_setup_tc) {
284710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
2853d0bd028SVinicius Costa Gomes 		return -EOPNOTSUPP;
286710fb396SAlexander Aring 	}
2873d0bd028SVinicius Costa Gomes 
2883d0bd028SVinicius Costa Gomes 	cbs.queue = q->queue;
2893d0bd028SVinicius Costa Gomes 
2903d0bd028SVinicius Costa Gomes 	cbs.enable = 1;
2913d0bd028SVinicius Costa Gomes 	cbs.hicredit = opt->hicredit;
2923d0bd028SVinicius Costa Gomes 	cbs.locredit = opt->locredit;
2933d0bd028SVinicius Costa Gomes 	cbs.idleslope = opt->idleslope;
2943d0bd028SVinicius Costa Gomes 	cbs.sendslope = opt->sendslope;
2953d0bd028SVinicius Costa Gomes 
2968521db4cSNogah Frankel 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
297710fb396SAlexander Aring 	if (err < 0) {
298710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
2993d0bd028SVinicius Costa Gomes 		return err;
300710fb396SAlexander Aring 	}
3013d0bd028SVinicius Costa Gomes 
3023d0bd028SVinicius Costa Gomes 	q->enqueue = cbs_enqueue_offload;
3033d0bd028SVinicius Costa Gomes 	q->dequeue = cbs_dequeue_offload;
3043d0bd028SVinicius Costa Gomes 
3053d0bd028SVinicius Costa Gomes 	return 0;
3063d0bd028SVinicius Costa Gomes }
3073d0bd028SVinicius Costa Gomes 
308e0a7683dSLeandro Dorileo static void cbs_set_port_rate(struct net_device *dev, struct cbs_sched_data *q)
309e0a7683dSLeandro Dorileo {
310e0a7683dSLeandro Dorileo 	struct ethtool_link_ksettings ecmd;
311e0a7683dSLeandro Dorileo 	int port_rate = -1;
312e0a7683dSLeandro Dorileo 
313e0a7683dSLeandro Dorileo 	if (!__ethtool_get_link_ksettings(dev, &ecmd) &&
314e0a7683dSLeandro Dorileo 	    ecmd.base.speed != SPEED_UNKNOWN)
315e0a7683dSLeandro Dorileo 		port_rate = ecmd.base.speed * 1000 * BYTES_PER_KBIT;
316e0a7683dSLeandro Dorileo 
317e0a7683dSLeandro Dorileo 	atomic64_set(&q->port_rate, port_rate);
318e0a7683dSLeandro Dorileo 	netdev_dbg(dev, "cbs: set %s's port_rate to: %lld, linkspeed: %d\n",
319e0a7683dSLeandro Dorileo 		   dev->name, (long long)atomic64_read(&q->port_rate),
320e0a7683dSLeandro Dorileo 		   ecmd.base.speed);
321e0a7683dSLeandro Dorileo }
322e0a7683dSLeandro Dorileo 
323e0a7683dSLeandro Dorileo static int cbs_dev_notifier(struct notifier_block *nb, unsigned long event,
324e0a7683dSLeandro Dorileo 			    void *ptr)
325e0a7683dSLeandro Dorileo {
326e0a7683dSLeandro Dorileo 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
327e0a7683dSLeandro Dorileo 	struct cbs_sched_data *q;
328e0a7683dSLeandro Dorileo 	struct net_device *qdev;
329e0a7683dSLeandro Dorileo 	bool found = false;
330e0a7683dSLeandro Dorileo 
331e0a7683dSLeandro Dorileo 	ASSERT_RTNL();
332e0a7683dSLeandro Dorileo 
333e0a7683dSLeandro Dorileo 	if (event != NETDEV_UP && event != NETDEV_CHANGE)
334e0a7683dSLeandro Dorileo 		return NOTIFY_DONE;
335e0a7683dSLeandro Dorileo 
336e0a7683dSLeandro Dorileo 	spin_lock(&cbs_list_lock);
337e0a7683dSLeandro Dorileo 	list_for_each_entry(q, &cbs_list, cbs_list) {
338e0a7683dSLeandro Dorileo 		qdev = qdisc_dev(q->qdisc);
339e0a7683dSLeandro Dorileo 		if (qdev == dev) {
340e0a7683dSLeandro Dorileo 			found = true;
341e0a7683dSLeandro Dorileo 			break;
342e0a7683dSLeandro Dorileo 		}
343e0a7683dSLeandro Dorileo 	}
344e0a7683dSLeandro Dorileo 	spin_unlock(&cbs_list_lock);
345e0a7683dSLeandro Dorileo 
346e0a7683dSLeandro Dorileo 	if (found)
347e0a7683dSLeandro Dorileo 		cbs_set_port_rate(dev, q);
348e0a7683dSLeandro Dorileo 
349e0a7683dSLeandro Dorileo 	return NOTIFY_DONE;
350e0a7683dSLeandro Dorileo }
351e0a7683dSLeandro Dorileo 
3522030721cSAlexander Aring static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
3532030721cSAlexander Aring 		      struct netlink_ext_ack *extack)
354585d763aSVinicius Costa Gomes {
355585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
356585d763aSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
357585d763aSVinicius Costa Gomes 	struct nlattr *tb[TCA_CBS_MAX + 1];
358585d763aSVinicius Costa Gomes 	struct tc_cbs_qopt *qopt;
359585d763aSVinicius Costa Gomes 	int err;
360585d763aSVinicius Costa Gomes 
361710fb396SAlexander Aring 	err = nla_parse_nested(tb, TCA_CBS_MAX, opt, cbs_policy, extack);
362585d763aSVinicius Costa Gomes 	if (err < 0)
363585d763aSVinicius Costa Gomes 		return err;
364585d763aSVinicius Costa Gomes 
365710fb396SAlexander Aring 	if (!tb[TCA_CBS_PARMS]) {
366710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
367585d763aSVinicius Costa Gomes 		return -EINVAL;
368710fb396SAlexander Aring 	}
369585d763aSVinicius Costa Gomes 
370585d763aSVinicius Costa Gomes 	qopt = nla_data(tb[TCA_CBS_PARMS]);
371585d763aSVinicius Costa Gomes 
3723d0bd028SVinicius Costa Gomes 	if (!qopt->offload) {
373e0a7683dSLeandro Dorileo 		cbs_set_port_rate(dev, q);
3743d0bd028SVinicius Costa Gomes 		cbs_disable_offload(dev, q);
3753d0bd028SVinicius Costa Gomes 	} else {
376710fb396SAlexander Aring 		err = cbs_enable_offload(dev, q, qopt, extack);
3773d0bd028SVinicius Costa Gomes 		if (err < 0)
3783d0bd028SVinicius Costa Gomes 			return err;
3793d0bd028SVinicius Costa Gomes 	}
380585d763aSVinicius Costa Gomes 
3813d0bd028SVinicius Costa Gomes 	/* Everything went OK, save the parameters used. */
382585d763aSVinicius Costa Gomes 	q->hicredit = qopt->hicredit;
383585d763aSVinicius Costa Gomes 	q->locredit = qopt->locredit;
384585d763aSVinicius Costa Gomes 	q->idleslope = qopt->idleslope * BYTES_PER_KBIT;
385585d763aSVinicius Costa Gomes 	q->sendslope = qopt->sendslope * BYTES_PER_KBIT;
3863d0bd028SVinicius Costa Gomes 	q->offload = qopt->offload;
387585d763aSVinicius Costa Gomes 
388585d763aSVinicius Costa Gomes 	return 0;
389585d763aSVinicius Costa Gomes }
390585d763aSVinicius Costa Gomes 
391e63d7dfdSAlexander Aring static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
392e63d7dfdSAlexander Aring 		    struct netlink_ext_ack *extack)
393585d763aSVinicius Costa Gomes {
394585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
3953d0bd028SVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
396e0a7683dSLeandro Dorileo 	int err;
397585d763aSVinicius Costa Gomes 
398710fb396SAlexander Aring 	if (!opt) {
399710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Missing CBS qdisc options  which are mandatory");
400585d763aSVinicius Costa Gomes 		return -EINVAL;
401710fb396SAlexander Aring 	}
402585d763aSVinicius Costa Gomes 
403990e35ecSVinicius Costa Gomes 	q->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
404990e35ecSVinicius Costa Gomes 				     sch->handle, extack);
405990e35ecSVinicius Costa Gomes 	if (!q->qdisc)
406990e35ecSVinicius Costa Gomes 		return -ENOMEM;
407990e35ecSVinicius Costa Gomes 
408990e35ecSVinicius Costa Gomes 	qdisc_hash_add(q->qdisc, false);
409990e35ecSVinicius Costa Gomes 
4103d0bd028SVinicius Costa Gomes 	q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
4113d0bd028SVinicius Costa Gomes 
4123d0bd028SVinicius Costa Gomes 	q->enqueue = cbs_enqueue_soft;
4133d0bd028SVinicius Costa Gomes 	q->dequeue = cbs_dequeue_soft;
4143d0bd028SVinicius Costa Gomes 
415585d763aSVinicius Costa Gomes 	qdisc_watchdog_init(&q->watchdog, sch);
416585d763aSVinicius Costa Gomes 
417e0a7683dSLeandro Dorileo 	err = cbs_change(sch, opt, extack);
418e0a7683dSLeandro Dorileo 	if (err)
419e0a7683dSLeandro Dorileo 		return err;
420e0a7683dSLeandro Dorileo 
421e0a7683dSLeandro Dorileo 	if (!q->offload) {
422e0a7683dSLeandro Dorileo 		spin_lock(&cbs_list_lock);
423e0a7683dSLeandro Dorileo 		list_add(&q->cbs_list, &cbs_list);
424e0a7683dSLeandro Dorileo 		spin_unlock(&cbs_list_lock);
425e0a7683dSLeandro Dorileo 	}
426e0a7683dSLeandro Dorileo 
427e0a7683dSLeandro Dorileo 	return 0;
428585d763aSVinicius Costa Gomes }
429585d763aSVinicius Costa Gomes 
430585d763aSVinicius Costa Gomes static void cbs_destroy(struct Qdisc *sch)
431585d763aSVinicius Costa Gomes {
432585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
4333d0bd028SVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
434585d763aSVinicius Costa Gomes 
435e0a7683dSLeandro Dorileo 	spin_lock(&cbs_list_lock);
436e0a7683dSLeandro Dorileo 	list_del(&q->cbs_list);
437e0a7683dSLeandro Dorileo 	spin_unlock(&cbs_list_lock);
4383d0bd028SVinicius Costa Gomes 
439e0a7683dSLeandro Dorileo 	qdisc_watchdog_cancel(&q->watchdog);
4403d0bd028SVinicius Costa Gomes 	cbs_disable_offload(dev, q);
441990e35ecSVinicius Costa Gomes 
442990e35ecSVinicius Costa Gomes 	if (q->qdisc)
44386bd446bSVlad Buslov 		qdisc_put(q->qdisc);
444585d763aSVinicius Costa Gomes }
445585d763aSVinicius Costa Gomes 
446585d763aSVinicius Costa Gomes static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
447585d763aSVinicius Costa Gomes {
448585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
449585d763aSVinicius Costa Gomes 	struct tc_cbs_qopt opt = { };
450585d763aSVinicius Costa Gomes 	struct nlattr *nest;
451585d763aSVinicius Costa Gomes 
452*ae0be8deSMichal Kubecek 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
453585d763aSVinicius Costa Gomes 	if (!nest)
454585d763aSVinicius Costa Gomes 		goto nla_put_failure;
455585d763aSVinicius Costa Gomes 
456585d763aSVinicius Costa Gomes 	opt.hicredit = q->hicredit;
457585d763aSVinicius Costa Gomes 	opt.locredit = q->locredit;
458585d763aSVinicius Costa Gomes 	opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT);
459585d763aSVinicius Costa Gomes 	opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT);
4603d0bd028SVinicius Costa Gomes 	opt.offload = q->offload;
461585d763aSVinicius Costa Gomes 
462585d763aSVinicius Costa Gomes 	if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
463585d763aSVinicius Costa Gomes 		goto nla_put_failure;
464585d763aSVinicius Costa Gomes 
465585d763aSVinicius Costa Gomes 	return nla_nest_end(skb, nest);
466585d763aSVinicius Costa Gomes 
467585d763aSVinicius Costa Gomes nla_put_failure:
468585d763aSVinicius Costa Gomes 	nla_nest_cancel(skb, nest);
469585d763aSVinicius Costa Gomes 	return -1;
470585d763aSVinicius Costa Gomes }
471585d763aSVinicius Costa Gomes 
472990e35ecSVinicius Costa Gomes static int cbs_dump_class(struct Qdisc *sch, unsigned long cl,
473990e35ecSVinicius Costa Gomes 			  struct sk_buff *skb, struct tcmsg *tcm)
474990e35ecSVinicius Costa Gomes {
475990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
476990e35ecSVinicius Costa Gomes 
477990e35ecSVinicius Costa Gomes 	if (cl != 1 || !q->qdisc)	/* only one class */
478990e35ecSVinicius Costa Gomes 		return -ENOENT;
479990e35ecSVinicius Costa Gomes 
480990e35ecSVinicius Costa Gomes 	tcm->tcm_handle |= TC_H_MIN(1);
481990e35ecSVinicius Costa Gomes 	tcm->tcm_info = q->qdisc->handle;
482990e35ecSVinicius Costa Gomes 
483990e35ecSVinicius Costa Gomes 	return 0;
484990e35ecSVinicius Costa Gomes }
485990e35ecSVinicius Costa Gomes 
486990e35ecSVinicius Costa Gomes static int cbs_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
487990e35ecSVinicius Costa Gomes 		     struct Qdisc **old, struct netlink_ext_ack *extack)
488990e35ecSVinicius Costa Gomes {
489990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
490990e35ecSVinicius Costa Gomes 
491990e35ecSVinicius Costa Gomes 	if (!new) {
492990e35ecSVinicius Costa Gomes 		new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
493990e35ecSVinicius Costa Gomes 					sch->handle, NULL);
494990e35ecSVinicius Costa Gomes 		if (!new)
495990e35ecSVinicius Costa Gomes 			new = &noop_qdisc;
496990e35ecSVinicius Costa Gomes 	}
497990e35ecSVinicius Costa Gomes 
498990e35ecSVinicius Costa Gomes 	*old = qdisc_replace(sch, new, &q->qdisc);
499990e35ecSVinicius Costa Gomes 	return 0;
500990e35ecSVinicius Costa Gomes }
501990e35ecSVinicius Costa Gomes 
502990e35ecSVinicius Costa Gomes static struct Qdisc *cbs_leaf(struct Qdisc *sch, unsigned long arg)
503990e35ecSVinicius Costa Gomes {
504990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
505990e35ecSVinicius Costa Gomes 
506990e35ecSVinicius Costa Gomes 	return q->qdisc;
507990e35ecSVinicius Costa Gomes }
508990e35ecSVinicius Costa Gomes 
509990e35ecSVinicius Costa Gomes static unsigned long cbs_find(struct Qdisc *sch, u32 classid)
510990e35ecSVinicius Costa Gomes {
511990e35ecSVinicius Costa Gomes 	return 1;
512990e35ecSVinicius Costa Gomes }
513990e35ecSVinicius Costa Gomes 
514990e35ecSVinicius Costa Gomes static void cbs_walk(struct Qdisc *sch, struct qdisc_walker *walker)
515990e35ecSVinicius Costa Gomes {
516990e35ecSVinicius Costa Gomes 	if (!walker->stop) {
517990e35ecSVinicius Costa Gomes 		if (walker->count >= walker->skip) {
518990e35ecSVinicius Costa Gomes 			if (walker->fn(sch, 1, walker) < 0) {
519990e35ecSVinicius Costa Gomes 				walker->stop = 1;
520990e35ecSVinicius Costa Gomes 				return;
521990e35ecSVinicius Costa Gomes 			}
522990e35ecSVinicius Costa Gomes 		}
523990e35ecSVinicius Costa Gomes 		walker->count++;
524990e35ecSVinicius Costa Gomes 	}
525990e35ecSVinicius Costa Gomes }
526990e35ecSVinicius Costa Gomes 
527990e35ecSVinicius Costa Gomes static const struct Qdisc_class_ops cbs_class_ops = {
528990e35ecSVinicius Costa Gomes 	.graft		=	cbs_graft,
529990e35ecSVinicius Costa Gomes 	.leaf		=	cbs_leaf,
530990e35ecSVinicius Costa Gomes 	.find		=	cbs_find,
531990e35ecSVinicius Costa Gomes 	.walk		=	cbs_walk,
532990e35ecSVinicius Costa Gomes 	.dump		=	cbs_dump_class,
533990e35ecSVinicius Costa Gomes };
534990e35ecSVinicius Costa Gomes 
535585d763aSVinicius Costa Gomes static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
536585d763aSVinicius Costa Gomes 	.id		=	"cbs",
537990e35ecSVinicius Costa Gomes 	.cl_ops		=	&cbs_class_ops,
538585d763aSVinicius Costa Gomes 	.priv_size	=	sizeof(struct cbs_sched_data),
539585d763aSVinicius Costa Gomes 	.enqueue	=	cbs_enqueue,
540585d763aSVinicius Costa Gomes 	.dequeue	=	cbs_dequeue,
541585d763aSVinicius Costa Gomes 	.peek		=	qdisc_peek_dequeued,
542585d763aSVinicius Costa Gomes 	.init		=	cbs_init,
543585d763aSVinicius Costa Gomes 	.reset		=	qdisc_reset_queue,
544585d763aSVinicius Costa Gomes 	.destroy	=	cbs_destroy,
545585d763aSVinicius Costa Gomes 	.change		=	cbs_change,
546585d763aSVinicius Costa Gomes 	.dump		=	cbs_dump,
547585d763aSVinicius Costa Gomes 	.owner		=	THIS_MODULE,
548585d763aSVinicius Costa Gomes };
549585d763aSVinicius Costa Gomes 
550e0a7683dSLeandro Dorileo static struct notifier_block cbs_device_notifier = {
551e0a7683dSLeandro Dorileo 	.notifier_call = cbs_dev_notifier,
552e0a7683dSLeandro Dorileo };
553e0a7683dSLeandro Dorileo 
554585d763aSVinicius Costa Gomes static int __init cbs_module_init(void)
555585d763aSVinicius Costa Gomes {
556e0a7683dSLeandro Dorileo 	int err = register_netdevice_notifier(&cbs_device_notifier);
557e0a7683dSLeandro Dorileo 
558e0a7683dSLeandro Dorileo 	if (err)
559e0a7683dSLeandro Dorileo 		return err;
560e0a7683dSLeandro Dorileo 
561585d763aSVinicius Costa Gomes 	return register_qdisc(&cbs_qdisc_ops);
562585d763aSVinicius Costa Gomes }
563585d763aSVinicius Costa Gomes 
564585d763aSVinicius Costa Gomes static void __exit cbs_module_exit(void)
565585d763aSVinicius Costa Gomes {
566585d763aSVinicius Costa Gomes 	unregister_qdisc(&cbs_qdisc_ops);
567e0a7683dSLeandro Dorileo 	unregister_netdevice_notifier(&cbs_device_notifier);
568585d763aSVinicius Costa Gomes }
569585d763aSVinicius Costa Gomes module_init(cbs_module_init)
570585d763aSVinicius Costa Gomes module_exit(cbs_module_exit)
571585d763aSVinicius Costa Gomes MODULE_LICENSE("GPL");
572