sch_multiq.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Author: Alexander Duyck <alexander.h.duyck@intel.com>
  18. */
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include <linux/kernel.h>
  23. #include <linux/string.h>
  24. #include <linux/errno.h>
  25. #include <linux/skbuff.h>
  26. #include <net/netlink.h>
  27. #include <net/pkt_sched.h>
  28. struct multiq_sched_data {
  29. u16 bands;
  30. u16 max_bands;
  31. u16 curband;
  32. struct tcf_proto *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. int err;
  42. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  43. err = tc_classify(skb, q->filter_list, &res);
  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. sch->qstats.drops++;
  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. sch->qstats.drops++;
  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_subqueue_stopped(qdisc_dev(sch), q->curband)) {
  96. qdisc = q->queues[q->curband];
  97. skb = qdisc->dequeue(qdisc);
  98. if (skb) {
  99. qdisc_bstats_update(sch, skb);
  100. sch->q.qlen--;
  101. return skb;
  102. }
  103. }
  104. }
  105. return NULL;
  106. }
  107. static struct sk_buff *multiq_peek(struct Qdisc *sch)
  108. {
  109. struct multiq_sched_data *q = qdisc_priv(sch);
  110. unsigned int curband = q->curband;
  111. struct Qdisc *qdisc;
  112. struct sk_buff *skb;
  113. int band;
  114. for (band = 0; band < q->bands; band++) {
  115. /* cycle through bands to ensure fairness */
  116. curband++;
  117. if (curband >= q->bands)
  118. curband = 0;
  119. /* Check that target subqueue is available before
  120. * pulling an skb to avoid head-of-line blocking.
  121. */
  122. if (!__netif_subqueue_stopped(qdisc_dev(sch), curband)) {
  123. qdisc = q->queues[curband];
  124. skb = qdisc->ops->peek(qdisc);
  125. if (skb)
  126. return skb;
  127. }
  128. }
  129. return NULL;
  130. }
  131. static unsigned int multiq_drop(struct Qdisc *sch)
  132. {
  133. struct multiq_sched_data *q = qdisc_priv(sch);
  134. int band;
  135. unsigned int len;
  136. struct Qdisc *qdisc;
  137. for (band = q->bands - 1; band >= 0; band--) {
  138. qdisc = q->queues[band];
  139. if (qdisc->ops->drop) {
  140. len = qdisc->ops->drop(qdisc);
  141. if (len != 0) {
  142. sch->q.qlen--;
  143. return len;
  144. }
  145. }
  146. }
  147. return 0;
  148. }
  149. static void
  150. multiq_reset(struct Qdisc *sch)
  151. {
  152. u16 band;
  153. struct multiq_sched_data *q = qdisc_priv(sch);
  154. for (band = 0; band < q->bands; band++)
  155. qdisc_reset(q->queues[band]);
  156. sch->q.qlen = 0;
  157. q->curband = 0;
  158. }
  159. static void
  160. multiq_destroy(struct Qdisc *sch)
  161. {
  162. int band;
  163. struct multiq_sched_data *q = qdisc_priv(sch);
  164. tcf_destroy_chain(&q->filter_list);
  165. for (band = 0; band < q->bands; band++)
  166. qdisc_destroy(q->queues[band]);
  167. kfree(q->queues);
  168. }
  169. static int multiq_tune(struct Qdisc *sch, struct nlattr *opt)
  170. {
  171. struct multiq_sched_data *q = qdisc_priv(sch);
  172. struct tc_multiq_qopt *qopt;
  173. int i;
  174. if (!netif_is_multiqueue(qdisc_dev(sch)))
  175. return -EOPNOTSUPP;
  176. if (nla_len(opt) < sizeof(*qopt))
  177. return -EINVAL;
  178. qopt = nla_data(opt);
  179. qopt->bands = qdisc_dev(sch)->real_num_tx_queues;
  180. sch_tree_lock(sch);
  181. q->bands = qopt->bands;
  182. for (i = q->bands; i < q->max_bands; i++) {
  183. if (q->queues[i] != &noop_qdisc) {
  184. struct Qdisc *child = q->queues[i];
  185. q->queues[i] = &noop_qdisc;
  186. qdisc_tree_decrease_qlen(child, child->q.qlen);
  187. qdisc_destroy(child);
  188. }
  189. }
  190. sch_tree_unlock(sch);
  191. for (i = 0; i < q->bands; i++) {
  192. if (q->queues[i] == &noop_qdisc) {
  193. struct Qdisc *child, *old;
  194. child = qdisc_create_dflt(sch->dev_queue,
  195. &pfifo_qdisc_ops,
  196. TC_H_MAKE(sch->handle,
  197. i + 1));
  198. if (child) {
  199. sch_tree_lock(sch);
  200. old = q->queues[i];
  201. q->queues[i] = child;
  202. if (old != &noop_qdisc) {
  203. qdisc_tree_decrease_qlen(old,
  204. old->q.qlen);
  205. qdisc_destroy(old);
  206. }
  207. sch_tree_unlock(sch);
  208. }
  209. }
  210. }
  211. return 0;
  212. }
  213. static int multiq_init(struct Qdisc *sch, struct nlattr *opt)
  214. {
  215. struct multiq_sched_data *q = qdisc_priv(sch);
  216. int i, err;
  217. q->queues = NULL;
  218. if (opt == NULL)
  219. return -EINVAL;
  220. q->max_bands = qdisc_dev(sch)->num_tx_queues;
  221. q->queues = kcalloc(q->max_bands, sizeof(struct Qdisc *), GFP_KERNEL);
  222. if (!q->queues)
  223. return -ENOBUFS;
  224. for (i = 0; i < q->max_bands; i++)
  225. q->queues[i] = &noop_qdisc;
  226. err = multiq_tune(sch, opt);
  227. if (err)
  228. kfree(q->queues);
  229. return err;
  230. }
  231. static int multiq_dump(struct Qdisc *sch, struct sk_buff *skb)
  232. {
  233. struct multiq_sched_data *q = qdisc_priv(sch);
  234. unsigned char *b = skb_tail_pointer(skb);
  235. struct tc_multiq_qopt opt;
  236. opt.bands = q->bands;
  237. opt.max_bands = q->max_bands;
  238. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  239. return skb->len;
  240. nla_put_failure:
  241. nlmsg_trim(skb, b);
  242. return -1;
  243. }
  244. static int multiq_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  245. struct Qdisc **old)
  246. {
  247. struct multiq_sched_data *q = qdisc_priv(sch);
  248. unsigned long band = arg - 1;
  249. if (new == NULL)
  250. new = &noop_qdisc;
  251. sch_tree_lock(sch);
  252. *old = q->queues[band];
  253. q->queues[band] = new;
  254. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  255. qdisc_reset(*old);
  256. sch_tree_unlock(sch);
  257. return 0;
  258. }
  259. static struct Qdisc *
  260. multiq_leaf(struct Qdisc *sch, unsigned long arg)
  261. {
  262. struct multiq_sched_data *q = qdisc_priv(sch);
  263. unsigned long band = arg - 1;
  264. return q->queues[band];
  265. }
  266. static unsigned long multiq_get(struct Qdisc *sch, u32 classid)
  267. {
  268. struct multiq_sched_data *q = qdisc_priv(sch);
  269. unsigned long band = TC_H_MIN(classid);
  270. if (band - 1 >= q->bands)
  271. return 0;
  272. return band;
  273. }
  274. static unsigned long multiq_bind(struct Qdisc *sch, unsigned long parent,
  275. u32 classid)
  276. {
  277. return multiq_get(sch, classid);
  278. }
  279. static void multiq_put(struct Qdisc *q, unsigned long cl)
  280. {
  281. }
  282. static int multiq_dump_class(struct Qdisc *sch, unsigned long cl,
  283. struct sk_buff *skb, struct tcmsg *tcm)
  284. {
  285. struct multiq_sched_data *q = qdisc_priv(sch);
  286. tcm->tcm_handle |= TC_H_MIN(cl);
  287. tcm->tcm_info = q->queues[cl - 1]->handle;
  288. return 0;
  289. }
  290. static int multiq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  291. struct gnet_dump *d)
  292. {
  293. struct multiq_sched_data *q = qdisc_priv(sch);
  294. struct Qdisc *cl_q;
  295. cl_q = q->queues[cl - 1];
  296. cl_q->qstats.qlen = cl_q->q.qlen;
  297. if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
  298. gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
  299. return -1;
  300. return 0;
  301. }
  302. static void multiq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  303. {
  304. struct multiq_sched_data *q = qdisc_priv(sch);
  305. int band;
  306. if (arg->stop)
  307. return;
  308. for (band = 0; band < q->bands; band++) {
  309. if (arg->count < arg->skip) {
  310. arg->count++;
  311. continue;
  312. }
  313. if (arg->fn(sch, band + 1, arg) < 0) {
  314. arg->stop = 1;
  315. break;
  316. }
  317. arg->count++;
  318. }
  319. }
  320. static struct tcf_proto **multiq_find_tcf(struct Qdisc *sch, unsigned long cl)
  321. {
  322. struct multiq_sched_data *q = qdisc_priv(sch);
  323. if (cl)
  324. return NULL;
  325. return &q->filter_list;
  326. }
  327. static const struct Qdisc_class_ops multiq_class_ops = {
  328. .graft = multiq_graft,
  329. .leaf = multiq_leaf,
  330. .get = multiq_get,
  331. .put = multiq_put,
  332. .walk = multiq_walk,
  333. .tcf_chain = multiq_find_tcf,
  334. .bind_tcf = multiq_bind,
  335. .unbind_tcf = multiq_put,
  336. .dump = multiq_dump_class,
  337. .dump_stats = multiq_dump_class_stats,
  338. };
  339. static struct Qdisc_ops multiq_qdisc_ops __read_mostly = {
  340. .next = NULL,
  341. .cl_ops = &multiq_class_ops,
  342. .id = "multiq",
  343. .priv_size = sizeof(struct multiq_sched_data),
  344. .enqueue = multiq_enqueue,
  345. .dequeue = multiq_dequeue,
  346. .peek = multiq_peek,
  347. .drop = multiq_drop,
  348. .init = multiq_init,
  349. .reset = multiq_reset,
  350. .destroy = multiq_destroy,
  351. .change = multiq_tune,
  352. .dump = multiq_dump,
  353. .owner = THIS_MODULE,
  354. };
  355. static int __init multiq_module_init(void)
  356. {
  357. return register_qdisc(&multiq_qdisc_ops);
  358. }
  359. static void __exit multiq_module_exit(void)
  360. {
  361. unregister_qdisc(&multiq_qdisc_ops);
  362. }
  363. module_init(multiq_module_init)
  364. module_exit(multiq_module_exit)
  365. MODULE_LICENSE("GPL");