sch_prio.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * net/sched/sch_prio.c Simple 3-band priority "scheduler".
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10. * Fixes: 19990609: J Hadi Salim <hadi@nortelnetworks.com>:
  11. * Init -- EINVAL when opt undefined
  12. */
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/errno.h>
  19. #include <linux/skbuff.h>
  20. #include <net/netlink.h>
  21. #include <net/pkt_sched.h>
  22. #include <net/pkt_cls.h>
  23. struct prio_sched_data {
  24. int bands;
  25. struct tcf_proto __rcu *filter_list;
  26. struct tcf_block *block;
  27. u8 prio2band[TC_PRIO_MAX+1];
  28. struct Qdisc *queues[TCQ_PRIO_BANDS];
  29. };
  30. static struct Qdisc *
  31. prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  32. {
  33. struct prio_sched_data *q = qdisc_priv(sch);
  34. u32 band = skb->priority;
  35. struct tcf_result res;
  36. struct tcf_proto *fl;
  37. int err;
  38. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  39. if (TC_H_MAJ(skb->priority) != sch->handle) {
  40. fl = rcu_dereference_bh(q->filter_list);
  41. err = tcf_classify(skb, fl, &res, false);
  42. #ifdef CONFIG_NET_CLS_ACT
  43. switch (err) {
  44. case TC_ACT_STOLEN:
  45. case TC_ACT_QUEUED:
  46. case TC_ACT_TRAP:
  47. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  48. case TC_ACT_SHOT:
  49. return NULL;
  50. }
  51. #endif
  52. if (!fl || err < 0) {
  53. if (TC_H_MAJ(band))
  54. band = 0;
  55. return q->queues[q->prio2band[band & TC_PRIO_MAX]];
  56. }
  57. band = res.classid;
  58. }
  59. band = TC_H_MIN(band) - 1;
  60. if (band >= q->bands)
  61. return q->queues[q->prio2band[0]];
  62. return q->queues[band];
  63. }
  64. static int
  65. prio_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
  66. {
  67. struct Qdisc *qdisc;
  68. int ret;
  69. qdisc = prio_classify(skb, sch, &ret);
  70. #ifdef CONFIG_NET_CLS_ACT
  71. if (qdisc == NULL) {
  72. if (ret & __NET_XMIT_BYPASS)
  73. qdisc_qstats_drop(sch);
  74. __qdisc_drop(skb, to_free);
  75. return ret;
  76. }
  77. #endif
  78. ret = qdisc_enqueue(skb, qdisc, to_free);
  79. if (ret == NET_XMIT_SUCCESS) {
  80. qdisc_qstats_backlog_inc(sch, skb);
  81. sch->q.qlen++;
  82. return NET_XMIT_SUCCESS;
  83. }
  84. if (net_xmit_drop_count(ret))
  85. qdisc_qstats_drop(sch);
  86. return ret;
  87. }
  88. static struct sk_buff *prio_peek(struct Qdisc *sch)
  89. {
  90. struct prio_sched_data *q = qdisc_priv(sch);
  91. int prio;
  92. for (prio = 0; prio < q->bands; prio++) {
  93. struct Qdisc *qdisc = q->queues[prio];
  94. struct sk_buff *skb = qdisc->ops->peek(qdisc);
  95. if (skb)
  96. return skb;
  97. }
  98. return NULL;
  99. }
  100. static struct sk_buff *prio_dequeue(struct Qdisc *sch)
  101. {
  102. struct prio_sched_data *q = qdisc_priv(sch);
  103. int prio;
  104. for (prio = 0; prio < q->bands; prio++) {
  105. struct Qdisc *qdisc = q->queues[prio];
  106. struct sk_buff *skb = qdisc_dequeue_peeked(qdisc);
  107. if (skb) {
  108. qdisc_bstats_update(sch, skb);
  109. qdisc_qstats_backlog_dec(sch, skb);
  110. sch->q.qlen--;
  111. return skb;
  112. }
  113. }
  114. return NULL;
  115. }
  116. static void
  117. prio_reset(struct Qdisc *sch)
  118. {
  119. int prio;
  120. struct prio_sched_data *q = qdisc_priv(sch);
  121. for (prio = 0; prio < q->bands; prio++)
  122. qdisc_reset(q->queues[prio]);
  123. sch->qstats.backlog = 0;
  124. sch->q.qlen = 0;
  125. }
  126. static void
  127. prio_destroy(struct Qdisc *sch)
  128. {
  129. int prio;
  130. struct prio_sched_data *q = qdisc_priv(sch);
  131. tcf_block_put(q->block);
  132. for (prio = 0; prio < q->bands; prio++)
  133. qdisc_destroy(q->queues[prio]);
  134. }
  135. static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
  136. {
  137. struct prio_sched_data *q = qdisc_priv(sch);
  138. struct Qdisc *queues[TCQ_PRIO_BANDS];
  139. int oldbands = q->bands, i;
  140. struct tc_prio_qopt *qopt;
  141. if (nla_len(opt) < sizeof(*qopt))
  142. return -EINVAL;
  143. qopt = nla_data(opt);
  144. if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
  145. return -EINVAL;
  146. for (i = 0; i <= TC_PRIO_MAX; i++) {
  147. if (qopt->priomap[i] >= qopt->bands)
  148. return -EINVAL;
  149. }
  150. /* Before commit, make sure we can allocate all new qdiscs */
  151. for (i = oldbands; i < qopt->bands; i++) {
  152. queues[i] = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  153. TC_H_MAKE(sch->handle, i + 1));
  154. if (!queues[i]) {
  155. while (i > oldbands)
  156. qdisc_destroy(queues[--i]);
  157. return -ENOMEM;
  158. }
  159. }
  160. sch_tree_lock(sch);
  161. q->bands = qopt->bands;
  162. memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
  163. for (i = q->bands; i < oldbands; i++) {
  164. struct Qdisc *child = q->queues[i];
  165. qdisc_tree_reduce_backlog(child, child->q.qlen,
  166. child->qstats.backlog);
  167. qdisc_destroy(child);
  168. }
  169. for (i = oldbands; i < q->bands; i++) {
  170. q->queues[i] = queues[i];
  171. if (q->queues[i] != &noop_qdisc)
  172. qdisc_hash_add(q->queues[i], true);
  173. }
  174. sch_tree_unlock(sch);
  175. return 0;
  176. }
  177. static int prio_init(struct Qdisc *sch, struct nlattr *opt)
  178. {
  179. struct prio_sched_data *q = qdisc_priv(sch);
  180. int err;
  181. if (!opt)
  182. return -EINVAL;
  183. err = tcf_block_get(&q->block, &q->filter_list);
  184. if (err)
  185. return err;
  186. return prio_tune(sch, opt);
  187. }
  188. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  189. {
  190. struct prio_sched_data *q = qdisc_priv(sch);
  191. unsigned char *b = skb_tail_pointer(skb);
  192. struct tc_prio_qopt opt;
  193. opt.bands = q->bands;
  194. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1);
  195. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  196. goto nla_put_failure;
  197. return skb->len;
  198. nla_put_failure:
  199. nlmsg_trim(skb, b);
  200. return -1;
  201. }
  202. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  203. struct Qdisc **old)
  204. {
  205. struct prio_sched_data *q = qdisc_priv(sch);
  206. unsigned long band = arg - 1;
  207. if (!new) {
  208. new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  209. TC_H_MAKE(sch->handle, arg));
  210. if (!new)
  211. new = &noop_qdisc;
  212. else
  213. qdisc_hash_add(new, true);
  214. }
  215. *old = qdisc_replace(sch, new, &q->queues[band]);
  216. return 0;
  217. }
  218. static struct Qdisc *
  219. prio_leaf(struct Qdisc *sch, unsigned long arg)
  220. {
  221. struct prio_sched_data *q = qdisc_priv(sch);
  222. unsigned long band = arg - 1;
  223. return q->queues[band];
  224. }
  225. static unsigned long prio_find(struct Qdisc *sch, u32 classid)
  226. {
  227. struct prio_sched_data *q = qdisc_priv(sch);
  228. unsigned long band = TC_H_MIN(classid);
  229. if (band - 1 >= q->bands)
  230. return 0;
  231. return band;
  232. }
  233. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  234. {
  235. return prio_find(sch, classid);
  236. }
  237. static void prio_unbind(struct Qdisc *q, unsigned long cl)
  238. {
  239. }
  240. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  241. struct tcmsg *tcm)
  242. {
  243. struct prio_sched_data *q = qdisc_priv(sch);
  244. tcm->tcm_handle |= TC_H_MIN(cl);
  245. tcm->tcm_info = q->queues[cl-1]->handle;
  246. return 0;
  247. }
  248. static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  249. struct gnet_dump *d)
  250. {
  251. struct prio_sched_data *q = qdisc_priv(sch);
  252. struct Qdisc *cl_q;
  253. cl_q = q->queues[cl - 1];
  254. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
  255. d, cl_q->cpu_bstats, &cl_q->bstats) < 0 ||
  256. gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0)
  257. return -1;
  258. return 0;
  259. }
  260. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  261. {
  262. struct prio_sched_data *q = qdisc_priv(sch);
  263. int prio;
  264. if (arg->stop)
  265. return;
  266. for (prio = 0; prio < q->bands; prio++) {
  267. if (arg->count < arg->skip) {
  268. arg->count++;
  269. continue;
  270. }
  271. if (arg->fn(sch, prio + 1, arg) < 0) {
  272. arg->stop = 1;
  273. break;
  274. }
  275. arg->count++;
  276. }
  277. }
  278. static struct tcf_block *prio_tcf_block(struct Qdisc *sch, unsigned long cl)
  279. {
  280. struct prio_sched_data *q = qdisc_priv(sch);
  281. if (cl)
  282. return NULL;
  283. return q->block;
  284. }
  285. static const struct Qdisc_class_ops prio_class_ops = {
  286. .graft = prio_graft,
  287. .leaf = prio_leaf,
  288. .find = prio_find,
  289. .walk = prio_walk,
  290. .tcf_block = prio_tcf_block,
  291. .bind_tcf = prio_bind,
  292. .unbind_tcf = prio_unbind,
  293. .dump = prio_dump_class,
  294. .dump_stats = prio_dump_class_stats,
  295. };
  296. static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
  297. .next = NULL,
  298. .cl_ops = &prio_class_ops,
  299. .id = "prio",
  300. .priv_size = sizeof(struct prio_sched_data),
  301. .enqueue = prio_enqueue,
  302. .dequeue = prio_dequeue,
  303. .peek = prio_peek,
  304. .init = prio_init,
  305. .reset = prio_reset,
  306. .destroy = prio_destroy,
  307. .change = prio_tune,
  308. .dump = prio_dump,
  309. .owner = THIS_MODULE,
  310. };
  311. static int __init prio_module_init(void)
  312. {
  313. return register_qdisc(&prio_qdisc_ops);
  314. }
  315. static void __exit prio_module_exit(void)
  316. {
  317. unregister_qdisc(&prio_qdisc_ops);
  318. }
  319. module_init(prio_module_init)
  320. module_exit(prio_module_exit)
  321. MODULE_LICENSE("GPL");