sch_multiq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 struct sk_buff *multiq_peek(struct Qdisc *sch)
  137. {
  138. struct multiq_sched_data *q = qdisc_priv(sch);
  139. unsigned int curband = q->curband;
  140. struct Qdisc *qdisc;
  141. struct sk_buff *skb;
  142. int band;
  143. for (band = 0; band < q->bands; band++) {
  144. /* cycle through bands to ensure fairness */
  145. curband++;
  146. if (curband >= q->bands)
  147. curband = 0;
  148. /* Check that target subqueue is available before
  149. * pulling an skb to avoid excessive requeues
  150. */
  151. if (!__netif_subqueue_stopped(qdisc_dev(sch), curband)) {
  152. qdisc = q->queues[curband];
  153. skb = qdisc->ops->peek(qdisc);
  154. if (skb)
  155. return skb;
  156. }
  157. }
  158. return NULL;
  159. }
  160. static unsigned int multiq_drop(struct Qdisc *sch)
  161. {
  162. struct multiq_sched_data *q = qdisc_priv(sch);
  163. int band;
  164. unsigned int len;
  165. struct Qdisc *qdisc;
  166. for (band = q->bands-1; band >= 0; band--) {
  167. qdisc = q->queues[band];
  168. if (qdisc->ops->drop) {
  169. len = qdisc->ops->drop(qdisc);
  170. if (len != 0) {
  171. sch->q.qlen--;
  172. return len;
  173. }
  174. }
  175. }
  176. return 0;
  177. }
  178. static void
  179. multiq_reset(struct Qdisc *sch)
  180. {
  181. u16 band;
  182. struct multiq_sched_data *q = qdisc_priv(sch);
  183. for (band = 0; band < q->bands; band++)
  184. qdisc_reset(q->queues[band]);
  185. sch->q.qlen = 0;
  186. q->curband = 0;
  187. }
  188. static void
  189. multiq_destroy(struct Qdisc *sch)
  190. {
  191. int band;
  192. struct multiq_sched_data *q = qdisc_priv(sch);
  193. tcf_destroy_chain(&q->filter_list);
  194. for (band = 0; band < q->bands; band++)
  195. qdisc_destroy(q->queues[band]);
  196. kfree(q->queues);
  197. }
  198. static int multiq_tune(struct Qdisc *sch, struct nlattr *opt)
  199. {
  200. struct multiq_sched_data *q = qdisc_priv(sch);
  201. struct tc_multiq_qopt *qopt;
  202. int i;
  203. if (!netif_is_multiqueue(qdisc_dev(sch)))
  204. return -EINVAL;
  205. if (nla_len(opt) < sizeof(*qopt))
  206. return -EINVAL;
  207. qopt = nla_data(opt);
  208. qopt->bands = qdisc_dev(sch)->real_num_tx_queues;
  209. sch_tree_lock(sch);
  210. q->bands = qopt->bands;
  211. for (i = q->bands; i < q->max_bands; i++) {
  212. if (q->queues[i] != &noop_qdisc) {
  213. struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
  214. qdisc_tree_decrease_qlen(child, child->q.qlen);
  215. qdisc_destroy(child);
  216. }
  217. }
  218. sch_tree_unlock(sch);
  219. for (i = 0; i < q->bands; i++) {
  220. if (q->queues[i] == &noop_qdisc) {
  221. struct Qdisc *child;
  222. child = qdisc_create_dflt(qdisc_dev(sch),
  223. sch->dev_queue,
  224. &pfifo_qdisc_ops,
  225. TC_H_MAKE(sch->handle,
  226. i + 1));
  227. if (child) {
  228. sch_tree_lock(sch);
  229. child = xchg(&q->queues[i], child);
  230. if (child != &noop_qdisc) {
  231. qdisc_tree_decrease_qlen(child,
  232. child->q.qlen);
  233. qdisc_destroy(child);
  234. }
  235. sch_tree_unlock(sch);
  236. }
  237. }
  238. }
  239. return 0;
  240. }
  241. static int multiq_init(struct Qdisc *sch, struct nlattr *opt)
  242. {
  243. struct multiq_sched_data *q = qdisc_priv(sch);
  244. int i, err;
  245. q->queues = NULL;
  246. if (opt == NULL)
  247. return -EINVAL;
  248. q->max_bands = qdisc_dev(sch)->num_tx_queues;
  249. q->queues = kcalloc(q->max_bands, sizeof(struct Qdisc *), GFP_KERNEL);
  250. if (!q->queues)
  251. return -ENOBUFS;
  252. for (i = 0; i < q->max_bands; i++)
  253. q->queues[i] = &noop_qdisc;
  254. err = multiq_tune(sch,opt);
  255. if (err)
  256. kfree(q->queues);
  257. return err;
  258. }
  259. static int multiq_dump(struct Qdisc *sch, struct sk_buff *skb)
  260. {
  261. struct multiq_sched_data *q = qdisc_priv(sch);
  262. unsigned char *b = skb_tail_pointer(skb);
  263. struct tc_multiq_qopt opt;
  264. opt.bands = q->bands;
  265. opt.max_bands = q->max_bands;
  266. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  267. return skb->len;
  268. nla_put_failure:
  269. nlmsg_trim(skb, b);
  270. return -1;
  271. }
  272. static int multiq_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  273. struct Qdisc **old)
  274. {
  275. struct multiq_sched_data *q = qdisc_priv(sch);
  276. unsigned long band = arg - 1;
  277. if (band >= q->bands)
  278. return -EINVAL;
  279. if (new == NULL)
  280. new = &noop_qdisc;
  281. sch_tree_lock(sch);
  282. *old = q->queues[band];
  283. q->queues[band] = new;
  284. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  285. qdisc_reset(*old);
  286. sch_tree_unlock(sch);
  287. return 0;
  288. }
  289. static struct Qdisc *
  290. multiq_leaf(struct Qdisc *sch, unsigned long arg)
  291. {
  292. struct multiq_sched_data *q = qdisc_priv(sch);
  293. unsigned long band = arg - 1;
  294. if (band >= q->bands)
  295. return NULL;
  296. return q->queues[band];
  297. }
  298. static unsigned long multiq_get(struct Qdisc *sch, u32 classid)
  299. {
  300. struct multiq_sched_data *q = qdisc_priv(sch);
  301. unsigned long band = TC_H_MIN(classid);
  302. if (band - 1 >= q->bands)
  303. return 0;
  304. return band;
  305. }
  306. static unsigned long multiq_bind(struct Qdisc *sch, unsigned long parent,
  307. u32 classid)
  308. {
  309. return multiq_get(sch, classid);
  310. }
  311. static void multiq_put(struct Qdisc *q, unsigned long cl)
  312. {
  313. return;
  314. }
  315. static int multiq_change(struct Qdisc *sch, u32 handle, u32 parent,
  316. struct nlattr **tca, unsigned long *arg)
  317. {
  318. unsigned long cl = *arg;
  319. struct multiq_sched_data *q = qdisc_priv(sch);
  320. if (cl - 1 > q->bands)
  321. return -ENOENT;
  322. return 0;
  323. }
  324. static int multiq_delete(struct Qdisc *sch, unsigned long cl)
  325. {
  326. struct multiq_sched_data *q = qdisc_priv(sch);
  327. if (cl - 1 > q->bands)
  328. return -ENOENT;
  329. return 0;
  330. }
  331. static int multiq_dump_class(struct Qdisc *sch, unsigned long cl,
  332. struct sk_buff *skb, struct tcmsg *tcm)
  333. {
  334. struct multiq_sched_data *q = qdisc_priv(sch);
  335. if (cl - 1 > q->bands)
  336. return -ENOENT;
  337. tcm->tcm_handle |= TC_H_MIN(cl);
  338. if (q->queues[cl-1])
  339. tcm->tcm_info = q->queues[cl-1]->handle;
  340. return 0;
  341. }
  342. static int multiq_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  343. struct gnet_dump *d)
  344. {
  345. struct multiq_sched_data *q = qdisc_priv(sch);
  346. struct Qdisc *cl_q;
  347. cl_q = q->queues[cl - 1];
  348. if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
  349. gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
  350. return -1;
  351. return 0;
  352. }
  353. static void multiq_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  354. {
  355. struct multiq_sched_data *q = qdisc_priv(sch);
  356. int band;
  357. if (arg->stop)
  358. return;
  359. for (band = 0; band < q->bands; band++) {
  360. if (arg->count < arg->skip) {
  361. arg->count++;
  362. continue;
  363. }
  364. if (arg->fn(sch, band+1, arg) < 0) {
  365. arg->stop = 1;
  366. break;
  367. }
  368. arg->count++;
  369. }
  370. }
  371. static struct tcf_proto **multiq_find_tcf(struct Qdisc *sch, unsigned long cl)
  372. {
  373. struct multiq_sched_data *q = qdisc_priv(sch);
  374. if (cl)
  375. return NULL;
  376. return &q->filter_list;
  377. }
  378. static const struct Qdisc_class_ops multiq_class_ops = {
  379. .graft = multiq_graft,
  380. .leaf = multiq_leaf,
  381. .get = multiq_get,
  382. .put = multiq_put,
  383. .change = multiq_change,
  384. .delete = multiq_delete,
  385. .walk = multiq_walk,
  386. .tcf_chain = multiq_find_tcf,
  387. .bind_tcf = multiq_bind,
  388. .unbind_tcf = multiq_put,
  389. .dump = multiq_dump_class,
  390. .dump_stats = multiq_dump_class_stats,
  391. };
  392. static struct Qdisc_ops multiq_qdisc_ops __read_mostly = {
  393. .next = NULL,
  394. .cl_ops = &multiq_class_ops,
  395. .id = "multiq",
  396. .priv_size = sizeof(struct multiq_sched_data),
  397. .enqueue = multiq_enqueue,
  398. .dequeue = multiq_dequeue,
  399. .peek = multiq_peek,
  400. .requeue = multiq_requeue,
  401. .drop = multiq_drop,
  402. .init = multiq_init,
  403. .reset = multiq_reset,
  404. .destroy = multiq_destroy,
  405. .change = multiq_tune,
  406. .dump = multiq_dump,
  407. .owner = THIS_MODULE,
  408. };
  409. static int __init multiq_module_init(void)
  410. {
  411. return register_qdisc(&multiq_qdisc_ops);
  412. }
  413. static void __exit multiq_module_exit(void)
  414. {
  415. unregister_qdisc(&multiq_qdisc_ops);
  416. }
  417. module_init(multiq_module_init)
  418. module_exit(multiq_module_exit)
  419. MODULE_LICENSE("GPL");