12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2585d763aSVinicius Costa Gomes /*
3585d763aSVinicius Costa Gomes * net/sched/sch_cbs.c Credit Based Shaper
4585d763aSVinicius Costa Gomes *
5585d763aSVinicius Costa Gomes * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com>
6585d763aSVinicius Costa Gomes */
7585d763aSVinicius Costa Gomes
8585d763aSVinicius Costa Gomes /* Credit Based Shaper (CBS)
9585d763aSVinicius Costa Gomes * =========================
10585d763aSVinicius Costa Gomes *
11585d763aSVinicius Costa Gomes * This is a simple rate-limiting shaper aimed at TSN applications on
12585d763aSVinicius Costa Gomes * systems with known traffic workloads.
13585d763aSVinicius Costa Gomes *
14585d763aSVinicius Costa Gomes * Its algorithm is defined by the IEEE 802.1Q-2014 Specification,
15585d763aSVinicius Costa Gomes * Section 8.6.8.2, and explained in more detail in the Annex L of the
16585d763aSVinicius Costa Gomes * same specification.
17585d763aSVinicius Costa Gomes *
18585d763aSVinicius Costa Gomes * There are four tunables to be considered:
19585d763aSVinicius Costa Gomes *
20585d763aSVinicius Costa Gomes * 'idleslope': Idleslope is the rate of credits that is
21585d763aSVinicius Costa Gomes * accumulated (in kilobits per second) when there is at least
22585d763aSVinicius Costa Gomes * one packet waiting for transmission. Packets are transmitted
23585d763aSVinicius Costa Gomes * when the current value of credits is equal or greater than
24585d763aSVinicius Costa Gomes * zero. When there is no packet to be transmitted the amount of
25585d763aSVinicius Costa Gomes * credits is set to zero. This is the main tunable of the CBS
26585d763aSVinicius Costa Gomes * algorithm.
27585d763aSVinicius Costa Gomes *
28585d763aSVinicius Costa Gomes * 'sendslope':
29585d763aSVinicius Costa Gomes * Sendslope is the rate of credits that is depleted (it should be a
30585d763aSVinicius Costa Gomes * negative number of kilobits per second) when a transmission is
31585d763aSVinicius Costa Gomes * ocurring. It can be calculated as follows, (IEEE 802.1Q-2014 Section
32585d763aSVinicius Costa Gomes * 8.6.8.2 item g):
33585d763aSVinicius Costa Gomes *
34585d763aSVinicius Costa Gomes * sendslope = idleslope - port_transmit_rate
35585d763aSVinicius Costa Gomes *
36585d763aSVinicius Costa Gomes * 'hicredit': Hicredit defines the maximum amount of credits (in
37585d763aSVinicius Costa Gomes * bytes) that can be accumulated. Hicredit depends on the
38585d763aSVinicius Costa Gomes * characteristics of interfering traffic,
39585d763aSVinicius Costa Gomes * 'max_interference_size' is the maximum size of any burst of
40585d763aSVinicius Costa Gomes * traffic that can delay the transmission of a frame that is
41585d763aSVinicius Costa Gomes * available for transmission for this traffic class, (IEEE
42585d763aSVinicius Costa Gomes * 802.1Q-2014 Annex L, Equation L-3):
43585d763aSVinicius Costa Gomes *
44585d763aSVinicius Costa Gomes * hicredit = max_interference_size * (idleslope / port_transmit_rate)
45585d763aSVinicius Costa Gomes *
46585d763aSVinicius Costa Gomes * 'locredit': Locredit is the minimum amount of credits that can
47585d763aSVinicius Costa Gomes * be reached. It is a function of the traffic flowing through
48585d763aSVinicius Costa Gomes * this qdisc (IEEE 802.1Q-2014 Annex L, Equation L-2):
49585d763aSVinicius Costa Gomes *
50585d763aSVinicius Costa Gomes * locredit = max_frame_size * (sendslope / port_transmit_rate)
51585d763aSVinicius Costa Gomes */
52585d763aSVinicius Costa Gomes
53cc69837fSJakub Kicinski #include <linux/ethtool.h>
54585d763aSVinicius Costa Gomes #include <linux/module.h>
55585d763aSVinicius Costa Gomes #include <linux/types.h>
56585d763aSVinicius Costa Gomes #include <linux/kernel.h>
57585d763aSVinicius Costa Gomes #include <linux/string.h>
58585d763aSVinicius Costa Gomes #include <linux/errno.h>
59585d763aSVinicius Costa Gomes #include <linux/skbuff.h>
60000db9e9SAndy Shevchenko #include <linux/units.h>
61000db9e9SAndy Shevchenko
62e0a7683dSLeandro Dorileo #include <net/netevent.h>
63585d763aSVinicius Costa Gomes #include <net/netlink.h>
64585d763aSVinicius Costa Gomes #include <net/sch_generic.h>
65585d763aSVinicius Costa Gomes #include <net/pkt_sched.h>
66585d763aSVinicius Costa Gomes
67e0a7683dSLeandro Dorileo static LIST_HEAD(cbs_list);
68e0a7683dSLeandro Dorileo static DEFINE_SPINLOCK(cbs_list_lock);
69e0a7683dSLeandro Dorileo
70585d763aSVinicius Costa Gomes struct cbs_sched_data {
713d0bd028SVinicius Costa Gomes bool offload;
723d0bd028SVinicius Costa Gomes int queue;
73e0a7683dSLeandro Dorileo atomic64_t port_rate; /* in bytes/s */
74585d763aSVinicius Costa Gomes s64 last; /* timestamp in ns */
75585d763aSVinicius Costa Gomes s64 credits; /* in bytes */
76585d763aSVinicius Costa Gomes s32 locredit; /* in bytes */
77585d763aSVinicius Costa Gomes s32 hicredit; /* in bytes */
78585d763aSVinicius Costa Gomes s64 sendslope; /* in bytes/s */
79585d763aSVinicius Costa Gomes s64 idleslope; /* in bytes/s */
80585d763aSVinicius Costa Gomes struct qdisc_watchdog watchdog;
81990e35ecSVinicius Costa Gomes int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch,
82990e35ecSVinicius Costa Gomes struct sk_buff **to_free);
83585d763aSVinicius Costa Gomes struct sk_buff *(*dequeue)(struct Qdisc *sch);
84990e35ecSVinicius Costa Gomes struct Qdisc *qdisc;
85e0a7683dSLeandro Dorileo struct list_head cbs_list;
86585d763aSVinicius Costa Gomes };
87585d763aSVinicius Costa Gomes
cbs_child_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct Qdisc * child,struct sk_buff ** to_free)88990e35ecSVinicius Costa Gomes static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch,
89990e35ecSVinicius Costa Gomes struct Qdisc *child,
90990e35ecSVinicius Costa Gomes struct sk_buff **to_free)
913d0bd028SVinicius Costa Gomes {
92f6bab199SToke Høiland-Jørgensen unsigned int len = qdisc_pkt_len(skb);
93990e35ecSVinicius Costa Gomes int err;
94990e35ecSVinicius Costa Gomes
95990e35ecSVinicius Costa Gomes err = child->ops->enqueue(skb, child, to_free);
96990e35ecSVinicius Costa Gomes if (err != NET_XMIT_SUCCESS)
97990e35ecSVinicius Costa Gomes return err;
98990e35ecSVinicius Costa Gomes
99f6bab199SToke Høiland-Jørgensen sch->qstats.backlog += len;
100990e35ecSVinicius Costa Gomes sch->q.qlen++;
101990e35ecSVinicius Costa Gomes
102990e35ecSVinicius Costa Gomes return NET_XMIT_SUCCESS;
1033d0bd028SVinicius Costa Gomes }
1043d0bd028SVinicius Costa Gomes
cbs_enqueue_offload(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)105990e35ecSVinicius Costa Gomes static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch,
106990e35ecSVinicius Costa Gomes struct sk_buff **to_free)
107585d763aSVinicius Costa Gomes {
108585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
109990e35ecSVinicius Costa Gomes struct Qdisc *qdisc = q->qdisc;
110990e35ecSVinicius Costa Gomes
111990e35ecSVinicius Costa Gomes return cbs_child_enqueue(skb, sch, qdisc, to_free);
112990e35ecSVinicius Costa Gomes }
113990e35ecSVinicius Costa Gomes
cbs_enqueue_soft(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)114990e35ecSVinicius Costa Gomes static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch,
115990e35ecSVinicius Costa Gomes struct sk_buff **to_free)
116990e35ecSVinicius Costa Gomes {
117990e35ecSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
118990e35ecSVinicius Costa Gomes struct Qdisc *qdisc = q->qdisc;
119585d763aSVinicius Costa Gomes
120585d763aSVinicius Costa Gomes if (sch->q.qlen == 0 && q->credits > 0) {
121585d763aSVinicius Costa Gomes /* We need to stop accumulating credits when there's
122585d763aSVinicius Costa Gomes * no enqueued packets and q->credits is positive.
123585d763aSVinicius Costa Gomes */
124585d763aSVinicius Costa Gomes q->credits = 0;
125585d763aSVinicius Costa Gomes q->last = ktime_get_ns();
126585d763aSVinicius Costa Gomes }
127585d763aSVinicius Costa Gomes
128990e35ecSVinicius Costa Gomes return cbs_child_enqueue(skb, sch, qdisc, to_free);
129585d763aSVinicius Costa Gomes }
130585d763aSVinicius Costa Gomes
cbs_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)131585d763aSVinicius Costa Gomes static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
132585d763aSVinicius Costa Gomes struct sk_buff **to_free)
133585d763aSVinicius Costa Gomes {
134585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
135585d763aSVinicius Costa Gomes
136990e35ecSVinicius Costa Gomes return q->enqueue(skb, sch, to_free);
137585d763aSVinicius Costa Gomes }
138585d763aSVinicius Costa Gomes
139585d763aSVinicius Costa Gomes /* timediff is in ns, slope is in bytes/s */
timediff_to_credits(s64 timediff,s64 slope)140585d763aSVinicius Costa Gomes static s64 timediff_to_credits(s64 timediff, s64 slope)
141585d763aSVinicius Costa Gomes {
142585d763aSVinicius Costa Gomes return div64_s64(timediff * slope, NSEC_PER_SEC);
143585d763aSVinicius Costa Gomes }
144585d763aSVinicius Costa Gomes
delay_from_credits(s64 credits,s64 slope)145585d763aSVinicius Costa Gomes static s64 delay_from_credits(s64 credits, s64 slope)
146585d763aSVinicius Costa Gomes {
147585d763aSVinicius Costa Gomes if (unlikely(slope == 0))
148585d763aSVinicius Costa Gomes return S64_MAX;
149585d763aSVinicius Costa Gomes
150585d763aSVinicius Costa Gomes return div64_s64(-credits * NSEC_PER_SEC, slope);
151585d763aSVinicius Costa Gomes }
152585d763aSVinicius Costa Gomes
credits_from_len(unsigned int len,s64 slope,s64 port_rate)153585d763aSVinicius Costa Gomes static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
154585d763aSVinicius Costa Gomes {
155585d763aSVinicius Costa Gomes if (unlikely(port_rate == 0))
156585d763aSVinicius Costa Gomes return S64_MAX;
157585d763aSVinicius Costa Gomes
158585d763aSVinicius Costa Gomes return div64_s64(len * slope, port_rate);
159585d763aSVinicius Costa Gomes }
160585d763aSVinicius Costa Gomes
cbs_child_dequeue(struct Qdisc * sch,struct Qdisc * child)161990e35ecSVinicius Costa Gomes static struct sk_buff *cbs_child_dequeue(struct Qdisc *sch, struct Qdisc *child)
162990e35ecSVinicius Costa Gomes {
163990e35ecSVinicius Costa Gomes struct sk_buff *skb;
164990e35ecSVinicius Costa Gomes
165990e35ecSVinicius Costa Gomes skb = child->ops->dequeue(child);
166990e35ecSVinicius Costa Gomes if (!skb)
167990e35ecSVinicius Costa Gomes return NULL;
168990e35ecSVinicius Costa Gomes
169990e35ecSVinicius Costa Gomes qdisc_qstats_backlog_dec(sch, skb);
170990e35ecSVinicius Costa Gomes qdisc_bstats_update(sch, skb);
171990e35ecSVinicius Costa Gomes sch->q.qlen--;
172990e35ecSVinicius Costa Gomes
173990e35ecSVinicius Costa Gomes return skb;
174990e35ecSVinicius Costa Gomes }
175990e35ecSVinicius Costa Gomes
cbs_dequeue_soft(struct Qdisc * sch)176585d763aSVinicius Costa Gomes static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
177585d763aSVinicius Costa Gomes {
178585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
179990e35ecSVinicius Costa Gomes struct Qdisc *qdisc = q->qdisc;
180585d763aSVinicius Costa Gomes s64 now = ktime_get_ns();
181585d763aSVinicius Costa Gomes struct sk_buff *skb;
182585d763aSVinicius Costa Gomes s64 credits;
183585d763aSVinicius Costa Gomes int len;
184585d763aSVinicius Costa Gomes
185961d0e5bSZh-yuan Ye /* The previous packet is still being sent */
186961d0e5bSZh-yuan Ye if (now < q->last) {
187961d0e5bSZh-yuan Ye qdisc_watchdog_schedule_ns(&q->watchdog, q->last);
188961d0e5bSZh-yuan Ye return NULL;
189961d0e5bSZh-yuan Ye }
190585d763aSVinicius Costa Gomes if (q->credits < 0) {
191585d763aSVinicius Costa Gomes credits = timediff_to_credits(now - q->last, q->idleslope);
192585d763aSVinicius Costa Gomes
193585d763aSVinicius Costa Gomes credits = q->credits + credits;
194585d763aSVinicius Costa Gomes q->credits = min_t(s64, credits, q->hicredit);
195585d763aSVinicius Costa Gomes
196585d763aSVinicius Costa Gomes if (q->credits < 0) {
197585d763aSVinicius Costa Gomes s64 delay;
198585d763aSVinicius Costa Gomes
199585d763aSVinicius Costa Gomes delay = delay_from_credits(q->credits, q->idleslope);
200585d763aSVinicius Costa Gomes qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
201585d763aSVinicius Costa Gomes
202585d763aSVinicius Costa Gomes q->last = now;
203585d763aSVinicius Costa Gomes
204585d763aSVinicius Costa Gomes return NULL;
205585d763aSVinicius Costa Gomes }
206585d763aSVinicius Costa Gomes }
207990e35ecSVinicius Costa Gomes skb = cbs_child_dequeue(sch, qdisc);
208585d763aSVinicius Costa Gomes if (!skb)
209585d763aSVinicius Costa Gomes return NULL;
210585d763aSVinicius Costa Gomes
211585d763aSVinicius Costa Gomes len = qdisc_pkt_len(skb);
212585d763aSVinicius Costa Gomes
213585d763aSVinicius Costa Gomes /* As sendslope is a negative number, this will decrease the
214585d763aSVinicius Costa Gomes * amount of q->credits.
215585d763aSVinicius Costa Gomes */
216e0a7683dSLeandro Dorileo credits = credits_from_len(len, q->sendslope,
217e0a7683dSLeandro Dorileo atomic64_read(&q->port_rate));
218585d763aSVinicius Costa Gomes credits += q->credits;
219585d763aSVinicius Costa Gomes
220585d763aSVinicius Costa Gomes q->credits = max_t(s64, credits, q->locredit);
221961d0e5bSZh-yuan Ye /* Estimate of the transmission of the last byte of the packet in ns */
222961d0e5bSZh-yuan Ye if (unlikely(atomic64_read(&q->port_rate) == 0))
223585d763aSVinicius Costa Gomes q->last = now;
224961d0e5bSZh-yuan Ye else
225961d0e5bSZh-yuan Ye q->last = now + div64_s64(len * NSEC_PER_SEC,
226961d0e5bSZh-yuan Ye atomic64_read(&q->port_rate));
227585d763aSVinicius Costa Gomes
228585d763aSVinicius Costa Gomes return skb;
229585d763aSVinicius Costa Gomes }
230585d763aSVinicius Costa Gomes
cbs_dequeue_offload(struct Qdisc * sch)2313d0bd028SVinicius Costa Gomes static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
2323d0bd028SVinicius Costa Gomes {
233990e35ecSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
234990e35ecSVinicius Costa Gomes struct Qdisc *qdisc = q->qdisc;
235990e35ecSVinicius Costa Gomes
236990e35ecSVinicius Costa Gomes return cbs_child_dequeue(sch, qdisc);
2373d0bd028SVinicius Costa Gomes }
2383d0bd028SVinicius Costa Gomes
cbs_dequeue(struct Qdisc * sch)239585d763aSVinicius Costa Gomes static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
240585d763aSVinicius Costa Gomes {
241585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
242585d763aSVinicius Costa Gomes
243585d763aSVinicius Costa Gomes return q->dequeue(sch);
244585d763aSVinicius Costa Gomes }
245585d763aSVinicius Costa Gomes
246585d763aSVinicius Costa Gomes static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
247585d763aSVinicius Costa Gomes [TCA_CBS_PARMS] = { .len = sizeof(struct tc_cbs_qopt) },
248585d763aSVinicius Costa Gomes };
249585d763aSVinicius Costa Gomes
cbs_disable_offload(struct net_device * dev,struct cbs_sched_data * q)2503d0bd028SVinicius Costa Gomes static void cbs_disable_offload(struct net_device *dev,
2513d0bd028SVinicius Costa Gomes struct cbs_sched_data *q)
2523d0bd028SVinicius Costa Gomes {
2533d0bd028SVinicius Costa Gomes struct tc_cbs_qopt_offload cbs = { };
2543d0bd028SVinicius Costa Gomes const struct net_device_ops *ops;
2553d0bd028SVinicius Costa Gomes int err;
2563d0bd028SVinicius Costa Gomes
2573d0bd028SVinicius Costa Gomes if (!q->offload)
2583d0bd028SVinicius Costa Gomes return;
2593d0bd028SVinicius Costa Gomes
2603d0bd028SVinicius Costa Gomes q->enqueue = cbs_enqueue_soft;
2613d0bd028SVinicius Costa Gomes q->dequeue = cbs_dequeue_soft;
2623d0bd028SVinicius Costa Gomes
2633d0bd028SVinicius Costa Gomes ops = dev->netdev_ops;
2643d0bd028SVinicius Costa Gomes if (!ops->ndo_setup_tc)
2653d0bd028SVinicius Costa Gomes return;
2663d0bd028SVinicius Costa Gomes
2673d0bd028SVinicius Costa Gomes cbs.queue = q->queue;
2683d0bd028SVinicius Costa Gomes cbs.enable = 0;
2693d0bd028SVinicius Costa Gomes
2708521db4cSNogah Frankel err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
2713d0bd028SVinicius Costa Gomes if (err < 0)
2723d0bd028SVinicius Costa Gomes pr_warn("Couldn't disable CBS offload for queue %d\n",
2733d0bd028SVinicius Costa Gomes cbs.queue);
2743d0bd028SVinicius Costa Gomes }
2753d0bd028SVinicius Costa Gomes
cbs_enable_offload(struct net_device * dev,struct cbs_sched_data * q,const struct tc_cbs_qopt * opt,struct netlink_ext_ack * extack)2763d0bd028SVinicius Costa Gomes static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
277710fb396SAlexander Aring const struct tc_cbs_qopt *opt,
278710fb396SAlexander Aring struct netlink_ext_ack *extack)
2793d0bd028SVinicius Costa Gomes {
2803d0bd028SVinicius Costa Gomes const struct net_device_ops *ops = dev->netdev_ops;
2813d0bd028SVinicius Costa Gomes struct tc_cbs_qopt_offload cbs = { };
2823d0bd028SVinicius Costa Gomes int err;
2833d0bd028SVinicius Costa Gomes
284710fb396SAlexander Aring if (!ops->ndo_setup_tc) {
285710fb396SAlexander Aring NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
2863d0bd028SVinicius Costa Gomes return -EOPNOTSUPP;
287710fb396SAlexander Aring }
2883d0bd028SVinicius Costa Gomes
2893d0bd028SVinicius Costa Gomes cbs.queue = q->queue;
2903d0bd028SVinicius Costa Gomes
2913d0bd028SVinicius Costa Gomes cbs.enable = 1;
2923d0bd028SVinicius Costa Gomes cbs.hicredit = opt->hicredit;
2933d0bd028SVinicius Costa Gomes cbs.locredit = opt->locredit;
2943d0bd028SVinicius Costa Gomes cbs.idleslope = opt->idleslope;
2953d0bd028SVinicius Costa Gomes cbs.sendslope = opt->sendslope;
2963d0bd028SVinicius Costa Gomes
2978521db4cSNogah Frankel err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
298710fb396SAlexander Aring if (err < 0) {
299710fb396SAlexander Aring NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
3003d0bd028SVinicius Costa Gomes return err;
301710fb396SAlexander Aring }
3023d0bd028SVinicius Costa Gomes
3033d0bd028SVinicius Costa Gomes q->enqueue = cbs_enqueue_offload;
3043d0bd028SVinicius Costa Gomes q->dequeue = cbs_dequeue_offload;
3053d0bd028SVinicius Costa Gomes
3063d0bd028SVinicius Costa Gomes return 0;
3073d0bd028SVinicius Costa Gomes }
3083d0bd028SVinicius Costa Gomes
cbs_set_port_rate(struct net_device * dev,struct cbs_sched_data * q)309e0a7683dSLeandro Dorileo static void cbs_set_port_rate(struct net_device *dev, struct cbs_sched_data *q)
310e0a7683dSLeandro Dorileo {
311e0a7683dSLeandro Dorileo struct ethtool_link_ksettings ecmd;
3121c6c09a0SVladimir Oltean int speed = SPEED_10;
3139367fa08SColin Ian King int port_rate;
3141c6c09a0SVladimir Oltean int err;
315e0a7683dSLeandro Dorileo
3161c6c09a0SVladimir Oltean err = __ethtool_get_link_ksettings(dev, &ecmd);
3171c6c09a0SVladimir Oltean if (err < 0)
3181c6c09a0SVladimir Oltean goto skip;
3191c6c09a0SVladimir Oltean
32083c8c3cfSVladimir Oltean if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
3211c6c09a0SVladimir Oltean speed = ecmd.base.speed;
3221c6c09a0SVladimir Oltean
3231c6c09a0SVladimir Oltean skip:
3241c6c09a0SVladimir Oltean port_rate = speed * 1000 * BYTES_PER_KBIT;
325e0a7683dSLeandro Dorileo
326e0a7683dSLeandro Dorileo atomic64_set(&q->port_rate, port_rate);
327e0a7683dSLeandro Dorileo netdev_dbg(dev, "cbs: set %s's port_rate to: %lld, linkspeed: %d\n",
328e0a7683dSLeandro Dorileo dev->name, (long long)atomic64_read(&q->port_rate),
329e0a7683dSLeandro Dorileo ecmd.base.speed);
330e0a7683dSLeandro Dorileo }
331e0a7683dSLeandro Dorileo
cbs_dev_notifier(struct notifier_block * nb,unsigned long event,void * ptr)332e0a7683dSLeandro Dorileo static int cbs_dev_notifier(struct notifier_block *nb, unsigned long event,
333e0a7683dSLeandro Dorileo void *ptr)
334e0a7683dSLeandro Dorileo {
335e0a7683dSLeandro Dorileo struct net_device *dev = netdev_notifier_info_to_dev(ptr);
336e0a7683dSLeandro Dorileo struct cbs_sched_data *q;
337e0a7683dSLeandro Dorileo struct net_device *qdev;
338e0a7683dSLeandro Dorileo bool found = false;
339e0a7683dSLeandro Dorileo
340e0a7683dSLeandro Dorileo ASSERT_RTNL();
341e0a7683dSLeandro Dorileo
342e0a7683dSLeandro Dorileo if (event != NETDEV_UP && event != NETDEV_CHANGE)
343e0a7683dSLeandro Dorileo return NOTIFY_DONE;
344e0a7683dSLeandro Dorileo
345e0a7683dSLeandro Dorileo spin_lock(&cbs_list_lock);
346e0a7683dSLeandro Dorileo list_for_each_entry(q, &cbs_list, cbs_list) {
347e0a7683dSLeandro Dorileo qdev = qdisc_dev(q->qdisc);
348e0a7683dSLeandro Dorileo if (qdev == dev) {
349e0a7683dSLeandro Dorileo found = true;
350e0a7683dSLeandro Dorileo break;
351e0a7683dSLeandro Dorileo }
352e0a7683dSLeandro Dorileo }
353e0a7683dSLeandro Dorileo spin_unlock(&cbs_list_lock);
354e0a7683dSLeandro Dorileo
355e0a7683dSLeandro Dorileo if (found)
356e0a7683dSLeandro Dorileo cbs_set_port_rate(dev, q);
357e0a7683dSLeandro Dorileo
358e0a7683dSLeandro Dorileo return NOTIFY_DONE;
359e0a7683dSLeandro Dorileo }
360e0a7683dSLeandro Dorileo
cbs_change(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)3612030721cSAlexander Aring static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
3622030721cSAlexander Aring struct netlink_ext_ack *extack)
363585d763aSVinicius Costa Gomes {
364585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
365585d763aSVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch);
366585d763aSVinicius Costa Gomes struct nlattr *tb[TCA_CBS_MAX + 1];
367585d763aSVinicius Costa Gomes struct tc_cbs_qopt *qopt;
368585d763aSVinicius Costa Gomes int err;
369585d763aSVinicius Costa Gomes
3708cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_CBS_MAX, opt, cbs_policy,
3718cb08174SJohannes Berg extack);
372585d763aSVinicius Costa Gomes if (err < 0)
373585d763aSVinicius Costa Gomes return err;
374585d763aSVinicius Costa Gomes
375710fb396SAlexander Aring if (!tb[TCA_CBS_PARMS]) {
376710fb396SAlexander Aring NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
377585d763aSVinicius Costa Gomes return -EINVAL;
378710fb396SAlexander Aring }
379585d763aSVinicius Costa Gomes
380585d763aSVinicius Costa Gomes qopt = nla_data(tb[TCA_CBS_PARMS]);
381585d763aSVinicius Costa Gomes
3823d0bd028SVinicius Costa Gomes if (!qopt->offload) {
383e0a7683dSLeandro Dorileo cbs_set_port_rate(dev, q);
3843d0bd028SVinicius Costa Gomes cbs_disable_offload(dev, q);
3853d0bd028SVinicius Costa Gomes } else {
386710fb396SAlexander Aring err = cbs_enable_offload(dev, q, qopt, extack);
3873d0bd028SVinicius Costa Gomes if (err < 0)
3883d0bd028SVinicius Costa Gomes return err;
3893d0bd028SVinicius Costa Gomes }
390585d763aSVinicius Costa Gomes
3913d0bd028SVinicius Costa Gomes /* Everything went OK, save the parameters used. */
392*8eb54a42SEric Dumazet WRITE_ONCE(q->hicredit, qopt->hicredit);
393*8eb54a42SEric Dumazet WRITE_ONCE(q->locredit, qopt->locredit);
394*8eb54a42SEric Dumazet WRITE_ONCE(q->idleslope, qopt->idleslope * BYTES_PER_KBIT);
395*8eb54a42SEric Dumazet WRITE_ONCE(q->sendslope, qopt->sendslope * BYTES_PER_KBIT);
396*8eb54a42SEric Dumazet WRITE_ONCE(q->offload, qopt->offload);
397585d763aSVinicius Costa Gomes
398585d763aSVinicius Costa Gomes return 0;
399585d763aSVinicius Costa Gomes }
400585d763aSVinicius Costa Gomes
cbs_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)401e63d7dfdSAlexander Aring static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
402e63d7dfdSAlexander Aring struct netlink_ext_ack *extack)
403585d763aSVinicius Costa Gomes {
404585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
4053d0bd028SVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch);
406585d763aSVinicius Costa Gomes
407710fb396SAlexander Aring if (!opt) {
408710fb396SAlexander Aring NL_SET_ERR_MSG(extack, "Missing CBS qdisc options which are mandatory");
409585d763aSVinicius Costa Gomes return -EINVAL;
410710fb396SAlexander Aring }
411585d763aSVinicius Costa Gomes
412990e35ecSVinicius Costa Gomes q->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
413990e35ecSVinicius Costa Gomes sch->handle, extack);
414990e35ecSVinicius Costa Gomes if (!q->qdisc)
415990e35ecSVinicius Costa Gomes return -ENOMEM;
416990e35ecSVinicius Costa Gomes
4173e8b9bfaSVinicius Costa Gomes spin_lock(&cbs_list_lock);
4183e8b9bfaSVinicius Costa Gomes list_add(&q->cbs_list, &cbs_list);
4193e8b9bfaSVinicius Costa Gomes spin_unlock(&cbs_list_lock);
4203e8b9bfaSVinicius Costa Gomes
421990e35ecSVinicius Costa Gomes qdisc_hash_add(q->qdisc, false);
422990e35ecSVinicius Costa Gomes
4233d0bd028SVinicius Costa Gomes q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
4243d0bd028SVinicius Costa Gomes
4253d0bd028SVinicius Costa Gomes q->enqueue = cbs_enqueue_soft;
4263d0bd028SVinicius Costa Gomes q->dequeue = cbs_dequeue_soft;
4273d0bd028SVinicius Costa Gomes
428585d763aSVinicius Costa Gomes qdisc_watchdog_init(&q->watchdog, sch);
429585d763aSVinicius Costa Gomes
4303e8b9bfaSVinicius Costa Gomes return cbs_change(sch, opt, extack);
431585d763aSVinicius Costa Gomes }
432585d763aSVinicius Costa Gomes
cbs_destroy(struct Qdisc * sch)433585d763aSVinicius Costa Gomes static void cbs_destroy(struct Qdisc *sch)
434585d763aSVinicius Costa Gomes {
435585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
4363d0bd028SVinicius Costa Gomes struct net_device *dev = qdisc_dev(sch);
437585d763aSVinicius Costa Gomes
4383e8b9bfaSVinicius Costa Gomes /* Nothing to do if we couldn't create the underlying qdisc */
4393e8b9bfaSVinicius Costa Gomes if (!q->qdisc)
4403e8b9bfaSVinicius Costa Gomes return;
4413d0bd028SVinicius Costa Gomes
442e0a7683dSLeandro Dorileo qdisc_watchdog_cancel(&q->watchdog);
4433d0bd028SVinicius Costa Gomes cbs_disable_offload(dev, q);
444990e35ecSVinicius Costa Gomes
4453e8b9bfaSVinicius Costa Gomes spin_lock(&cbs_list_lock);
4463e8b9bfaSVinicius Costa Gomes list_del(&q->cbs_list);
4473e8b9bfaSVinicius Costa Gomes spin_unlock(&cbs_list_lock);
4483e8b9bfaSVinicius Costa Gomes
44986bd446bSVlad Buslov qdisc_put(q->qdisc);
450585d763aSVinicius Costa Gomes }
451585d763aSVinicius Costa Gomes
cbs_dump(struct Qdisc * sch,struct sk_buff * skb)452585d763aSVinicius Costa Gomes static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
453585d763aSVinicius Costa Gomes {
454585d763aSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
455585d763aSVinicius Costa Gomes struct tc_cbs_qopt opt = { };
456585d763aSVinicius Costa Gomes struct nlattr *nest;
457585d763aSVinicius Costa Gomes
458ae0be8deSMichal Kubecek nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
459585d763aSVinicius Costa Gomes if (!nest)
460585d763aSVinicius Costa Gomes goto nla_put_failure;
461585d763aSVinicius Costa Gomes
462*8eb54a42SEric Dumazet opt.hicredit = READ_ONCE(q->hicredit);
463*8eb54a42SEric Dumazet opt.locredit = READ_ONCE(q->locredit);
464*8eb54a42SEric Dumazet opt.sendslope = div64_s64(READ_ONCE(q->sendslope), BYTES_PER_KBIT);
465*8eb54a42SEric Dumazet opt.idleslope = div64_s64(READ_ONCE(q->idleslope), BYTES_PER_KBIT);
466*8eb54a42SEric Dumazet opt.offload = READ_ONCE(q->offload);
467585d763aSVinicius Costa Gomes
468585d763aSVinicius Costa Gomes if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
469585d763aSVinicius Costa Gomes goto nla_put_failure;
470585d763aSVinicius Costa Gomes
471585d763aSVinicius Costa Gomes return nla_nest_end(skb, nest);
472585d763aSVinicius Costa Gomes
473585d763aSVinicius Costa Gomes nla_put_failure:
474585d763aSVinicius Costa Gomes nla_nest_cancel(skb, nest);
475585d763aSVinicius Costa Gomes return -1;
476585d763aSVinicius Costa Gomes }
477585d763aSVinicius Costa Gomes
cbs_dump_class(struct Qdisc * sch,unsigned long cl,struct sk_buff * skb,struct tcmsg * tcm)478990e35ecSVinicius Costa Gomes static int cbs_dump_class(struct Qdisc *sch, unsigned long cl,
479990e35ecSVinicius Costa Gomes struct sk_buff *skb, struct tcmsg *tcm)
480990e35ecSVinicius Costa Gomes {
481990e35ecSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
482990e35ecSVinicius Costa Gomes
483990e35ecSVinicius Costa Gomes if (cl != 1 || !q->qdisc) /* only one class */
484990e35ecSVinicius Costa Gomes return -ENOENT;
485990e35ecSVinicius Costa Gomes
486990e35ecSVinicius Costa Gomes tcm->tcm_handle |= TC_H_MIN(1);
487990e35ecSVinicius Costa Gomes tcm->tcm_info = q->qdisc->handle;
488990e35ecSVinicius Costa Gomes
489990e35ecSVinicius Costa Gomes return 0;
490990e35ecSVinicius Costa Gomes }
491990e35ecSVinicius Costa Gomes
cbs_graft(struct Qdisc * sch,unsigned long arg,struct Qdisc * new,struct Qdisc ** old,struct netlink_ext_ack * extack)492990e35ecSVinicius Costa Gomes static int cbs_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
493990e35ecSVinicius Costa Gomes struct Qdisc **old, struct netlink_ext_ack *extack)
494990e35ecSVinicius Costa Gomes {
495990e35ecSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
496990e35ecSVinicius Costa Gomes
497990e35ecSVinicius Costa Gomes if (!new) {
498990e35ecSVinicius Costa Gomes new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
499990e35ecSVinicius Costa Gomes sch->handle, NULL);
500990e35ecSVinicius Costa Gomes if (!new)
501990e35ecSVinicius Costa Gomes new = &noop_qdisc;
502990e35ecSVinicius Costa Gomes }
503990e35ecSVinicius Costa Gomes
504990e35ecSVinicius Costa Gomes *old = qdisc_replace(sch, new, &q->qdisc);
505990e35ecSVinicius Costa Gomes return 0;
506990e35ecSVinicius Costa Gomes }
507990e35ecSVinicius Costa Gomes
cbs_leaf(struct Qdisc * sch,unsigned long arg)508990e35ecSVinicius Costa Gomes static struct Qdisc *cbs_leaf(struct Qdisc *sch, unsigned long arg)
509990e35ecSVinicius Costa Gomes {
510990e35ecSVinicius Costa Gomes struct cbs_sched_data *q = qdisc_priv(sch);
511990e35ecSVinicius Costa Gomes
512990e35ecSVinicius Costa Gomes return q->qdisc;
513990e35ecSVinicius Costa Gomes }
514990e35ecSVinicius Costa Gomes
cbs_find(struct Qdisc * sch,u32 classid)515990e35ecSVinicius Costa Gomes static unsigned long cbs_find(struct Qdisc *sch, u32 classid)
516990e35ecSVinicius Costa Gomes {
517990e35ecSVinicius Costa Gomes return 1;
518990e35ecSVinicius Costa Gomes }
519990e35ecSVinicius Costa Gomes
cbs_walk(struct Qdisc * sch,struct qdisc_walker * walker)520990e35ecSVinicius Costa Gomes static void cbs_walk(struct Qdisc *sch, struct qdisc_walker *walker)
521990e35ecSVinicius Costa Gomes {
522990e35ecSVinicius Costa Gomes if (!walker->stop) {
523e046fa89SZhengchao Shao tc_qdisc_stats_dump(sch, 1, walker);
524990e35ecSVinicius Costa Gomes }
525990e35ecSVinicius Costa Gomes }
526990e35ecSVinicius Costa Gomes
527990e35ecSVinicius Costa Gomes static const struct Qdisc_class_ops cbs_class_ops = {
528990e35ecSVinicius Costa Gomes .graft = cbs_graft,
529990e35ecSVinicius Costa Gomes .leaf = cbs_leaf,
530990e35ecSVinicius Costa Gomes .find = cbs_find,
531990e35ecSVinicius Costa Gomes .walk = cbs_walk,
532990e35ecSVinicius Costa Gomes .dump = cbs_dump_class,
533990e35ecSVinicius Costa Gomes };
534990e35ecSVinicius Costa Gomes
535585d763aSVinicius Costa Gomes static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
536585d763aSVinicius Costa Gomes .id = "cbs",
537990e35ecSVinicius Costa Gomes .cl_ops = &cbs_class_ops,
538585d763aSVinicius Costa Gomes .priv_size = sizeof(struct cbs_sched_data),
539585d763aSVinicius Costa Gomes .enqueue = cbs_enqueue,
540585d763aSVinicius Costa Gomes .dequeue = cbs_dequeue,
541585d763aSVinicius Costa Gomes .peek = qdisc_peek_dequeued,
542585d763aSVinicius Costa Gomes .init = cbs_init,
543585d763aSVinicius Costa Gomes .reset = qdisc_reset_queue,
544585d763aSVinicius Costa Gomes .destroy = cbs_destroy,
545585d763aSVinicius Costa Gomes .change = cbs_change,
546585d763aSVinicius Costa Gomes .dump = cbs_dump,
547585d763aSVinicius Costa Gomes .owner = THIS_MODULE,
548585d763aSVinicius Costa Gomes };
549241a94abSMichal Koutný MODULE_ALIAS_NET_SCH("cbs");
550585d763aSVinicius Costa Gomes
551e0a7683dSLeandro Dorileo static struct notifier_block cbs_device_notifier = {
552e0a7683dSLeandro Dorileo .notifier_call = cbs_dev_notifier,
553e0a7683dSLeandro Dorileo };
554e0a7683dSLeandro Dorileo
cbs_module_init(void)555585d763aSVinicius Costa Gomes static int __init cbs_module_init(void)
556585d763aSVinicius Costa Gomes {
55745d5cb13SYueHaibing int err;
558e0a7683dSLeandro Dorileo
55945d5cb13SYueHaibing err = register_netdevice_notifier(&cbs_device_notifier);
560e0a7683dSLeandro Dorileo if (err)
561e0a7683dSLeandro Dorileo return err;
562e0a7683dSLeandro Dorileo
56345d5cb13SYueHaibing err = register_qdisc(&cbs_qdisc_ops);
56445d5cb13SYueHaibing if (err)
56545d5cb13SYueHaibing unregister_netdevice_notifier(&cbs_device_notifier);
56645d5cb13SYueHaibing
56745d5cb13SYueHaibing return err;
568585d763aSVinicius Costa Gomes }
569585d763aSVinicius Costa Gomes
cbs_module_exit(void)570585d763aSVinicius Costa Gomes static void __exit cbs_module_exit(void)
571585d763aSVinicius Costa Gomes {
572585d763aSVinicius Costa Gomes unregister_qdisc(&cbs_qdisc_ops);
573e0a7683dSLeandro Dorileo unregister_netdevice_notifier(&cbs_device_notifier);
574585d763aSVinicius Costa Gomes }
575585d763aSVinicius Costa Gomes module_init(cbs_module_init)
576585d763aSVinicius Costa Gomes module_exit(cbs_module_exit)
577585d763aSVinicius Costa Gomes MODULE_LICENSE("GPL");
578f96118c5SVictor Nogueira MODULE_DESCRIPTION("Credit Based shaper");
579