sch_multiq.c 9.0 KB

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