sch_etf.c 11 KB

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