sch_cbs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * net/sched/sch_cbs.c Credit Based Shaper
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Vinicius Costa Gomes <vinicius.gomes@intel.com>
  10. *
  11. */
  12. /* Credit Based Shaper (CBS)
  13. * =========================
  14. *
  15. * This is a simple rate-limiting shaper aimed at TSN applications on
  16. * systems with known traffic workloads.
  17. *
  18. * Its algorithm is defined by the IEEE 802.1Q-2014 Specification,
  19. * Section 8.6.8.2, and explained in more detail in the Annex L of the
  20. * same specification.
  21. *
  22. * There are four tunables to be considered:
  23. *
  24. * 'idleslope': Idleslope is the rate of credits that is
  25. * accumulated (in kilobits per second) when there is at least
  26. * one packet waiting for transmission. Packets are transmitted
  27. * when the current value of credits is equal or greater than
  28. * zero. When there is no packet to be transmitted the amount of
  29. * credits is set to zero. This is the main tunable of the CBS
  30. * algorithm.
  31. *
  32. * 'sendslope':
  33. * Sendslope is the rate of credits that is depleted (it should be a
  34. * negative number of kilobits per second) when a transmission is
  35. * ocurring. It can be calculated as follows, (IEEE 802.1Q-2014 Section
  36. * 8.6.8.2 item g):
  37. *
  38. * sendslope = idleslope - port_transmit_rate
  39. *
  40. * 'hicredit': Hicredit defines the maximum amount of credits (in
  41. * bytes) that can be accumulated. Hicredit depends on the
  42. * characteristics of interfering traffic,
  43. * 'max_interference_size' is the maximum size of any burst of
  44. * traffic that can delay the transmission of a frame that is
  45. * available for transmission for this traffic class, (IEEE
  46. * 802.1Q-2014 Annex L, Equation L-3):
  47. *
  48. * hicredit = max_interference_size * (idleslope / port_transmit_rate)
  49. *
  50. * 'locredit': Locredit is the minimum amount of credits that can
  51. * be reached. It is a function of the traffic flowing through
  52. * this qdisc (IEEE 802.1Q-2014 Annex L, Equation L-2):
  53. *
  54. * locredit = max_frame_size * (sendslope / port_transmit_rate)
  55. */
  56. #include <linux/module.h>
  57. #include <linux/types.h>
  58. #include <linux/kernel.h>
  59. #include <linux/string.h>
  60. #include <linux/errno.h>
  61. #include <linux/skbuff.h>
  62. #include <net/netlink.h>
  63. #include <net/sch_generic.h>
  64. #include <net/pkt_sched.h>
  65. #define BYTES_PER_KBIT (1000LL / 8)
  66. struct cbs_sched_data {
  67. bool offload;
  68. int queue;
  69. s64 port_rate; /* in bytes/s */
  70. s64 last; /* timestamp in ns */
  71. s64 credits; /* in bytes */
  72. s32 locredit; /* in bytes */
  73. s32 hicredit; /* in bytes */
  74. s64 sendslope; /* in bytes/s */
  75. s64 idleslope; /* in bytes/s */
  76. struct qdisc_watchdog watchdog;
  77. int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch,
  78. struct sk_buff **to_free);
  79. struct sk_buff *(*dequeue)(struct Qdisc *sch);
  80. struct Qdisc *qdisc;
  81. };
  82. static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  83. struct Qdisc *child,
  84. struct sk_buff **to_free)
  85. {
  86. int err;
  87. err = child->ops->enqueue(skb, child, to_free);
  88. if (err != NET_XMIT_SUCCESS)
  89. return err;
  90. qdisc_qstats_backlog_inc(sch, skb);
  91. sch->q.qlen++;
  92. return NET_XMIT_SUCCESS;
  93. }
  94. static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch,
  95. struct sk_buff **to_free)
  96. {
  97. struct cbs_sched_data *q = qdisc_priv(sch);
  98. struct Qdisc *qdisc = q->qdisc;
  99. return cbs_child_enqueue(skb, sch, qdisc, to_free);
  100. }
  101. static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch,
  102. struct sk_buff **to_free)
  103. {
  104. struct cbs_sched_data *q = qdisc_priv(sch);
  105. struct Qdisc *qdisc = q->qdisc;
  106. if (sch->q.qlen == 0 && q->credits > 0) {
  107. /* We need to stop accumulating credits when there's
  108. * no enqueued packets and q->credits is positive.
  109. */
  110. q->credits = 0;
  111. q->last = ktime_get_ns();
  112. }
  113. return cbs_child_enqueue(skb, sch, qdisc, to_free);
  114. }
  115. static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  116. struct sk_buff **to_free)
  117. {
  118. struct cbs_sched_data *q = qdisc_priv(sch);
  119. return q->enqueue(skb, sch, to_free);
  120. }
  121. /* timediff is in ns, slope is in bytes/s */
  122. static s64 timediff_to_credits(s64 timediff, s64 slope)
  123. {
  124. return div64_s64(timediff * slope, NSEC_PER_SEC);
  125. }
  126. static s64 delay_from_credits(s64 credits, s64 slope)
  127. {
  128. if (unlikely(slope == 0))
  129. return S64_MAX;
  130. return div64_s64(-credits * NSEC_PER_SEC, slope);
  131. }
  132. static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
  133. {
  134. if (unlikely(port_rate == 0))
  135. return S64_MAX;
  136. return div64_s64(len * slope, port_rate);
  137. }
  138. static struct sk_buff *cbs_child_dequeue(struct Qdisc *sch, struct Qdisc *child)
  139. {
  140. struct sk_buff *skb;
  141. skb = child->ops->dequeue(child);
  142. if (!skb)
  143. return NULL;
  144. qdisc_qstats_backlog_dec(sch, skb);
  145. qdisc_bstats_update(sch, skb);
  146. sch->q.qlen--;
  147. return skb;
  148. }
  149. static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
  150. {
  151. struct cbs_sched_data *q = qdisc_priv(sch);
  152. struct Qdisc *qdisc = q->qdisc;
  153. s64 now = ktime_get_ns();
  154. struct sk_buff *skb;
  155. s64 credits;
  156. int len;
  157. if (q->credits < 0) {
  158. credits = timediff_to_credits(now - q->last, q->idleslope);
  159. credits = q->credits + credits;
  160. q->credits = min_t(s64, credits, q->hicredit);
  161. if (q->credits < 0) {
  162. s64 delay;
  163. delay = delay_from_credits(q->credits, q->idleslope);
  164. qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
  165. q->last = now;
  166. return NULL;
  167. }
  168. }
  169. skb = cbs_child_dequeue(sch, qdisc);
  170. if (!skb)
  171. return NULL;
  172. len = qdisc_pkt_len(skb);
  173. /* As sendslope is a negative number, this will decrease the
  174. * amount of q->credits.
  175. */
  176. credits = credits_from_len(len, q->sendslope, q->port_rate);
  177. credits += q->credits;
  178. q->credits = max_t(s64, credits, q->locredit);
  179. q->last = now;
  180. return skb;
  181. }
  182. static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
  183. {
  184. struct cbs_sched_data *q = qdisc_priv(sch);
  185. struct Qdisc *qdisc = q->qdisc;
  186. return cbs_child_dequeue(sch, qdisc);
  187. }
  188. static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
  189. {
  190. struct cbs_sched_data *q = qdisc_priv(sch);
  191. return q->dequeue(sch);
  192. }
  193. static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
  194. [TCA_CBS_PARMS] = { .len = sizeof(struct tc_cbs_qopt) },
  195. };
  196. static void cbs_disable_offload(struct net_device *dev,
  197. struct cbs_sched_data *q)
  198. {
  199. struct tc_cbs_qopt_offload cbs = { };
  200. const struct net_device_ops *ops;
  201. int err;
  202. if (!q->offload)
  203. return;
  204. q->enqueue = cbs_enqueue_soft;
  205. q->dequeue = cbs_dequeue_soft;
  206. ops = dev->netdev_ops;
  207. if (!ops->ndo_setup_tc)
  208. return;
  209. cbs.queue = q->queue;
  210. cbs.enable = 0;
  211. err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
  212. if (err < 0)
  213. pr_warn("Couldn't disable CBS offload for queue %d\n",
  214. cbs.queue);
  215. }
  216. static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
  217. const struct tc_cbs_qopt *opt,
  218. struct netlink_ext_ack *extack)
  219. {
  220. const struct net_device_ops *ops = dev->netdev_ops;
  221. struct tc_cbs_qopt_offload cbs = { };
  222. int err;
  223. if (!ops->ndo_setup_tc) {
  224. NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
  225. return -EOPNOTSUPP;
  226. }
  227. cbs.queue = q->queue;
  228. cbs.enable = 1;
  229. cbs.hicredit = opt->hicredit;
  230. cbs.locredit = opt->locredit;
  231. cbs.idleslope = opt->idleslope;
  232. cbs.sendslope = opt->sendslope;
  233. err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
  234. if (err < 0) {
  235. NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
  236. return err;
  237. }
  238. q->enqueue = cbs_enqueue_offload;
  239. q->dequeue = cbs_dequeue_offload;
  240. return 0;
  241. }
  242. static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
  243. struct netlink_ext_ack *extack)
  244. {
  245. struct cbs_sched_data *q = qdisc_priv(sch);
  246. struct net_device *dev = qdisc_dev(sch);
  247. struct nlattr *tb[TCA_CBS_MAX + 1];
  248. struct tc_cbs_qopt *qopt;
  249. int err;
  250. err = nla_parse_nested(tb, TCA_CBS_MAX, opt, cbs_policy, extack);
  251. if (err < 0)
  252. return err;
  253. if (!tb[TCA_CBS_PARMS]) {
  254. NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
  255. return -EINVAL;
  256. }
  257. qopt = nla_data(tb[TCA_CBS_PARMS]);
  258. if (!qopt->offload) {
  259. struct ethtool_link_ksettings ecmd;
  260. s64 link_speed;
  261. if (!__ethtool_get_link_ksettings(dev, &ecmd))
  262. link_speed = ecmd.base.speed;
  263. else
  264. link_speed = SPEED_1000;
  265. q->port_rate = link_speed * 1000 * BYTES_PER_KBIT;
  266. cbs_disable_offload(dev, q);
  267. } else {
  268. err = cbs_enable_offload(dev, q, qopt, extack);
  269. if (err < 0)
  270. return err;
  271. }
  272. /* Everything went OK, save the parameters used. */
  273. q->hicredit = qopt->hicredit;
  274. q->locredit = qopt->locredit;
  275. q->idleslope = qopt->idleslope * BYTES_PER_KBIT;
  276. q->sendslope = qopt->sendslope * BYTES_PER_KBIT;
  277. q->offload = qopt->offload;
  278. return 0;
  279. }
  280. static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
  281. struct netlink_ext_ack *extack)
  282. {
  283. struct cbs_sched_data *q = qdisc_priv(sch);
  284. struct net_device *dev = qdisc_dev(sch);
  285. if (!opt) {
  286. NL_SET_ERR_MSG(extack, "Missing CBS qdisc options which are mandatory");
  287. return -EINVAL;
  288. }
  289. q->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  290. sch->handle, extack);
  291. if (!q->qdisc)
  292. return -ENOMEM;
  293. qdisc_hash_add(q->qdisc, false);
  294. q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
  295. q->enqueue = cbs_enqueue_soft;
  296. q->dequeue = cbs_dequeue_soft;
  297. qdisc_watchdog_init(&q->watchdog, sch);
  298. return cbs_change(sch, opt, extack);
  299. }
  300. static void cbs_destroy(struct Qdisc *sch)
  301. {
  302. struct cbs_sched_data *q = qdisc_priv(sch);
  303. struct net_device *dev = qdisc_dev(sch);
  304. qdisc_watchdog_cancel(&q->watchdog);
  305. cbs_disable_offload(dev, q);
  306. if (q->qdisc)
  307. qdisc_put(q->qdisc);
  308. }
  309. static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
  310. {
  311. struct cbs_sched_data *q = qdisc_priv(sch);
  312. struct tc_cbs_qopt opt = { };
  313. struct nlattr *nest;
  314. nest = nla_nest_start(skb, TCA_OPTIONS);
  315. if (!nest)
  316. goto nla_put_failure;
  317. opt.hicredit = q->hicredit;
  318. opt.locredit = q->locredit;
  319. opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT);
  320. opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT);
  321. opt.offload = q->offload;
  322. if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
  323. goto nla_put_failure;
  324. return nla_nest_end(skb, nest);
  325. nla_put_failure:
  326. nla_nest_cancel(skb, nest);
  327. return -1;
  328. }
  329. static int cbs_dump_class(struct Qdisc *sch, unsigned long cl,
  330. struct sk_buff *skb, struct tcmsg *tcm)
  331. {
  332. struct cbs_sched_data *q = qdisc_priv(sch);
  333. if (cl != 1 || !q->qdisc) /* only one class */
  334. return -ENOENT;
  335. tcm->tcm_handle |= TC_H_MIN(1);
  336. tcm->tcm_info = q->qdisc->handle;
  337. return 0;
  338. }
  339. static int cbs_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  340. struct Qdisc **old, struct netlink_ext_ack *extack)
  341. {
  342. struct cbs_sched_data *q = qdisc_priv(sch);
  343. if (!new) {
  344. new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  345. sch->handle, NULL);
  346. if (!new)
  347. new = &noop_qdisc;
  348. }
  349. *old = qdisc_replace(sch, new, &q->qdisc);
  350. return 0;
  351. }
  352. static struct Qdisc *cbs_leaf(struct Qdisc *sch, unsigned long arg)
  353. {
  354. struct cbs_sched_data *q = qdisc_priv(sch);
  355. return q->qdisc;
  356. }
  357. static unsigned long cbs_find(struct Qdisc *sch, u32 classid)
  358. {
  359. return 1;
  360. }
  361. static void cbs_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  362. {
  363. if (!walker->stop) {
  364. if (walker->count >= walker->skip) {
  365. if (walker->fn(sch, 1, walker) < 0) {
  366. walker->stop = 1;
  367. return;
  368. }
  369. }
  370. walker->count++;
  371. }
  372. }
  373. static const struct Qdisc_class_ops cbs_class_ops = {
  374. .graft = cbs_graft,
  375. .leaf = cbs_leaf,
  376. .find = cbs_find,
  377. .walk = cbs_walk,
  378. .dump = cbs_dump_class,
  379. };
  380. static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
  381. .id = "cbs",
  382. .cl_ops = &cbs_class_ops,
  383. .priv_size = sizeof(struct cbs_sched_data),
  384. .enqueue = cbs_enqueue,
  385. .dequeue = cbs_dequeue,
  386. .peek = qdisc_peek_dequeued,
  387. .init = cbs_init,
  388. .reset = qdisc_reset_queue,
  389. .destroy = cbs_destroy,
  390. .change = cbs_change,
  391. .dump = cbs_dump,
  392. .owner = THIS_MODULE,
  393. };
  394. static int __init cbs_module_init(void)
  395. {
  396. return register_qdisc(&cbs_qdisc_ops);
  397. }
  398. static void __exit cbs_module_exit(void)
  399. {
  400. unregister_qdisc(&cbs_qdisc_ops);
  401. }
  402. module_init(cbs_module_init)
  403. module_exit(cbs_module_exit)
  404. MODULE_LICENSE("GPL");