sch_prio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/sch_prio.c Simple 3-band priority "scheduler".
  4. *
  5. * Authors: Alexey Kuznetsov, <[email protected]>
  6. * Fixes: 19990609: J Hadi Salim <[email protected]>:
  7. * Init -- EINVAL when opt undefined
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/string.h>
  14. #include <linux/errno.h>
  15. #include <linux/skbuff.h>
  16. #include <net/netlink.h>
  17. #include <net/pkt_sched.h>
  18. #include <net/pkt_cls.h>
  19. struct prio_sched_data {
  20. int bands;
  21. struct tcf_proto __rcu *filter_list;
  22. struct tcf_block *block;
  23. u8 prio2band[TC_PRIO_MAX+1];
  24. struct Qdisc *queues[TCQ_PRIO_BANDS];
  25. };
  26. static struct Qdisc *
  27. prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  28. {
  29. struct prio_sched_data *q = qdisc_priv(sch);
  30. u32 band = skb->priority;
  31. struct tcf_result res;
  32. struct tcf_proto *fl;
  33. int err;
  34. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  35. if (TC_H_MAJ(skb->priority) != sch->handle) {
  36. fl = rcu_dereference_bh(q->filter_list);
  37. err = tcf_classify(skb, NULL, fl, &res, false);
  38. #ifdef CONFIG_NET_CLS_ACT
  39. switch (err) {
  40. case TC_ACT_STOLEN:
  41. case TC_ACT_QUEUED:
  42. case TC_ACT_TRAP:
  43. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  44. fallthrough;
  45. case TC_ACT_SHOT:
  46. return NULL;
  47. }
  48. #endif
  49. if (!fl || err < 0) {
  50. if (TC_H_MAJ(band))
  51. band = 0;
  52. return q->queues[q->prio2band[band & TC_PRIO_MAX]];
  53. }
  54. band = res.classid;
  55. }
  56. band = TC_H_MIN(band) - 1;
  57. if (band >= q->bands)
  58. return q->queues[q->prio2band[0]];
  59. return q->queues[band];
  60. }
  61. static int
  62. prio_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
  63. {
  64. unsigned int len = qdisc_pkt_len(skb);
  65. struct Qdisc *qdisc;
  66. int ret;
  67. qdisc = prio_classify(skb, sch, &ret);
  68. #ifdef CONFIG_NET_CLS_ACT
  69. if (qdisc == NULL) {
  70. if (ret & __NET_XMIT_BYPASS)
  71. qdisc_qstats_drop(sch);
  72. __qdisc_drop(skb, to_free);
  73. return ret;
  74. }
  75. #endif
  76. ret = qdisc_enqueue(skb, qdisc, to_free);
  77. if (ret == NET_XMIT_SUCCESS) {
  78. sch->qstats.backlog += len;
  79. sch->q.qlen++;
  80. return NET_XMIT_SUCCESS;
  81. }
  82. if (net_xmit_drop_count(ret))
  83. qdisc_qstats_drop(sch);
  84. return ret;
  85. }
  86. static struct sk_buff *prio_peek(struct Qdisc *sch)
  87. {
  88. struct prio_sched_data *q = qdisc_priv(sch);
  89. int prio;
  90. for (prio = 0; prio < q->bands; prio++) {
  91. struct Qdisc *qdisc = q->queues[prio];
  92. struct sk_buff *skb = qdisc->ops->peek(qdisc);
  93. if (skb)
  94. return skb;
  95. }
  96. return NULL;
  97. }
  98. static struct sk_buff *prio_dequeue(struct Qdisc *sch)
  99. {
  100. struct prio_sched_data *q = qdisc_priv(sch);
  101. int prio;
  102. for (prio = 0; prio < q->bands; prio++) {
  103. struct Qdisc *qdisc = q->queues[prio];
  104. struct sk_buff *skb = qdisc_dequeue_peeked(qdisc);
  105. if (skb) {
  106. qdisc_bstats_update(sch, skb);
  107. qdisc_qstats_backlog_dec(sch, skb);
  108. sch->q.qlen--;
  109. return skb;
  110. }
  111. }
  112. return NULL;
  113. }
  114. static void
  115. prio_reset(struct Qdisc *sch)
  116. {
  117. int prio;
  118. struct prio_sched_data *q = qdisc_priv(sch);
  119. for (prio = 0; prio < q->bands; prio++)
  120. qdisc_reset(q->queues[prio]);
  121. }
  122. static int prio_offload(struct Qdisc *sch, struct tc_prio_qopt *qopt)
  123. {
  124. struct net_device *dev = qdisc_dev(sch);
  125. struct tc_prio_qopt_offload opt = {
  126. .handle = sch->handle,
  127. .parent = sch->parent,
  128. };
  129. if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc)
  130. return -EOPNOTSUPP;
  131. if (qopt) {
  132. opt.command = TC_PRIO_REPLACE;
  133. opt.replace_params.bands = qopt->bands;
  134. memcpy(&opt.replace_params.priomap, qopt->priomap,
  135. TC_PRIO_MAX + 1);
  136. opt.replace_params.qstats = &sch->qstats;
  137. } else {
  138. opt.command = TC_PRIO_DESTROY;
  139. }
  140. return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_QDISC_PRIO, &opt);
  141. }
  142. static void
  143. prio_destroy(struct Qdisc *sch)
  144. {
  145. int prio;
  146. struct prio_sched_data *q = qdisc_priv(sch);
  147. tcf_block_put(q->block);
  148. prio_offload(sch, NULL);
  149. for (prio = 0; prio < q->bands; prio++)
  150. qdisc_put(q->queues[prio]);
  151. }
  152. static int prio_tune(struct Qdisc *sch, struct nlattr *opt,
  153. struct netlink_ext_ack *extack)
  154. {
  155. struct prio_sched_data *q = qdisc_priv(sch);
  156. struct Qdisc *queues[TCQ_PRIO_BANDS];
  157. int oldbands = q->bands, i;
  158. struct tc_prio_qopt *qopt;
  159. if (nla_len(opt) < sizeof(*qopt))
  160. return -EINVAL;
  161. qopt = nla_data(opt);
  162. if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < TCQ_MIN_PRIO_BANDS)
  163. return -EINVAL;
  164. for (i = 0; i <= TC_PRIO_MAX; i++) {
  165. if (qopt->priomap[i] >= qopt->bands)
  166. return -EINVAL;
  167. }
  168. /* Before commit, make sure we can allocate all new qdiscs */
  169. for (i = oldbands; i < qopt->bands; i++) {
  170. queues[i] = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  171. TC_H_MAKE(sch->handle, i + 1),
  172. extack);
  173. if (!queues[i]) {
  174. while (i > oldbands)
  175. qdisc_put(queues[--i]);
  176. return -ENOMEM;
  177. }
  178. }
  179. prio_offload(sch, qopt);
  180. sch_tree_lock(sch);
  181. q->bands = qopt->bands;
  182. memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
  183. for (i = q->bands; i < oldbands; i++)
  184. qdisc_tree_flush_backlog(q->queues[i]);
  185. for (i = oldbands; i < q->bands; i++) {
  186. q->queues[i] = queues[i];
  187. if (q->queues[i] != &noop_qdisc)
  188. qdisc_hash_add(q->queues[i], true);
  189. }
  190. sch_tree_unlock(sch);
  191. for (i = q->bands; i < oldbands; i++)
  192. qdisc_put(q->queues[i]);
  193. return 0;
  194. }
  195. static int prio_init(struct Qdisc *sch, struct nlattr *opt,
  196. struct netlink_ext_ack *extack)
  197. {
  198. struct prio_sched_data *q = qdisc_priv(sch);
  199. int err;
  200. if (!opt)
  201. return -EINVAL;
  202. err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
  203. if (err)
  204. return err;
  205. return prio_tune(sch, opt, extack);
  206. }
  207. static int prio_dump_offload(struct Qdisc *sch)
  208. {
  209. struct tc_prio_qopt_offload hw_stats = {
  210. .command = TC_PRIO_STATS,
  211. .handle = sch->handle,
  212. .parent = sch->parent,
  213. {
  214. .stats = {
  215. .bstats = &sch->bstats,
  216. .qstats = &sch->qstats,
  217. },
  218. },
  219. };
  220. return qdisc_offload_dump_helper(sch, TC_SETUP_QDISC_PRIO, &hw_stats);
  221. }
  222. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  223. {
  224. struct prio_sched_data *q = qdisc_priv(sch);
  225. unsigned char *b = skb_tail_pointer(skb);
  226. struct tc_prio_qopt opt;
  227. int err;
  228. opt.bands = q->bands;
  229. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1);
  230. err = prio_dump_offload(sch);
  231. if (err)
  232. goto nla_put_failure;
  233. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  234. goto nla_put_failure;
  235. return skb->len;
  236. nla_put_failure:
  237. nlmsg_trim(skb, b);
  238. return -1;
  239. }
  240. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  241. struct Qdisc **old, struct netlink_ext_ack *extack)
  242. {
  243. struct prio_sched_data *q = qdisc_priv(sch);
  244. struct tc_prio_qopt_offload graft_offload;
  245. unsigned long band = arg - 1;
  246. if (!new) {
  247. new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  248. TC_H_MAKE(sch->handle, arg), extack);
  249. if (!new)
  250. new = &noop_qdisc;
  251. else
  252. qdisc_hash_add(new, true);
  253. }
  254. *old = qdisc_replace(sch, new, &q->queues[band]);
  255. graft_offload.handle = sch->handle;
  256. graft_offload.parent = sch->parent;
  257. graft_offload.graft_params.band = band;
  258. graft_offload.graft_params.child_handle = new->handle;
  259. graft_offload.command = TC_PRIO_GRAFT;
  260. qdisc_offload_graft_helper(qdisc_dev(sch), sch, new, *old,
  261. TC_SETUP_QDISC_PRIO, &graft_offload,
  262. extack);
  263. return 0;
  264. }
  265. static struct Qdisc *
  266. prio_leaf(struct Qdisc *sch, unsigned long arg)
  267. {
  268. struct prio_sched_data *q = qdisc_priv(sch);
  269. unsigned long band = arg - 1;
  270. return q->queues[band];
  271. }
  272. static unsigned long prio_find(struct Qdisc *sch, u32 classid)
  273. {
  274. struct prio_sched_data *q = qdisc_priv(sch);
  275. unsigned long band = TC_H_MIN(classid);
  276. if (band - 1 >= q->bands)
  277. return 0;
  278. return band;
  279. }
  280. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  281. {
  282. return prio_find(sch, classid);
  283. }
  284. static void prio_unbind(struct Qdisc *q, unsigned long cl)
  285. {
  286. }
  287. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  288. struct tcmsg *tcm)
  289. {
  290. struct prio_sched_data *q = qdisc_priv(sch);
  291. tcm->tcm_handle |= TC_H_MIN(cl);
  292. tcm->tcm_info = q->queues[cl-1]->handle;
  293. return 0;
  294. }
  295. static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  296. struct gnet_dump *d)
  297. {
  298. struct prio_sched_data *q = qdisc_priv(sch);
  299. struct Qdisc *cl_q;
  300. cl_q = q->queues[cl - 1];
  301. if (gnet_stats_copy_basic(d, cl_q->cpu_bstats,
  302. &cl_q->bstats, true) < 0 ||
  303. qdisc_qstats_copy(d, cl_q) < 0)
  304. return -1;
  305. return 0;
  306. }
  307. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  308. {
  309. struct prio_sched_data *q = qdisc_priv(sch);
  310. int prio;
  311. if (arg->stop)
  312. return;
  313. for (prio = 0; prio < q->bands; prio++) {
  314. if (!tc_qdisc_stats_dump(sch, prio + 1, arg))
  315. break;
  316. }
  317. }
  318. static struct tcf_block *prio_tcf_block(struct Qdisc *sch, unsigned long cl,
  319. struct netlink_ext_ack *extack)
  320. {
  321. struct prio_sched_data *q = qdisc_priv(sch);
  322. if (cl)
  323. return NULL;
  324. return q->block;
  325. }
  326. static const struct Qdisc_class_ops prio_class_ops = {
  327. .graft = prio_graft,
  328. .leaf = prio_leaf,
  329. .find = prio_find,
  330. .walk = prio_walk,
  331. .tcf_block = prio_tcf_block,
  332. .bind_tcf = prio_bind,
  333. .unbind_tcf = prio_unbind,
  334. .dump = prio_dump_class,
  335. .dump_stats = prio_dump_class_stats,
  336. };
  337. static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
  338. .next = NULL,
  339. .cl_ops = &prio_class_ops,
  340. .id = "prio",
  341. .priv_size = sizeof(struct prio_sched_data),
  342. .enqueue = prio_enqueue,
  343. .dequeue = prio_dequeue,
  344. .peek = prio_peek,
  345. .init = prio_init,
  346. .reset = prio_reset,
  347. .destroy = prio_destroy,
  348. .change = prio_tune,
  349. .dump = prio_dump,
  350. .owner = THIS_MODULE,
  351. };
  352. static int __init prio_module_init(void)
  353. {
  354. return register_qdisc(&prio_qdisc_ops);
  355. }
  356. static void __exit prio_module_exit(void)
  357. {
  358. unregister_qdisc(&prio_qdisc_ops);
  359. }
  360. module_init(prio_module_init)
  361. module_exit(prio_module_exit)
  362. MODULE_LICENSE("GPL");