sch_prio.c 8.3 KB

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