Skip to content

Commit 3d0bd02

Browse files
vcgomesJeff Kirsher
authored and
Jeff Kirsher
committed
net/sched: Add support for HW offloading for CBS
This adds support for offloading the CBS algorithm to the controller, if supported. Drivers wanting to support CBS offload must implement the .ndo_setup_tc callback and handle the TC_SETUP_CBS (introduced here) type. Signed-off-by: Vinicius Costa Gomes <[email protected]> Tested-by: Henrik Austad <[email protected]> Signed-off-by: Jeff Kirsher <[email protected]>
1 parent 585d763 commit 3d0bd02

File tree

3 files changed

+102
-12
lines changed

3 files changed

+102
-12
lines changed

include/linux/netdevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ enum tc_setup_type {
776776
TC_SETUP_CLSMATCHALL,
777777
TC_SETUP_CLSBPF,
778778
TC_SETUP_BLOCK,
779+
TC_SETUP_CBS,
779780
};
780781

781782
/* These structures hold the attributes of xdp state that are being passed

include/net/pkt_sched.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,13 @@ static inline struct net *qdisc_net(struct Qdisc *q)
140140
return dev_net(q->dev_queue->dev);
141141
}
142142

143+
struct tc_cbs_qopt_offload {
144+
u8 enable;
145+
s32 queue;
146+
s32 hicredit;
147+
s32 locredit;
148+
s32 idleslope;
149+
s32 sendslope;
150+
};
151+
143152
#endif

net/sched/sch_cbs.c

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
#define BYTES_PER_KBIT (1000LL / 8)
6969

7070
struct cbs_sched_data {
71+
bool offload;
72+
int queue;
7173
s64 port_rate; /* in bytes/s */
7274
s64 last; /* timestamp in ns */
7375
s64 credits; /* in bytes */
@@ -80,6 +82,11 @@ struct cbs_sched_data {
8082
struct sk_buff *(*dequeue)(struct Qdisc *sch);
8183
};
8284

85+
static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch)
86+
{
87+
return qdisc_enqueue_tail(skb, sch);
88+
}
89+
8390
static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch)
8491
{
8592
struct cbs_sched_data *q = qdisc_priv(sch);
@@ -169,6 +176,11 @@ static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
169176
return skb;
170177
}
171178

179+
static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
180+
{
181+
return qdisc_dequeue_head(sch);
182+
}
183+
172184
static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
173185
{
174186
struct cbs_sched_data *q = qdisc_priv(sch);
@@ -180,14 +192,66 @@ static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
180192
[TCA_CBS_PARMS] = { .len = sizeof(struct tc_cbs_qopt) },
181193
};
182194

