sch_multiq.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (c) 2008, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * Author: Alexander Duyck <alexander.h.duyck@intel.com>
  17. */
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/string.h>
  23. #include <linux/errno.h>
  24. #include <linux/skbuff.h>
  25. #include <net/netlink.h>
  26. #include <net/pkt_sched.h>
  27. #include <net/pkt_cls.h>
  28. struct multiq_sched_data {
  29. u16 bands;
  30. u16 max_bands;
  31. u16 curband;
  32. struct tcf_proto __rcu *filter_list;
  33. struct tcf_block *block;
  34. struct Qdisc **queues;
  35. };
  36. static struct Qdisc *
  37. multiq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  38. {
  39. struct multiq_sched_data *q = qdisc_priv(sch);
  40. u32 band;
  41. struct tcf_result res;
  42. struct tcf_proto *fl = rcu_dereference_bh(q->filter_list);
  43. int err;
  44. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  45. err = tcf_classify(skb, fl, &res, false);
  46. #ifdef CONFIG_NET_CLS_ACT
  47. switch (err) {
  48. case TC_ACT_STOLEN:
  49. case TC_ACT_QUEUED:
  50. case TC_ACT_TRAP:
  51. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  52. /* fall through */
  53. case TC_ACT_SHOT:
  54. return NULL;
  55. }
  56. #endif
  57. band = skb_get_queue_mapping(skb);
  58. if (band >= q->bands)
  59. return q->queues[0];
  60. return q->queues[band];
  61. }
  62. static int
  63. multiq_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  64. struct sk_buff **to_free)
  65. {
  66. struct Qdisc *qdisc;
  67. int ret;
  68. qdisc = multiq_classify(skb, sch, &ret);
  69. #ifdef CONFIG_NET_CLS_ACT
  70. if (qdisc == NULL) {
  71. if (ret & __NET_XMIT_BYPASS)
  72. qdisc_qstats_drop(sch);
  73. __qdisc_drop(skb, to_free);
  74. return ret;
  75. }
  76. #endif
  77. ret = qdisc_enqueue(skb, qdisc, to_free);
  78. if (ret == NET_XMIT_SUCCESS) {
  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 *multiq_dequeue(struct Qdisc *sch)
  87. {
  88. struct multiq_sched_data *q = qdisc_priv(sch);
  89. struct Qdisc *qdisc;
  90. struct sk_buff *skb;
  91. int band;
  92. for (band = 0; band < q->bands; band++) {
  93. /* cycle through bands to ensure fairness */
  94. q->curband++;
  95. if (q->curband >= q->bands)
  96. q->curband = 0;
  97. /* Check that target subqueue is available before
  98. * pulling an skb to avoid head-of-line blocking.
  99. */
  100. if (!netif_xmit_stopped(
  101. netdev_get_tx_queue(qdisc_dev(sch), q->curband))) {
  102. qdisc = q->queues[q->curband];
  103. skb = qdisc->dequeue(qdisc);
  104. if (skb) {
  105. qdisc_bstats_update(sch, skb);
  106. sch->q.qlen--;
  107. return skb;
  108. }
  109. }
  110. }
  111. return NULL;
  112. }
  113. static struct sk_buff *multiq_peek(struct Qdisc *sch)
  114. {
  115. struct multiq_sched_data *q = qdisc_priv(sch);
  116. unsigned int curband = q->curband;
  117. struct Qdisc *qdisc;
  118. struct sk_buff *skb;
  119. int band;
  120. for (band = 0; band < q->bands; band++) {
  121. /* cycle through bands to ensure fairness */
  122. curband++;
  123. if (curband >= q->bands)
  124. curband = 0;
  125. /* Check that target subqueue is available before
  126. * pulling an skb to avoid head-of-line blocking.
  127. */
  128. if (!netif_xmit_stopped(
  129. netdev_get_tx_queue(qdisc_dev(sch), curband))) {
  130. qdisc = q->queues[curband];
  131. skb = qdisc->ops->peek(qdisc);
  132. if (skb)
  133. return skb;
  134. }
  135. }
  136. return NULL;
  137. }
  138. static void
  139. multiq_reset(struct Qdisc *sch)
  140. {
  141. u16 band;
  142. struct multiq_sched_data *q = qdisc_priv(sch);
  143. for (band = 0; band < q->bands; band++)
  144. qdisc_reset(q->queues[band]);
  145. sch->q.qlen = 0;
  146. q->curband = 0;
  147. }
  148. static void
  149. multiq_destroy(struct Qdisc *sch)
  150. {
  151. int band;
  152. struct multiq_sched_data *q = qdisc_priv(sch);
  153. tcf_block_put(q->block);
  154. for (band = 0; band < q->bands; band++)
  155. qdisc_destroy(q->queues[band]);
  156. kfree(q->queues);
  157. }
  158. static int multiq_tune(struct Qdisc *sch, struct nlattr *opt,
  159. struct netlink_ext_ack *extack)
  160. {
  161. struct multiq_sched_data *q = qdisc_priv(sch);
  162. struct tc_multiq_qopt *qopt;
  163. int i;
  164. if (!netif_is_multiqueue(qdisc_dev(sch)))
  165. return -EOPNOTSUPP;
  166. if (nla_len(opt) < sizeof(*qopt))
  167. return -EINVAL;
  168. qopt = nla_data(opt);
  169. qopt->bands = qdisc_dev(sch)->real_num_tx_queues;
  170. sch_tree_lock(sch);
  171. q->bands = qopt->bands;
  172. for (i = q->bands; i < q->max_bands; i++) {
  173. if (q->queues[i] != &noop_qdisc) {
  174. struct Qdisc *child = q->queues[i];
  175. q->queues[i] = &noop_qdisc;
  176. qdisc_tree_reduce_backlog(child, child->q.qlen,
  177. child->qstats.backlog);
  178. qdisc_destroy(child);
  179. }
  180. }
  181. sch_tree_unlock(sch);
  182. for (i = 0; i < q->bands; i++) {
  183. if (q->queues[i] == &noop_qdisc) {
  184. struct Qdisc *child, *old;
  185. child = qdisc_create_dflt(sch->dev_queue,
  186. &pfifo_qdisc_ops,
  187. TC_H_MAKE(sch->handle,
  188. i + 1), extack);
  189. if (child) {
  190. sch_tree_lock(sch);
  191. old = q->queues[i];
  192. q->queues[i] = child;
  193. if (child != &noop_qdisc)
  194. qdisc_hash_add(child, true);
  195. if (old != &noop_qdisc) {
  196. qdisc_tree_reduce_backlog(old,
  197. old->q.qlen,
  198. old->qstats.backlog);
  199. qdisc_destroy(old);
  200. }
  201. sch_tree_unlock(sch);
  202. }
  203. }
  204. }
  205. return 0;
  206. }
  207. static int multiq_init(struct Qdisc *sch, struct nlattr *opt,
  208. struct netlink_ext_ack *extack)
  209. {
  210. struct multiq_sched_data *q = qdisc_priv(sch);
  211. int i, err;
  212. q->queues = NULL;
  213. if (!opt)
  214. return -EINVAL;
  215. err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
  216. if (err)
  217. return err;
  218. q->max_bands = qdisc_dev(sch)->num_tx_queues;
  219. q->queues = kcalloc(q->max_bands, sizeof(struct Qdisc *), GFP_KERNEL);
  220. if (!q->queues)
  221. return -ENOBUFS;
  222. for (i = 0; i < q->max_bands; i++)
  223. q->queues[i] = &noop_qdisc;
  224. return multiq_tune(sch, opt, extack);
  225. }
  226. static int multiq_dump(struct Qdisc *sch, struct sk_buff *skb)
  227. {
  228. struct multiq_sched_data *q = qdisc_priv(sch);
  229. unsigned char *b = skb_tail_pointer(skb);
  230. struct tc_multiq_qopt opt;
  231. opt.bands = q->bands;
  232. opt.max_bands = q->max_bands;
  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 multiq_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  241. struct Qdisc **old, struct netlink_ext_ack *extack)
  242. {
  243. struct multiq_sched_data *q = qdisc_priv(sch);
  244. unsigned long band = arg - 1;
  245. if (new == NULL)
  246. new = &noop_qdisc;
  247. *old = qdisc_replace(sch, new, &q->queues[band]);
  248. return 0;
  249. }
  250. static struct Qdisc *
  251. multiq_leaf(struct Qdisc *sch, unsigned long arg)
  252. {
  253. struct multiq_sched_data *q = qdisc_priv(sch);
  254. unsigned long band = arg - 1;
  255. return q->queues[band];
  256. }
  257. static unsigned long multiq_find(struct Qdisc *sch, u32 classid)
  258. {
  259. struct multiq_sched_data *q = qdisc_priv(sch);
  260. unsigned long band = TC_H_MIN(classid);
  261. if (band - 1 >= q->bands)
  262. return 0;
  263. return band;
  264. }
  265. static unsigned long multiq_bind(struct Qdisc *sch, unsigned long parent,
  266. u32 classid)
  267. {
  268. return multiq_find(sch, classid);
  269. }
  270. static void multiq_unbind(struct Qdisc *q, unsigned long cl)
  271. {
  272. }
  273. static int multiq_dump_class(struct Qdisc *sch, unsigned long cl,
  274. struct sk_buff *skb, struct tcmsg *tcm)
  275. {
  276. struct multiq_sched_data *q = qdisc_priv(sch);
  277. tcm->tcm_handle |= TC_H_MIN(cl);
  278. tcm->tcm_info = q->queues[cl - 1]->handle;
  279. return 0;
  280. }
  281. static int multiq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  282. struct gnet_dump *d)
  283. {
  284. struct multiq_sched_data *q = qdisc_priv(sch);
  285. struct Qdisc *cl_q;
  286. cl_q = q->queues[cl - 1];
  287. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
  288. d, cl_q->cpu_bstats, &cl_q->bstats) < 0 ||
  289. gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0)
  290. return -1;
  291. return 0;
  292. }
  293. static void multiq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  294. {
  295. struct multiq_sched_data *q = qdisc_priv(sch);
  296. int band;
  297. if (arg->stop)
  298. return;
  299. for (band = 0; band < q->bands; band++) {
  300. if (arg->count < arg->skip) {
  301. arg->count++;
  302. continue;
  303. }
  304. if (arg->fn(sch, band + 1, arg) < 0) {
  305. arg->stop = 1;
  306. break;
  307. }
  308. arg->count++;
  309. }
  310. }
  311. static struct tcf_block *multiq_tcf_block(struct Qdisc *sch, unsigned long cl,
  312. struct netlink_ext_ack *extack)
  313. {
  314. struct multiq_sched_data *q = qdisc_priv(sch);
  315. if (cl)
  316. return NULL;
  317. return q->block;
  318. }
  319. static const struct Qdisc_class_ops multiq_class_ops = {
  320. .graft = multiq_graft,
  321. .leaf = multiq_leaf,
  322. .find = multiq_find,
  323. .walk = multiq_walk,
  324. .tcf_block = multiq_tcf_block,
  325. .bind_tcf = multiq_bind,
  326. .unbind_tcf = multiq_unbind,
  327. .dump = multiq_dump_class,
  328. .dump_stats = multiq_dump_class_stats,
  329. };
  330. static struct Qdisc_ops multiq_qdisc_ops __read_mostly = {
  331. .next = NULL,
  332. .cl_ops = &multiq_class_ops,
  333. .id = "multiq",
  334. .priv_size = sizeof(struct multiq_sched_data),
  335. .enqueue = multiq_enqueue,
  336. .dequeue = multiq_dequeue,
  337. .peek = multiq_peek,
  338. .init = multiq_init,
  339. .reset = multiq_reset,
  340. .destroy = multiq_destroy,
  341. .change = multiq_tune,
  342. .dump = multiq_dump,
  343. .owner = THIS_MODULE,
  344. };
  345. static int __init multiq_module_init(void)
  346. {
  347. return register_qdisc(&multiq_qdisc_ops);
  348. }
  349. static void __exit multiq_module_exit(void)
  350. {
  351. unregister_qdisc(&multiq_qdisc_ops);
  352. }
  353. module_init(multiq_module_init)
  354. module_exit(multiq_module_exit)
  355. MODULE_LICENSE("GPL");