Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 1 | // 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> |
| 14 | #include <linux/rbtree.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/posix-timers.h> |
| 17 | #include <net/netlink.h> |
| 18 | #include <net/sch_generic.h> |
| 19 | #include <net/pkt_sched.h> |
| 20 | #include <net/sock.h> |
| 21 | |
| 22 | #define DEADLINE_MODE_IS_ON(x) ((x)->flags & TC_ETF_DEADLINE_MODE_ON) |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 23 | #define OFFLOAD_IS_ON(x) ((x)->flags & TC_ETF_OFFLOAD_ON) |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 24 | |
| 25 | struct etf_sched_data { |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 26 | bool offload; |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 27 | bool deadline_mode; |
| 28 | int clockid; |
| 29 | int queue; |
| 30 | s32 delta; /* in ns */ |
| 31 | ktime_t last; /* The txtime of the last skb sent to the netdevice. */ |
| 32 | struct rb_root head; |
| 33 | struct qdisc_watchdog watchdog; |
| 34 | ktime_t (*get_time)(void); |
| 35 | }; |
| 36 | |
| 37 | static const struct nla_policy etf_policy[TCA_ETF_MAX + 1] = { |
| 38 | [TCA_ETF_PARMS] = { .len = sizeof(struct tc_etf_qopt) }, |
| 39 | }; |
| 40 | |
| 41 | static inline int validate_input_params(struct tc_etf_qopt *qopt, |
| 42 | struct netlink_ext_ack *extack) |
| 43 | { |
| 44 | /* Check if params comply to the following rules: |
| 45 | * * Clockid and delta must be valid. |
| 46 | * |
| 47 | * * Dynamic clockids are not supported. |
| 48 | * |
| 49 | * * Delta must be a positive integer. |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 50 | * |
| 51 | * Also note that for the HW offload case, we must |
| 52 | * expect that system clocks have been synchronized to PHC. |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 53 | */ |
| 54 | if (qopt->clockid < 0) { |
| 55 | NL_SET_ERR_MSG(extack, "Dynamic clockids are not supported"); |
| 56 | return -ENOTSUPP; |
| 57 | } |
| 58 | |
| 59 | if (qopt->clockid != CLOCK_TAI) { |
| 60 | NL_SET_ERR_MSG(extack, "Invalid clockid. CLOCK_TAI must be used"); |
| 61 | return -EINVAL; |
| 62 | } |
| 63 | |
| 64 | if (qopt->delta < 0) { |
| 65 | NL_SET_ERR_MSG(extack, "Delta must be positive"); |
| 66 | return -EINVAL; |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static bool is_packet_valid(struct Qdisc *sch, struct sk_buff *nskb) |
| 73 | { |
| 74 | struct etf_sched_data *q = qdisc_priv(sch); |
| 75 | ktime_t txtime = nskb->tstamp; |
| 76 | struct sock *sk = nskb->sk; |
| 77 | ktime_t now; |
| 78 | |
| 79 | if (!sk) |
| 80 | return false; |
| 81 | |
| 82 | if (!sock_flag(sk, SOCK_TXTIME)) |
| 83 | return false; |
| 84 | |
| 85 | /* We don't perform crosstimestamping. |
| 86 | * Drop if packet's clockid differs from qdisc's. |
| 87 | */ |
| 88 | if (sk->sk_clockid != q->clockid) |
| 89 | return false; |
| 90 | |
| 91 | if (sk->sk_txtime_deadline_mode != q->deadline_mode) |
| 92 | return false; |
| 93 | |
| 94 | now = q->get_time(); |
| 95 | if (ktime_before(txtime, now) || ktime_before(txtime, q->last)) |
| 96 | return false; |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | static struct sk_buff *etf_peek_timesortedlist(struct Qdisc *sch) |
| 102 | { |
| 103 | struct etf_sched_data *q = qdisc_priv(sch); |
| 104 | struct rb_node *p; |
| 105 | |
| 106 | p = rb_first(&q->head); |
| 107 | if (!p) |
| 108 | return NULL; |
| 109 | |
| 110 | return rb_to_skb(p); |
| 111 | } |
| 112 | |
| 113 | static void reset_watchdog(struct Qdisc *sch) |
| 114 | { |
| 115 | struct etf_sched_data *q = qdisc_priv(sch); |
| 116 | struct sk_buff *skb = etf_peek_timesortedlist(sch); |
| 117 | ktime_t next; |
| 118 | |
| 119 | if (!skb) |
| 120 | return; |
| 121 | |
| 122 | next = ktime_sub_ns(skb->tstamp, q->delta); |
| 123 | qdisc_watchdog_schedule_ns(&q->watchdog, ktime_to_ns(next)); |
| 124 | } |
| 125 | |
| 126 | static int etf_enqueue_timesortedlist(struct sk_buff *nskb, struct Qdisc *sch, |
| 127 | struct sk_buff **to_free) |
| 128 | { |
| 129 | struct etf_sched_data *q = qdisc_priv(sch); |
| 130 | struct rb_node **p = &q->head.rb_node, *parent = NULL; |
| 131 | ktime_t txtime = nskb->tstamp; |
| 132 | |
| 133 | if (!is_packet_valid(sch, nskb)) |
| 134 | return qdisc_drop(nskb, sch, to_free); |
| 135 | |
| 136 | while (*p) { |
| 137 | struct sk_buff *skb; |
| 138 | |
| 139 | parent = *p; |
| 140 | skb = rb_to_skb(parent); |
| 141 | if (ktime_after(txtime, skb->tstamp)) |
| 142 | p = &parent->rb_right; |
| 143 | else |
| 144 | p = &parent->rb_left; |
| 145 | } |
| 146 | rb_link_node(&nskb->rbnode, parent, p); |
| 147 | rb_insert_color(&nskb->rbnode, &q->head); |
| 148 | |
| 149 | qdisc_qstats_backlog_inc(sch, nskb); |
| 150 | sch->q.qlen++; |
| 151 | |
| 152 | /* Now we may need to re-arm the qdisc watchdog for the next packet. */ |
| 153 | reset_watchdog(sch); |
| 154 | |
| 155 | return NET_XMIT_SUCCESS; |
| 156 | } |
| 157 | |
| 158 | static void timesortedlist_erase(struct Qdisc *sch, struct sk_buff *skb, |
| 159 | bool drop) |
| 160 | { |
| 161 | struct etf_sched_data *q = qdisc_priv(sch); |
| 162 | |
| 163 | rb_erase(&skb->rbnode, &q->head); |
| 164 | |
| 165 | /* The rbnode field in the skb re-uses these fields, now that |
| 166 | * we are done with the rbnode, reset them. |
| 167 | */ |
| 168 | skb->next = NULL; |
| 169 | skb->prev = NULL; |
| 170 | skb->dev = qdisc_dev(sch); |
| 171 | |
| 172 | qdisc_qstats_backlog_dec(sch, skb); |
| 173 | |
| 174 | if (drop) { |
| 175 | struct sk_buff *to_free = NULL; |
| 176 | |
| 177 | qdisc_drop(skb, sch, &to_free); |
| 178 | kfree_skb_list(to_free); |
| 179 | qdisc_qstats_overlimit(sch); |
| 180 | } else { |
| 181 | qdisc_bstats_update(sch, skb); |
| 182 | |
| 183 | q->last = skb->tstamp; |
| 184 | } |
| 185 | |
| 186 | sch->q.qlen--; |
| 187 | } |
| 188 | |
| 189 | static struct sk_buff *etf_dequeue_timesortedlist(struct Qdisc *sch) |
| 190 | { |
| 191 | struct etf_sched_data *q = qdisc_priv(sch); |
| 192 | struct sk_buff *skb; |
| 193 | ktime_t now, next; |
| 194 | |
| 195 | skb = etf_peek_timesortedlist(sch); |
| 196 | if (!skb) |
| 197 | return NULL; |
| 198 | |
| 199 | now = q->get_time(); |
| 200 | |
| 201 | /* Drop if packet has expired while in queue. */ |
| 202 | /* FIXME: Must return error on the socket's error queue */ |
| 203 | if (ktime_before(skb->tstamp, now)) { |
| 204 | timesortedlist_erase(sch, skb, true); |
| 205 | skb = NULL; |
| 206 | goto out; |
| 207 | } |
| 208 | |
| 209 | /* When in deadline mode, dequeue as soon as possible and change the |
| 210 | * txtime from deadline to (now + delta). |
| 211 | */ |
| 212 | if (q->deadline_mode) { |
| 213 | timesortedlist_erase(sch, skb, false); |
| 214 | skb->tstamp = now; |
| 215 | goto out; |
| 216 | } |
| 217 | |
| 218 | next = ktime_sub_ns(skb->tstamp, q->delta); |
| 219 | |
| 220 | /* Dequeue only if now is within the [txtime - delta, txtime] range. */ |
| 221 | if (ktime_after(now, next)) |
| 222 | timesortedlist_erase(sch, skb, false); |
| 223 | else |
| 224 | skb = NULL; |
| 225 | |
| 226 | out: |
| 227 | /* Now we may need to re-arm the qdisc watchdog for the next packet. */ |
| 228 | reset_watchdog(sch); |
| 229 | |
| 230 | return skb; |
| 231 | } |
| 232 | |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 233 | static void etf_disable_offload(struct net_device *dev, |
| 234 | struct etf_sched_data *q) |
| 235 | { |
| 236 | struct tc_etf_qopt_offload etf = { }; |
| 237 | const struct net_device_ops *ops; |
| 238 | int err; |
| 239 | |
| 240 | if (!q->offload) |
| 241 | return; |
| 242 | |
| 243 | ops = dev->netdev_ops; |
| 244 | if (!ops->ndo_setup_tc) |
| 245 | return; |
| 246 | |
| 247 | etf.queue = q->queue; |
| 248 | etf.enable = 0; |
| 249 | |
| 250 | err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_ETF, &etf); |
| 251 | if (err < 0) |
| 252 | pr_warn("Couldn't disable ETF offload for queue %d\n", |
| 253 | etf.queue); |
| 254 | } |
| 255 | |
| 256 | static int etf_enable_offload(struct net_device *dev, struct etf_sched_data *q, |
| 257 | struct netlink_ext_ack *extack) |
| 258 | { |
| 259 | const struct net_device_ops *ops = dev->netdev_ops; |
| 260 | struct tc_etf_qopt_offload etf = { }; |
| 261 | int err; |
| 262 | |
| 263 | if (q->offload) |
| 264 | return 0; |
| 265 | |
| 266 | if (!ops->ndo_setup_tc) { |
| 267 | NL_SET_ERR_MSG(extack, "Specified device does not support ETF offload"); |
| 268 | return -EOPNOTSUPP; |
| 269 | } |
| 270 | |
| 271 | etf.queue = q->queue; |
| 272 | etf.enable = 1; |
| 273 | |
| 274 | err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_ETF, &etf); |
| 275 | if (err < 0) { |
| 276 | NL_SET_ERR_MSG(extack, "Specified device failed to setup ETF hardware offload"); |
| 277 | return err; |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 283 | static int etf_init(struct Qdisc *sch, struct nlattr *opt, |
| 284 | struct netlink_ext_ack *extack) |
| 285 | { |
| 286 | struct etf_sched_data *q = qdisc_priv(sch); |
| 287 | struct net_device *dev = qdisc_dev(sch); |
| 288 | struct nlattr *tb[TCA_ETF_MAX + 1]; |
| 289 | struct tc_etf_qopt *qopt; |
| 290 | int err; |
| 291 | |
| 292 | if (!opt) { |
| 293 | NL_SET_ERR_MSG(extack, |
| 294 | "Missing ETF qdisc options which are mandatory"); |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
| 298 | err = nla_parse_nested(tb, TCA_ETF_MAX, opt, etf_policy, extack); |
| 299 | if (err < 0) |
| 300 | return err; |
| 301 | |
| 302 | if (!tb[TCA_ETF_PARMS]) { |
| 303 | NL_SET_ERR_MSG(extack, "Missing mandatory ETF parameters"); |
| 304 | return -EINVAL; |
| 305 | } |
| 306 | |
| 307 | qopt = nla_data(tb[TCA_ETF_PARMS]); |
| 308 | |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 309 | pr_debug("delta %d clockid %d offload %s deadline %s\n", |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 310 | qopt->delta, qopt->clockid, |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 311 | OFFLOAD_IS_ON(qopt) ? "on" : "off", |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 312 | DEADLINE_MODE_IS_ON(qopt) ? "on" : "off"); |
| 313 | |
| 314 | err = validate_input_params(qopt, extack); |
| 315 | if (err < 0) |
| 316 | return err; |
| 317 | |
| 318 | q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0); |
| 319 | |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 320 | if (OFFLOAD_IS_ON(qopt)) { |
| 321 | err = etf_enable_offload(dev, q, extack); |
| 322 | if (err < 0) |
| 323 | return err; |
| 324 | } |
| 325 | |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 326 | /* Everything went OK, save the parameters used. */ |
| 327 | q->delta = qopt->delta; |
| 328 | q->clockid = qopt->clockid; |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 329 | q->offload = OFFLOAD_IS_ON(qopt); |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 330 | q->deadline_mode = DEADLINE_MODE_IS_ON(qopt); |
| 331 | |
| 332 | switch (q->clockid) { |
| 333 | case CLOCK_REALTIME: |
| 334 | q->get_time = ktime_get_real; |
| 335 | break; |
| 336 | case CLOCK_MONOTONIC: |
| 337 | q->get_time = ktime_get; |
| 338 | break; |
| 339 | case CLOCK_BOOTTIME: |
| 340 | q->get_time = ktime_get_boottime; |
| 341 | break; |
| 342 | case CLOCK_TAI: |
| 343 | q->get_time = ktime_get_clocktai; |
| 344 | break; |
| 345 | default: |
| 346 | NL_SET_ERR_MSG(extack, "Clockid is not supported"); |
| 347 | return -ENOTSUPP; |
| 348 | } |
| 349 | |
| 350 | qdisc_watchdog_init_clockid(&q->watchdog, sch, q->clockid); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static void timesortedlist_clear(struct Qdisc *sch) |
| 356 | { |
| 357 | struct etf_sched_data *q = qdisc_priv(sch); |
| 358 | struct rb_node *p = rb_first(&q->head); |
| 359 | |
| 360 | while (p) { |
| 361 | struct sk_buff *skb = rb_to_skb(p); |
| 362 | |
| 363 | p = rb_next(p); |
| 364 | |
| 365 | rb_erase(&skb->rbnode, &q->head); |
| 366 | rtnl_kfree_skbs(skb, skb); |
| 367 | sch->q.qlen--; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | static void etf_reset(struct Qdisc *sch) |
| 372 | { |
| 373 | struct etf_sched_data *q = qdisc_priv(sch); |
| 374 | |
| 375 | /* Only cancel watchdog if it's been initialized. */ |
| 376 | if (q->watchdog.qdisc == sch) |
| 377 | qdisc_watchdog_cancel(&q->watchdog); |
| 378 | |
| 379 | /* No matter which mode we are on, it's safe to clear both lists. */ |
| 380 | timesortedlist_clear(sch); |
| 381 | __qdisc_reset_queue(&sch->q); |
| 382 | |
| 383 | sch->qstats.backlog = 0; |
| 384 | sch->q.qlen = 0; |
| 385 | |
| 386 | q->last = 0; |
| 387 | } |
| 388 | |
| 389 | static void etf_destroy(struct Qdisc *sch) |
| 390 | { |
| 391 | struct etf_sched_data *q = qdisc_priv(sch); |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 392 | struct net_device *dev = qdisc_dev(sch); |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 393 | |
| 394 | /* Only cancel watchdog if it's been initialized. */ |
| 395 | if (q->watchdog.qdisc == sch) |
| 396 | qdisc_watchdog_cancel(&q->watchdog); |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 397 | |
| 398 | etf_disable_offload(dev, q); |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static int etf_dump(struct Qdisc *sch, struct sk_buff *skb) |
| 402 | { |
| 403 | struct etf_sched_data *q = qdisc_priv(sch); |
| 404 | struct tc_etf_qopt opt = { }; |
| 405 | struct nlattr *nest; |
| 406 | |
| 407 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 408 | if (!nest) |
| 409 | goto nla_put_failure; |
| 410 | |
| 411 | opt.delta = q->delta; |
| 412 | opt.clockid = q->clockid; |
Jesus Sanchez-Palencia | 88cab77 | 2018-07-03 15:42:54 -0700 | [diff] [blame^] | 413 | if (q->offload) |
| 414 | opt.flags |= TC_ETF_OFFLOAD_ON; |
| 415 | |
Vinicius Costa Gomes | 25db26a | 2018-07-03 15:42:53 -0700 | [diff] [blame] | 416 | if (q->deadline_mode) |
| 417 | opt.flags |= TC_ETF_DEADLINE_MODE_ON; |
| 418 | |
| 419 | if (nla_put(skb, TCA_ETF_PARMS, sizeof(opt), &opt)) |
| 420 | goto nla_put_failure; |
| 421 | |
| 422 | return nla_nest_end(skb, nest); |
| 423 | |
| 424 | nla_put_failure: |
| 425 | nla_nest_cancel(skb, nest); |
| 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | static struct Qdisc_ops etf_qdisc_ops __read_mostly = { |
| 430 | .id = "etf", |
| 431 | .priv_size = sizeof(struct etf_sched_data), |
| 432 | .enqueue = etf_enqueue_timesortedlist, |
| 433 | .dequeue = etf_dequeue_timesortedlist, |
| 434 | .peek = etf_peek_timesortedlist, |
| 435 | .init = etf_init, |
| 436 | .reset = etf_reset, |
| 437 | .destroy = etf_destroy, |
| 438 | .dump = etf_dump, |
| 439 | .owner = THIS_MODULE, |
| 440 | }; |
| 441 | |
| 442 | static int __init etf_module_init(void) |
| 443 | { |
| 444 | return register_qdisc(&etf_qdisc_ops); |
| 445 | } |
| 446 | |
| 447 | static void __exit etf_module_exit(void) |
| 448 | { |
| 449 | unregister_qdisc(&etf_qdisc_ops); |
| 450 | } |
| 451 | module_init(etf_module_init) |
| 452 | module_exit(etf_module_exit) |
| 453 | MODULE_LICENSE("GPL"); |