2 * net/sched/sch_cbs.c Credit Based Shaper
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.
9 * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com>
13 /* Credit Based Shaper (CBS)
14 * =========================
16 * This is a simple rate-limiting shaper aimed at TSN applications on
17 * systems with known traffic workloads.
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
23 * There are four tunables to be considered:
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
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
39 * sendslope = idleslope - port_transmit_rate
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):
49 * hicredit = max_interference_size * (idleslope / port_transmit_rate)
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):
55 * locredit = max_frame_size * (sendslope / port_transmit_rate)
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>
68 #define BYTES_PER_KBIT (1000LL / 8)
70 struct cbs_sched_data
{
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
**to_free
);
83 struct sk_buff
*(*dequeue
)(struct Qdisc
*sch
);
87 static int cbs_child_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
89 struct sk_buff
**to_free
)
93 err
= child
->ops
->enqueue(skb
, child
, to_free
);
94 if (err
!= NET_XMIT_SUCCESS
)
97 qdisc_qstats_backlog_inc(sch
, skb
);
100 return NET_XMIT_SUCCESS
;
103 static int cbs_enqueue_offload(struct sk_buff
*skb
, struct Qdisc
*sch
,
104 struct sk_buff
**to_free
)
106 struct cbs_sched_data
*q
= qdisc_priv(sch
);
107 struct Qdisc
*qdisc
= q
->qdisc
;
109 return cbs_child_enqueue(skb
, sch
, qdisc
, to_free
);
112 static int cbs_enqueue_soft(struct sk_buff
*skb
, struct Qdisc
*sch
,
113 struct sk_buff
**to_free
)
115 struct cbs_sched_data
*q
= qdisc_priv(sch
);
116 struct Qdisc
*qdisc
= q
->qdisc
;
118 if (sch
->q
.qlen
== 0 && q
->credits
> 0) {
119 /* We need to stop accumulating credits when there's
120 * no enqueued packets and q->credits is positive.
123 q
->last
= ktime_get_ns();
126 return cbs_child_enqueue(skb
, sch
, qdisc
, to_free
);
129 static int cbs_enqueue(struct sk_buff
*skb
, struct Qdisc
*sch
,
130 struct sk_buff
**to_free
)
132 struct cbs_sched_data
*q
= qdisc_priv(sch
);
134 return q
->enqueue(skb
, sch
, to_free
);
137 /* timediff is in ns, slope is in bytes/s */
138 static s64
timediff_to_credits(s64 timediff
, s64 slope
)
140 return div64_s64(timediff
* slope
, NSEC_PER_SEC
);
143 static s64
delay_from_credits(s64 credits
, s64 slope
)
145 if (unlikely(slope
== 0))
148 return div64_s64(-credits
* NSEC_PER_SEC
, slope
);
151 static s64
credits_from_len(unsigned int len
, s64 slope
, s64 port_rate
)
153 if (unlikely(port_rate
== 0))
156 return div64_s64(len
* slope
, port_rate
);
159 static struct sk_buff
*cbs_child_dequeue(struct Qdisc
*sch
, struct Qdisc
*child
)
163 skb
= child
->ops
->dequeue(child
);
167 qdisc_qstats_backlog_dec(sch
, skb
);
168 qdisc_bstats_update(sch
, skb
);
174 static struct sk_buff
*cbs_dequeue_soft(struct Qdisc
*sch
)
176 struct cbs_sched_data
*q
= qdisc_priv(sch
);
177 struct Qdisc
*qdisc
= q
->qdisc
;
178 s64 now
= ktime_get_ns();
183 if (q
->credits
< 0) {
184 credits
= timediff_to_credits(now
- q
->last
, q
->idleslope
);
186 credits
= q
->credits
+ credits
;
187 q
->credits
= min_t(s64
, credits
, q
->hicredit
);
189 if (q
->credits
< 0) {
192 delay
= delay_from_credits(q
->credits
, q
->idleslope
);
193 qdisc_watchdog_schedule_ns(&q
->watchdog
, now
+ delay
);
200 skb
= cbs_child_dequeue(sch
, qdisc
);
204 len
= qdisc_pkt_len(skb
);
206 /* As sendslope is a negative number, this will decrease the
207 * amount of q->credits.
209 credits
= credits_from_len(len
, q
->sendslope
, q
->port_rate
);
210 credits
+= q
->credits
;
212 q
->credits
= max_t(s64
, credits
, q
->locredit
);
218 static struct sk_buff
*cbs_dequeue_offload(struct Qdisc
*sch
)
220 struct cbs_sched_data
*q
= qdisc_priv(sch
);
221 struct Qdisc
*qdisc
= q
->qdisc
;
223 return cbs_child_dequeue(sch
, qdisc
);
226 static struct sk_buff
*cbs_dequeue(struct Qdisc
*sch
)
228 struct cbs_sched_data
*q
= qdisc_priv(sch
);
230 return q
->dequeue(sch
);
233 static const struct nla_policy cbs_policy
[TCA_CBS_MAX
+ 1] = {
234 [TCA_CBS_PARMS
] = { .len
= sizeof(struct tc_cbs_qopt
) },
237 static void cbs_disable_offload(struct net_device
*dev
,
238 struct cbs_sched_data
*q
)
240 struct tc_cbs_qopt_offload cbs
= { };
241 const struct net_device_ops
*ops
;
247 q
->enqueue
= cbs_enqueue_soft
;
248 q
->dequeue
= cbs_dequeue_soft
;
250 ops
= dev
->netdev_ops
;
251 if (!ops
->ndo_setup_tc
)
254 cbs
.queue
= q
->queue
;
257 err
= ops
->ndo_setup_tc(dev
, TC_SETUP_QDISC_CBS
, &cbs
);
259 pr_warn("Couldn't disable CBS offload for queue %d\n",
263 static int cbs_enable_offload(struct net_device
*dev
, struct cbs_sched_data
*q
,
264 const struct tc_cbs_qopt
*opt
,
265 struct netlink_ext_ack
*extack
)
267 const struct net_device_ops
*ops
= dev
->netdev_ops
;
268 struct tc_cbs_qopt_offload cbs
= { };
271 if (!ops
->ndo_setup_tc
) {
272 NL_SET_ERR_MSG(extack
, "Specified device does not support cbs offload");
276 cbs
.queue
= q
->queue
;
279 cbs
.hicredit
= opt
->hicredit
;
280 cbs
.locredit
= opt
->locredit
;
281 cbs
.idleslope
= opt
->idleslope
;
282 cbs
.sendslope
= opt
->sendslope
;
284 err
= ops
->ndo_setup_tc(dev
, TC_SETUP_QDISC_CBS
, &cbs
);
286 NL_SET_ERR_MSG(extack
, "Specified device failed to setup cbs hardware offload");
290 q
->enqueue
= cbs_enqueue_offload
;
291 q
->dequeue
= cbs_dequeue_offload
;
296 static int cbs_change(struct Qdisc
*sch
, struct nlattr
*opt
,
297 struct netlink_ext_ack
*extack
)
299 struct cbs_sched_data
*q
= qdisc_priv(sch
);
300 struct net_device
*dev
= qdisc_dev(sch
);
301 struct nlattr
*tb
[TCA_CBS_MAX
+ 1];
302 struct tc_cbs_qopt
*qopt
;
305 err
= nla_parse_nested(tb
, TCA_CBS_MAX
, opt
, cbs_policy
, extack
);
309 if (!tb
[TCA_CBS_PARMS
]) {
310 NL_SET_ERR_MSG(extack
, "Missing CBS parameter which are mandatory");
314 qopt
= nla_data(tb
[TCA_CBS_PARMS
]);
316 if (!qopt
->offload
) {
317 struct ethtool_link_ksettings ecmd
;
320 if (!__ethtool_get_link_ksettings(dev
, &ecmd
))
321 link_speed
= ecmd
.base
.speed
;
323 link_speed
= SPEED_1000
;
325 q
->port_rate
= link_speed
* 1000 * BYTES_PER_KBIT
;
327 cbs_disable_offload(dev
, q
);
329 err
= cbs_enable_offload(dev
, q
, qopt
, extack
);
334 /* Everything went OK, save the parameters used. */
335 q
->hicredit
= qopt
->hicredit
;
336 q
->locredit
= qopt
->locredit
;
337 q
->idleslope
= qopt
->idleslope
* BYTES_PER_KBIT
;
338 q
->sendslope
= qopt
->sendslope
* BYTES_PER_KBIT
;
339 q
->offload
= qopt
->offload
;
344 static int cbs_init(struct Qdisc
*sch
, struct nlattr
*opt
,
345 struct netlink_ext_ack
*extack
)
347 struct cbs_sched_data
*q
= qdisc_priv(sch
);
348 struct net_device
*dev
= qdisc_dev(sch
);
351 NL_SET_ERR_MSG(extack
, "Missing CBS qdisc options which are mandatory");
355 q
->qdisc
= qdisc_create_dflt(sch
->dev_queue
, &pfifo_qdisc_ops
,
356 sch
->handle
, extack
);
360 qdisc_hash_add(q
->qdisc
, false);
362 q
->queue
= sch
->dev_queue
- netdev_get_tx_queue(dev
, 0);
364 q
->enqueue
= cbs_enqueue_soft
;
365 q
->dequeue
= cbs_dequeue_soft
;
367 qdisc_watchdog_init(&q
->watchdog
, sch
);
369 return cbs_change(sch
, opt
, extack
);
372 static void cbs_destroy(struct Qdisc
*sch
)
374 struct cbs_sched_data
*q
= qdisc_priv(sch
);
375 struct net_device
*dev
= qdisc_dev(sch
);
377 qdisc_watchdog_cancel(&q
->watchdog
);
379 cbs_disable_offload(dev
, q
);
385 static int cbs_dump(struct Qdisc
*sch
, struct sk_buff
*skb
)
387 struct cbs_sched_data
*q
= qdisc_priv(sch
);
388 struct tc_cbs_qopt opt
= { };
391 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
393 goto nla_put_failure
;
395 opt
.hicredit
= q
->hicredit
;
396 opt
.locredit
= q
->locredit
;
397 opt
.sendslope
= div64_s64(q
->sendslope
, BYTES_PER_KBIT
);
398 opt
.idleslope
= div64_s64(q
->idleslope
, BYTES_PER_KBIT
);
399 opt
.offload
= q
->offload
;
401 if (nla_put(skb
, TCA_CBS_PARMS
, sizeof(opt
), &opt
))
402 goto nla_put_failure
;
404 return nla_nest_end(skb
, nest
);
407 nla_nest_cancel(skb
, nest
);
411 static int cbs_dump_class(struct Qdisc
*sch
, unsigned long cl
,
412 struct sk_buff
*skb
, struct tcmsg
*tcm
)
414 struct cbs_sched_data
*q
= qdisc_priv(sch
);
416 if (cl
!= 1 || !q
->qdisc
) /* only one class */
419 tcm
->tcm_handle
|= TC_H_MIN(1);
420 tcm
->tcm_info
= q
->qdisc
->handle
;
425 static int cbs_graft(struct Qdisc
*sch
, unsigned long arg
, struct Qdisc
*new,
426 struct Qdisc
**old
, struct netlink_ext_ack
*extack
)
428 struct cbs_sched_data
*q
= qdisc_priv(sch
);
431 new = qdisc_create_dflt(sch
->dev_queue
, &pfifo_qdisc_ops
,
437 *old
= qdisc_replace(sch
, new, &q
->qdisc
);
441 static struct Qdisc
*cbs_leaf(struct Qdisc
*sch
, unsigned long arg
)
443 struct cbs_sched_data
*q
= qdisc_priv(sch
);
448 static unsigned long cbs_find(struct Qdisc
*sch
, u32 classid
)
453 static void cbs_walk(struct Qdisc
*sch
, struct qdisc_walker
*walker
)
456 if (walker
->count
>= walker
->skip
) {
457 if (walker
->fn(sch
, 1, walker
) < 0) {
466 static const struct Qdisc_class_ops cbs_class_ops
= {
471 .dump
= cbs_dump_class
,
474 static struct Qdisc_ops cbs_qdisc_ops __read_mostly
= {
476 .cl_ops
= &cbs_class_ops
,
477 .priv_size
= sizeof(struct cbs_sched_data
),
478 .enqueue
= cbs_enqueue
,
479 .dequeue
= cbs_dequeue
,
480 .peek
= qdisc_peek_dequeued
,
482 .reset
= qdisc_reset_queue
,
483 .destroy
= cbs_destroy
,
484 .change
= cbs_change
,
486 .owner
= THIS_MODULE
,
489 static int __init
cbs_module_init(void)
491 return register_qdisc(&cbs_qdisc_ops
);
494 static void __exit
cbs_module_exit(void)
496 unregister_qdisc(&cbs_qdisc_ops
);
498 module_init(cbs_module_init
)
499 module_exit(cbs_module_exit
)
500 MODULE_LICENSE("GPL");