195+
static void cbs_disable_offload(struct net_device *dev,
196+
struct cbs_sched_data *q)
197+
{
198+
struct tc_cbs_qopt_offload cbs = { };
199+
const struct net_device_ops *ops;
200+
int err;
201+
202+
if (!q->offload)
203+
return;
204+
205+
q->enqueue = cbs_enqueue_soft;
206+
q->dequeue = cbs_dequeue_soft;
207+
208+
ops = dev->netdev_ops;
209+
if (!ops->ndo_setup_tc)
210+
return;
211+
212+
cbs.queue = q->queue;
213+
cbs.enable = 0;
214+
215+
err = ops->ndo_setup_tc(dev, TC_SETUP_CBS, &cbs);
216+
if (err < 0)
217+
pr_warn("Couldn't disable CBS offload for queue %d\n",
218+
cbs.queue);
219+
}
220+
221+
static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
222+
const struct tc_cbs_qopt *opt)
223+
{
224+
const struct net_device_ops *ops = dev->netdev_ops;
225+
struct tc_cbs_qopt_offload cbs = { };
226+
int err;
227+
228+
if (!ops->ndo_setup_tc)
229+
return -EOPNOTSUPP;
230+
231+
cbs.queue = q->queue;
232+
233+
cbs.enable = 1;
234+
cbs.hicredit = opt->hicredit;
235+
cbs.locredit = opt->locredit;
236+
cbs.idleslope = opt->idleslope;
237+
cbs.sendslope = opt->sendslope;
238+
239+
err = ops->ndo_setup_tc(dev, TC_SETUP_CBS, &cbs);
240+
if (err < 0)
241+
return err;
242+
243+
q->enqueue = cbs_enqueue_offload;
244+
q->dequeue = cbs_dequeue_offload;
245+
246+
return 0;
247+
}
248+
183249
static int cbs_change(struct Qdisc *sch, struct nlattr *opt)
184250
{
185251
struct cbs_sched_data *q = qdisc_priv(sch);
186252
struct net_device *dev = qdisc_dev(sch);
187253
struct nlattr *tb[TCA_CBS_MAX + 1];
188-
struct ethtool_link_ksettings ecmd;
189254
struct tc_cbs_qopt *qopt;
190-
s64 link_speed;
191255
int err;
192256

193257
err = nla_parse_nested(tb, TCA_CBS_MAX, opt, cbs_policy, NULL);
@@ -199,34 +263,47 @@ static int cbs_change(struct Qdisc *sch, struct nlattr *opt)
199263

200264
qopt = nla_data(tb[TCA_CBS_PARMS]);
201265

202-
if (qopt->offload)
203-
return -EOPNOTSUPP;
266+
if (!qopt->offload) {
267+
struct ethtool_link_ksettings ecmd;
268+
s64 link_speed;
204269

205-
if (!__ethtool_get_link_ksettings(dev, &ecmd))
206-
link_speed = ecmd.base.speed;
207-
else
208-
link_speed = SPEED_1000;
270+
if (!__ethtool_get_link_ksettings(dev, &ecmd))
271+
link_speed = ecmd.base.speed;
272+
else
273+
link_speed = SPEED_1000;
209274

210-
q->port_rate = link_speed * 1000 * BYTES_PER_KBIT;
275+
q->port_rate = link_speed * 1000 * BYTES_PER_KBIT;
211276

212-
q->enqueue = cbs_enqueue_soft;
213-
q->dequeue = cbs_dequeue_soft;
277+
cbs_disable_offload(dev, q);
278+
} else {
279+
err = cbs_enable_offload(dev, q, qopt);
280+
if (err < 0)
281+
return err;
282+
}
214283

284+
/* Everything went OK, save the parameters used. */
215285
q->hicredit = qopt->hicredit;
216286
q->locredit = qopt->locredit;
217287
q->idleslope = qopt->idleslope * BYTES_PER_KBIT;
218288
q->sendslope = qopt->sendslope * BYTES_PER_KBIT;
289+
q->offload = qopt->offload;
219290

220291
return 0;
221292
}
222293

223294
static int cbs_init(struct Qdisc *sch, struct nlattr *opt)
224295
{
225296
struct cbs_sched_data *q = qdisc_priv(sch);
297+
struct net_device *dev = qdisc_dev(sch);
226298

227299
if (!opt)
228300
return -EINVAL;
229301

302+
q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
303+
304+
q->enqueue = cbs_enqueue_soft;
305+
q->dequeue = cbs_dequeue_soft;
306+
230307
qdisc_watchdog_init(&q->watchdog, sch);
231308

232309
return cbs_change(sch, opt);
@@ -235,8 +312,11 @@ static int cbs_init(struct Qdisc *sch, struct nlattr *opt)
235312
static void cbs_destroy(struct Qdisc *sch)
236313
{
237314
struct cbs_sched_data *q = qdisc_priv(sch);
315+
struct net_device *dev = qdisc_dev(sch);
238316

239317
qdisc_watchdog_cancel(&q->watchdog);
318+
319+
cbs_disable_offload(dev, q);
240320
}
241321

242322
static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
@@ -253,7 +333,7 @@ static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
253333
opt.locredit = q->locredit;
254334
opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT);
255335
opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT);
256-
opt.offload = 0;
336+
opt.offload = q->offload;
257337

258338
if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
259339
goto nla_put_failure;

0 commit comments

Comments
 (0)