sch_cbs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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/netevent.h>
  63. #include <net/netlink.h>
  64. #include <net/sch_generic.h>
  65. #include <net/pkt_sched.h>
  66. static LIST_HEAD(cbs_list);
  67. static DEFINE_SPINLOCK(cbs_list_lock);
  68. #define BYTES_PER_KBIT (1000LL / 8)
  69. struct cbs_sched_data {
  70. bool offload;
  71. int queue;
  72. atomic64_t port_rate; /* in bytes/s */
  73. s64 last; /* timestamp in ns */
  74. s64 credits; /* in bytes */
  75. s32 locredit; /* in bytes */
  76. s32 hicredit; /* in bytes */
  77. s64 sendslope; /* in bytes/s */
  78. s64 idleslope; /* in bytes/s */
  79. struct qdisc_watchdog watchdog;
  80. int (*enqueue)(struct sk_buff *skb, struct Qdisc *sch,
  81. struct sk_buff **to_free);
  82. struct sk_buff *(*dequeue)(struct Qdisc *sch);
  83. struct Qdisc *qdisc;
  84. struct list_head cbs_list;
  85. };
  86. static int cbs_child_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  87. struct Qdisc *child,
  88. struct sk_buff **to_free)
  89. {
  90. int err;
  91. err = child->ops->enqueue(skb, child, to_free);
  92. if (err != NET_XMIT_SUCCESS)
  93. return err;
  94. qdisc_qstats_backlog_inc(sch, skb);
  95. sch->q.qlen++;
  96. return NET_XMIT_SUCCESS;
  97. }
  98. static int cbs_enqueue_offload(struct sk_buff *skb, struct Qdisc *sch,
  99. struct sk_buff **to_free)
  100. {
  101. struct cbs_sched_data *q = qdisc_priv(sch);
  102. struct Qdisc *qdisc = q->qdisc;
  103. return cbs_child_enqueue(skb, sch, qdisc, to_free);
  104. }
  105. static int cbs_enqueue_soft(struct sk_buff *skb, struct Qdisc *sch,
  106. struct sk_buff **to_free)
  107. {
  108. struct cbs_sched_data *q = qdisc_priv(sch);
  109. struct Qdisc *qdisc = q->qdisc;
  110. if (sch->q.qlen == 0 && q->credits > 0) {
  111. /* We need to stop accumulating credits when there's
  112. * no enqueued packets and q->credits is positive.
  113. */
  114. q->credits = 0;
  115. q->last = ktime_get_ns();
  116. }
  117. return cbs_child_enqueue(skb, sch, qdisc, to_free);
  118. }
  119. static int cbs_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  120. struct sk_buff **to_free)
  121. {
  122. struct cbs_sched_data *q = qdisc_priv(sch);
  123. return q->enqueue(skb, sch, to_free);
  124. }
  125. /* timediff is in ns, slope is in bytes/s */
  126. static s64 timediff_to_credits(s64 timediff, s64 slope)
  127. {
  128. return div64_s64(timediff * slope, NSEC_PER_SEC);
  129. }
  130. static s64 delay_from_credits(s64 credits, s64 slope)
  131. {
  132. if (unlikely(slope == 0))
  133. return S64_MAX;
  134. return div64_s64(-credits * NSEC_PER_SEC, slope);
  135. }
  136. static s64 credits_from_len(unsigned int len, s64 slope, s64 port_rate)
  137. {
  138. if (unlikely(port_rate == 0))
  139. return S64_MAX;
  140. return div64_s64(len * slope, port_rate);
  141. }
  142. static struct sk_buff *cbs_child_dequeue(struct Qdisc *sch, struct Qdisc *child)
  143. {
  144. struct sk_buff *skb;
  145. skb = child->ops->dequeue(child);
  146. if (!skb)
  147. return NULL;
  148. qdisc_qstats_backlog_dec(sch, skb);
  149. qdisc_bstats_update(sch, skb);
  150. sch->q.qlen--;
  151. return skb;
  152. }
  153. static struct sk_buff *cbs_dequeue_soft(struct Qdisc *sch)
  154. {
  155. struct cbs_sched_data *q = qdisc_priv(sch);
  156. struct Qdisc *qdisc = q->qdisc;
  157. s64 now = ktime_get_ns();
  158. struct sk_buff *skb;
  159. s64 credits;
  160. int len;
  161. /* The previous packet is still being sent */
  162. if (now < q->last) {
  163. qdisc_watchdog_schedule_ns(&q->watchdog, q->last);
  164. return NULL;
  165. }
  166. if (q->credits < 0) {
  167. credits = timediff_to_credits(now - q->last, q->idleslope);
  168. credits = q->credits + credits;
  169. q->credits = min_t(s64, credits, q->hicredit);
  170. if (q->credits < 0) {
  171. s64 delay;
  172. delay = delay_from_credits(q->credits, q->idleslope);
  173. qdisc_watchdog_schedule_ns(&q->watchdog, now + delay);
  174. q->last = now;
  175. return NULL;
  176. }
  177. }
  178. skb = cbs_child_dequeue(sch, qdisc);
  179. if (!skb)
  180. return NULL;
  181. len = qdisc_pkt_len(skb);
  182. /* As sendslope is a negative number, this will decrease the
  183. * amount of q->credits.
  184. */
  185. credits = credits_from_len(len, q->sendslope,
  186. atomic64_read(&q->port_rate));
  187. credits += q->credits;
  188. q->credits = max_t(s64, credits, q->locredit);
  189. /* Estimate of the transmission of the last byte of the packet in ns */
  190. if (unlikely(atomic64_read(&q->port_rate) == 0))
  191. q->last = now;
  192. else
  193. q->last = now + div64_s64(len * NSEC_PER_SEC,
  194. atomic64_read(&q->port_rate));
  195. return skb;
  196. }
  197. static struct sk_buff *cbs_dequeue_offload(struct Qdisc *sch)
  198. {
  199. struct cbs_sched_data *q = qdisc_priv(sch);
  200. struct Qdisc *qdisc = q->qdisc;
  201. return cbs_child_dequeue(sch, qdisc);
  202. }
  203. static struct sk_buff *cbs_dequeue(struct Qdisc *sch)
  204. {
  205. struct cbs_sched_data *q = qdisc_priv(sch);
  206. return q->dequeue(sch);
  207. }
  208. static const struct nla_policy cbs_policy[TCA_CBS_MAX + 1] = {
  209. [TCA_CBS_PARMS] = { .len = sizeof(struct tc_cbs_qopt) },
  210. };
  211. static void cbs_disable_offload(struct net_device *dev,
  212. struct cbs_sched_data *q)
  213. {
  214. struct tc_cbs_qopt_offload cbs = { };
  215. const struct net_device_ops *ops;
  216. int err;
  217. if (!q->offload)
  218. return;
  219. q->enqueue = cbs_enqueue_soft;
  220. q->dequeue = cbs_dequeue_soft;
  221. ops = dev->netdev_ops;
  222. if (!ops->ndo_setup_tc)
  223. return;
  224. cbs.queue = q->queue;
  225. cbs.enable = 0;
  226. err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
  227. if (err < 0)
  228. pr_warn("Couldn't disable CBS offload for queue %d\n",
  229. cbs.queue);
  230. }
  231. static int cbs_enable_offload(struct net_device *dev, struct cbs_sched_data *q,
  232. const struct tc_cbs_qopt *opt,
  233. struct netlink_ext_ack *extack)
  234. {
  235. const struct net_device_ops *ops = dev->netdev_ops;
  236. struct tc_cbs_qopt_offload cbs = { };
  237. int err;
  238. if (!ops->ndo_setup_tc) {
  239. NL_SET_ERR_MSG(extack, "Specified device does not support cbs offload");
  240. return -EOPNOTSUPP;
  241. }
  242. cbs.queue = q->queue;
  243. cbs.enable = 1;
  244. cbs.hicredit = opt->hicredit;
  245. cbs.locredit = opt->locredit;
  246. cbs.idleslope = opt->idleslope;
  247. cbs.sendslope = opt->sendslope;
  248. err = ops->ndo_setup_tc(dev, TC_SETUP_QDISC_CBS, &cbs);
  249. if (err < 0) {
  250. NL_SET_ERR_MSG(extack, "Specified device failed to setup cbs hardware offload");
  251. return err;
  252. }
  253. q->enqueue = cbs_enqueue_offload;
  254. q->dequeue = cbs_dequeue_offload;
  255. return 0;
  256. }
  257. static void cbs_set_port_rate(struct net_device *dev, struct cbs_sched_data *q)
  258. {
  259. struct ethtool_link_ksettings ecmd;
  260. int speed = SPEED_10;
  261. int port_rate = -1;
  262. int err;
  263. err = __ethtool_get_link_ksettings(dev, &ecmd);
  264. if (err < 0)
  265. goto skip;
  266. if (ecmd.base.speed && ecmd.base.speed != SPEED_UNKNOWN)
  267. speed = ecmd.base.speed;
  268. skip:
  269. port_rate = speed * 1000 * BYTES_PER_KBIT;
  270. atomic64_set(&q->port_rate, port_rate);
  271. netdev_dbg(dev, "cbs: set %s's port_rate to: %lld, linkspeed: %d\n",
  272. dev->name, (long long)atomic64_read(&q->port_rate),
  273. ecmd.base.speed);
  274. }
  275. static int cbs_dev_notifier(struct notifier_block *nb, unsigned long event,
  276. void *ptr)
  277. {
  278. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  279. struct cbs_sched_data *q;
  280. struct net_device *qdev;
  281. bool found = false;
  282. ASSERT_RTNL();
  283. if (event != NETDEV_UP && event != NETDEV_CHANGE)
  284. return NOTIFY_DONE;
  285. spin_lock(&cbs_list_lock);
  286. list_for_each_entry(q, &cbs_list, cbs_list) {
  287. qdev = qdisc_dev(q->qdisc);
  288. if (qdev == dev) {
  289. found = true;
  290. break;
  291. }
  292. }
  293. spin_unlock(&cbs_list_lock);
  294. if (found)
  295. cbs_set_port_rate(dev, q);
  296. return NOTIFY_DONE;
  297. }
  298. static int cbs_change(struct Qdisc *sch, struct nlattr *opt,
  299. struct netlink_ext_ack *extack)
  300. {
  301. struct cbs_sched_data *q = qdisc_priv(sch);
  302. struct net_device *dev = qdisc_dev(sch);
  303. struct nlattr *tb[TCA_CBS_MAX + 1];
  304. struct tc_cbs_qopt *qopt;
  305. int err;
  306. err = nla_parse_nested(tb, TCA_CBS_MAX, opt, cbs_policy, extack);
  307. if (err < 0)
  308. return err;
  309. if (!tb[TCA_CBS_PARMS]) {
  310. NL_SET_ERR_MSG(extack, "Missing CBS parameter which are mandatory");
  311. return -EINVAL;
  312. }
  313. qopt = nla_data(tb[TCA_CBS_PARMS]);
  314. if (!qopt->offload) {
  315. cbs_set_port_rate(dev, q);
  316. cbs_disable_offload(dev, q);
  317. } else {
  318. err = cbs_enable_offload(dev, q, qopt, extack);
  319. if (err < 0)
  320. return err;
  321. }
  322. /* Everything went OK, save the parameters used. */
  323. q->hicredit = qopt->hicredit;
  324. q->locredit = qopt->locredit;
  325. q->idleslope = qopt->idleslope * BYTES_PER_KBIT;
  326. q->sendslope = qopt->sendslope * BYTES_PER_KBIT;
  327. q->offload = qopt->offload;
  328. return 0;
  329. }
  330. static int cbs_init(struct Qdisc *sch, struct nlattr *opt,
  331. struct netlink_ext_ack *extack)
  332. {
  333. struct cbs_sched_data *q = qdisc_priv(sch);
  334. struct net_device *dev = qdisc_dev(sch);
  335. int err;
  336. if (!opt) {
  337. NL_SET_ERR_MSG(extack, "Missing CBS qdisc options which are mandatory");
  338. return -EINVAL;
  339. }
  340. q->qdisc = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  341. sch->handle, extack);
  342. if (!q->qdisc)
  343. return -ENOMEM;
  344. qdisc_hash_add(q->qdisc, false);
  345. q->queue = sch->dev_queue - netdev_get_tx_queue(dev, 0);
  346. q->enqueue = cbs_enqueue_soft;
  347. q->dequeue = cbs_dequeue_soft;
  348. qdisc_watchdog_init(&q->watchdog, sch);
  349. err = cbs_change(sch, opt, extack);
  350. if (err)
  351. return err;
  352. if (!q->offload) {
  353. spin_lock(&cbs_list_lock);
  354. list_add(&q->cbs_list, &cbs_list);
  355. spin_unlock(&cbs_list_lock);
  356. }
  357. return 0;
  358. }
  359. static void cbs_destroy(struct Qdisc *sch)
  360. {
  361. struct cbs_sched_data *q = qdisc_priv(sch);
  362. struct net_device *dev = qdisc_dev(sch);
  363. spin_lock(&cbs_list_lock);
  364. list_del(&q->cbs_list);
  365. spin_unlock(&cbs_list_lock);
  366. qdisc_watchdog_cancel(&q->watchdog);
  367. cbs_disable_offload(dev, q);
  368. if (q->qdisc)
  369. qdisc_destroy(q->qdisc);
  370. }
  371. static int cbs_dump(struct Qdisc *sch, struct sk_buff *skb)
  372. {
  373. struct cbs_sched_data *q = qdisc_priv(sch);
  374. struct tc_cbs_qopt opt = { };
  375. struct nlattr *nest;
  376. nest = nla_nest_start(skb, TCA_OPTIONS);
  377. if (!nest)
  378. goto nla_put_failure;
  379. opt.hicredit = q->hicredit;
  380. opt.locredit = q->locredit;
  381. opt.sendslope = div64_s64(q->sendslope, BYTES_PER_KBIT);
  382. opt.idleslope = div64_s64(q->idleslope, BYTES_PER_KBIT);
  383. opt.offload = q->offload;
  384. if (nla_put(skb, TCA_CBS_PARMS, sizeof(opt), &opt))
  385. goto nla_put_failure;
  386. return nla_nest_end(skb, nest);
  387. nla_put_failure:
  388. nla_nest_cancel(skb, nest);
  389. return -1;
  390. }
  391. static int cbs_dump_class(struct Qdisc *sch, unsigned long cl,
  392. struct sk_buff *skb, struct tcmsg *tcm)
  393. {
  394. struct cbs_sched_data *q = qdisc_priv(sch);
  395. if (cl != 1 || !q->qdisc) /* only one class */
  396. return -ENOENT;
  397. tcm->tcm_handle |= TC_H_MIN(1);
  398. tcm->tcm_info = q->qdisc->handle;
  399. return 0;
  400. }
  401. static int cbs_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  402. struct Qdisc **old, struct netlink_ext_ack *extack)
  403. {
  404. struct cbs_sched_data *q = qdisc_priv(sch);
  405. if (!new) {
  406. new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  407. sch->handle, NULL);
  408. if (!new)
  409. new = &noop_qdisc;
  410. }
  411. *old = qdisc_replace(sch, new, &q->qdisc);
  412. return 0;
  413. }
  414. static struct Qdisc *cbs_leaf(struct Qdisc *sch, unsigned long arg)
  415. {
  416. struct cbs_sched_data *q = qdisc_priv(sch);
  417. return q->qdisc;
  418. }
  419. static unsigned long cbs_find(struct Qdisc *sch, u32 classid)
  420. {
  421. return 1;
  422. }
  423. static void cbs_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  424. {
  425. if (!walker->stop) {
  426. if (walker->count >= walker->skip) {
  427. if (walker->fn(sch, 1, walker) < 0) {
  428. walker->stop = 1;
  429. return;
  430. }
  431. }
  432. walker->count++;
  433. }
  434. }
  435. static const struct Qdisc_class_ops cbs_class_ops = {
  436. .graft = cbs_graft,
  437. .leaf = cbs_leaf,
  438. .find = cbs_find,
  439. .walk = cbs_walk,
  440. .dump = cbs_dump_class,
  441. };
  442. static struct Qdisc_ops cbs_qdisc_ops __read_mostly = {
  443. .id = "cbs",
  444. .cl_ops = &cbs_class_ops,
  445. .priv_size = sizeof(struct cbs_sched_data),
  446. .enqueue = cbs_enqueue,
  447. .dequeue = cbs_dequeue,
  448. .peek = qdisc_peek_dequeued,
  449. .init = cbs_init,
  450. .reset = qdisc_reset_queue,
  451. .destroy = cbs_destroy,
  452. .change = cbs_change,
  453. .dump = cbs_dump,
  454. .owner = THIS_MODULE,
  455. };
  456. static struct notifier_block cbs_device_notifier = {
  457. .notifier_call = cbs_dev_notifier,
  458. };
  459. static int __init cbs_module_init(void)
  460. {
  461. int err;
  462. err = register_netdevice_notifier(&cbs_device_notifier);
  463. if (err)
  464. return err;
  465. err = register_qdisc(&cbs_qdisc_ops);
  466. if (err)
  467. unregister_netdevice_notifier(&cbs_device_notifier);
  468. return err;
  469. }
  470. static void __exit cbs_module_exit(void)
  471. {
  472. unregister_qdisc(&cbs_qdisc_ops);
  473. unregister_netdevice_notifier(&cbs_device_notifier);
  474. }
  475. module_init(cbs_module_init)
  476. module_exit(cbs_module_exit)
  477. MODULE_LICENSE("GPL");