sch_prio.c 8.3 KB

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