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