sch_cbs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 *(*dequeue)(struct Qdisc *sch);
  79. };
  80. static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch)
  81. {
  82. return qdisc_enqueue_tail(skb, sch);
  83. }
  84. static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch)
  85. {
  86. struct cbs_sched_data *q = qdisc_priv(sch);
  87. if (sch->q.qlen == 0 && q->credits > 0) {
  88. /* We need to stop accumulating credits when there's
  89. * no enqueued packets and q->credits is positive.
  90. */
  91. q->credits = 0;
  92. q->last = ktime_get_ns();
  93. }
  94. return qdisc_enqueue_tail(skb, sch);
  95. }
  96. static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  97. struct sk_buff **to_free)
  98. {
  99. struct cbs_sched_data *q = qdisc_priv(sch);
  100. return q->enqueue(skb, sch);
  101. }
  102. /* timediff is in ns, slope is in bytes/s */
  103. static s64 timediff_to_credits(s64 timediff, s64 slope)
  104. {
  105. return div64_s64(timediff * slope, NSEC_PER_SEC);
  106. }
  107. static s64 delay_from_credits(s64 credits, s64 slope)
  108. {
  109. if (unlikely(slope == 0))
  110. return S64_MAX;
  111. return div64_s64(-credits * NSEC_PER_SEC, slope);
  112. }
  113. static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
  114. {
  115. if (unlikely(port_rate == 0))
  116. return S64_MAX;
  117. return div64_s64(len * slope, port_rate);
  118. }
  119. static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
  120. {
  121. struct cbs_sched_data *q = qdisc_priv(sch);
  122. s64 now = ktime_get_ns();
  123. struct sk_buff *skb;
  124. s64 credits;
  125. int len;
  126. if (q->credits < 0) {
  127. credits = timediff_to_credits(now - q->last, q->idleslope);
  128. credits = q->credits + credits;
  129. q->credits = min_t(s64, credits, q->hicredit);
  130. if (q->credits < 0) {
  131. s64 delay;
  132. delay = delay_from_credits(q->credits, q->idleslope);
  133. qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
  134. q->last = now;
  135. return NULL;
  136. }
  137. }
  138. skb = qdisc_dequeue_head(sch);
  139. if (!skb)
  140. return NULL;
  141. len = qdisc_pkt_len(skb);
  142. /* As sendslope is a negative number, this will decrease the
  143. * amount of q->credits.
  144. */
  145. credits = credits_from_len(len, q->sendslope, q->port_rate);
  146. credits += q->credits;
  147. q->credits = max_t(s64, credits, q->locredit);
  148. q->last = now;
  149. return skb;
  150. }
  151. static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
  152. {
  153. return qdisc_dequeue_head(sch);
  154. }
  155. static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
  156. {
  157. struct cbs_sched_data *q = qdisc_priv(sch);
  158. return q->dequeue(sch);
  159. }
  160. static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
  161. [TCA_CBS_PARMS] = { .len = sizeof(struct tc_cbs_qopt) },
  162. };
  163. static void cbs_disable_offload(struct net_device *dev,
  164. struct cbs_sched_data *q)
  165. {
  166. struct tc_cbs_qopt_offload cbs = { };
  167. const struct net_device_ops *ops;
  168. int err;
  169. if (!q->offload)
  170. return;
  171. q->enqueue = cbs_enqueue_soft;
  172. q->dequeue = cbs_dequeue_soft;
  173. ops = dev->netdev_ops;
  174. if (!ops->ndo_setup_tc)
  175. return;
  176. cbs.queue = q->queue;
  177. cbs.enable = 0;
  178. err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
  179. if (err < 0)
  180. pr_warn("Couldn't disable CBS offload for queue %d\n",
  181. cbs.queue);
  182. }
  183. static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
  184. const struct tc_cbs_qopt *opt,
  185. struct netlink_ext_ack *extack)
  186. {
  187. const struct net_device_ops *ops = dev->netdev_ops;
  188. struct tc_cbs_qopt_offload cbs = { };
  189. int err;
  190. if (!ops->ndo_setup_tc) {
  191. NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
  192. return -EOPNOTSUPP;
  193. }
  194. cbs.queue = q->queue;
  195. cbs.enable = 1;
  196. cbs.hicredit = opt->hicredit;
  197. cbs.locredit = opt->locredit;
  198. cbs.idleslope = opt->idleslope;
  199. cbs.sendslope = opt->sendslope;
  200. err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
  201. if (err < 0) {
  202. NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
  203. return err;
  204. }
  205. q->enqueue = cbs_enqueue_offload;
  206. q->dequeue = cbs_dequeue_offload;
  207. return 0;
  208. }
  209. static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
  210. struct netlink_ext_ack *extack)
  211. {
  212. struct cbs_sched_data *q = qdisc_priv(sch);
  213. struct net_device *dev = qdisc_dev(sch);
  214. struct nlattr *tb[TCA_CBS_MAX + 1];
  215. struct tc_cbs_qopt *qopt;
  216. int err;
  217. err = nla_parse_nested(tb, TCA_CBS_MAX, opt, cbs_policy, extack);
  218. if (err < 0)
  219. return err;
  220. if (!tb[TCA_CBS_PARMS]) {
  221. NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
  222. return -EINVAL;
  223. }
  224. qopt = nla_data(tb[TCA_CBS_PARMS]);
  225. if (!qopt->offload) {
  226. struct ethtool_link_ksettings ecmd;
  227. s64 link_speed;
  228. if (!__ethtool_get_link_ksettings(dev, &ecmd))
  229. link_speed = ecmd.base.speed;
  230. else
  231. link_speed = SPEED_1000;
  232. q->port_rate = link_speed * 1000 * BYTES_PER_KBIT;
  233. cbs_disable_offload(dev, q);
  234. } else {
  235. err = cbs_enable_offload(dev, q, qopt, extack);
  236. if (err < 0)
  237. return err;
  238. }
  239. /* Everything went OK, save the parameters used. */
  240. q->hicredit = qopt->hicredit;
  241. q->locredit = qopt->locredit;
  242. q->idleslope = qopt->idleslope * BYTES_PER_KBIT;
  243. q->sendslope = qopt->sendslope * BYTES_PER_KBIT;
  244. q->offload = qopt->offload;
  245. return 0;
  246. }
  247. static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
  248. struct netlink_ext_ack *extack)
  249. {
  250. struct cbs_sched_data *q = qdisc_priv(sch);
  251. struct net_device *dev = qdisc_dev(sch);
  252. if (!opt) {
  253. NL_SET_ERR_MSG(extack, "Missing CBS qdisc options which are mandatory");
  254. return -EINVAL;
  255. }
  256. q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
  257. q->enqueue = cbs_enqueue_soft;
  258. q->dequeue = cbs_dequeue_soft;
  259. qdisc_watchdog_init(&q->watchdog, sch);
  260. return cbs_change(sch, opt, extack);
  261. }
  262. static void cbs_destroy(struct Qdisc *sch)
  263. {
  264. struct cbs_sched_data *q = qdisc_priv(sch);
  265. struct net_device *dev = qdisc_dev(sch);
  266. qdisc_watchdog_cancel(&q->watchdog);
  267. cbs_disable_offload(dev, q);
  268. }
  269. static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
  270. {
  271. struct cbs_sched_data *q = qdisc_priv(sch);
  272. struct tc_cbs_qopt opt = { };
  273. struct nlattr *nest;
  274. nest = nla_nest_start(skb, TCA_OPTIONS);
  275. if (!nest)
  276. goto nla_put_failure;
  277. opt.hicredit = q->hicredit;
  278. opt.locredit = q->locredit;
  279. opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT);
  280. opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT);
  281. opt.offload = q->offload;
  282. if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
  283. goto nla_put_failure;
  284. return nla_nest_end(skb, nest);
  285. nla_put_failure:
  286. nla_nest_cancel(skb, nest);
  287. return -1;
  288. }
  289. static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
  290. .id = "cbs",
  291. .priv_size = sizeof(struct cbs_sched_data),
  292. .enqueue = cbs_enqueue,
  293. .dequeue = cbs_dequeue,
  294. .peek = qdisc_peek_dequeued,
  295. .init = cbs_init,
  296. .reset = qdisc_reset_queue,
  297. .destroy = cbs_destroy,
  298. .change = cbs_change,
  299. .dump = cbs_dump,
  300. .owner = THIS_MODULE,
  301. };
  302. static int __init cbs_module_init(void)
  303. {
  304. return register_qdisc(&cbs_qdisc_ops);
  305. }
  306. static void __exit cbs_module_exit(void)
  307. {
  308. unregister_qdisc(&cbs_qdisc_ops);
  309. }
  310. module_init(cbs_module_init)
  311. module_exit(cbs_module_exit)
  312. MODULE_LICENSE("GPL");