sch_multiq.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. struct multiq_sched_data {
  28. u16 bands;
  29. u16 max_bands;
  30. u16 curband;
  31. struct tcf_proto __rcu *filter_list;
  32. struct Qdisc **queues;
  33. };
  34. static struct Qdisc *
  35. multiq_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  36. {
  37. struct multiq_sched_data *q = qdisc_priv(sch);
  38. u32 band;
  39. struct tcf_result res;
  40. struct tcf_proto *fl = rcu_dereference_bh(q->filter_list);
  41. int err;
  42. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  43. err = tc_classify(skb, fl, &res, false);
  44. #ifdef CONFIG_NET_CLS_ACT
  45. switch (err) {
  46. case TC_ACT_STOLEN:
  47. case TC_ACT_QUEUED:
  48. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  49. case TC_ACT_SHOT:
  50. return NULL;
  51. }
  52. #endif
  53. band = skb_get_queue_mapping(skb);
  54. if (band >= q->bands)
  55. return q->queues[0];
  56. return q->queues[band];
  57. }
  58. static int
  59. multiq_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  60. struct sk_buff **to_free)
  61. {
  62. struct Qdisc *qdisc;
  63. int ret;
  64. qdisc = multiq_classify(skb, sch, &ret);
  65. #ifdef CONFIG_NET_CLS_ACT
  66. if (qdisc == NULL) {
  67. if (ret & __NET_XMIT_BYPASS)
  68. qdisc_qstats_drop(sch);
  69. __qdisc_drop(skb, to_free);
  70. return ret;
  71. }
  72. #endif
  73. ret = qdisc_enqueue(skb, qdisc, to_free);
  74. if (ret == NET_XMIT_SUCCESS) {
  75. sch->q.qlen++;
  76. return NET_XMIT_SUCCESS;
  77. }
  78. if (net_xmit_drop_count(ret))
  79. qdisc_qstats_drop(sch);
  80. return ret;
  81. }
  82. static struct sk_buff *multiq_dequeue(struct Qdisc *sch)
  83. {
  84. struct multiq_sched_data *q = qdisc_priv(sch);
  85. struct Qdisc *qdisc;
  86. struct sk_buff *skb;
  87. int band;
  88. for (band = 0; band < q->bands; band++) {
  89. /* cycle through bands to ensure fairness */
  90. q->curband++;
  91. if (q->curband >= q->bands)
  92. q->curband = 0;
  93. /* Check that target subqueue is available before
  94. * pulling an skb to avoid head-of-line blocking.
  95. */
  96. if (!netif_xmit_stopped(
  97. netdev_get_tx_queue(qdisc_dev(sch), q->curband))) {
  98. qdisc = q->queues[q->curband];
  99. skb = qdisc->dequeue(qdisc);
  100. if (skb) {
  101. qdisc_bstats_update(sch, skb);
  102. sch->q.qlen--;
  103. return skb;
  104. }
  105. }
  106. }
  107. return NULL;
  108. }
  109. static struct sk_buff *multiq_peek(struct Qdisc *sch)
  110. {
  111. struct multiq_sched_data *q = qdisc_priv(sch);
  112. unsigned int curband = q->curband;
  113. struct Qdisc *qdisc;
  114. struct sk_buff *skb;
  115. int band;
  116. for (band = 0; band < q->bands; band++) {
  117. /* cycle through bands to ensure fairness */
  118. curband++;
  119. if (curband >= q->bands)
  120. curband = 0;
  121. /* Check that target subqueue is available before
  122. * pulling an skb to avoid head-of-line blocking.
  123. */
  124. if (!netif_xmit_stopped(
  125. netdev_get_tx_queue(qdisc_dev(sch), curband))) {
  126. qdisc = q->queues[curband];
  127. skb = qdisc->ops->peek(qdisc);
  128. if (skb)
  129. return skb;
  130. }
  131. }
  132. return NULL;
  133. }
  134. static void
  135. multiq_reset(struct Qdisc *sch)
  136. {
  137. u16 band;
  138. struct multiq_sched_data *q = qdisc_priv(sch);
  139. for (band = 0; band < q->bands; band++)
  140. qdisc_reset(q->queues[band]);
  141. sch->q.qlen = 0;
  142. q->curband = 0;
  143. }
  144. static void
  145. multiq_destroy(struct Qdisc *sch)
  146. {
  147. int band;
  148. struct multiq_sched_data *q = qdisc_priv(sch);
  149. tcf_destroy_chain(&q->filter_list);
  150. for (band = 0; band < q->bands; band++)
  151. qdisc_destroy(q->queues[band]);
  152. kfree(q->queues);
  153. }
  154. static int multiq_tune(struct Qdisc *sch, struct nlattr *opt)
  155. {
  156. struct multiq_sched_data *q = qdisc_priv(sch);
  157. struct tc_multiq_qopt *qopt;
  158. int i;
  159. if (!netif_is_multiqueue(qdisc_dev(sch)))
  160. return -EOPNOTSUPP;
  161. if (nla_len(opt) < sizeof(*qopt))
  162. return -EINVAL;
  163. qopt = nla_data(opt);
  164. qopt->bands = qdisc_dev(sch)->real_num_tx_queues;
  165. sch_tree_lock(sch);
  166. q->bands = qopt->bands;
  167. for (i = q->bands; i < q->max_bands; i++) {
  168. if (q->queues[i] != &noop_qdisc) {
  169. struct Qdisc *child = q->queues[i];
  170. q->queues[i] = &noop_qdisc;
  171. qdisc_tree_reduce_backlog(child, child->q.qlen,
  172. child->qstats.backlog);
  173. qdisc_destroy(child);
  174. }
  175. }
  176. sch_tree_unlock(sch);
  177. for (i = 0; i < q->bands; i++) {
  178. if (q->queues[i] == &noop_qdisc) {
  179. struct Qdisc *child, *old;
  180. child = qdisc_create_dflt(sch->dev_queue,
  181. &pfifo_qdisc_ops,
  182. TC_H_MAKE(sch->handle,
  183. i + 1));
  184. if (child) {
  185. sch_tree_lock(sch);
  186. old = q->queues[i];
  187. q->queues[i] = child;
  188. if (old != &noop_qdisc) {
  189. qdisc_tree_reduce_backlog(old,
  190. old->q.qlen,
  191. old->qstats.backlog);
  192. qdisc_destroy(old);
  193. }
  194. sch_tree_unlock(sch);
  195. }
  196. }
  197. }
  198. return 0;
  199. }
  200. static int multiq_init(struct Qdisc *sch, struct nlattr *opt)
  201. {
  202. struct multiq_sched_data *q = qdisc_priv(sch);
  203. int i, err;
  204. q->queues = NULL;
  205. if (opt == NULL)
  206. return -EINVAL;
  207. q->max_bands = qdisc_dev(sch)->num_tx_queues;
  208. q->queues = kcalloc(q->max_bands, sizeof(struct Qdisc *), GFP_KERNEL);
  209. if (!q->queues)
  210. return -ENOBUFS;
  211. for (i = 0; i < q->max_bands; i++)
  212. q->queues[i] = &noop_qdisc;
  213. err = multiq_tune(sch, opt);
  214. if (err)
  215. kfree(q->queues);
  216. return err;
  217. }
  218. static int multiq_dump(struct Qdisc *sch, struct sk_buff *skb)
  219. {
  220. struct multiq_sched_data *q = qdisc_priv(sch);
  221. unsigned char *b = skb_tail_pointer(skb);
  222. struct tc_multiq_qopt opt;
  223. opt.bands = q->bands;
  224. opt.max_bands = q->max_bands;
  225. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  226. goto nla_put_failure;
  227. return skb->len;
  228. nla_put_failure:
  229. nlmsg_trim(skb, b);
  230. return -1;
  231. }
  232. static int multiq_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  233. struct Qdisc **old)
  234. {
  235. struct multiq_sched_data *q = qdisc_priv(sch);
  236. unsigned long band = arg - 1;
  237. if (new == NULL)
  238. new = &noop_qdisc;
  239. *old = qdisc_replace(sch, new, &q->queues[band]);
  240. return 0;
  241. }
  242. static struct Qdisc *
  243. multiq_leaf(struct Qdisc *sch, unsigned long arg)
  244. {
  245. struct multiq_sched_data *q = qdisc_priv(sch);
  246. unsigned long band = arg - 1;
  247. return q->queues[band];
  248. }
  249. static unsigned long multiq_get(struct Qdisc *sch, u32 classid)
  250. {
  251. struct multiq_sched_data *q = qdisc_priv(sch);
  252. unsigned long band = TC_H_MIN(classid);
  253. if (band - 1 >= q->bands)
  254. return 0;
  255. return band;
  256. }
  257. static unsigned long multiq_bind(struct Qdisc *sch, unsigned long parent,
  258. u32 classid)
  259. {
  260. return multiq_get(sch, classid);
  261. }
  262. static void multiq_put(struct Qdisc *q, unsigned long cl)
  263. {
  264. }
  265. static int multiq_dump_class(struct Qdisc *sch, unsigned long cl,
  266. struct sk_buff *skb, struct tcmsg *tcm)
  267. {
  268. struct multiq_sched_data *q = qdisc_priv(sch);
  269. tcm->tcm_handle |= TC_H_MIN(cl);
  270. tcm->tcm_info = q->queues[cl - 1]->handle;
  271. return 0;
  272. }
  273. static int multiq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  274. struct gnet_dump *d)
  275. {
  276. struct multiq_sched_data *q = qdisc_priv(sch);
  277. struct Qdisc *cl_q;
  278. cl_q = q->queues[cl - 1];
  279. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
  280. d, NULL, &cl_q->bstats) < 0 ||
  281. gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0)
  282. return -1;
  283. return 0;
  284. }
  285. static void multiq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  286. {
  287. struct multiq_sched_data *q = qdisc_priv(sch);
  288. int band;
  289. if (arg->stop)
  290. return;
  291. for (band = 0; band < q->bands; band++) {
  292. if (arg->count < arg->skip) {
  293. arg->count++;
  294. continue;
  295. }
  296. if (arg->fn(sch, band + 1, arg) < 0) {
  297. arg->stop = 1;
  298. break;
  299. }
  300. arg->count++;
  301. }
  302. }
  303. static struct tcf_proto __rcu **multiq_find_tcf(struct Qdisc *sch,
  304. unsigned long cl)
  305. {
  306. struct multiq_sched_data *q = qdisc_priv(sch);
  307. if (cl)
  308. return NULL;
  309. return &q->filter_list;
  310. }
  311. static const struct Qdisc_class_ops multiq_class_ops = {
  312. .graft = multiq_graft,
  313. .leaf = multiq_leaf,
  314. .get = multiq_get,
  315. .put = multiq_put,
  316. .walk = multiq_walk,
  317. .tcf_chain = multiq_find_tcf,
  318. .bind_tcf = multiq_bind,
  319. .unbind_tcf = multiq_put,
  320. .dump = multiq_dump_class,
  321. .dump_stats = multiq_dump_class_stats,
  322. };
  323. static struct Qdisc_ops multiq_qdisc_ops __read_mostly = {
  324. .next = NULL,
  325. .cl_ops = &multiq_class_ops,
  326. .id = "multiq",
  327. .priv_size = sizeof(struct multiq_sched_data),
  328. .enqueue = multiq_enqueue,
  329. .dequeue = multiq_dequeue,
  330. .peek = multiq_peek,
  331. .init = multiq_init,
  332. .reset = multiq_reset,
  333. .destroy = multiq_destroy,
  334. .change = multiq_tune,
  335. .dump = multiq_dump,
  336. .owner = THIS_MODULE,
  337. };
  338. static int __init multiq_module_init(void)
  339. {
  340. return register_qdisc(&multiq_qdisc_ops);
  341. }
  342. static void __exit multiq_module_exit(void)
  343. {
  344. unregister_qdisc(&multiq_qdisc_ops);
  345. }
  346. module_init(multiq_module_init)
  347. module_exit(multiq_module_exit)
  348. MODULE_LICENSE("GPL");