xref: /linux/net/sched/sch_cbs.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2585d763aSVinicius Costa Gomes /*
3585d763aSVinicius Costa Gomes  * net/sched/sch_cbs.c	Credit Based Shaper
4585d763aSVinicius Costa Gomes  *
5585d763aSVinicius Costa Gomes  * Authors:	Vinicius Costa Gomes <vinicius.gomes@intel.com>
6585d763aSVinicius Costa Gomes  */
7585d763aSVinicius Costa Gomes 
8585d763aSVinicius Costa Gomes /* Credit Based Shaper (CBS)
9585d763aSVinicius Costa Gomes  * =========================
10585d763aSVinicius Costa Gomes  *
11585d763aSVinicius Costa Gomes  * This is a simple rate-limiting shaper aimed at TSN applications on
12585d763aSVinicius Costa Gomes  * systems with known traffic workloads.
13585d763aSVinicius Costa Gomes  *
14585d763aSVinicius Costa Gomes  * Its algorithm is defined by the IEEE 802.1Q-2014 Specification,
15585d763aSVinicius Costa Gomes  * Section 8.6.8.2, and explained in more detail in the Annex L of the
16585d763aSVinicius Costa Gomes  * same specification.
17585d763aSVinicius Costa Gomes  *
18585d763aSVinicius Costa Gomes  * There are four tunables to be considered:
19585d763aSVinicius Costa Gomes  *
20585d763aSVinicius Costa Gomes  *	'idleslope': Idleslope is the rate of credits that is
21585d763aSVinicius Costa Gomes  *	accumulated (in kilobits per second) when there is at least
22585d763aSVinicius Costa Gomes  *	one packet waiting for transmission. Packets are transmitted
23585d763aSVinicius Costa Gomes  *	when the current value of credits is equal or greater than
24585d763aSVinicius Costa Gomes  *	zero. When there is no packet to be transmitted the amount of
25585d763aSVinicius Costa Gomes  *	credits is set to zero. This is the main tunable of the CBS
26585d763aSVinicius Costa Gomes  *	algorithm.
27585d763aSVinicius Costa Gomes  *
28585d763aSVinicius Costa Gomes  *	'sendslope':
29585d763aSVinicius Costa Gomes  *	Sendslope is the rate of credits that is depleted (it should be a
30585d763aSVinicius Costa Gomes  *	negative number of kilobits per second) when a transmission is
31585d763aSVinicius Costa Gomes  *	ocurring. It can be calculated as follows, (IEEE 802.1Q-2014 Section
32585d763aSVinicius Costa Gomes  *	8.6.8.2 item g):
33585d763aSVinicius Costa Gomes  *
34585d763aSVinicius Costa Gomes  *	sendslope = idleslope - port_transmit_rate
35585d763aSVinicius Costa Gomes  *
36585d763aSVinicius Costa Gomes  *	'hicredit': Hicredit defines the maximum amount of credits (in
37585d763aSVinicius Costa Gomes  *	bytes) that can be accumulated. Hicredit depends on the
38585d763aSVinicius Costa Gomes  *	characteristics of interfering traffic,
39585d763aSVinicius Costa Gomes  *	'max_interference_size' is the maximum size of any burst of
40585d763aSVinicius Costa Gomes  *	traffic that can delay the transmission of a frame that is
41585d763aSVinicius Costa Gomes  *	available for transmission for this traffic class, (IEEE
42585d763aSVinicius Costa Gomes  *	802.1Q-2014 Annex L, Equation L-3):
43585d763aSVinicius Costa Gomes  *
44585d763aSVinicius Costa Gomes  *	hicredit = max_interference_size * (idleslope / port_transmit_rate)
45585d763aSVinicius Costa Gomes  *
46585d763aSVinicius Costa Gomes  *	'locredit': Locredit is the minimum amount of credits that can
47585d763aSVinicius Costa Gomes  *	be reached. It is a function of the traffic flowing through
48585d763aSVinicius Costa Gomes  *	this qdisc (IEEE 802.1Q-2014 Annex L, Equation L-2):
49585d763aSVinicius Costa Gomes  *
50585d763aSVinicius Costa Gomes  *	locredit = max_frame_size * (sendslope / port_transmit_rate)
51585d763aSVinicius Costa Gomes  */
52585d763aSVinicius Costa Gomes 
53585d763aSVinicius Costa Gomes #include <linux/module.h>
54585d763aSVinicius Costa Gomes #include <linux/types.h>
55585d763aSVinicius Costa Gomes #include <linux/kernel.h>
56585d763aSVinicius Costa Gomes #include <linux/string.h>
57585d763aSVinicius Costa Gomes #include <linux/errno.h>
58585d763aSVinicius Costa Gomes #include <linux/skbuff.h>
59e0a7683dSLeandro Dorileo #include <net/netevent.h>
60585d763aSVinicius Costa Gomes #include <net/netlink.h>
61585d763aSVinicius Costa Gomes #include <net/sch_generic.h>
62585d763aSVinicius Costa Gomes #include <net/pkt_sched.h>
63585d763aSVinicius Costa Gomes 
64e0a7683dSLeandro Dorileo static LIST_HEAD(cbs_list);
65e0a7683dSLeandro Dorileo static DEFINE_SPINLOCK(cbs_list_lock);
66e0a7683dSLeandro Dorileo 
67585d763aSVinicius Costa Gomes #define BYTES_PER_KBIT (1000LL / 8)
68585d763aSVinicius Costa Gomes 
69585d763aSVinicius Costa Gomes struct cbs_sched_data {
703d0bd028SVinicius Costa Gomes 	bool offload;
713d0bd028SVinicius Costa Gomes 	int queue;
72e0a7683dSLeandro Dorileo 	atomic64_t port_rate; /* in bytes/s */
73585d763aSVinicius Costa Gomes 	s64 last; /* timestamp in ns */
74585d763aSVinicius Costa Gomes 	s64 credits; /* in bytes */
75585d763aSVinicius Costa Gomes 	s32 locredit; /* in bytes */
76585d763aSVinicius Costa Gomes 	s32 hicredit; /* in bytes */
77585d763aSVinicius Costa Gomes 	s64 sendslope; /* in bytes/s */
78585d763aSVinicius Costa Gomes 	s64 idleslope; /* in bytes/s */
79585d763aSVinicius Costa Gomes 	struct qdisc_watchdog watchdog;
80990e35ecSVinicius Costa Gomes 	int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch,
81990e35ecSVinicius Costa Gomes 		       struct sk_buff **to_free);
82585d763aSVinicius Costa Gomes 	struct sk_buff *(*dequeue)(struct Qdisc *sch);
83990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc;
84e0a7683dSLeandro Dorileo 	struct list_head cbs_list;
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 {
91f6bab199SToke Høiland-Jørgensen 	unsigned int len = qdisc_pkt_len(skb);
92990e35ecSVinicius Costa Gomes 	int err;
93990e35ecSVinicius Costa Gomes 
94990e35ecSVinicius Costa Gomes 	err = child->ops->enqueue(skb, child, to_free);
95990e35ecSVinicius Costa Gomes 	if (err != NET_XMIT_SUCCESS)
96990e35ecSVinicius Costa Gomes 		return err;
97990e35ecSVinicius Costa Gomes 
98f6bab199SToke Høiland-Jørgensen 	sch->qstats.backlog += len;
99990e35ecSVinicius Costa Gomes 	sch->q.qlen++;
100990e35ecSVinicius Costa Gomes 
101990e35ecSVinicius Costa Gomes 	return NET_XMIT_SUCCESS;
1023d0bd028SVinicius Costa Gomes }
1033d0bd028SVinicius Costa Gomes 
104990e35ecSVinicius Costa Gomes static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch,
105990e35ecSVinicius Costa Gomes 			       struct sk_buff **to_free)
106585d763aSVinicius Costa Gomes {
107585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
108990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
109990e35ecSVinicius Costa Gomes 
110990e35ecSVinicius Costa Gomes 	return cbs_child_enqueue(skb, sch, qdisc, to_free);
111990e35ecSVinicius Costa Gomes }
112990e35ecSVinicius Costa Gomes 
113990e35ecSVinicius Costa Gomes static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch,
114990e35ecSVinicius Costa Gomes 			    struct sk_buff **to_free)
115990e35ecSVinicius Costa Gomes {
116990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
117990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
118585d763aSVinicius Costa Gomes 
119585d763aSVinicius Costa Gomes 	if (sch->q.qlen == 0 && q->credits > 0) {
120585d763aSVinicius Costa Gomes 		/* We need to stop accumulating credits when there's
121585d763aSVinicius Costa Gomes 		 * no enqueued packets and q->credits is positive.
122585d763aSVinicius Costa Gomes 		 */
123585d763aSVinicius Costa Gomes 		q->credits = 0;
124585d763aSVinicius Costa Gomes 		q->last = ktime_get_ns();
125585d763aSVinicius Costa Gomes 	}
126585d763aSVinicius Costa Gomes 
127990e35ecSVinicius Costa Gomes 	return cbs_child_enqueue(skb, sch, qdisc, to_free);
128585d763aSVinicius Costa Gomes }
129585d763aSVinicius Costa Gomes 
130585d763aSVinicius Costa Gomes static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
131585d763aSVinicius Costa Gomes 		       struct sk_buff **to_free)
132585d763aSVinicius Costa Gomes {
133585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
134585d763aSVinicius Costa Gomes 
135990e35ecSVinicius Costa Gomes 	return q->enqueue(skb, sch, to_free);
136585d763aSVinicius Costa Gomes }
137585d763aSVinicius Costa Gomes 
138585d763aSVinicius Costa Gomes /* timediff is in ns, slope is in bytes/s */
139585d763aSVinicius Costa Gomes static s64 timediff_to_credits(s64 timediff, s64 slope)
140585d763aSVinicius Costa Gomes {
141585d763aSVinicius Costa Gomes 	return div64_s64(timediff * slope, NSEC_PER_SEC);
142585d763aSVinicius Costa Gomes }
143585d763aSVinicius Costa Gomes 
144585d763aSVinicius Costa Gomes static s64 delay_from_credits(s64 credits, s64 slope)
145585d763aSVinicius Costa Gomes {
146585d763aSVinicius Costa Gomes 	if (unlikely(slope == 0))
147585d763aSVinicius Costa Gomes 		return S64_MAX;
148585d763aSVinicius Costa Gomes 
149585d763aSVinicius Costa Gomes 	return div64_s64(-credits * NSEC_PER_SEC, slope);
150585d763aSVinicius Costa Gomes }
151585d763aSVinicius Costa Gomes 
152585d763aSVinicius Costa Gomes static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
153585d763aSVinicius Costa Gomes {
154585d763aSVinicius Costa Gomes 	if (unlikely(port_rate == 0))
155585d763aSVinicius Costa Gomes 		return S64_MAX;
156585d763aSVinicius Costa Gomes 
157585d763aSVinicius Costa Gomes 	return div64_s64(len * slope, port_rate);
158585d763aSVinicius Costa Gomes }
159585d763aSVinicius Costa Gomes 
160990e35ecSVinicius Costa Gomes static struct sk_buff *cbs_child_dequeue(struct Qdisc *sch, struct Qdisc *child)
161990e35ecSVinicius Costa Gomes {
162990e35ecSVinicius Costa Gomes 	struct sk_buff *skb;
163990e35ecSVinicius Costa Gomes 
164990e35ecSVinicius Costa Gomes 	skb = child->ops->dequeue(child);
165990e35ecSVinicius Costa Gomes 	if (!skb)
166990e35ecSVinicius Costa Gomes 		return NULL;
167990e35ecSVinicius Costa Gomes 
168990e35ecSVinicius Costa Gomes 	qdisc_qstats_backlog_dec(sch, skb);
169990e35ecSVinicius Costa Gomes 	qdisc_bstats_update(sch, skb);
170990e35ecSVinicius Costa Gomes 	sch->q.qlen--;
171990e35ecSVinicius Costa Gomes 
172990e35ecSVinicius Costa Gomes 	return skb;
173990e35ecSVinicius Costa Gomes }
174990e35ecSVinicius Costa Gomes 
175585d763aSVinicius Costa Gomes static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
176585d763aSVinicius Costa Gomes {
177585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
178990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
179585d763aSVinicius Costa Gomes 	s64 now = ktime_get_ns();
180585d763aSVinicius Costa Gomes 	struct sk_buff *skb;
181585d763aSVinicius Costa Gomes 	s64 credits;
182585d763aSVinicius Costa Gomes 	int len;
183585d763aSVinicius Costa Gomes 
184e0a7683dSLeandro Dorileo 	if (atomic64_read(&q->port_rate) == -1) {
185e0a7683dSLeandro Dorileo 		WARN_ONCE(1, "cbs: dequeue() called with unknown port rate.");
186e0a7683dSLeandro Dorileo 		return NULL;
187e0a7683dSLeandro Dorileo 	}
188e0a7683dSLeandro Dorileo 
189585d763aSVinicius Costa Gomes 	if (q->credits < 0) {
190585d763aSVinicius Costa Gomes 		credits = timediff_to_credits(now - q->last, q->idleslope);
191585d763aSVinicius Costa Gomes 
192585d763aSVinicius Costa Gomes 		credits = q->credits + credits;
193585d763aSVinicius Costa Gomes 		q->credits = min_t(s64, credits, q->hicredit);
194585d763aSVinicius Costa Gomes 
195585d763aSVinicius Costa Gomes 		if (q->credits < 0) {
196585d763aSVinicius Costa Gomes 			s64 delay;
197585d763aSVinicius Costa Gomes 
198585d763aSVinicius Costa Gomes 			delay = delay_from_credits(q->credits, q->idleslope);
199585d763aSVinicius Costa Gomes 			qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
200585d763aSVinicius Costa Gomes 
201585d763aSVinicius Costa Gomes 			q->last = now;
202585d763aSVinicius Costa Gomes 
203585d763aSVinicius Costa Gomes 			return NULL;
204585d763aSVinicius Costa Gomes 		}
205585d763aSVinicius Costa Gomes 	}
206990e35ecSVinicius Costa Gomes 	skb = cbs_child_dequeue(sch, qdisc);
207585d763aSVinicius Costa Gomes 	if (!skb)
208585d763aSVinicius Costa Gomes 		return NULL;
209585d763aSVinicius Costa Gomes 
210585d763aSVinicius Costa Gomes 	len = qdisc_pkt_len(skb);
211585d763aSVinicius Costa Gomes 
212585d763aSVinicius Costa Gomes 	/* As sendslope is a negative number, this will decrease the
213585d763aSVinicius Costa Gomes 	 * amount of q->credits.
214585d763aSVinicius Costa Gomes 	 */
215e0a7683dSLeandro Dorileo 	credits = credits_from_len(len, q->sendslope,
216e0a7683dSLeandro Dorileo 				   atomic64_read(&q->port_rate));
217585d763aSVinicius Costa Gomes 	credits += q->credits;
218585d763aSVinicius Costa Gomes 
219585d763aSVinicius Costa Gomes 	q->credits = max_t(s64, credits, q->locredit);
220585d763aSVinicius Costa Gomes 	q->last = now;
221585d763aSVinicius Costa Gomes 
222585d763aSVinicius Costa Gomes 	return skb;
223585d763aSVinicius Costa Gomes }
224585d763aSVinicius Costa Gomes 
2253d0bd028SVinicius Costa Gomes static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
2263d0bd028SVinicius Costa Gomes {
227990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
228990e35ecSVinicius Costa Gomes 	struct Qdisc *qdisc = q->qdisc;
229990e35ecSVinicius Costa Gomes 
230990e35ecSVinicius Costa Gomes 	return cbs_child_dequeue(sch, qdisc);
2313d0bd028SVinicius Costa Gomes }
2323d0bd028SVinicius Costa Gomes 
233585d763aSVinicius Costa Gomes static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
234585d763aSVinicius Costa Gomes {
235585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
236585d763aSVinicius Costa Gomes 
237585d763aSVinicius Costa Gomes 	return q->dequeue(sch);
238585d763aSVinicius Costa Gomes }
239585d763aSVinicius Costa Gomes 
240585d763aSVinicius Costa Gomes static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
241585d763aSVinicius Costa Gomes 	[TCA_CBS_PARMS]	= { .len = sizeof(struct tc_cbs_qopt) },
242585d763aSVinicius Costa Gomes };
243585d763aSVinicius Costa Gomes 
2443d0bd028SVinicius Costa Gomes static void cbs_disable_offload(struct net_device *dev,
2453d0bd028SVinicius Costa Gomes 				struct cbs_sched_data *q)
2463d0bd028SVinicius Costa Gomes {
2473d0bd028SVinicius Costa Gomes 	struct tc_cbs_qopt_offload cbs = { };
2483d0bd028SVinicius Costa Gomes 	const struct net_device_ops *ops;
2493d0bd028SVinicius Costa Gomes 	int err;
2503d0bd028SVinicius Costa Gomes 
2513d0bd028SVinicius Costa Gomes 	if (!q->offload)
2523d0bd028SVinicius Costa Gomes 		return;
2533d0bd028SVinicius Costa Gomes 
2543d0bd028SVinicius Costa Gomes 	q->enqueue = cbs_enqueue_soft;
2553d0bd028SVinicius Costa Gomes 	q->dequeue = cbs_dequeue_soft;
2563d0bd028SVinicius Costa Gomes 
2573d0bd028SVinicius Costa Gomes 	ops = dev->netdev_ops;
2583d0bd028SVinicius Costa Gomes 	if (!ops->ndo_setup_tc)
2593d0bd028SVinicius Costa Gomes 		return;
2603d0bd028SVinicius Costa Gomes 
2613d0bd028SVinicius Costa Gomes 	cbs.queue = q->queue;
2623d0bd028SVinicius Costa Gomes 	cbs.enable = 0;
2633d0bd028SVinicius Costa Gomes 
2648521db4cSNogah Frankel 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
2653d0bd028SVinicius Costa Gomes 	if (err < 0)
2663d0bd028SVinicius Costa Gomes 		pr_warn("Couldn't disable CBS offload for queue %d\n",
2673d0bd028SVinicius Costa Gomes 			cbs.queue);
2683d0bd028SVinicius Costa Gomes }
2693d0bd028SVinicius Costa Gomes 
2703d0bd028SVinicius Costa Gomes static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
271710fb396SAlexander Aring 			      const struct tc_cbs_qopt *opt,
272710fb396SAlexander Aring 			      struct netlink_ext_ack *extack)
2733d0bd028SVinicius Costa Gomes {
2743d0bd028SVinicius Costa Gomes 	const struct net_device_ops *ops = dev->netdev_ops;
2753d0bd028SVinicius Costa Gomes 	struct tc_cbs_qopt_offload cbs = { };
2763d0bd028SVinicius Costa Gomes 	int err;
2773d0bd028SVinicius Costa Gomes 
278710fb396SAlexander Aring 	if (!ops->ndo_setup_tc) {
279710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
2803d0bd028SVinicius Costa Gomes 		return -EOPNOTSUPP;
281710fb396SAlexander Aring 	}
2823d0bd028SVinicius Costa Gomes 
2833d0bd028SVinicius Costa Gomes 	cbs.queue = q->queue;
2843d0bd028SVinicius Costa Gomes 
2853d0bd028SVinicius Costa Gomes 	cbs.enable = 1;
2863d0bd028SVinicius Costa Gomes 	cbs.hicredit = opt->hicredit;
2873d0bd028SVinicius Costa Gomes 	cbs.locredit = opt->locredit;
2883d0bd028SVinicius Costa Gomes 	cbs.idleslope = opt->idleslope;
2893d0bd028SVinicius Costa Gomes 	cbs.sendslope = opt->sendslope;
2903d0bd028SVinicius Costa Gomes 
2918521db4cSNogah Frankel 	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
292710fb396SAlexander Aring 	if (err < 0) {
293710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
2943d0bd028SVinicius Costa Gomes 		return err;
295710fb396SAlexander Aring 	}
2963d0bd028SVinicius Costa Gomes 
2973d0bd028SVinicius Costa Gomes 	q->enqueue = cbs_enqueue_offload;
2983d0bd028SVinicius Costa Gomes 	q->dequeue = cbs_dequeue_offload;
2993d0bd028SVinicius Costa Gomes 
3003d0bd028SVinicius Costa Gomes 	return 0;
3013d0bd028SVinicius Costa Gomes }
3023d0bd028SVinicius Costa Gomes 
303e0a7683dSLeandro Dorileo static void cbs_set_port_rate(struct net_device *dev, struct cbs_sched_data *q)
304e0a7683dSLeandro Dorileo {
305e0a7683dSLeandro Dorileo 	struct ethtool_link_ksettings ecmd;
306e0a7683dSLeandro Dorileo 	int port_rate = -1;
307e0a7683dSLeandro Dorileo 
308e0a7683dSLeandro Dorileo 	if (!__ethtool_get_link_ksettings(dev, &ecmd) &&
309e0a7683dSLeandro Dorileo 	    ecmd.base.speed != SPEED_UNKNOWN)
310e0a7683dSLeandro Dorileo 		port_rate = ecmd.base.speed * 1000 * BYTES_PER_KBIT;
311e0a7683dSLeandro Dorileo 
312e0a7683dSLeandro Dorileo 	atomic64_set(&q->port_rate, port_rate);
313e0a7683dSLeandro Dorileo 	netdev_dbg(dev, "cbs: set %s's port_rate to: %lld, linkspeed: %d\n",
314e0a7683dSLeandro Dorileo 		   dev->name, (long long)atomic64_read(&q->port_rate),
315e0a7683dSLeandro Dorileo 		   ecmd.base.speed);
316e0a7683dSLeandro Dorileo }
317e0a7683dSLeandro Dorileo 
318e0a7683dSLeandro Dorileo static int cbs_dev_notifier(struct notifier_block *nb, unsigned long event,
319e0a7683dSLeandro Dorileo 			    void *ptr)
320e0a7683dSLeandro Dorileo {
321e0a7683dSLeandro Dorileo 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
322e0a7683dSLeandro Dorileo 	struct cbs_sched_data *q;
323e0a7683dSLeandro Dorileo 	struct net_device *qdev;
324e0a7683dSLeandro Dorileo 	bool found = false;
325e0a7683dSLeandro Dorileo 
326e0a7683dSLeandro Dorileo 	ASSERT_RTNL();
327e0a7683dSLeandro Dorileo 
328e0a7683dSLeandro Dorileo 	if (event != NETDEV_UP && event != NETDEV_CHANGE)
329e0a7683dSLeandro Dorileo 		return NOTIFY_DONE;
330e0a7683dSLeandro Dorileo 
331e0a7683dSLeandro Dorileo 	spin_lock(&cbs_list_lock);
332e0a7683dSLeandro Dorileo 	list_for_each_entry(q, &cbs_list, cbs_list) {
333e0a7683dSLeandro Dorileo 		qdev = qdisc_dev(q->qdisc);
334e0a7683dSLeandro Dorileo 		if (qdev == dev) {
335e0a7683dSLeandro Dorileo 			found = true;
336e0a7683dSLeandro Dorileo 			break;
337e0a7683dSLeandro Dorileo 		}
338e0a7683dSLeandro Dorileo 	}
339e0a7683dSLeandro Dorileo 	spin_unlock(&cbs_list_lock);
340e0a7683dSLeandro Dorileo 
341e0a7683dSLeandro Dorileo 	if (found)
342e0a7683dSLeandro Dorileo 		cbs_set_port_rate(dev, q);
343e0a7683dSLeandro Dorileo 
344e0a7683dSLeandro Dorileo 	return NOTIFY_DONE;
345e0a7683dSLeandro Dorileo }
346e0a7683dSLeandro Dorileo 
3472030721cSAlexander Aring static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
3482030721cSAlexander Aring 		      struct netlink_ext_ack *extack)
349585d763aSVinicius Costa Gomes {
350585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
351585d763aSVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
352585d763aSVinicius Costa Gomes 	struct nlattr *tb[TCA_CBS_MAX + 1];
353585d763aSVinicius Costa Gomes 	struct tc_cbs_qopt *qopt;
354585d763aSVinicius Costa Gomes 	int err;
355585d763aSVinicius Costa Gomes 
3568cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_CBS_MAX, opt, cbs_policy,
3578cb08174SJohannes Berg 					  extack);
358585d763aSVinicius Costa Gomes 	if (err < 0)
359585d763aSVinicius Costa Gomes 		return err;
360585d763aSVinicius Costa Gomes 
361710fb396SAlexander Aring 	if (!tb[TCA_CBS_PARMS]) {
362710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
363585d763aSVinicius Costa Gomes 		return -EINVAL;
364710fb396SAlexander Aring 	}
365585d763aSVinicius Costa Gomes 
366585d763aSVinicius Costa Gomes 	qopt = nla_data(tb[TCA_CBS_PARMS]);
367585d763aSVinicius Costa Gomes 
3683d0bd028SVinicius Costa Gomes 	if (!qopt->offload) {
369e0a7683dSLeandro Dorileo 		cbs_set_port_rate(dev, q);
3703d0bd028SVinicius Costa Gomes 		cbs_disable_offload(dev, q);
3713d0bd028SVinicius Costa Gomes 	} else {
372710fb396SAlexander Aring 		err = cbs_enable_offload(dev, q, qopt, extack);
3733d0bd028SVinicius Costa Gomes 		if (err < 0)
3743d0bd028SVinicius Costa Gomes 			return err;
3753d0bd028SVinicius Costa Gomes 	}
376585d763aSVinicius Costa Gomes 
3773d0bd028SVinicius Costa Gomes 	/* Everything went OK, save the parameters used. */
378585d763aSVinicius Costa Gomes 	q->hicredit = qopt->hicredit;
379585d763aSVinicius Costa Gomes 	q->locredit = qopt->locredit;
380585d763aSVinicius Costa Gomes 	q->idleslope = qopt->idleslope * BYTES_PER_KBIT;
381585d763aSVinicius Costa Gomes 	q->sendslope = qopt->sendslope * BYTES_PER_KBIT;
3823d0bd028SVinicius Costa Gomes 	q->offload = qopt->offload;
383585d763aSVinicius Costa Gomes 
384585d763aSVinicius Costa Gomes 	return 0;
385585d763aSVinicius Costa Gomes }
386585d763aSVinicius Costa Gomes 
387e63d7dfdSAlexander Aring static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
388e63d7dfdSAlexander Aring 		    struct netlink_ext_ack *extack)
389585d763aSVinicius Costa Gomes {
390585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
3913d0bd028SVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
392e0a7683dSLeandro Dorileo 	int err;
393585d763aSVinicius Costa Gomes 
394710fb396SAlexander Aring 	if (!opt) {
395710fb396SAlexander Aring 		NL_SET_ERR_MSG(extack, "Missing CBS qdisc options  which are mandatory");
396585d763aSVinicius Costa Gomes 		return -EINVAL;
397710fb396SAlexander Aring 	}
398585d763aSVinicius Costa Gomes 
399990e35ecSVinicius Costa Gomes 	q->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
400990e35ecSVinicius Costa Gomes 				     sch->handle, extack);
401990e35ecSVinicius Costa Gomes 	if (!q->qdisc)
402990e35ecSVinicius Costa Gomes 		return -ENOMEM;
403990e35ecSVinicius Costa Gomes 
404990e35ecSVinicius Costa Gomes 	qdisc_hash_add(q->qdisc, false);
405990e35ecSVinicius Costa Gomes 
4063d0bd028SVinicius Costa Gomes 	q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
4073d0bd028SVinicius Costa Gomes 
4083d0bd028SVinicius Costa Gomes 	q->enqueue = cbs_enqueue_soft;
4093d0bd028SVinicius Costa Gomes 	q->dequeue = cbs_dequeue_soft;
4103d0bd028SVinicius Costa Gomes 
411585d763aSVinicius Costa Gomes 	qdisc_watchdog_init(&q->watchdog, sch);
412585d763aSVinicius Costa Gomes 
413e0a7683dSLeandro Dorileo 	err = cbs_change(sch, opt, extack);
414e0a7683dSLeandro Dorileo 	if (err)
415e0a7683dSLeandro Dorileo 		return err;
416e0a7683dSLeandro Dorileo 
417e0a7683dSLeandro Dorileo 	if (!q->offload) {
418e0a7683dSLeandro Dorileo 		spin_lock(&cbs_list_lock);
419e0a7683dSLeandro Dorileo 		list_add(&q->cbs_list, &cbs_list);
420e0a7683dSLeandro Dorileo 		spin_unlock(&cbs_list_lock);
421e0a7683dSLeandro Dorileo 	}
422e0a7683dSLeandro Dorileo 
423e0a7683dSLeandro Dorileo 	return 0;
424585d763aSVinicius Costa Gomes }
425585d763aSVinicius Costa Gomes 
426585d763aSVinicius Costa Gomes static void cbs_destroy(struct Qdisc *sch)
427585d763aSVinicius Costa Gomes {
428585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
4293d0bd028SVinicius Costa Gomes 	struct net_device *dev = qdisc_dev(sch);
430585d763aSVinicius Costa Gomes 
431e0a7683dSLeandro Dorileo 	spin_lock(&cbs_list_lock);
432e0a7683dSLeandro Dorileo 	list_del(&q->cbs_list);
433e0a7683dSLeandro Dorileo 	spin_unlock(&cbs_list_lock);
4343d0bd028SVinicius Costa Gomes 
435e0a7683dSLeandro Dorileo 	qdisc_watchdog_cancel(&q->watchdog);
4363d0bd028SVinicius Costa Gomes 	cbs_disable_offload(dev, q);
437990e35ecSVinicius Costa Gomes 
438990e35ecSVinicius Costa Gomes 	if (q->qdisc)
43986bd446bSVlad Buslov 		qdisc_put(q->qdisc);
440585d763aSVinicius Costa Gomes }
441585d763aSVinicius Costa Gomes 
442585d763aSVinicius Costa Gomes static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
443585d763aSVinicius Costa Gomes {
444585d763aSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
445585d763aSVinicius Costa Gomes 	struct tc_cbs_qopt opt = { };
446585d763aSVinicius Costa Gomes 	struct nlattr *nest;
447585d763aSVinicius Costa Gomes 
448ae0be8deSMichal Kubecek 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
449585d763aSVinicius Costa Gomes 	if (!nest)
450585d763aSVinicius Costa Gomes 		goto nla_put_failure;
451585d763aSVinicius Costa Gomes 
452585d763aSVinicius Costa Gomes 	opt.hicredit = q->hicredit;
453585d763aSVinicius Costa Gomes 	opt.locredit = q->locredit;
454585d763aSVinicius Costa Gomes 	opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT);
455585d763aSVinicius Costa Gomes 	opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT);
4563d0bd028SVinicius Costa Gomes 	opt.offload = q->offload;
457585d763aSVinicius Costa Gomes 
458585d763aSVinicius Costa Gomes 	if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
459585d763aSVinicius Costa Gomes 		goto nla_put_failure;
460585d763aSVinicius Costa Gomes 
461585d763aSVinicius Costa Gomes 	return nla_nest_end(skb, nest);
462585d763aSVinicius Costa Gomes 
463585d763aSVinicius Costa Gomes nla_put_failure:
464585d763aSVinicius Costa Gomes 	nla_nest_cancel(skb, nest);
465585d763aSVinicius Costa Gomes 	return -1;
466585d763aSVinicius Costa Gomes }
467585d763aSVinicius Costa Gomes 
468990e35ecSVinicius Costa Gomes static int cbs_dump_class(struct Qdisc *sch, unsigned long cl,
469990e35ecSVinicius Costa Gomes 			  struct sk_buff *skb, struct tcmsg *tcm)
470990e35ecSVinicius Costa Gomes {
471990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
472990e35ecSVinicius Costa Gomes 
473990e35ecSVinicius Costa Gomes 	if (cl != 1 || !q->qdisc)	/* only one class */
474990e35ecSVinicius Costa Gomes 		return -ENOENT;
475990e35ecSVinicius Costa Gomes 
476990e35ecSVinicius Costa Gomes 	tcm->tcm_handle |= TC_H_MIN(1);
477990e35ecSVinicius Costa Gomes 	tcm->tcm_info = q->qdisc->handle;
478990e35ecSVinicius Costa Gomes 
479990e35ecSVinicius Costa Gomes 	return 0;
480990e35ecSVinicius Costa Gomes }
481990e35ecSVinicius Costa Gomes 
482990e35ecSVinicius Costa Gomes static int cbs_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
483990e35ecSVinicius Costa Gomes 		     struct Qdisc **old, struct netlink_ext_ack *extack)
484990e35ecSVinicius Costa Gomes {
485990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
486990e35ecSVinicius Costa Gomes 
487990e35ecSVinicius Costa Gomes 	if (!new) {
488990e35ecSVinicius Costa Gomes 		new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
489990e35ecSVinicius Costa Gomes 					sch->handle, NULL);
490990e35ecSVinicius Costa Gomes 		if (!new)
491990e35ecSVinicius Costa Gomes 			new = &noop_qdisc;
492990e35ecSVinicius Costa Gomes 	}
493990e35ecSVinicius Costa Gomes 
494990e35ecSVinicius Costa Gomes 	*old = qdisc_replace(sch, new, &q->qdisc);
495990e35ecSVinicius Costa Gomes 	return 0;
496990e35ecSVinicius Costa Gomes }
497990e35ecSVinicius Costa Gomes 
498990e35ecSVinicius Costa Gomes static struct Qdisc *cbs_leaf(struct Qdisc *sch, unsigned long arg)
499990e35ecSVinicius Costa Gomes {
500990e35ecSVinicius Costa Gomes 	struct cbs_sched_data *q = qdisc_priv(sch);
501990e35ecSVinicius Costa Gomes 
502990e35ecSVinicius Costa Gomes 	return q->qdisc;
503990e35ecSVinicius Costa Gomes }
504990e35ecSVinicius Costa Gomes 
505990e35ecSVinicius Costa Gomes static unsigned long cbs_find(struct Qdisc *sch, u32 classid)
506990e35ecSVinicius Costa Gomes {
507990e35ecSVinicius Costa Gomes 	return 1;
508990e35ecSVinicius Costa Gomes }
509990e35ecSVinicius Costa Gomes 
510990e35ecSVinicius Costa Gomes static void cbs_walk(struct Qdisc *sch, struct qdisc_walker *walker)
511990e35ecSVinicius Costa Gomes {
512990e35ecSVinicius Costa Gomes 	if (!walker->stop) {
513990e35ecSVinicius Costa Gomes 		if (walker->count >= walker->skip) {
514990e35ecSVinicius Costa Gomes 			if (walker->fn(sch, 1, walker) < 0) {
515990e35ecSVinicius Costa Gomes 				walker->stop = 1;
516990e35ecSVinicius Costa Gomes 				return;
517990e35ecSVinicius Costa Gomes 			}
518990e35ecSVinicius Costa Gomes 		}
519990e35ecSVinicius Costa Gomes 		walker->count++;
520990e35ecSVinicius Costa Gomes 	}
521990e35ecSVinicius Costa Gomes }
522990e35ecSVinicius Costa Gomes 
523990e35ecSVinicius Costa Gomes static const struct Qdisc_class_ops cbs_class_ops = {
524990e35ecSVinicius Costa Gomes 	.graft		=	cbs_graft,
525990e35ecSVinicius Costa Gomes 	.leaf		=	cbs_leaf,
526990e35ecSVinicius Costa Gomes 	.find		=	cbs_find,
527990e35ecSVinicius Costa Gomes 	.walk		=	cbs_walk,
528990e35ecSVinicius Costa Gomes 	.dump		=	cbs_dump_class,
529990e35ecSVinicius Costa Gomes };
530990e35ecSVinicius Costa Gomes 
531585d763aSVinicius Costa Gomes static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
532585d763aSVinicius Costa Gomes 	.id		=	"cbs",
533990e35ecSVinicius Costa Gomes 	.cl_ops		=	&cbs_class_ops,
534585d763aSVinicius Costa Gomes 	.priv_size	=	sizeof(struct cbs_sched_data),
535585d763aSVinicius Costa Gomes 	.enqueue	=	cbs_enqueue,
536585d763aSVinicius Costa Gomes 	.dequeue	=	cbs_dequeue,
537585d763aSVinicius Costa Gomes 	.peek		=	qdisc_peek_dequeued,
538585d763aSVinicius Costa Gomes 	.init		=	cbs_init,
539585d763aSVinicius Costa Gomes 	.reset		=	qdisc_reset_queue,
540585d763aSVinicius Costa Gomes 	.destroy	=	cbs_destroy,
541585d763aSVinicius Costa Gomes 	.change		=	cbs_change,
542585d763aSVinicius Costa Gomes 	.dump		=	cbs_dump,
543585d763aSVinicius Costa Gomes 	.owner		=	THIS_MODULE,
544585d763aSVinicius Costa Gomes };
545585d763aSVinicius Costa Gomes 
546e0a7683dSLeandro Dorileo static struct notifier_block cbs_device_notifier = {
547e0a7683dSLeandro Dorileo 	.notifier_call = cbs_dev_notifier,
548e0a7683dSLeandro Dorileo };
549e0a7683dSLeandro Dorileo 
550585d763aSVinicius Costa Gomes static int __init cbs_module_init(void)
551585d763aSVinicius Costa Gomes {
552e0a7683dSLeandro Dorileo 	int err = register_netdevice_notifier(&cbs_device_notifier);
553e0a7683dSLeandro Dorileo 
554e0a7683dSLeandro Dorileo 	if (err)
555e0a7683dSLeandro Dorileo 		return err;
556e0a7683dSLeandro Dorileo 
557585d763aSVinicius Costa Gomes 	return register_qdisc(&cbs_qdisc_ops);
558585d763aSVinicius Costa Gomes }
559585d763aSVinicius Costa Gomes 
560585d763aSVinicius Costa Gomes static void __exit cbs_module_exit(void)
561585d763aSVinicius Costa Gomes {
562585d763aSVinicius Costa Gomes 	unregister_qdisc(&cbs_qdisc_ops);
563e0a7683dSLeandro Dorileo 	unregister_netdevice_notifier(&cbs_device_notifier);
564585d763aSVinicius Costa Gomes }
565585d763aSVinicius Costa Gomes module_init(cbs_module_init)
566585d763aSVinicius Costa Gomes module_exit(cbs_module_exit)
567585d763aSVinicius Costa Gomes MODULE_LICENSE("GPL");
568