Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * net/sched/sch_cbs.c	Credit Based Shaper
  4 *
 
 
 
 
 
  5 * Authors:	Vinicius Costa Gomes <vinicius.gomes@intel.com>
 
  6 */
  7
  8/* Credit Based Shaper (CBS)
  9 * =========================
 10 *
 11 * This is a simple rate-limiting shaper aimed at TSN applications on
 12 * systems with known traffic workloads.
 13 *
 14 * Its algorithm is defined by the IEEE 802.1Q-2014 Specification,
 15 * Section 8.6.8.2, and explained in more detail in the Annex L of the
 16 * same specification.
 17 *
 18 * There are four tunables to be considered:
 19 *
 20 *	'idleslope': Idleslope is the rate of credits that is
 21 *	accumulated (in kilobits per second) when there is at least
 22 *	one packet waiting for transmission. Packets are transmitted
 23 *	when the current value of credits is equal or greater than
 24 *	zero. When there is no packet to be transmitted the amount of
 25 *	credits is set to zero. This is the main tunable of the CBS
 26 *	algorithm.
 27 *
 28 *	'sendslope':
 29 *	Sendslope is the rate of credits that is depleted (it should be a
 30 *	negative number of kilobits per second) when a transmission is
 31 *	ocurring. It can be calculated as follows, (IEEE 802.1Q-2014 Section
 32 *	8.6.8.2 item g):
 33 *
 34 *	sendslope = idleslope - port_transmit_rate
 35 *
 36 *	'hicredit': Hicredit defines the maximum amount of credits (in
 37 *	bytes) that can be accumulated. Hicredit depends on the
 38 *	characteristics of interfering traffic,
 39 *	'max_interference_size' is the maximum size of any burst of
 40 *	traffic that can delay the transmission of a frame that is
 41 *	available for transmission for this traffic class, (IEEE
 42 *	802.1Q-2014 Annex L, Equation L-3):
 43 *
 44 *	hicredit = max_interference_size * (idleslope / port_transmit_rate)
 45 *
 46 *	'locredit': Locredit is the minimum amount of credits that can
 47 *	be reached. It is a function of the traffic flowing through
 48 *	this qdisc (IEEE 802.1Q-2014 Annex L, Equation L-2):
 49 *
 50 *	locredit = max_frame_size * (sendslope / port_transmit_rate)
 51 */
 52
 53#include <linux/module.h>
 54#include <linux/types.h>
 55#include <linux/kernel.h>
 56#include <linux/string.h>
 57#include <linux/errno.h>
 58#include <linux/skbuff.h>
 59#include <net/netevent.h>
 60#include <net/netlink.h>
 61#include <net/sch_generic.h>
 62#include <net/pkt_sched.h>
 63
 64static LIST_HEAD(cbs_list);
 65static DEFINE_SPINLOCK(cbs_list_lock);
 66
 67#define BYTES_PER_KBIT (1000LL / 8)
 68
 69struct cbs_sched_data {
 70	bool offload;
 71	int queue;
 72	atomic64_t port_rate; /* in bytes/s */
 73	s64 last; /* timestamp in ns */
 74	s64 credits; /* in bytes */
 75	s32 locredit; /* in bytes */
 76	s32 hicredit; /* in bytes */
 77	s64 sendslope; /* in bytes/s */
 78	s64 idleslope; /* in bytes/s */
 79	struct qdisc_watchdog watchdog;
 80	int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch,
 81		       struct sk_buff **to_free);
 82	struct sk_buff *(*dequeue)(struct Qdisc *sch);
 83	struct Qdisc *qdisc;
 84	struct list_head cbs_list;
 85};
 86
 87static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch,
 88			     struct Qdisc *child,
 89			     struct sk_buff **to_free)
 90{
 91	unsigned int len = qdisc_pkt_len(skb);
 92	int err;
 93
 94	err = child->ops->enqueue(skb, child, to_free);
 95	if (err != NET_XMIT_SUCCESS)
 96		return err;
 97
 98	sch->qstats.backlog += len;
 99	sch->q.qlen++;
100
101	return NET_XMIT_SUCCESS;
102}
103
104static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch,
105			       struct sk_buff **to_free)
106{
107	struct cbs_sched_data *q = qdisc_priv(sch);
108	struct Qdisc *qdisc = q->qdisc;
109
110	return cbs_child_enqueue(skb, sch, qdisc, to_free);
111}
112
113static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch,
114			    struct sk_buff **to_free)
115{
116	struct cbs_sched_data *q = qdisc_priv(sch);
117	struct Qdisc *qdisc = q->qdisc;
118
119	if (sch->q.qlen == 0 && q->credits > 0) {
120		/* We need to stop accumulating credits when there's
121		 * no enqueued packets and q->credits is positive.
122		 */
123		q->credits = 0;
124		q->last = ktime_get_ns();
125	}
126
127	return cbs_child_enqueue(skb, sch, qdisc, to_free);
128}
129
130static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
131		       struct sk_buff **to_free)
132{
133	struct cbs_sched_data *q = qdisc_priv(sch);
134
135	return q->enqueue(skb, sch, to_free);
136}
137
138/* timediff is in ns, slope is in bytes/s */
139static s64 timediff_to_credits(s64 timediff, s64 slope)
140{
141	return div64_s64(timediff * slope, NSEC_PER_SEC);
142}
143
144static s64 delay_from_credits(s64 credits, s64 slope)
145{
146	if (unlikely(slope == 0))
147		return S64_MAX;
148
149	return div64_s64(-credits * NSEC_PER_SEC, slope);
150}
151
152static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
153{
154	if (unlikely(port_rate == 0))
155		return S64_MAX;
156
157	return div64_s64(len * slope, port_rate);
158}
159
160static struct sk_buff *cbs_child_dequeue(struct Qdisc *sch, struct Qdisc *child)
161{
162	struct sk_buff *skb;
163
164	skb = child->ops->dequeue(child);
165	if (!skb)
166		return NULL;
167
168	qdisc_qstats_backlog_dec(sch, skb);
169	qdisc_bstats_update(sch, skb);
170	sch->q.qlen--;
171
172	return skb;
173}
174
175static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
176{
177	struct cbs_sched_data *q = qdisc_priv(sch);
178	struct Qdisc *qdisc = q->qdisc;
179	s64 now = ktime_get_ns();
180	struct sk_buff *skb;
181	s64 credits;
182	int len;
183
184	if (q->credits < 0) {
185		credits = timediff_to_credits(now - q->last, q->idleslope);
186
187		credits = q->credits + credits;
188		q->credits = min_t(s64, credits, q->hicredit);
189
190		if (q->credits < 0) {
191			s64 delay;
192
193			delay = delay_from_credits(q->credits, q->idleslope);
194			qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
195
196			q->last = now;
197
198			return NULL;
199		}
200	}
201	skb = cbs_child_dequeue(sch, qdisc);
 
202	if (!skb)
203		return NULL;
204
205	len = qdisc_pkt_len(skb);
206
207	/* As sendslope is a negative number, this will decrease the
208	 * amount of q->credits.
209	 */
210	credits = credits_from_len(len, q->sendslope,
211				   atomic64_read(&q->port_rate));
212	credits += q->credits;
213
214	q->credits = max_t(s64, credits, q->locredit);
215	q->last = now;
216
217	return skb;
218}
219
220static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
221{
222	struct cbs_sched_data *q = qdisc_priv(sch);
223	struct Qdisc *qdisc = q->qdisc;
224
225	return cbs_child_dequeue(sch, qdisc);
226}
227
228static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
229{
230	struct cbs_sched_data *q = qdisc_priv(sch);
231
232	return q->dequeue(sch);
233}
234
235static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
236	[TCA_CBS_PARMS]	= { .len = sizeof(struct tc_cbs_qopt) },
237};
238
239static void cbs_disable_offload(struct net_device *dev,
240				struct cbs_sched_data *q)
241{
242	struct tc_cbs_qopt_offload cbs = { };
243	const struct net_device_ops *ops;
244	int err;
245
246	if (!q->offload)
247		return;
248
249	q->enqueue = cbs_enqueue_soft;
250	q->dequeue = cbs_dequeue_soft;
251
252	ops = dev->netdev_ops;
253	if (!ops->ndo_setup_tc)
254		return;
255
256	cbs.queue = q->queue;
257	cbs.enable = 0;
258
259	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
260	if (err < 0)
261		pr_warn("Couldn't disable CBS offload for queue %d\n",
262			cbs.queue);
263}
264
265static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
266			      const struct tc_cbs_qopt *opt,
267			      struct netlink_ext_ack *extack)
268{
269	const struct net_device_ops *ops = dev->netdev_ops;
270	struct tc_cbs_qopt_offload cbs = { };
271	int err;
272
273	if (!ops->ndo_setup_tc) {
274		NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
275		return -EOPNOTSUPP;
276	}
277
278	cbs.queue = q->queue;
279
280	cbs.enable = 1;
281	cbs.hicredit = opt->hicredit;
282	cbs.locredit = opt->locredit;
283	cbs.idleslope = opt->idleslope;
284	cbs.sendslope = opt->sendslope;
285
286	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
287	if (err < 0) {
288		NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
289		return err;
290	}
291
292	q->enqueue = cbs_enqueue_offload;
293	q->dequeue = cbs_dequeue_offload;
294
295	return 0;
296}
297
298static void cbs_set_port_rate(struct net_device *dev, struct cbs_sched_data *q)
299{
300	struct ethtool_link_ksettings ecmd;
301	int speed = SPEED_10;
302	int port_rate;
303	int err;
304
305	err = __ethtool_get_link_ksettings(dev, &ecmd);
306	if (err < 0)
307		goto skip;
308
309	if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
310		speed = ecmd.base.speed;
311
312skip:
313	port_rate = speed * 1000 * BYTES_PER_KBIT;
314
315	atomic64_set(&q->port_rate, port_rate);
316	netdev_dbg(dev, "cbs: set %s's port_rate to: %lld, linkspeed: %d\n",
317		   dev->name, (long long)atomic64_read(&q->port_rate),
318		   ecmd.base.speed);
319}
320
321static int cbs_dev_notifier(struct notifier_block *nb, unsigned long event,
322			    void *ptr)
323{
324	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
325	struct cbs_sched_data *q;
326	struct net_device *qdev;
327	bool found = false;
328
329	ASSERT_RTNL();
330
331	if (event != NETDEV_UP && event != NETDEV_CHANGE)
332		return NOTIFY_DONE;
333
334	spin_lock(&cbs_list_lock);
335	list_for_each_entry(q, &cbs_list, cbs_list) {
336		qdev = qdisc_dev(q->qdisc);
337		if (qdev == dev) {
338			found = true;
339			break;
340		}
341	}
342	spin_unlock(&cbs_list_lock);
343
344	if (found)
345		cbs_set_port_rate(dev, q);
346
347	return NOTIFY_DONE;
348}
349
350static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
351		      struct netlink_ext_ack *extack)
352{
353	struct cbs_sched_data *q = qdisc_priv(sch);
354	struct net_device *dev = qdisc_dev(sch);
355	struct nlattr *tb[TCA_CBS_MAX + 1];
356	struct tc_cbs_qopt *qopt;
357	int err;
358
359	err = nla_parse_nested_deprecated(tb, TCA_CBS_MAX, opt, cbs_policy,
360					  extack);
361	if (err < 0)
362		return err;
363
364	if (!tb[TCA_CBS_PARMS]) {
365		NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
366		return -EINVAL;
367	}
368
369	qopt = nla_data(tb[TCA_CBS_PARMS]);
370
371	if (!qopt->offload) {
372		cbs_set_port_rate(dev, q);
 
 
 
 
 
 
 
 
 
373		cbs_disable_offload(dev, q);
374	} else {
375		err = cbs_enable_offload(dev, q, qopt, extack);
376		if (err < 0)
377			return err;
378	}
379
380	/* Everything went OK, save the parameters used. */
381	q->hicredit = qopt->hicredit;
382	q->locredit = qopt->locredit;
383	q->idleslope = qopt->idleslope * BYTES_PER_KBIT;
384	q->sendslope = qopt->sendslope * BYTES_PER_KBIT;
385	q->offload = qopt->offload;
386
387	return 0;
388}
389
390static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
391		    struct netlink_ext_ack *extack)
392{
393	struct cbs_sched_data *q = qdisc_priv(sch);
394	struct net_device *dev = qdisc_dev(sch);
395
396	if (!opt) {
397		NL_SET_ERR_MSG(extack, "Missing CBS qdisc options  which are mandatory");
398		return -EINVAL;
399	}
400
401	q->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
402				     sch->handle, extack);
403	if (!q->qdisc)
404		return -ENOMEM;
405
406	spin_lock(&cbs_list_lock);
407	list_add(&q->cbs_list, &cbs_list);
408	spin_unlock(&cbs_list_lock);
409
410	qdisc_hash_add(q->qdisc, false);
411
412	q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
413
414	q->enqueue = cbs_enqueue_soft;
415	q->dequeue = cbs_dequeue_soft;
416
417	qdisc_watchdog_init(&q->watchdog, sch);
418
419	return cbs_change(sch, opt, extack);
420}
421
422static void cbs_destroy(struct Qdisc *sch)
423{
424	struct cbs_sched_data *q = qdisc_priv(sch);
425	struct net_device *dev = qdisc_dev(sch);
426
427	/* Nothing to do if we couldn't create the underlying qdisc */
428	if (!q->qdisc)
429		return;
430
431	qdisc_watchdog_cancel(&q->watchdog);
432	cbs_disable_offload(dev, q);
433
434	spin_lock(&cbs_list_lock);
435	list_del(&q->cbs_list);
436	spin_unlock(&cbs_list_lock);
437
438	qdisc_put(q->qdisc);
439}
440
441static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
442{
443	struct cbs_sched_data *q = qdisc_priv(sch);
444	struct tc_cbs_qopt opt = { };
445	struct nlattr *nest;
446
447	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
448	if (!nest)
449		goto nla_put_failure;
450
451	opt.hicredit = q->hicredit;
452	opt.locredit = q->locredit;
453	opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT);
454	opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT);
455	opt.offload = q->offload;
456
457	if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
458		goto nla_put_failure;
459
460	return nla_nest_end(skb, nest);
461
462nla_put_failure:
463	nla_nest_cancel(skb, nest);
464	return -1;
465}
466
467static int cbs_dump_class(struct Qdisc *sch, unsigned long cl,
468			  struct sk_buff *skb, struct tcmsg *tcm)
469{
470	struct cbs_sched_data *q = qdisc_priv(sch);
471
472	if (cl != 1 || !q->qdisc)	/* only one class */
473		return -ENOENT;
474
475	tcm->tcm_handle |= TC_H_MIN(1);
476	tcm->tcm_info = q->qdisc->handle;
477
478	return 0;
479}
480
481static int cbs_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
482		     struct Qdisc **old, struct netlink_ext_ack *extack)
483{
484	struct cbs_sched_data *q = qdisc_priv(sch);
485
486	if (!new) {
487		new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
488					sch->handle, NULL);
489		if (!new)
490			new = &noop_qdisc;
491	}
492
493	*old = qdisc_replace(sch, new, &q->qdisc);
494	return 0;
495}
496
497static struct Qdisc *cbs_leaf(struct Qdisc *sch, unsigned long arg)
498{
499	struct cbs_sched_data *q = qdisc_priv(sch);
500
501	return q->qdisc;
502}
503
504static unsigned long cbs_find(struct Qdisc *sch, u32 classid)
505{
506	return 1;
507}
508
509static void cbs_walk(struct Qdisc *sch, struct qdisc_walker *walker)
510{
511	if (!walker->stop) {
512		if (walker->count >= walker->skip) {
513			if (walker->fn(sch, 1, walker) < 0) {
514				walker->stop = 1;
515				return;
516			}
517		}
518		walker->count++;
519	}
520}
521
522static const struct Qdisc_class_ops cbs_class_ops = {
523	.graft		=	cbs_graft,
524	.leaf		=	cbs_leaf,
525	.find		=	cbs_find,
526	.walk		=	cbs_walk,
527	.dump		=	cbs_dump_class,
528};
529
530static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
531	.id		=	"cbs",
532	.cl_ops		=	&cbs_class_ops,
533	.priv_size	=	sizeof(struct cbs_sched_data),
534	.enqueue	=	cbs_enqueue,
535	.dequeue	=	cbs_dequeue,
536	.peek		=	qdisc_peek_dequeued,
537	.init		=	cbs_init,
538	.reset		=	qdisc_reset_queue,
539	.destroy	=	cbs_destroy,
540	.change		=	cbs_change,
541	.dump		=	cbs_dump,
542	.owner		=	THIS_MODULE,
543};
544
545static struct notifier_block cbs_device_notifier = {
546	.notifier_call = cbs_dev_notifier,
547};
548
549static int __init cbs_module_init(void)
550{
551	int err;
552
553	err = register_netdevice_notifier(&cbs_device_notifier);
554	if (err)
555		return err;
556
557	err = register_qdisc(&cbs_qdisc_ops);
558	if (err)
559		unregister_netdevice_notifier(&cbs_device_notifier);
560
561	return err;
562}
563
564static void __exit cbs_module_exit(void)
565{
566	unregister_qdisc(&cbs_qdisc_ops);
567	unregister_netdevice_notifier(&cbs_device_notifier);
568}
569module_init(cbs_module_init)
570module_exit(cbs_module_exit)
571MODULE_LICENSE("GPL");
v4.17
 
  1/*
  2 * net/sched/sch_cbs.c	Credit Based Shaper
  3 *
  4 *		This program is free software; you can redistribute it and/or
  5 *		modify it under the terms of the GNU General Public License
  6 *		as published by the Free Software Foundation; either version
  7 *		2 of the License, or (at your option) any later version.
  8 *
  9 * Authors:	Vinicius Costa Gomes <vinicius.gomes@intel.com>
 10 *
 11 */
 12
 13/* Credit Based Shaper (CBS)
 14 * =========================
 15 *
 16 * This is a simple rate-limiting shaper aimed at TSN applications on
 17 * systems with known traffic workloads.
 18 *
 19 * Its algorithm is defined by the IEEE 802.1Q-2014 Specification,
 20 * Section 8.6.8.2, and explained in more detail in the Annex L of the
 21 * same specification.
 22 *
 23 * There are four tunables to be considered:
 24 *
 25 *	'idleslope': Idleslope is the rate of credits that is
 26 *	accumulated (in kilobits per second) when there is at least
 27 *	one packet waiting for transmission. Packets are transmitted
 28 *	when the current value of credits is equal or greater than
 29 *	zero. When there is no packet to be transmitted the amount of
 30 *	credits is set to zero. This is the main tunable of the CBS
 31 *	algorithm.
 32 *
 33 *	'sendslope':
 34 *	Sendslope is the rate of credits that is depleted (it should be a
 35 *	negative number of kilobits per second) when a transmission is
 36 *	ocurring. It can be calculated as follows, (IEEE 802.1Q-2014 Section
 37 *	8.6.8.2 item g):
 38 *
 39 *	sendslope = idleslope - port_transmit_rate
 40 *
 41 *	'hicredit': Hicredit defines the maximum amount of credits (in
 42 *	bytes) that can be accumulated. Hicredit depends on the
 43 *	characteristics of interfering traffic,
 44 *	'max_interference_size' is the maximum size of any burst of
 45 *	traffic that can delay the transmission of a frame that is
 46 *	available for transmission for this traffic class, (IEEE
 47 *	802.1Q-2014 Annex L, Equation L-3):
 48 *
 49 *	hicredit = max_interference_size * (idleslope / port_transmit_rate)
 50 *
 51 *	'locredit': Locredit is the minimum amount of credits that can
 52 *	be reached. It is a function of the traffic flowing through
 53 *	this qdisc (IEEE 802.1Q-2014 Annex L, Equation L-2):
 54 *
 55 *	locredit = max_frame_size * (sendslope / port_transmit_rate)
 56 */
 57
 58#include <linux/module.h>
 59#include <linux/types.h>
 60#include <linux/kernel.h>
 61#include <linux/string.h>
 62#include <linux/errno.h>
 63#include <linux/skbuff.h>
 
 64#include <net/netlink.h>
 65#include <net/sch_generic.h>
 66#include <net/pkt_sched.h>
 67
 
 
 
 68#define BYTES_PER_KBIT (1000LL / 8)
 69
 70struct cbs_sched_data {
 71	bool offload;
 72	int queue;
 73	s64 port_rate; /* in bytes/s */
 74	s64 last; /* timestamp in ns */
 75	s64 credits; /* in bytes */
 76	s32 locredit; /* in bytes */
 77	s32 hicredit; /* in bytes */
 78	s64 sendslope; /* in bytes/s */
 79	s64 idleslope; /* in bytes/s */
 80	struct qdisc_watchdog watchdog;
 81	int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch);
 
 82	struct sk_buff *(*dequeue)(struct Qdisc *sch);
 
 
 83};
 84
 85static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 86{
 87	return qdisc_enqueue_tail(skb, sch);
 
 
 
 88}
 89
 90static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch)
 
 91{
 92	struct cbs_sched_data *q = qdisc_priv(sch);
 
 93
 94	if (sch->q.qlen == 0 && q->credits > 0) {
 95		/* We need to stop accumulating credits when there's
 96		 * no enqueued packets and q->credits is positive.
 97		 */
 98		q->credits = 0;
 99		q->last = ktime_get_ns();
100	}
101
102	return qdisc_enqueue_tail(skb, sch);
103}
104
105static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
106		       struct sk_buff **to_free)
107{
108	struct cbs_sched_data *q = qdisc_priv(sch);
109
110	return q->enqueue(skb, sch);
111}
112
113/* timediff is in ns, slope is in bytes/s */
114static s64 timediff_to_credits(s64 timediff, s64 slope)
115{
116	return div64_s64(timediff * slope, NSEC_PER_SEC);
117}
118
119static s64 delay_from_credits(s64 credits, s64 slope)
120{
121	if (unlikely(slope == 0))
122		return S64_MAX;
123
124	return div64_s64(-credits * NSEC_PER_SEC, slope);
125}
126
127static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
128{
129	if (unlikely(port_rate == 0))
130		return S64_MAX;
131
132	return div64_s64(len * slope, port_rate);
133}
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
136{
137	struct cbs_sched_data *q = qdisc_priv(sch);
 
138	s64 now = ktime_get_ns();
139	struct sk_buff *skb;
140	s64 credits;
141	int len;
142
143	if (q->credits < 0) {
144		credits = timediff_to_credits(now - q->last, q->idleslope);
145
146		credits = q->credits + credits;
147		q->credits = min_t(s64, credits, q->hicredit);
148
149		if (q->credits < 0) {
150			s64 delay;
151
152			delay = delay_from_credits(q->credits, q->idleslope);
153			qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
154
155			q->last = now;
156
157			return NULL;
158		}
159	}
160
161	skb = qdisc_dequeue_head(sch);
162	if (!skb)
163		return NULL;
164
165	len = qdisc_pkt_len(skb);
166
167	/* As sendslope is a negative number, this will decrease the
168	 * amount of q->credits.
169	 */
170	credits = credits_from_len(len, q->sendslope, q->port_rate);
 
171	credits += q->credits;
172
173	q->credits = max_t(s64, credits, q->locredit);
174	q->last = now;
175
176	return skb;
177}
178
179static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
180{
181	return qdisc_dequeue_head(sch);
 
 
 
182}
183
184static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
185{
186	struct cbs_sched_data *q = qdisc_priv(sch);
187
188	return q->dequeue(sch);
189}
190
191static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
192	[TCA_CBS_PARMS]	= { .len = sizeof(struct tc_cbs_qopt) },
193};
194
195static void cbs_disable_offload(struct net_device *dev,
196				struct cbs_sched_data *q)
197{
198	struct tc_cbs_qopt_offload cbs = { };
199	const struct net_device_ops *ops;
200	int err;
201
202	if (!q->offload)
203		return;
204
205	q->enqueue = cbs_enqueue_soft;
206	q->dequeue = cbs_dequeue_soft;
207
208	ops = dev->netdev_ops;
209	if (!ops->ndo_setup_tc)
210		return;
211
212	cbs.queue = q->queue;
213	cbs.enable = 0;
214
215	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
216	if (err < 0)
217		pr_warn("Couldn't disable CBS offload for queue %d\n",
218			cbs.queue);
219}
220
221static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
222			      const struct tc_cbs_qopt *opt,
223			      struct netlink_ext_ack *extack)
224{
225	const struct net_device_ops *ops = dev->netdev_ops;
226	struct tc_cbs_qopt_offload cbs = { };
227	int err;
228
229	if (!ops->ndo_setup_tc) {
230		NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
231		return -EOPNOTSUPP;
232	}
233
234	cbs.queue = q->queue;
235
236	cbs.enable = 1;
237	cbs.hicredit = opt->hicredit;
238	cbs.locredit = opt->locredit;
239	cbs.idleslope = opt->idleslope;
240	cbs.sendslope = opt->sendslope;
241
242	err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
243	if (err < 0) {
244		NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
245		return err;
246	}
247
248	q->enqueue = cbs_enqueue_offload;
249	q->dequeue = cbs_dequeue_offload;
250
251	return 0;
252}
253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
255		      struct netlink_ext_ack *extack)
256{
257	struct cbs_sched_data *q = qdisc_priv(sch);
258	struct net_device *dev = qdisc_dev(sch);
259	struct nlattr *tb[TCA_CBS_MAX + 1];
260	struct tc_cbs_qopt *qopt;
261	int err;
262
263	err = nla_parse_nested(tb, TCA_CBS_MAX, opt, cbs_policy, extack);
 
264	if (err < 0)
265		return err;
266
267	if (!tb[TCA_CBS_PARMS]) {
268		NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
269		return -EINVAL;
270	}
271
272	qopt = nla_data(tb[TCA_CBS_PARMS]);
273
274	if (!qopt->offload) {
275		struct ethtool_link_ksettings ecmd;
276		s64 link_speed;
277
278		if (!__ethtool_get_link_ksettings(dev, &ecmd))
279			link_speed = ecmd.base.speed;
280		else
281			link_speed = SPEED_1000;
282
283		q->port_rate = link_speed * 1000 * BYTES_PER_KBIT;
284
285		cbs_disable_offload(dev, q);
286	} else {
287		err = cbs_enable_offload(dev, q, qopt, extack);
288		if (err < 0)
289			return err;
290	}
291
292	/* Everything went OK, save the parameters used. */
293	q->hicredit = qopt->hicredit;
294	q->locredit = qopt->locredit;
295	q->idleslope = qopt->idleslope * BYTES_PER_KBIT;
296	q->sendslope = qopt->sendslope * BYTES_PER_KBIT;
297	q->offload = qopt->offload;
298
299	return 0;
300}
301
302static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
303		    struct netlink_ext_ack *extack)
304{
305	struct cbs_sched_data *q = qdisc_priv(sch);
306	struct net_device *dev = qdisc_dev(sch);
307
308	if (!opt) {
309		NL_SET_ERR_MSG(extack, "Missing CBS qdisc options  which are mandatory");
310		return -EINVAL;
311	}
312
 
 
 
 
 
 
 
 
 
 
 
313	q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
314
315	q->enqueue = cbs_enqueue_soft;
316	q->dequeue = cbs_dequeue_soft;
317
318	qdisc_watchdog_init(&q->watchdog, sch);
319
320	return cbs_change(sch, opt, extack);
321}
322
323static void cbs_destroy(struct Qdisc *sch)
324{
325	struct cbs_sched_data *q = qdisc_priv(sch);
326	struct net_device *dev = qdisc_dev(sch);
327
 
 
 
 
328	qdisc_watchdog_cancel(&q->watchdog);
 
329
330	cbs_disable_offload(dev, q);
 
 
 
 
331}
332
333static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
334{
335	struct cbs_sched_data *q = qdisc_priv(sch);
336	struct tc_cbs_qopt opt = { };
337	struct nlattr *nest;
338
339	nest = nla_nest_start(skb, TCA_OPTIONS);
340	if (!nest)
341		goto nla_put_failure;
342
343	opt.hicredit = q->hicredit;
344	opt.locredit = q->locredit;
345	opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT);
346	opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT);
347	opt.offload = q->offload;
348
349	if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
350		goto nla_put_failure;
351
352	return nla_nest_end(skb, nest);
353
354nla_put_failure:
355	nla_nest_cancel(skb, nest);
356	return -1;
357}
358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
360	.id		=	"cbs",
 
361	.priv_size	=	sizeof(struct cbs_sched_data),
362	.enqueue	=	cbs_enqueue,
363	.dequeue	=	cbs_dequeue,
364	.peek		=	qdisc_peek_dequeued,
365	.init		=	cbs_init,
366	.reset		=	qdisc_reset_queue,
367	.destroy	=	cbs_destroy,
368	.change		=	cbs_change,
369	.dump		=	cbs_dump,
370	.owner		=	THIS_MODULE,
371};
372
 
 
 
 
373static int __init cbs_module_init(void)
374{
375	return register_qdisc(&cbs_qdisc_ops);
 
 
 
 
 
 
 
 
 
 
376}
377
378static void __exit cbs_module_exit(void)
379{
380	unregister_qdisc(&cbs_qdisc_ops);
 
381}
382module_init(cbs_module_init)
383module_exit(cbs_module_exit)
384MODULE_LICENSE("GPL");