sch_multiq.c 9.2 KB

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