blob: 52452b546564c971ffbf4b63078063af809a8cd6 [file] [log] [blame]
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -07001// SPDX-License-Identifier: GPL-2.0
2
3/* net/sched/sch_etf.c Earliest TxTime First queueing discipline.
4 *
5 * Authors: Jesus Sanchez-Palencia <jesus.sanchez-palencia@intel.com>
6 * Vinicius Costa Gomes <vinicius.gomes@intel.com>
7 */
8
9#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/string.h>
13#include <linux/errno.h>
Jesus Sanchez-Palencia4b15c702018-07-03 15:43:00 -070014#include <linux/errqueue.h>
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -070015#include <linux/rbtree.h>
16#include <linux/skbuff.h>
17#include <linux/posix-timers.h>
18#include <net/netlink.h>
19#include <net/sch_generic.h>
20#include <net/pkt_sched.h>
21#include <net/sock.h>
22
23#define DEADLINE_MODE_IS_ON(x) ((x)->flags & TC_ETF_DEADLINE_MODE_ON)
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -070024#define OFFLOAD_IS_ON(x) ((x)->flags & TC_ETF_OFFLOAD_ON)
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -070025
26struct etf_sched_data {
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -070027 bool offload;
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -070028 bool deadline_mode;
29 int clockid;
30 int queue;
31 s32 delta; /* in ns */
32 ktime_t last; /* The txtime of the last skb sent to the netdevice. */
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -080033 struct rb_root_cached head;
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -070034 struct qdisc_watchdog watchdog;
35 ktime_t (*get_time)(void);
36};
37
38static const struct nla_policy etf_policy[TCA_ETF_MAX + 1] = {
39 [TCA_ETF_PARMS] = { .len = sizeof(struct tc_etf_qopt) },
40};
41
42static inline int validate_input_params(struct tc_etf_qopt *qopt,
43 struct netlink_ext_ack *extack)
44{
45 /* Check if params comply to the following rules:
46 * * Clockid and delta must be valid.
47 *
48 * * Dynamic clockids are not supported.
49 *
50 * * Delta must be a positive integer.
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -070051 *
52 * Also note that for the HW offload case, we must
53 * expect that system clocks have been synchronized to PHC.
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -070054 */
55 if (qopt->clockid < 0) {
56 NL_SET_ERR_MSG(extack, "Dynamic clockids are not supported");
57 return -ENOTSUPP;
58 }
59
60 if (qopt->clockid != CLOCK_TAI) {
61 NL_SET_ERR_MSG(extack, "Invalid clockid. CLOCK_TAI must be used");
62 return -EINVAL;
63 }
64
65 if (qopt->delta < 0) {
66 NL_SET_ERR_MSG(extack, "Delta must be positive");
67 return -EINVAL;
68 }
69
70 return 0;
71}
72
73static bool is_packet_valid(struct Qdisc *sch, struct sk_buff *nskb)
74{
75 struct etf_sched_data *q = qdisc_priv(sch);
76 ktime_t txtime = nskb->tstamp;
77 struct sock *sk = nskb->sk;
78 ktime_t now;
79
80 if (!sk)
81 return false;
82
83 if (!sock_flag(sk, SOCK_TXTIME))
84 return false;
85
86 /* We don't perform crosstimestamping.
87 * Drop if packet's clockid differs from qdisc's.
88 */
89 if (sk->sk_clockid != q->clockid)
90 return false;
91
92 if (sk->sk_txtime_deadline_mode != q->deadline_mode)
93 return false;
94
95 now = q->get_time();
96 if (ktime_before(txtime, now) || ktime_before(txtime, q->last))
97 return false;
98
99 return true;
100}
101
102static struct sk_buff *etf_peek_timesortedlist(struct Qdisc *sch)
103{
104 struct etf_sched_data *q = qdisc_priv(sch);
105 struct rb_node *p;
106
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -0800107 p = rb_first_cached(&q->head);
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700108 if (!p)
109 return NULL;
110
111 return rb_to_skb(p);
112}
113
114static void reset_watchdog(struct Qdisc *sch)
115{
116 struct etf_sched_data *q = qdisc_priv(sch);
117 struct sk_buff *skb = etf_peek_timesortedlist(sch);
118 ktime_t next;
119
Jesus Sanchez-Palencia3fcbdae2018-11-14 17:26:32 -0800120 if (!skb) {
121 qdisc_watchdog_cancel(&q->watchdog);
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700122 return;
Jesus Sanchez-Palencia3fcbdae2018-11-14 17:26:32 -0800123 }
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700124
125 next = ktime_sub_ns(skb->tstamp, q->delta);
126 qdisc_watchdog_schedule_ns(&q->watchdog, ktime_to_ns(next));
127}
128
Jesus Sanchez-Palencia4b15c702018-07-03 15:43:00 -0700129static void report_sock_error(struct sk_buff *skb, u32 err, u8 code)
130{
131 struct sock_exterr_skb *serr;
132 struct sk_buff *clone;
133 ktime_t txtime = skb->tstamp;
134
135 if (!skb->sk || !(skb->sk->sk_txtime_report_errors))
136 return;
137
138 clone = skb_clone(skb, GFP_ATOMIC);
139 if (!clone)
140 return;
141
142 serr = SKB_EXT_ERR(clone);
143 serr->ee.ee_errno = err;
144 serr->ee.ee_origin = SO_EE_ORIGIN_TXTIME;
145 serr->ee.ee_type = 0;
146 serr->ee.ee_code = code;
147 serr->ee.ee_pad = 0;
148 serr->ee.ee_data = (txtime >> 32); /* high part of tstamp */
149 serr->ee.ee_info = txtime; /* low part of tstamp */
150
151 if (sock_queue_err_skb(skb->sk, clone))
152 kfree_skb(clone);
153}
154
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700155static int etf_enqueue_timesortedlist(struct sk_buff *nskb, struct Qdisc *sch,
156 struct sk_buff **to_free)
157{
158 struct etf_sched_data *q = qdisc_priv(sch);
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -0800159 struct rb_node **p = &q->head.rb_root.rb_node, *parent = NULL;
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700160 ktime_t txtime = nskb->tstamp;
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -0800161 bool leftmost = true;
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700162
Jesus Sanchez-Palencia4b15c702018-07-03 15:43:00 -0700163 if (!is_packet_valid(sch, nskb)) {
164 report_sock_error(nskb, EINVAL,
165 SO_EE_CODE_TXTIME_INVALID_PARAM);
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700166 return qdisc_drop(nskb, sch, to_free);
Jesus Sanchez-Palencia4b15c702018-07-03 15:43:00 -0700167 }
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700168
169 while (*p) {
170 struct sk_buff *skb;
171
172 parent = *p;
173 skb = rb_to_skb(parent);
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -0800174 if (ktime_after(txtime, skb->tstamp)) {
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700175 p = &parent->rb_right;
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -0800176 leftmost = false;
177 } else {
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700178 p = &parent->rb_left;
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -0800179 }
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700180 }
181 rb_link_node(&nskb->rbnode, parent, p);
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -0800182 rb_insert_color_cached(&nskb->rbnode, &q->head, leftmost);
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700183
184 qdisc_qstats_backlog_inc(sch, nskb);
185 sch->q.qlen++;
186
187 /* Now we may need to re-arm the qdisc watchdog for the next packet. */
188 reset_watchdog(sch);
189
190 return NET_XMIT_SUCCESS;
191}
192
193static void timesortedlist_erase(struct Qdisc *sch, struct sk_buff *skb,
194 bool drop)
195{
196 struct etf_sched_data *q = qdisc_priv(sch);
197
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -0800198 rb_erase_cached(&skb->rbnode, &q->head);
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700199
200 /* The rbnode field in the skb re-uses these fields, now that
201 * we are done with the rbnode, reset them.
202 */
203 skb->next = NULL;
204 skb->prev = NULL;
205 skb->dev = qdisc_dev(sch);
206
207 qdisc_qstats_backlog_dec(sch, skb);
208
209 if (drop) {
210 struct sk_buff *to_free = NULL;
211
Jesus Sanchez-Palencia4b15c702018-07-03 15:43:00 -0700212 report_sock_error(skb, ECANCELED, SO_EE_CODE_TXTIME_MISSED);
213
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700214 qdisc_drop(skb, sch, &to_free);
215 kfree_skb_list(to_free);
216 qdisc_qstats_overlimit(sch);
217 } else {
218 qdisc_bstats_update(sch, skb);
219
220 q->last = skb->tstamp;
221 }
222
223 sch->q.qlen--;
224}
225
226static struct sk_buff *etf_dequeue_timesortedlist(struct Qdisc *sch)
227{
228 struct etf_sched_data *q = qdisc_priv(sch);
229 struct sk_buff *skb;
230 ktime_t now, next;
231
232 skb = etf_peek_timesortedlist(sch);
233 if (!skb)
234 return NULL;
235
236 now = q->get_time();
237
238 /* Drop if packet has expired while in queue. */
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700239 if (ktime_before(skb->tstamp, now)) {
240 timesortedlist_erase(sch, skb, true);
241 skb = NULL;
242 goto out;
243 }
244
245 /* When in deadline mode, dequeue as soon as possible and change the
246 * txtime from deadline to (now + delta).
247 */
248 if (q->deadline_mode) {
249 timesortedlist_erase(sch, skb, false);
250 skb->tstamp = now;
251 goto out;
252 }
253
254 next = ktime_sub_ns(skb->tstamp, q->delta);
255
256 /* Dequeue only if now is within the [txtime - delta, txtime] range. */
257 if (ktime_after(now, next))
258 timesortedlist_erase(sch, skb, false);
259 else
260 skb = NULL;
261
262out:
263 /* Now we may need to re-arm the qdisc watchdog for the next packet. */
264 reset_watchdog(sch);
265
266 return skb;
267}
268
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -0700269static void etf_disable_offload(struct net_device *dev,
270 struct etf_sched_data *q)
271{
272 struct tc_etf_qopt_offload etf = { };
273 const struct net_device_ops *ops;
274 int err;
275
276 if (!q->offload)
277 return;
278
279 ops = dev->netdev_ops;
280 if (!ops->ndo_setup_tc)
281 return;
282
283 etf.queue = q->queue;
284 etf.enable = 0;
285
286 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_ETF, &etf);
287 if (err < 0)
288 pr_warn("Couldn't disable ETF offload for queue %d\n",
289 etf.queue);
290}
291
292static int etf_enable_offload(struct net_device *dev, struct etf_sched_data *q,
293 struct netlink_ext_ack *extack)
294{
295 const struct net_device_ops *ops = dev->netdev_ops;
296 struct tc_etf_qopt_offload etf = { };
297 int err;
298
299 if (q->offload)
300 return 0;
301
302 if (!ops->ndo_setup_tc) {
303 NL_SET_ERR_MSG(extack, "Specified device does not support ETF offload");
304 return -EOPNOTSUPP;
305 }
306
307 etf.queue = q->queue;
308 etf.enable = 1;
309
310 err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_ETF, &etf);
311 if (err < 0) {
312 NL_SET_ERR_MSG(extack, "Specified device failed to setup ETF hardware offload");
313 return err;
314 }
315
316 return 0;
317}
318
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700319static int etf_init(struct Qdisc *sch, struct nlattr *opt,
320 struct netlink_ext_ack *extack)
321{
322 struct etf_sched_data *q = qdisc_priv(sch);
323 struct net_device *dev = qdisc_dev(sch);
324 struct nlattr *tb[TCA_ETF_MAX + 1];
325 struct tc_etf_qopt *qopt;
326 int err;
327
328 if (!opt) {
329 NL_SET_ERR_MSG(extack,
330 "Missing ETF qdisc options which are mandatory");
331 return -EINVAL;
332 }
333
334 err = nla_parse_nested(tb, TCA_ETF_MAX, opt, etf_policy, extack);
335 if (err < 0)
336 return err;
337
338 if (!tb[TCA_ETF_PARMS]) {
339 NL_SET_ERR_MSG(extack, "Missing mandatory ETF parameters");
340 return -EINVAL;
341 }
342
343 qopt = nla_data(tb[TCA_ETF_PARMS]);
344
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -0700345 pr_debug("delta %d clockid %d offload %s deadline %s\n",
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700346 qopt->delta, qopt->clockid,
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -0700347 OFFLOAD_IS_ON(qopt) ? "on" : "off",
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700348 DEADLINE_MODE_IS_ON(qopt) ? "on" : "off");
349
350 err = validate_input_params(qopt, extack);
351 if (err < 0)
352 return err;
353
354 q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
355
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -0700356 if (OFFLOAD_IS_ON(qopt)) {
357 err = etf_enable_offload(dev, q, extack);
358 if (err < 0)
359 return err;
360 }
361
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700362 /* Everything went OK, save the parameters used. */
363 q->delta = qopt->delta;
364 q->clockid = qopt->clockid;
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -0700365 q->offload = OFFLOAD_IS_ON(qopt);
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700366 q->deadline_mode = DEADLINE_MODE_IS_ON(qopt);
367
368 switch (q->clockid) {
369 case CLOCK_REALTIME:
370 q->get_time = ktime_get_real;
371 break;
372 case CLOCK_MONOTONIC:
373 q->get_time = ktime_get;
374 break;
375 case CLOCK_BOOTTIME:
376 q->get_time = ktime_get_boottime;
377 break;
378 case CLOCK_TAI:
379 q->get_time = ktime_get_clocktai;
380 break;
381 default:
382 NL_SET_ERR_MSG(extack, "Clockid is not supported");
383 return -ENOTSUPP;
384 }
385
386 qdisc_watchdog_init_clockid(&q->watchdog, sch, q->clockid);
387
388 return 0;
389}
390
391static void timesortedlist_clear(struct Qdisc *sch)
392{
393 struct etf_sched_data *q = qdisc_priv(sch);
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -0800394 struct rb_node *p = rb_first_cached(&q->head);
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700395
396 while (p) {
397 struct sk_buff *skb = rb_to_skb(p);
398
399 p = rb_next(p);
400
Jesus Sanchez-Palencia09fd4862018-11-14 17:26:33 -0800401 rb_erase_cached(&skb->rbnode, &q->head);
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700402 rtnl_kfree_skbs(skb, skb);
403 sch->q.qlen--;
404 }
405}
406
407static void etf_reset(struct Qdisc *sch)
408{
409 struct etf_sched_data *q = qdisc_priv(sch);
410
411 /* Only cancel watchdog if it's been initialized. */
412 if (q->watchdog.qdisc == sch)
413 qdisc_watchdog_cancel(&q->watchdog);
414
415 /* No matter which mode we are on, it's safe to clear both lists. */
416 timesortedlist_clear(sch);
417 __qdisc_reset_queue(&sch->q);
418
419 sch->qstats.backlog = 0;
420 sch->q.qlen = 0;
421
422 q->last = 0;
423}
424
425static void etf_destroy(struct Qdisc *sch)
426{
427 struct etf_sched_data *q = qdisc_priv(sch);
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -0700428 struct net_device *dev = qdisc_dev(sch);
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700429
430 /* Only cancel watchdog if it's been initialized. */
431 if (q->watchdog.qdisc == sch)
432 qdisc_watchdog_cancel(&q->watchdog);
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -0700433
434 etf_disable_offload(dev, q);
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700435}
436
437static int etf_dump(struct Qdisc *sch, struct sk_buff *skb)
438{
439 struct etf_sched_data *q = qdisc_priv(sch);
440 struct tc_etf_qopt opt = { };
441 struct nlattr *nest;
442
443 nest = nla_nest_start(skb, TCA_OPTIONS);
444 if (!nest)
445 goto nla_put_failure;
446
447 opt.delta = q->delta;
448 opt.clockid = q->clockid;
Jesus Sanchez-Palencia88cab772018-07-03 15:42:54 -0700449 if (q->offload)
450 opt.flags |= TC_ETF_OFFLOAD_ON;
451
Vinicius Costa Gomes25db26a2018-07-03 15:42:53 -0700452 if (q->deadline_mode)
453 opt.flags |= TC_ETF_DEADLINE_MODE_ON;
454
455 if (nla_put(skb, TCA_ETF_PARMS, sizeof(opt), &opt))
456 goto nla_put_failure;
457
458 return nla_nest_end(skb, nest);
459
460nla_put_failure:
461 nla_nest_cancel(skb, nest);
462 return -1;
463}
464
465static struct Qdisc_ops etf_qdisc_ops __read_mostly = {
466 .id = "etf",
467 .priv_size = sizeof(struct etf_sched_data),
468 .enqueue = etf_enqueue_timesortedlist,
469 .dequeue = etf_dequeue_timesortedlist,
470 .peek = etf_peek_timesortedlist,
471 .init = etf_init,
472 .reset = etf_reset,
473 .destroy = etf_destroy,
474 .dump = etf_dump,
475 .owner = THIS_MODULE,
476};
477
478static int __init etf_module_init(void)
479{
480 return register_qdisc(&etf_qdisc_ops);
481}
482
483static void __exit etf_module_exit(void)
484{
485 unregister_qdisc(&etf_qdisc_ops);
486}
487module_init(etf_module_init)
488module_exit(etf_module_exit)
489MODULE_LICENSE("GPL");