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/ethtool.h>
54 #include <linux/module.h>
55 #include <linux/types.h>
56 #include <linux/kernel.h>
57 #include <linux/string.h>
58 #include <linux/errno.h>
59 #include <linux/skbuff.h>
60 #include <linux/units.h>
61
62 #include <net/netevent.h>
63 #include <net/netlink.h>
64 #include <net/sch_generic.h>
65 #include <net/pkt_sched.h>
66
67 static LIST_HEAD(cbs_list);
68 static DEFINE_SPINLOCK(cbs_list_lock);
69
70 struct cbs_sched_data {
71 bool offload;
72 int queue;
73 atomic64_t 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 **to_free);
83 struct sk_buff *(*dequeue)(struct Qdisc *sch);
84 struct Qdisc *qdisc;
85 struct list_head cbs_list;
86 };
87
cbs_child_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct Qdisc * child,struct sk_buff ** to_free)88 static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch,
89 struct Qdisc *child,
90 struct sk_buff **to_free)
91 {
92 unsigned int len = qdisc_pkt_len(skb);
93 int err;
94
95 err = child->ops->enqueue(skb, child, to_free);
96 if (err != NET_XMIT_SUCCESS)
97 return err;
98
99 sch->qstats.backlog += len;
100 sch->q.qlen++;
101
102 return NET_XMIT_SUCCESS;
103 }
104
cbs_enqueue_offload(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)105 static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch,
106 struct sk_buff **to_free)
107 {
108 struct cbs_sched_data *q = qdisc_priv(sch);
109 struct Qdisc *qdisc = q->qdisc;
110
111 return cbs_child_enqueue(skb, sch, qdisc, to_free);
112 }
113
cbs_enqueue_soft(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)114 static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch,
115 struct sk_buff **to_free)
116 {
117 struct cbs_sched_data *q = qdisc_priv(sch);
118 struct Qdisc *qdisc = q->qdisc;
119
120 if (sch->q.qlen == 0 && q->credits > 0) {
121 /* We need to stop accumulating credits when there's
122 * no enqueued packets and q->credits is positive.
123 */
124 q->credits = 0;
125 q->last = ktime_get_ns();
126 }
127
128 return cbs_child_enqueue(skb, sch, qdisc, to_free);
129 }
130
cbs_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)131 static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
132 struct sk_buff **to_free)
133 {
134 struct cbs_sched_data *q = qdisc_priv(sch);
135
136 return q->enqueue(skb, sch, to_free);
137 }
138
139 /* timediff is in ns, slope is in bytes/s */
timediff_to_credits(s64 timediff,s64 slope)140 static s64 timediff_to_credits(s64 timediff, s64 slope)
141 {
142 return div64_s64(timediff * slope, NSEC_PER_SEC);
143 }
144
delay_from_credits(s64 credits,s64 slope)145 static s64 delay_from_credits(s64 credits, s64 slope)
146 {
147 if (unlikely(slope == 0))
148 return S64_MAX;
149
150 return div64_s64(-credits * NSEC_PER_SEC, slope);
151 }
152
credits_from_len(unsigned int len,s64 slope,s64 port_rate)153 static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
154 {
155 if (unlikely(port_rate == 0))
156 return S64_MAX;
157
158 return div64_s64(len * slope, port_rate);
159 }
160
cbs_child_dequeue(struct Qdisc * sch,struct Qdisc * child)161 static struct sk_buff *cbs_child_dequeue(struct Qdisc *sch, struct Qdisc *child)
162 {
163 struct sk_buff *skb;
164
165 skb = child->ops->dequeue(child);
166 if (!skb)
167 return NULL;
168
169 qdisc_qstats_backlog_dec(sch, skb);
170 qdisc_bstats_update(sch, skb);
171 sch->q.qlen--;
172
173 return skb;
174 }
175
cbs_dequeue_soft(struct Qdisc * sch)176 static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
177 {
178 struct cbs_sched_data *q = qdisc_priv(sch);
179 struct Qdisc *qdisc = q->qdisc;
180 s64 now = ktime_get_ns();
181 struct sk_buff *skb;
182 s64 credits;
183 int len;
184
185 /* The previous packet is still being sent */
186 if (now < q->last) {
187 qdisc_watchdog_schedule_ns(&q->watchdog, q->last);
188 return NULL;
189 }
190 if (q->credits < 0) {
191 credits = timediff_to_credits(now - q->last, q->idleslope);
192
193 credits = q->credits + credits;
194 q->credits = min_t(s64, credits, q->hicredit);
195
196 if (q->credits < 0) {
197 s64 delay;
198
199 delay = delay_from_credits(q->credits, q->idleslope);
200 qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
201
202 q->last = now;
203
204 return NULL;
205 }
206 }
207 skb = cbs_child_dequeue(sch, qdisc);
208 if (!skb)
209 return NULL;
210
211 len = qdisc_pkt_len(skb);
212
213 /* As sendslope is a negative number, this will decrease the
214 * amount of q->credits.
215 */
216 credits = credits_from_len(len, q->sendslope,
217 atomic64_read(&q->port_rate));
218 credits += q->credits;
219
220 q->credits = max_t(s64, credits, q->locredit);
221 /* Estimate of the transmission of the last byte of the packet in ns */
222 if (unlikely(atomic64_read(&q->port_rate) == 0))
223 q->last = now;
224 else
225 q->last = now + div64_s64(len * NSEC_PER_SEC,
226 atomic64_read(&q->port_rate));
227
228 return skb;
229 }
230
cbs_dequeue_offload(struct Qdisc * sch)231 static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
232 {
233 struct cbs_sched_data *q = qdisc_priv(sch);
234 struct Qdisc *qdisc = q->qdisc;
235
236 return cbs_child_dequeue(sch, qdisc);
237 }
238
cbs_dequeue(struct Qdisc * sch)239 static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
240 {
241 struct cbs_sched_data *q = qdisc_priv(sch);
242
243 return q->dequeue(sch);
244 }
245
cbs_reset(struct Qdisc * sch)246 static void cbs_reset(struct Qdisc *sch)
247 {
248 struct cbs_sched_data *q = qdisc_priv(sch);
249
250 /* Nothing to do if we couldn't create the underlying qdisc */
251 if (!q->qdisc)
252 return;
253
254 qdisc_reset(q->qdisc);
255 qdisc_watchdog_cancel(&q->watchdog);
256 q->credits = 0;
257 q->last = 0;
258 }
259
260 static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
261 [TCA_CBS_PARMS] = { .len = sizeof(struct tc_cbs_qopt) },
262 };
263
cbs_disable_offload(struct net_device * dev,struct cbs_sched_data * q)264 static void cbs_disable_offload(struct net_device *dev,
265 struct cbs_sched_data *q)
266 {
267 struct tc_cbs_qopt_offload cbs = { };
268 const struct net_device_ops *ops;
269 int err;
270
271 if (!q->offload)
272 return;
273
274 q->enqueue = cbs_enqueue_soft;
275 q->dequeue = cbs_dequeue_soft;
276
277 ops = dev->netdev_ops;
278 if (!ops->ndo_setup_tc)
279 return;
280
281 cbs.queue = q->queue;
282 cbs.enable = 0;
283
284 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
285 if (err < 0)
286 pr_warn("Couldn't disable CBS offload for queue %d\n",
287 cbs.queue);
288 }
289
cbs_enable_offload(struct net_device * dev,struct cbs_sched_data * q,const struct tc_cbs_qopt * opt,struct netlink_ext_ack * extack)290 static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
291 const struct tc_cbs_qopt *opt,
292 struct netlink_ext_ack *extack)
293 {
294 const struct net_device_ops *ops = dev->netdev_ops;
295 struct tc_cbs_qopt_offload cbs = { };
296 int err;
297
298 if (!ops->ndo_setup_tc) {
299 NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
300 return -EOPNOTSUPP;
301 }
302
303 cbs.queue = q->queue;
304
305 cbs.enable = 1;
306 cbs.hicredit = opt->hicredit;
307 cbs.locredit = opt->locredit;
308 cbs.idleslope = opt->idleslope;
309 cbs.sendslope = opt->sendslope;
310
311 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
312 if (err < 0) {
313 NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
314 return err;
315 }
316
317 q->enqueue = cbs_enqueue_offload;
318 q->dequeue = cbs_dequeue_offload;
319
320 return 0;
321 }
322
cbs_set_port_rate(struct net_device * dev,struct cbs_sched_data * q)323 static void cbs_set_port_rate(struct net_device *dev, struct cbs_sched_data *q)
324 {
325 struct ethtool_link_ksettings ecmd;
326 int speed = SPEED_10;
327 s64 port_rate;
328 int err;
329
330 err = __ethtool_get_link_ksettings(dev, &ecmd);
331 if (err < 0)
332 goto skip;
333
334 if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
335 speed = ecmd.base.speed;
336
337 skip:
338 port_rate = speed * 1000 * BYTES_PER_KBIT;
339
340 atomic64_set(&q->port_rate, port_rate);
341 netdev_dbg(dev, "cbs: set %s's port_rate to: %lld, linkspeed: %d\n",
342 dev->name, (long long)atomic64_read(&q->port_rate),
343 ecmd.base.speed);
344 }
345
cbs_dev_notifier(struct notifier_block * nb,unsigned long event,void * ptr)346 static int cbs_dev_notifier(struct notifier_block *nb, unsigned long event,
347 void *ptr)
348 {
349 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
350 struct cbs_sched_data *q;
351 struct net_device *qdev;
352 bool found = false;
353
354 ASSERT_RTNL();
355
356 if (event != NETDEV_UP && event != NETDEV_CHANGE)
357 return NOTIFY_DONE;
358
359 spin_lock(&cbs_list_lock);
360 list_for_each_entry(q, &cbs_list, cbs_list) {
361 qdev = qdisc_dev(q->qdisc);
362 if (qdev == dev) {
363 found = true;
364 break;
365 }
366 }
367 spin_unlock(&cbs_list_lock);
368
369 if (found)
370 cbs_set_port_rate(dev, q);
371
372 return NOTIFY_DONE;
373 }
374
cbs_change(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)375 static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
376 struct netlink_ext_ack *extack)
377 {
378 struct cbs_sched_data *q = qdisc_priv(sch);
379 struct net_device *dev = qdisc_dev(sch);
380 struct nlattr *tb[TCA_CBS_MAX + 1];
381 struct tc_cbs_qopt *qopt;
382 int err;
383
384 err = nla_parse_nested_deprecated(tb, TCA_CBS_MAX, opt, cbs_policy,
385 extack);
386 if (err < 0)
387 return err;
388
389 if (!tb[TCA_CBS_PARMS]) {
390 NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
391 return -EINVAL;
392 }
393
394 qopt = nla_data(tb[TCA_CBS_PARMS]);
395
396 if (!qopt->offload) {
397 cbs_set_port_rate(dev, q);
398 cbs_disable_offload(dev, q);
399 } else {
400 err = cbs_enable_offload(dev, q, qopt, extack);
401 if (err < 0)
402 return err;
403 }
404
405 /* Everything went OK, save the parameters used. */
406 WRITE_ONCE(q->hicredit, qopt->hicredit);
407 WRITE_ONCE(q->locredit, qopt->locredit);
408 WRITE_ONCE(q->idleslope, qopt->idleslope * BYTES_PER_KBIT);
409 WRITE_ONCE(q->sendslope, qopt->sendslope * BYTES_PER_KBIT);
410 WRITE_ONCE(q->offload, qopt->offload);
411
412 return 0;
413 }
414
cbs_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)415 static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
416 struct netlink_ext_ack *extack)
417 {
418 struct cbs_sched_data *q = qdisc_priv(sch);
419 struct net_device *dev = qdisc_dev(sch);
420
421 if (!opt) {
422 NL_SET_ERR_MSG(extack, "Missing CBS qdisc options which are mandatory");
423 return -EINVAL;
424 }
425
426 q->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
427 sch->handle, extack);
428 if (!q->qdisc)
429 return -ENOMEM;
430
431 spin_lock(&cbs_list_lock);
432 list_add(&q->cbs_list, &cbs_list);
433 spin_unlock(&cbs_list_lock);
434
435 qdisc_hash_add(q->qdisc, false);
436
437 q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
438
439 q->enqueue = cbs_enqueue_soft;
440 q->dequeue = cbs_dequeue_soft;
441
442 qdisc_watchdog_init(&q->watchdog, sch);
443
444 return cbs_change(sch, opt, extack);
445 }
446
cbs_destroy(struct Qdisc * sch)447 static void cbs_destroy(struct Qdisc *sch)
448 {
449 struct cbs_sched_data *q = qdisc_priv(sch);
450 struct net_device *dev = qdisc_dev(sch);
451
452 /* Nothing to do if we couldn't create the underlying qdisc */
453 if (!q->qdisc)
454 return;
455
456 qdisc_watchdog_cancel(&q->watchdog);
457 cbs_disable_offload(dev, q);
458
459 spin_lock(&cbs_list_lock);
460 list_del(&q->cbs_list);
461 spin_unlock(&cbs_list_lock);
462
463 qdisc_put(q->qdisc);
464 }
465
cbs_dump(struct Qdisc * sch,struct sk_buff * skb)466 static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
467 {
468 struct cbs_sched_data *q = qdisc_priv(sch);
469 struct tc_cbs_qopt opt = { };
470 struct nlattr *nest;
471
472 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
473 if (!nest)
474 goto nla_put_failure;
475
476 opt.hicredit = READ_ONCE(q->hicredit);
477 opt.locredit = READ_ONCE(q->locredit);
478 opt.sendslope = div64_s64(READ_ONCE(q->sendslope), BYTES_PER_KBIT);
479 opt.idleslope = div64_s64(READ_ONCE(q->idleslope), BYTES_PER_KBIT);
480 opt.offload = READ_ONCE(q->offload);
481
482 if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
483 goto nla_put_failure;
484
485 return nla_nest_end(skb, nest);
486
487 nla_put_failure:
488 nla_nest_cancel(skb, nest);
489 return -1;
490 }
491
cbs_dump_class(struct Qdisc * sch,unsigned long cl,struct sk_buff * skb,struct tcmsg * tcm)492 static int cbs_dump_class(struct Qdisc *sch, unsigned long cl,
493 struct sk_buff *skb, struct tcmsg *tcm)
494 {
495 struct cbs_sched_data *q = qdisc_priv(sch);
496
497 if (cl != 1 || !q->qdisc) /* only one class */
498 return -ENOENT;
499
500 tcm->tcm_handle |= TC_H_MIN(1);
501 tcm->tcm_info = q->qdisc->handle;
502
503 return 0;
504 }
505
cbs_graft(struct Qdisc * sch,unsigned long arg,struct Qdisc * new,struct Qdisc ** old,struct netlink_ext_ack * extack)506 static int cbs_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
507 struct Qdisc **old, struct netlink_ext_ack *extack)
508 {
509 struct cbs_sched_data *q = qdisc_priv(sch);
510
511 if (!new) {
512 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
513 sch->handle, NULL);
514 if (!new)
515 new = &noop_qdisc;
516 }
517
518 *old = qdisc_replace(sch, new, &q->qdisc);
519 return 0;
520 }
521
cbs_leaf(struct Qdisc * sch,unsigned long arg)522 static struct Qdisc *cbs_leaf(struct Qdisc *sch, unsigned long arg)
523 {
524 struct cbs_sched_data *q = qdisc_priv(sch);
525
526 return q->qdisc;
527 }
528
cbs_find(struct Qdisc * sch,u32 classid)529 static unsigned long cbs_find(struct Qdisc *sch, u32 classid)
530 {
531 return 1;
532 }
533
cbs_walk(struct Qdisc * sch,struct qdisc_walker * walker)534 static void cbs_walk(struct Qdisc *sch, struct qdisc_walker *walker)
535 {
536 if (!walker->stop) {
537 tc_qdisc_stats_dump(sch, 1, walker);
538 }
539 }
540
541 static const struct Qdisc_class_ops cbs_class_ops = {
542 .graft = cbs_graft,
543 .leaf = cbs_leaf,
544 .find = cbs_find,
545 .walk = cbs_walk,
546 .dump = cbs_dump_class,
547 };
548
549 static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
550 .id = "cbs",
551 .cl_ops = &cbs_class_ops,
552 .priv_size = sizeof(struct cbs_sched_data),
553 .enqueue = cbs_enqueue,
554 .dequeue = cbs_dequeue,
555 .peek = qdisc_peek_dequeued,
556 .init = cbs_init,
557 .reset = cbs_reset,
558 .destroy = cbs_destroy,
559 .change = cbs_change,
560 .dump = cbs_dump,
561 .owner = THIS_MODULE,
562 };
563 MODULE_ALIAS_NET_SCH("cbs");
564
565 static struct notifier_block cbs_device_notifier = {
566 .notifier_call = cbs_dev_notifier,
567 };
568
cbs_module_init(void)569 static int __init cbs_module_init(void)
570 {
571 int err;
572
573 err = register_netdevice_notifier(&cbs_device_notifier);
574 if (err)
575 return err;
576
577 err = register_qdisc(&cbs_qdisc_ops);
578 if (err)
579 unregister_netdevice_notifier(&cbs_device_notifier);
580
581 return err;
582 }
583
cbs_module_exit(void)584 static void __exit cbs_module_exit(void)
585 {
586 unregister_qdisc(&cbs_qdisc_ops);
587 unregister_netdevice_notifier(&cbs_device_notifier);
588 }
589 module_init(cbs_module_init)
590 module_exit(cbs_module_exit)
591 MODULE_LICENSE("GPL");
592 MODULE_DESCRIPTION("Credit Based shaper");
593