media: mmp-driver: add needed __iomem marks to power_regs
[linux-2.6/btrfs-unstable.git] / net / sched / sch_cbs.c
blobcdd96b9a27bcf1ef510e282ff97a8710132b1ce6
1 /*
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
21 * same specification.
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
31 * algorithm.
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):
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 {
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);
85 static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch)
87 return qdisc_enqueue_tail(skb, sch);
90 static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch)
92 struct cbs_sched_data *q = qdisc_priv(sch);
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.
98 q->credits = 0;
99 q->last = ktime_get_ns();
102 return qdisc_enqueue_tail(skb, sch);
105 static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
106 struct sk_buff **to_free)
108 struct cbs_sched_data *q = qdisc_priv(sch);
110 return q->enqueue(skb, sch);
113 /* timediff is in ns, slope is in bytes/s */
114 static s64 timediff_to_credits(s64 timediff, s64 slope)
116 return div64_s64(timediff * slope, NSEC_PER_SEC);
119 static s64 delay_from_credits(s64 credits, s64 slope)
121 if (unlikely(slope == 0))
122 return S64_MAX;
124 return div64_s64(-credits * NSEC_PER_SEC, slope);
127 static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
129 if (unlikely(port_rate == 0))
130 return S64_MAX;
132 return div64_s64(len * slope, port_rate);
135 static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
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;
143 if (q->credits < 0) {
144 credits = timediff_to_credits(now - q->last, q->idleslope);
146 credits = q->credits + credits;
147 q->credits = min_t(s64, credits, q->hicredit);
149 if (q->credits < 0) {
150 s64 delay;
152 delay = delay_from_credits(q->credits, q->idleslope);
153 qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
155 q->last = now;
157 return NULL;
161 skb = qdisc_dequeue_head(sch);
162 if (!skb)
163 return NULL;
165 len = qdisc_pkt_len(skb);
167 /* As sendslope is a negative number, this will decrease the
168 * amount of q->credits.
170 credits = credits_from_len(len, q->sendslope, q->port_rate);
171 credits += q->credits;
173 q->credits = max_t(s64, credits, q->locredit);
174 q->last = now;
176 return skb;
179 static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
181 return qdisc_dequeue_head(sch);
184 static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
186 struct cbs_sched_data *q = qdisc_priv(sch);
188 return q->dequeue(sch);
191 static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
192 [TCA_CBS_PARMS] = { .len = sizeof(struct tc_cbs_qopt) },
195 static void cbs_disable_offload(struct net_device *dev,
196 struct cbs_sched_data *q)
198 struct tc_cbs_qopt_offload cbs = { };
199 const struct net_device_ops *ops;
200 int err;
202 if (!q->offload)
203 return;
205 q->enqueue = cbs_enqueue_soft;
206 q->dequeue = cbs_dequeue_soft;
208 ops = dev->netdev_ops;
209 if (!ops->ndo_setup_tc)
210 return;
212 cbs.queue = q->queue;
213 cbs.enable = 0;
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);
221 static 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)
225 const struct net_device_ops *ops = dev->netdev_ops;
226 struct tc_cbs_qopt_offload cbs = { };
227 int err;
229 if (!ops->ndo_setup_tc) {
230 NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
231 return -EOPNOTSUPP;
234 cbs.queue = q->queue;
236 cbs.enable = 1;
237 cbs.hicredit = opt->hicredit;
238 cbs.locredit = opt->locredit;
239 cbs.idleslope = opt->idleslope;
240 cbs.sendslope = opt->sendslope;
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;
248 q->enqueue = cbs_enqueue_offload;
249 q->dequeue = cbs_dequeue_offload;
251 return 0;
254 static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
255 struct netlink_ext_ack *extack)
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;
263 err = nla_parse_nested(tb, TCA_CBS_MAX, opt, cbs_policy, extack);
264 if (err < 0)
265 return err;
267 if (!tb[TCA_CBS_PARMS]) {
268 NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
269 return -EINVAL;
272 qopt = nla_data(tb[TCA_CBS_PARMS]);
274 if (!qopt->offload) {
275 struct ethtool_link_ksettings ecmd;
276 s64 link_speed;
278 if (!__ethtool_get_link_ksettings(dev, &ecmd))
279 link_speed = ecmd.base.speed;
280 else
281 link_speed = SPEED_1000;
283 q->port_rate = link_speed * 1000 * BYTES_PER_KBIT;
285 cbs_disable_offload(dev, q);
286 } else {
287 err = cbs_enable_offload(dev, q, qopt, extack);
288 if (err < 0)
289 return err;
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;
299 return 0;
302 static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
303 struct netlink_ext_ack *extack)
305 struct cbs_sched_data *q = qdisc_priv(sch);
306 struct net_device *dev = qdisc_dev(sch);
308 if (!opt) {
309 NL_SET_ERR_MSG(extack, "Missing CBS qdisc options which are mandatory");
310 return -EINVAL;
313 q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
315 q->enqueue = cbs_enqueue_soft;
316 q->dequeue = cbs_dequeue_soft;
318 qdisc_watchdog_init(&q->watchdog, sch);
320 return cbs_change(sch, opt, extack);
323 static void cbs_destroy(struct Qdisc *sch)
325 struct cbs_sched_data *q = qdisc_priv(sch);
326 struct net_device *dev = qdisc_dev(sch);
328 qdisc_watchdog_cancel(&q->watchdog);
330 cbs_disable_offload(dev, q);
333 static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
335 struct cbs_sched_data *q = qdisc_priv(sch);
336 struct tc_cbs_qopt opt = { };
337 struct nlattr *nest;
339 nest = nla_nest_start(skb, TCA_OPTIONS);
340 if (!nest)
341 goto nla_put_failure;
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;
349 if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
350 goto nla_put_failure;
352 return nla_nest_end(skb, nest);
354 nla_put_failure:
355 nla_nest_cancel(skb, nest);
356 return -1;
359 static 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,
373 static int __init cbs_module_init(void)
375 return register_qdisc(&cbs_qdisc_ops);
378 static void __exit cbs_module_exit(void)
380 unregister_qdisc(&cbs_qdisc_ops);
382 module_init(cbs_module_init)
383 module_exit(cbs_module_exit)
384 MODULE_LICENSE("GPL");