sch_prio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. struct prio_sched_data {
  23. int bands;
  24. struct tcf_proto __rcu *filter_list;
  25. u8 prio2band[TC_PRIO_MAX+1];
  26. struct Qdisc *queues[TCQ_PRIO_BANDS];
  27. };
  28. static struct Qdisc *
  29. prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  30. {
  31. struct prio_sched_data *q = qdisc_priv(sch);
  32. u32 band = skb->priority;
  33. struct tcf_result res;
  34. struct tcf_proto *fl;
  35. int err;
  36. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  37. if (TC_H_MAJ(skb->priority) != sch->handle) {
  38. fl = rcu_dereference_bh(q->filter_list);
  39. err = tc_classify(skb, fl, &res, false);
  40. #ifdef CONFIG_NET_CLS_ACT
  41. switch (err) {
  42. case TC_ACT_STOLEN:
  43. case TC_ACT_QUEUED:
  44. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  45. case TC_ACT_SHOT:
  46. return NULL;
  47. }
  48. #endif
  49. if (!fl || err < 0) {
  50. if (TC_H_MAJ(band))
  51. band = 0;
  52. return q->queues[q->prio2band[band & TC_PRIO_MAX]];
  53. }
  54. band = res.classid;
  55. }
  56. band = TC_H_MIN(band) - 1;
  57. if (band >= q->bands)
  58. return q->queues[q->prio2band[0]];
  59. return q->queues[band];
  60. }
  61. static int
  62. prio_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
  63. {
  64. struct Qdisc *qdisc;
  65. int ret;
  66. qdisc = prio_classify(skb, sch, &ret);
  67. #ifdef CONFIG_NET_CLS_ACT
  68. if (qdisc == NULL) {
  69. if (ret & __NET_XMIT_BYPASS)
  70. qdisc_qstats_drop(sch);
  71. kfree_skb(skb);
  72. return ret;
  73. }
  74. #endif
  75. ret = qdisc_enqueue(skb, qdisc, to_free);
  76. if (ret == NET_XMIT_SUCCESS) {
  77. qdisc_qstats_backlog_inc(sch, skb);
  78. sch->q.qlen++;
  79. return NET_XMIT_SUCCESS;
  80. }
  81. if (net_xmit_drop_count(ret))
  82. qdisc_qstats_drop(sch);
  83. return ret;
  84. }
  85. static struct sk_buff *prio_peek(struct Qdisc *sch)
  86. {
  87. struct prio_sched_data *q = qdisc_priv(sch);
  88. int prio;
  89. for (prio = 0; prio < q->bands; prio++) {
  90. struct Qdisc *qdisc = q->queues[prio];
  91. struct sk_buff *skb = qdisc->ops->peek(qdisc);
  92. if (skb)
  93. return skb;
  94. }
  95. return NULL;
  96. }
  97. static struct sk_buff *prio_dequeue(struct Qdisc *sch)
  98. {
  99. struct prio_sched_data *q = qdisc_priv(sch);
  100. int prio;
  101. for (prio = 0; prio < q->bands; prio++) {
  102. struct Qdisc *qdisc = q->queues[prio];
  103. struct sk_buff *skb = qdisc_dequeue_peeked(qdisc);
  104. if (skb) {
  105. qdisc_bstats_update(sch, skb);
  106. qdisc_qstats_backlog_dec(sch, skb);
  107. sch->q.qlen--;
  108. return skb;
  109. }
  110. }
  111. return NULL;
  112. }
  113. static void
  114. prio_reset(struct Qdisc *sch)
  115. {
  116. int prio;
  117. struct prio_sched_data *q = qdisc_priv(sch);
  118. for (prio = 0; prio < q->bands; prio++)
  119. qdisc_reset(q->queues[prio]);
  120. sch->qstats.backlog = 0;
  121. sch->q.qlen = 0;
  122. }
  123. static void
  124. prio_destroy(struct Qdisc *sch)
  125. {
  126. int prio;
  127. struct prio_sched_data *q = qdisc_priv(sch);
  128. tcf_destroy_chain(&q->filter_list);
  129. for (prio = 0; prio < q->bands; prio++)
  130. qdisc_destroy(q->queues[prio]);
  131. }
  132. static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
  133. {
  134. struct prio_sched_data *q = qdisc_priv(sch);
  135. struct Qdisc *queues[TCQ_PRIO_BANDS];
  136. int oldbands = q->bands, i;
  137. struct tc_prio_qopt *qopt;
  138. if (nla_len(opt) < sizeof(*qopt))
  139. return -EINVAL;
  140. qopt = nla_data(opt);
  141. if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
  142. return -EINVAL;
  143. for (i = 0; i <= TC_PRIO_MAX; i++) {
  144. if (qopt->priomap[i] >= qopt->bands)
  145. return -EINVAL;
  146. }
  147. /* Before commit, make sure we can allocate all new qdiscs */
  148. for (i = oldbands; i < qopt->bands; i++) {
  149. queues[i] = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  150. TC_H_MAKE(sch->handle, i + 1));
  151. if (!queues[i]) {
  152. while (i > oldbands)
  153. qdisc_destroy(queues[--i]);
  154. return -ENOMEM;
  155. }
  156. }
  157. sch_tree_lock(sch);
  158. q->bands = qopt->bands;
  159. memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
  160. for (i = q->bands; i < oldbands; i++) {
  161. struct Qdisc *child = q->queues[i];
  162. qdisc_tree_reduce_backlog(child, child->q.qlen,
  163. child->qstats.backlog);
  164. qdisc_destroy(child);
  165. }
  166. for (i = oldbands; i < q->bands; i++)
  167. q->queues[i] = queues[i];
  168. sch_tree_unlock(sch);
  169. return 0;
  170. }
  171. static int prio_init(struct Qdisc *sch, struct nlattr *opt)
  172. {
  173. if (!opt)
  174. return -EINVAL;
  175. return prio_tune(sch, opt);
  176. }
  177. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  178. {
  179. struct prio_sched_data *q = qdisc_priv(sch);
  180. unsigned char *b = skb_tail_pointer(skb);
  181. struct tc_prio_qopt opt;
  182. opt.bands = q->bands;
  183. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1);
  184. if (nla_put(skb, TCA_OPTIONS, sizeof(opt), &opt))
  185. goto nla_put_failure;
  186. return skb->len;
  187. nla_put_failure:
  188. nlmsg_trim(skb, b);
  189. return -1;
  190. }
  191. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  192. struct Qdisc **old)
  193. {
  194. struct prio_sched_data *q = qdisc_priv(sch);
  195. unsigned long band = arg - 1;
  196. if (new == NULL)
  197. new = &noop_qdisc;
  198. *old = qdisc_replace(sch, new, &q->queues[band]);
  199. return 0;
  200. }
  201. static struct Qdisc *
  202. prio_leaf(struct Qdisc *sch, unsigned long arg)
  203. {
  204. struct prio_sched_data *q = qdisc_priv(sch);
  205. unsigned long band = arg - 1;
  206. return q->queues[band];
  207. }
  208. static unsigned long prio_get(struct Qdisc *sch, u32 classid)
  209. {
  210. struct prio_sched_data *q = qdisc_priv(sch);
  211. unsigned long band = TC_H_MIN(classid);
  212. if (band - 1 >= q->bands)
  213. return 0;
  214. return band;
  215. }
  216. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  217. {
  218. return prio_get(sch, classid);
  219. }
  220. static void prio_put(struct Qdisc *q, unsigned long cl)
  221. {
  222. }
  223. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  224. struct tcmsg *tcm)
  225. {
  226. struct prio_sched_data *q = qdisc_priv(sch);
  227. tcm->tcm_handle |= TC_H_MIN(cl);
  228. tcm->tcm_info = q->queues[cl-1]->handle;
  229. return 0;
  230. }
  231. static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  232. struct gnet_dump *d)
  233. {
  234. struct prio_sched_data *q = qdisc_priv(sch);
  235. struct Qdisc *cl_q;
  236. cl_q = q->queues[cl - 1];
  237. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
  238. d, NULL, &cl_q->bstats) < 0 ||
  239. gnet_stats_copy_queue(d, NULL, &cl_q->qstats, cl_q->q.qlen) < 0)
  240. return -1;
  241. return 0;
  242. }
  243. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  244. {
  245. struct prio_sched_data *q = qdisc_priv(sch);
  246. int prio;
  247. if (arg->stop)
  248. return;
  249. for (prio = 0; prio < q->bands; prio++) {
  250. if (arg->count < arg->skip) {
  251. arg->count++;
  252. continue;
  253. }
  254. if (arg->fn(sch, prio + 1, arg) < 0) {
  255. arg->stop = 1;
  256. break;
  257. }
  258. arg->count++;
  259. }
  260. }
  261. static struct tcf_proto __rcu **prio_find_tcf(struct Qdisc *sch,
  262. unsigned long cl)
  263. {
  264. struct prio_sched_data *q = qdisc_priv(sch);
  265. if (cl)
  266. return NULL;
  267. return &q->filter_list;
  268. }
  269. static const struct Qdisc_class_ops prio_class_ops = {
  270. .graft = prio_graft,
  271. .leaf = prio_leaf,
  272. .get = prio_get,
  273. .put = prio_put,
  274. .walk = prio_walk,
  275. .tcf_chain = prio_find_tcf,
  276. .bind_tcf = prio_bind,
  277. .unbind_tcf = prio_put,
  278. .dump = prio_dump_class,
  279. .dump_stats = prio_dump_class_stats,
  280. };
  281. static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
  282. .next = NULL,
  283. .cl_ops = &prio_class_ops,
  284. .id = "prio",
  285. .priv_size = sizeof(struct prio_sched_data),
  286. .enqueue = prio_enqueue,
  287. .dequeue = prio_dequeue,
  288. .peek = prio_peek,
  289. .init = prio_init,
  290. .reset = prio_reset,
  291. .destroy = prio_destroy,
  292. .change = prio_tune,
  293. .dump = prio_dump,
  294. .owner = THIS_MODULE,
  295. };
  296. static int __init prio_module_init(void)
  297. {
  298. return register_qdisc(&prio_qdisc_ops);
  299. }
  300. static void __exit prio_module_exit(void)
  301. {
  302. unregister_qdisc(&prio_qdisc_ops);
  303. }
  304. module_init(prio_module_init)
  305. module_exit(prio_module_exit)
  306. MODULE_LICENSE("GPL");