sch_multiq.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/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->bstats.bytes += qdisc_pkt_len(skb);
  74. sch->bstats.packets++;
  75. sch->q.qlen++;
  76. return NET_XMIT_SUCCESS;
  77. }
  78. if (net_xmit_drop_count(ret))
  79. sch->qstats.drops++;
  80. return ret;
  81. }
  82. static int
  83. multiq_requeue(struct sk_buff *skb, struct Qdisc *sch)
  84. {
  85. struct Qdisc *qdisc;
  86. struct multiq_sched_data *q = qdisc_priv(sch);
  87. int ret;
  88. qdisc = multiq_classify(skb, sch, &ret);
  89. #ifdef CONFIG_NET_CLS_ACT
  90. if (qdisc == NULL) {
  91. if (ret & __NET_XMIT_BYPASS)
  92. sch->qstats.drops++;
  93. kfree_skb(skb);
  94. return ret;
  95. }
  96. #endif
  97. ret = qdisc->ops->requeue(skb, qdisc);
  98. if (ret == NET_XMIT_SUCCESS) {
  99. sch->q.qlen++;
  100. sch->qstats.requeues++;
  101. if (q->curband)
  102. q->curband--;
  103. else
  104. q->curband = q->bands - 1;
  105. return NET_XMIT_SUCCESS;
  106. }
  107. if (net_xmit_drop_count(ret))
  108. sch->qstats.drops++;
  109. return ret;
  110. }
  111. static struct sk_buff *multiq_dequeue(struct Qdisc *sch)
  112. {
  113. struct multiq_sched_data *q = qdisc_priv(sch);
  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. q->curband++;
  120. if (q->curband >= q->bands)
  121. q->curband = 0;
  122. /* Check that target subqueue is available before
  123. * pulling an skb to avoid excessive requeues
  124. */
  125. if (!__netif_subqueue_stopped(qdisc_dev(sch), q->curband)) {
  126. qdisc = q->queues[q->curband];
  127. skb = qdisc->dequeue(qdisc);
  128. if (skb) {
  129. sch->q.qlen--;
  130. return skb;
  131. }
  132. }
  133. }
  134. return NULL;
  135. }
  136. static unsigned int multiq_drop(struct Qdisc *sch)
  137. {
  138. struct multiq_sched_data *q = qdisc_priv(sch);
  139. int band;
  140. unsigned int len;
  141. struct Qdisc *qdisc;
  142. for (band = q->bands-1; band >= 0; band--) {
  143. qdisc = q->queues[band];
  144. if (qdisc->ops->drop) {
  145. len = qdisc->ops->drop(qdisc);
  146. if (len != 0) {
  147. sch->q.qlen--;
  148. return len;
  149. }
  150. }
  151. }
  152. return 0;
  153. }
  154. static void
  155. multiq_reset(struct Qdisc *sch)
  156. {
  157. u16 band;
  158. struct multiq_sched_data *q = qdisc_priv(sch);
  159. for (band = 0; band < q->bands; band++)
  160. qdisc_reset(q->queues[band]);
  161. sch->q.qlen = 0;
  162. q->curband = 0;
  163. }
  164. static void
  165. multiq_destroy(struct Qdisc *sch)
  166. {
  167. int band;
  168. struct multiq_sched_data *q = qdisc_priv(sch);
  169. tcf_destroy_chain(&q->filter_list);
  170. for (band = 0; band < q->bands; band++)
  171. qdisc_destroy(q->queues[band]);
  172. kfree(q->queues);
  173. }
  174. static int multiq_tune(struct Qdisc *sch, struct nlattr *opt)
  175. {
  176. struct multiq_sched_data *q = qdisc_priv(sch);
  177. struct tc_multiq_qopt *qopt;
  178. int i;
  179. if (!netif_is_multiqueue(qdisc_dev(sch)))
  180. return -EINVAL;
  181. if (nla_len(opt) < sizeof(*qopt))
  182. return -EINVAL;
  183. qopt = nla_data(opt);
  184. qopt->bands = qdisc_dev(sch)->real_num_tx_queues;
  185. sch_tree_lock(sch);
  186. q->bands = qopt->bands;
  187. for (i = q->bands; i < q->max_bands; i++) {
  188. if (q->queues[i] != &noop_qdisc) {
  189. struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
  190. qdisc_tree_decrease_qlen(child, child->q.qlen);
  191. qdisc_destroy(child);
  192. }
  193. }
  194. sch_tree_unlock(sch);
  195. for (i = 0; i < q->bands; i++) {
  196. if (q->queues[i] == &noop_qdisc) {
  197. struct Qdisc *child;
  198. child = qdisc_create_dflt(qdisc_dev(sch),
  199. sch->dev_queue,
  200. &pfifo_qdisc_ops,
  201. TC_H_MAKE(sch->handle,
  202. i + 1));
  203. if (child) {
  204. sch_tree_lock(sch);
  205. child = xchg(&q->queues[i], child);
  206. if (child != &noop_qdisc) {
  207. qdisc_tree_decrease_qlen(child,
  208. child->q.qlen);
  209. qdisc_destroy(child);
  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, err;
  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. err = multiq_tune(sch,opt);
  231. if (err)
  232. kfree(q->queues);
  233. return err;
  234. }
  235. static int multiq_dump(struct Qdisc *sch, struct sk_buff *skb)
  236. {
  237. struct multiq_sched_data *q = qdisc_priv(sch);
  238. unsigned char *b = skb_tail_pointer(skb);
  239. struct tc_multiq_qopt opt;
  240. opt.bands = q->bands;
  241. opt.max_bands = q->max_bands;
  242. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  243. return skb->len;
  244. nla_put_failure:
  245. nlmsg_trim(skb, b);
  246. return -1;
  247. }
  248. static int multiq_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  249. struct Qdisc **old)
  250. {
  251. struct multiq_sched_data *q = qdisc_priv(sch);
  252. unsigned long band = arg - 1;
  253. if (band >= q->bands)
  254. return -EINVAL;
  255. if (new == NULL)
  256. new = &noop_qdisc;
  257. sch_tree_lock(sch);
  258. *old = q->queues[band];
  259. q->queues[band] = new;
  260. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  261. qdisc_reset(*old);
  262. sch_tree_unlock(sch);
  263. return 0;
  264. }
  265. static struct Qdisc *
  266. multiq_leaf(struct Qdisc *sch, unsigned long arg)
  267. {
  268. struct multiq_sched_data *q = qdisc_priv(sch);
  269. unsigned long band = arg - 1;
  270. if (band >= q->bands)
  271. return NULL;
  272. return q->queues[band];
  273. }
  274. static unsigned long multiq_get(struct Qdisc *sch, u32 classid)
  275. {
  276. struct multiq_sched_data *q = qdisc_priv(sch);
  277. unsigned long band = TC_H_MIN(classid);
  278. if (band - 1 >= q->bands)
  279. return 0;
  280. return band;
  281. }
  282. static unsigned long multiq_bind(struct Qdisc *sch, unsigned long parent,
  283. u32 classid)
  284. {
  285. return multiq_get(sch, classid);
  286. }
  287. static void multiq_put(struct Qdisc *q, unsigned long cl)
  288. {
  289. return;
  290. }
  291. static int multiq_change(struct Qdisc *sch, u32 handle, u32 parent,
  292. struct nlattr **tca, unsigned long *arg)
  293. {
  294. unsigned long cl = *arg;
  295. struct multiq_sched_data *q = qdisc_priv(sch);
  296. if (cl - 1 > q->bands)
  297. return -ENOENT;
  298. return 0;
  299. }
  300. static int multiq_delete(struct Qdisc *sch, unsigned long cl)
  301. {
  302. struct multiq_sched_data *q = qdisc_priv(sch);
  303. if (cl - 1 > q->bands)
  304. return -ENOENT;
  305. return 0;
  306. }
  307. static int multiq_dump_class(struct Qdisc *sch, unsigned long cl,
  308. struct sk_buff *skb, struct tcmsg *tcm)
  309. {
  310. struct multiq_sched_data *q = qdisc_priv(sch);
  311. if (cl - 1 > q->bands)
  312. return -ENOENT;
  313. tcm->tcm_handle |= TC_H_MIN(cl);
  314. if (q->queues[cl-1])
  315. tcm->tcm_info = q->queues[cl-1]->handle;
  316. return 0;
  317. }
  318. static int multiq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  319. struct gnet_dump *d)
  320. {
  321. struct multiq_sched_data *q = qdisc_priv(sch);
  322. struct Qdisc *cl_q;
  323. cl_q = q->queues[cl - 1];
  324. if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
  325. gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
  326. return -1;
  327. return 0;
  328. }
  329. static void multiq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  330. {
  331. struct multiq_sched_data *q = qdisc_priv(sch);
  332. int band;
  333. if (arg->stop)
  334. return;
  335. for (band = 0; band < q->bands; band++) {
  336. if (arg->count < arg->skip) {
  337. arg->count++;
  338. continue;
  339. }
  340. if (arg->fn(sch, band+1, arg) < 0) {
  341. arg->stop = 1;
  342. break;
  343. }
  344. arg->count++;
  345. }
  346. }
  347. static struct tcf_proto **multiq_find_tcf(struct Qdisc *sch, unsigned long cl)
  348. {
  349. struct multiq_sched_data *q = qdisc_priv(sch);
  350. if (cl)
  351. return NULL;
  352. return &q->filter_list;
  353. }
  354. static const struct Qdisc_class_ops multiq_class_ops = {
  355. .graft = multiq_graft,
  356. .leaf = multiq_leaf,
  357. .get = multiq_get,
  358. .put = multiq_put,
  359. .change = multiq_change,
  360. .delete = multiq_delete,
  361. .walk = multiq_walk,
  362. .tcf_chain = multiq_find_tcf,
  363. .bind_tcf = multiq_bind,
  364. .unbind_tcf = multiq_put,
  365. .dump = multiq_dump_class,
  366. .dump_stats = multiq_dump_class_stats,
  367. };
  368. static struct Qdisc_ops multiq_qdisc_ops __read_mostly = {
  369. .next = NULL,
  370. .cl_ops = &multiq_class_ops,
  371. .id = "multiq",
  372. .priv_size = sizeof(struct multiq_sched_data),
  373. .enqueue = multiq_enqueue,
  374. .dequeue = multiq_dequeue,
  375. .requeue = multiq_requeue,
  376. .drop = multiq_drop,
  377. .init = multiq_init,
  378. .reset = multiq_reset,
  379. .destroy = multiq_destroy,
  380. .change = multiq_tune,
  381. .dump = multiq_dump,
  382. .owner = THIS_MODULE,
  383. };
  384. static int __init multiq_module_init(void)
  385. {
  386. return register_qdisc(&multiq_qdisc_ops);
  387. }
  388. static void __exit multiq_module_exit(void)
  389. {
  390. unregister_qdisc(&multiq_qdisc_ops);
  391. }
  392. module_init(multiq_module_init)
  393. module_exit(multiq_module_exit)
  394. MODULE_LICENSE("GPL");