blob: e16a3d37d2bcda944c28402eefd4ac9716bbb93b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -07002/*
3 * net/sched/sch_cbs.c Credit Based Shaper
4 *
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -07005 * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com>
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -07006 */
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>
Leandro Dorileoe0a76832019-04-08 10:12:18 -070059#include <net/netevent.h>
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -070060#include <net/netlink.h>
61#include <net/sch_generic.h>
62#include <net/pkt_sched.h>
63
Leandro Dorileoe0a76832019-04-08 10:12:18 -070064static LIST_HEAD(cbs_list);
65static DEFINE_SPINLOCK(cbs_list_lock);
66
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -070067#define BYTES_PER_KBIT (1000LL / 8)
68
69struct cbs_sched_data {
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -070070 bool offload;
71 int queue;
Leandro Dorileoe0a76832019-04-08 10:12:18 -070072 atomic64_t port_rate; /* in bytes/s */
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -070073 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;
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -070080 int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch,
81 struct sk_buff **to_free);
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -070082 struct sk_buff *(*dequeue)(struct Qdisc *sch);
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -070083 struct Qdisc *qdisc;
Leandro Dorileoe0a76832019-04-08 10:12:18 -070084 struct list_head cbs_list;
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -070085};
86
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -070087static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch,
88 struct Qdisc *child,
89 struct sk_buff **to_free)
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -070090{
Toke Høiland-Jørgensenf6bab192019-01-09 17:09:42 +010091 unsigned int len = qdisc_pkt_len(skb);
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -070092 int err;
93
94 err = child->ops->enqueue(skb, child, to_free);
95 if (err != NET_XMIT_SUCCESS)
96 return err;
97
Toke Høiland-Jørgensenf6bab192019-01-09 17:09:42 +010098 sch->qstats.backlog += len;
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -070099 sch->q.qlen++;
100
101 return NET_XMIT_SUCCESS;
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700102}
103
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700104static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch,
105 struct sk_buff **to_free)
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700106{
107 struct cbs_sched_data *q = qdisc_priv(sch);
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700108 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;
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700118
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
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700127 return cbs_child_enqueue(skb, sch, qdisc, to_free);
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700128}
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
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700135 return q->enqueue(skb, sch, to_free);
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700136}
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
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700160static 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
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700175static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
176{
177 struct cbs_sched_data *q = qdisc_priv(sch);
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700178 struct Qdisc *qdisc = q->qdisc;
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700179 s64 now = ktime_get_ns();
180 struct sk_buff *skb;
181 s64 credits;
182 int len;
183
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700184 if (atomic64_read(&q->port_rate) == -1) {
185 WARN_ONCE(1, "cbs: dequeue() called with unknown port rate.");
186 return NULL;
187 }
188
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700189 if (q->credits < 0) {
190 credits = timediff_to_credits(now - q->last, q->idleslope);
191
192 credits = q->credits + credits;
193 q->credits = min_t(s64, credits, q->hicredit);
194
195 if (q->credits < 0) {
196 s64 delay;
197
198 delay = delay_from_credits(q->credits, q->idleslope);
199 qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
200
201 q->last = now;
202
203 return NULL;
204 }
205 }
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700206 skb = cbs_child_dequeue(sch, qdisc);
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700207 if (!skb)
208 return NULL;
209
210 len = qdisc_pkt_len(skb);
211
212 /* As sendslope is a negative number, this will decrease the
213 * amount of q->credits.
214 */
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700215 credits = credits_from_len(len, q->sendslope,
216 atomic64_read(&q->port_rate));
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700217 credits += q->credits;
218
219 q->credits = max_t(s64, credits, q->locredit);
220 q->last = now;
221
222 return skb;
223}
224
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700225static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
226{
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700227 struct cbs_sched_data *q = qdisc_priv(sch);
228 struct Qdisc *qdisc = q->qdisc;
229
230 return cbs_child_dequeue(sch, qdisc);
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700231}
232
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700233static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
234{
235 struct cbs_sched_data *q = qdisc_priv(sch);
236
237 return q->dequeue(sch);
238}
239
240static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
241 [TCA_CBS_PARMS] = { .len = sizeof(struct tc_cbs_qopt) },
242};
243
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700244static void cbs_disable_offload(struct net_device *dev,
245 struct cbs_sched_data *q)
246{
247 struct tc_cbs_qopt_offload cbs = { };
248 const struct net_device_ops *ops;
249 int err;
250
251 if (!q->offload)
252 return;
253
254 q->enqueue = cbs_enqueue_soft;
255 q->dequeue = cbs_dequeue_soft;
256
257 ops = dev->netdev_ops;
258 if (!ops->ndo_setup_tc)
259 return;
260
261 cbs.queue = q->queue;
262 cbs.enable = 0;
263
Nogah Frankel8521db42017-11-06 07:23:43 +0100264 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700265 if (err < 0)
266 pr_warn("Couldn't disable CBS offload for queue %d\n",
267 cbs.queue);
268}
269
270static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
Alexander Aring710fb392017-12-20 12:35:23 -0500271 const struct tc_cbs_qopt *opt,
272 struct netlink_ext_ack *extack)
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700273{
274 const struct net_device_ops *ops = dev->netdev_ops;
275 struct tc_cbs_qopt_offload cbs = { };
276 int err;
277
Alexander Aring710fb392017-12-20 12:35:23 -0500278 if (!ops->ndo_setup_tc) {
279 NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700280 return -EOPNOTSUPP;
Alexander Aring710fb392017-12-20 12:35:23 -0500281 }
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700282
283 cbs.queue = q->queue;
284
285 cbs.enable = 1;
286 cbs.hicredit = opt->hicredit;
287 cbs.locredit = opt->locredit;
288 cbs.idleslope = opt->idleslope;
289 cbs.sendslope = opt->sendslope;
290
Nogah Frankel8521db42017-11-06 07:23:43 +0100291 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
Alexander Aring710fb392017-12-20 12:35:23 -0500292 if (err < 0) {
293 NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700294 return err;
Alexander Aring710fb392017-12-20 12:35:23 -0500295 }
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700296
297 q->enqueue = cbs_enqueue_offload;
298 q->dequeue = cbs_dequeue_offload;
299
300 return 0;
301}
302
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700303static void cbs_set_port_rate(struct net_device *dev, struct cbs_sched_data *q)
304{
305 struct ethtool_link_ksettings ecmd;
306 int port_rate = -1;
307
308 if (!__ethtool_get_link_ksettings(dev, &ecmd) &&
309 ecmd.base.speed != SPEED_UNKNOWN)
310 port_rate = ecmd.base.speed * 1000 * BYTES_PER_KBIT;
311
312 atomic64_set(&q->port_rate, port_rate);
313 netdev_dbg(dev, "cbs: set %s's port_rate to: %lld, linkspeed: %d\n",
314 dev->name, (long long)atomic64_read(&q->port_rate),
315 ecmd.base.speed);
316}
317
318static int cbs_dev_notifier(struct notifier_block *nb, unsigned long event,
319 void *ptr)
320{
321 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
322 struct cbs_sched_data *q;
323 struct net_device *qdev;
324 bool found = false;
325
326 ASSERT_RTNL();
327
328 if (event != NETDEV_UP && event != NETDEV_CHANGE)
329 return NOTIFY_DONE;
330
331 spin_lock(&cbs_list_lock);
332 list_for_each_entry(q, &cbs_list, cbs_list) {
333 qdev = qdisc_dev(q->qdisc);
334 if (qdev == dev) {
335 found = true;
336 break;
337 }
338 }
339 spin_unlock(&cbs_list_lock);
340
341 if (found)
342 cbs_set_port_rate(dev, q);
343
344 return NOTIFY_DONE;
345}
346
Alexander Aring20307212017-12-20 12:35:14 -0500347static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
348 struct netlink_ext_ack *extack)
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700349{
350 struct cbs_sched_data *q = qdisc_priv(sch);
351 struct net_device *dev = qdisc_dev(sch);
352 struct nlattr *tb[TCA_CBS_MAX + 1];
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700353 struct tc_cbs_qopt *qopt;
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700354 int err;
355
Johannes Berg8cb08172019-04-26 14:07:28 +0200356 err = nla_parse_nested_deprecated(tb, TCA_CBS_MAX, opt, cbs_policy,
357 extack);
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700358 if (err < 0)
359 return err;
360
Alexander Aring710fb392017-12-20 12:35:23 -0500361 if (!tb[TCA_CBS_PARMS]) {
362 NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700363 return -EINVAL;
Alexander Aring710fb392017-12-20 12:35:23 -0500364 }
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700365
366 qopt = nla_data(tb[TCA_CBS_PARMS]);
367
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700368 if (!qopt->offload) {
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700369 cbs_set_port_rate(dev, q);
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700370 cbs_disable_offload(dev, q);
371 } else {
Alexander Aring710fb392017-12-20 12:35:23 -0500372 err = cbs_enable_offload(dev, q, qopt, extack);
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700373 if (err < 0)
374 return err;
375 }
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700376
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700377 /* Everything went OK, save the parameters used. */
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700378 q->hicredit = qopt->hicredit;
379 q->locredit = qopt->locredit;
380 q->idleslope = qopt->idleslope * BYTES_PER_KBIT;
381 q->sendslope = qopt->sendslope * BYTES_PER_KBIT;
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700382 q->offload = qopt->offload;
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700383
384 return 0;
385}
386
Alexander Aringe63d7df2017-12-20 12:35:13 -0500387static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
388 struct netlink_ext_ack *extack)
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700389{
390 struct cbs_sched_data *q = qdisc_priv(sch);
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700391 struct net_device *dev = qdisc_dev(sch);
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700392 int err;
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700393
Alexander Aring710fb392017-12-20 12:35:23 -0500394 if (!opt) {
395 NL_SET_ERR_MSG(extack, "Missing CBS qdisc options which are mandatory");
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700396 return -EINVAL;
Alexander Aring710fb392017-12-20 12:35:23 -0500397 }
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700398
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700399 q->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
400 sch->handle, extack);
401 if (!q->qdisc)
402 return -ENOMEM;
403
404 qdisc_hash_add(q->qdisc, false);
405
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700406 q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
407
408 q->enqueue = cbs_enqueue_soft;
409 q->dequeue = cbs_dequeue_soft;
410
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700411 qdisc_watchdog_init(&q->watchdog, sch);
412
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700413 err = cbs_change(sch, opt, extack);
414 if (err)
415 return err;
416
417 if (!q->offload) {
418 spin_lock(&cbs_list_lock);
419 list_add(&q->cbs_list, &cbs_list);
420 spin_unlock(&cbs_list_lock);
421 }
422
423 return 0;
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700424}
425
426static void cbs_destroy(struct Qdisc *sch)
427{
428 struct cbs_sched_data *q = qdisc_priv(sch);
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700429 struct net_device *dev = qdisc_dev(sch);
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700430
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700431 spin_lock(&cbs_list_lock);
432 list_del(&q->cbs_list);
433 spin_unlock(&cbs_list_lock);
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700434
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700435 qdisc_watchdog_cancel(&q->watchdog);
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700436 cbs_disable_offload(dev, q);
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700437
438 if (q->qdisc)
Vlad Buslov86bd4462018-09-24 19:22:50 +0300439 qdisc_put(q->qdisc);
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700440}
441
442static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
443{
444 struct cbs_sched_data *q = qdisc_priv(sch);
445 struct tc_cbs_qopt opt = { };
446 struct nlattr *nest;
447
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200448 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700449 if (!nest)
450 goto nla_put_failure;
451
452 opt.hicredit = q->hicredit;
453 opt.locredit = q->locredit;
454 opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT);
455 opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT);
Vinicius Costa Gomes3d0bd022017-10-16 18:01:27 -0700456 opt.offload = q->offload;
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700457
458 if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
459 goto nla_put_failure;
460
461 return nla_nest_end(skb, nest);
462
463nla_put_failure:
464 nla_nest_cancel(skb, nest);
465 return -1;
466}
467
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700468static int cbs_dump_class(struct Qdisc *sch, unsigned long cl,
469 struct sk_buff *skb, struct tcmsg *tcm)
470{
471 struct cbs_sched_data *q = qdisc_priv(sch);
472
473 if (cl != 1 || !q->qdisc) /* only one class */
474 return -ENOENT;
475
476 tcm->tcm_handle |= TC_H_MIN(1);
477 tcm->tcm_info = q->qdisc->handle;
478
479 return 0;
480}
481
482static int cbs_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
483 struct Qdisc **old, struct netlink_ext_ack *extack)
484{
485 struct cbs_sched_data *q = qdisc_priv(sch);
486
487 if (!new) {
488 new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
489 sch->handle, NULL);
490 if (!new)
491 new = &noop_qdisc;
492 }
493
494 *old = qdisc_replace(sch, new, &q->qdisc);
495 return 0;
496}
497
498static struct Qdisc *cbs_leaf(struct Qdisc *sch, unsigned long arg)
499{
500 struct cbs_sched_data *q = qdisc_priv(sch);
501
502 return q->qdisc;
503}
504
505static unsigned long cbs_find(struct Qdisc *sch, u32 classid)
506{
507 return 1;
508}
509
510static void cbs_walk(struct Qdisc *sch, struct qdisc_walker *walker)
511{
512 if (!walker->stop) {
513 if (walker->count >= walker->skip) {
514 if (walker->fn(sch, 1, walker) < 0) {
515 walker->stop = 1;
516 return;
517 }
518 }
519 walker->count++;
520 }
521}
522
523static const struct Qdisc_class_ops cbs_class_ops = {
524 .graft = cbs_graft,
525 .leaf = cbs_leaf,
526 .find = cbs_find,
527 .walk = cbs_walk,
528 .dump = cbs_dump_class,
529};
530
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700531static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
532 .id = "cbs",
Vinicius Costa Gomes990e35e2018-07-23 17:08:00 -0700533 .cl_ops = &cbs_class_ops,
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700534 .priv_size = sizeof(struct cbs_sched_data),
535 .enqueue = cbs_enqueue,
536 .dequeue = cbs_dequeue,
537 .peek = qdisc_peek_dequeued,
538 .init = cbs_init,
539 .reset = qdisc_reset_queue,
540 .destroy = cbs_destroy,
541 .change = cbs_change,
542 .dump = cbs_dump,
543 .owner = THIS_MODULE,
544};
545
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700546static struct notifier_block cbs_device_notifier = {
547 .notifier_call = cbs_dev_notifier,
548};
549
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700550static int __init cbs_module_init(void)
551{
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700552 int err = register_netdevice_notifier(&cbs_device_notifier);
553
554 if (err)
555 return err;
556
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700557 return register_qdisc(&cbs_qdisc_ops);
558}
559
560static void __exit cbs_module_exit(void)
561{
562 unregister_qdisc(&cbs_qdisc_ops);
Leandro Dorileoe0a76832019-04-08 10:12:18 -0700563 unregister_netdevice_notifier(&cbs_device_notifier);
Vinicius Costa Gomes585d7632017-10-16 18:01:26 -0700564}
565module_init(cbs_module_init)
566module_exit(cbs_module_exit)
567MODULE_LICENSE("GPL");