sch_multiq.c 9.2 KB

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