sch_prio.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 <asm/uaccess.h>
  15. #include <asm/system.h>
  16. #include <linux/bitops.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/sched.h>
  20. #include <linux/string.h>
  21. #include <linux/mm.h>
  22. #include <linux/socket.h>
  23. #include <linux/sockios.h>
  24. #include <linux/in.h>
  25. #include <linux/errno.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/inet.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/notifier.h>
  32. #include <net/ip.h>
  33. #include <net/route.h>
  34. #include <linux/skbuff.h>
  35. #include <net/sock.h>
  36. #include <net/pkt_sched.h>
  37. struct prio_sched_data
  38. {
  39. int bands;
  40. struct tcf_proto *filter_list;
  41. u8 prio2band[TC_PRIO_MAX+1];
  42. struct Qdisc *queues[TCQ_PRIO_BANDS];
  43. };
  44. static struct Qdisc *
  45. prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  46. {
  47. struct prio_sched_data *q = qdisc_priv(sch);
  48. u32 band = skb->priority;
  49. struct tcf_result res;
  50. *qerr = NET_XMIT_BYPASS;
  51. if (TC_H_MAJ(skb->priority) != sch->handle) {
  52. #ifdef CONFIG_NET_CLS_ACT
  53. switch (tc_classify(skb, q->filter_list, &res)) {
  54. case TC_ACT_STOLEN:
  55. case TC_ACT_QUEUED:
  56. *qerr = NET_XMIT_SUCCESS;
  57. case TC_ACT_SHOT:
  58. return NULL;
  59. };
  60. if (!q->filter_list ) {
  61. #else
  62. if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
  63. #endif
  64. if (TC_H_MAJ(band))
  65. band = 0;
  66. return q->queues[q->prio2band[band&TC_PRIO_MAX]];
  67. }
  68. band = res.classid;
  69. }
  70. band = TC_H_MIN(band) - 1;
  71. if (band > q->bands)
  72. return q->queues[q->prio2band[0]];
  73. return q->queues[band];
  74. }
  75. static int
  76. prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  77. {
  78. struct Qdisc *qdisc;
  79. int ret;
  80. qdisc = prio_classify(skb, sch, &ret);
  81. #ifdef CONFIG_NET_CLS_ACT
  82. if (qdisc == NULL) {
  83. if (ret == NET_XMIT_BYPASS)
  84. sch->qstats.drops++;
  85. kfree_skb(skb);
  86. return ret;
  87. }
  88. #endif
  89. if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
  90. sch->bstats.bytes += skb->len;
  91. sch->bstats.packets++;
  92. sch->q.qlen++;
  93. return NET_XMIT_SUCCESS;
  94. }
  95. sch->qstats.drops++;
  96. return ret;
  97. }
  98. static int
  99. prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
  100. {
  101. struct Qdisc *qdisc;
  102. int ret;
  103. qdisc = prio_classify(skb, sch, &ret);
  104. #ifdef CONFIG_NET_CLS_ACT
  105. if (qdisc == NULL) {
  106. if (ret == NET_XMIT_BYPASS)
  107. sch->qstats.drops++;
  108. kfree_skb(skb);
  109. return ret;
  110. }
  111. #endif
  112. if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
  113. sch->q.qlen++;
  114. sch->qstats.requeues++;
  115. return 0;
  116. }
  117. sch->qstats.drops++;
  118. return NET_XMIT_DROP;
  119. }
  120. static struct sk_buff *
  121. prio_dequeue(struct Qdisc* sch)
  122. {
  123. struct sk_buff *skb;
  124. struct prio_sched_data *q = qdisc_priv(sch);
  125. int prio;
  126. struct Qdisc *qdisc;
  127. for (prio = 0; prio < q->bands; prio++) {
  128. qdisc = q->queues[prio];
  129. skb = qdisc->dequeue(qdisc);
  130. if (skb) {
  131. sch->q.qlen--;
  132. return skb;
  133. }
  134. }
  135. return NULL;
  136. }
  137. static unsigned int prio_drop(struct Qdisc* sch)
  138. {
  139. struct prio_sched_data *q = qdisc_priv(sch);
  140. int prio;
  141. unsigned int len;
  142. struct Qdisc *qdisc;
  143. for (prio = q->bands-1; prio >= 0; prio--) {
  144. qdisc = q->queues[prio];
  145. if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
  146. sch->q.qlen--;
  147. return len;
  148. }
  149. }
  150. return 0;
  151. }
  152. static void
  153. prio_reset(struct Qdisc* sch)
  154. {
  155. int prio;
  156. struct prio_sched_data *q = qdisc_priv(sch);
  157. for (prio=0; prio<q->bands; prio++)
  158. qdisc_reset(q->queues[prio]);
  159. sch->q.qlen = 0;
  160. }
  161. static void
  162. prio_destroy(struct Qdisc* sch)
  163. {
  164. int prio;
  165. struct prio_sched_data *q = qdisc_priv(sch);
  166. struct tcf_proto *tp;
  167. while ((tp = q->filter_list) != NULL) {
  168. q->filter_list = tp->next;
  169. tcf_destroy(tp);
  170. }
  171. for (prio=0; prio<q->bands; prio++)
  172. qdisc_destroy(q->queues[prio]);
  173. }
  174. static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
  175. {
  176. struct prio_sched_data *q = qdisc_priv(sch);
  177. struct tc_prio_qopt *qopt = RTA_DATA(opt);
  178. int i;
  179. if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
  180. return -EINVAL;
  181. if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
  182. return -EINVAL;
  183. for (i=0; i<=TC_PRIO_MAX; i++) {
  184. if (qopt->priomap[i] >= qopt->bands)
  185. return -EINVAL;
  186. }
  187. sch_tree_lock(sch);
  188. q->bands = qopt->bands;
  189. memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
  190. for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
  191. struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
  192. if (child != &noop_qdisc)
  193. qdisc_destroy(child);
  194. }
  195. sch_tree_unlock(sch);
  196. for (i=0; i<q->bands; i++) {
  197. if (q->queues[i] == &noop_qdisc) {
  198. struct Qdisc *child;
  199. child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
  200. if (child) {
  201. sch_tree_lock(sch);
  202. child = xchg(&q->queues[i], child);
  203. if (child != &noop_qdisc)
  204. qdisc_destroy(child);
  205. sch_tree_unlock(sch);
  206. }
  207. }
  208. }
  209. return 0;
  210. }
  211. static int prio_init(struct Qdisc *sch, struct rtattr *opt)
  212. {
  213. struct prio_sched_data *q = qdisc_priv(sch);
  214. int i;
  215. for (i=0; i<TCQ_PRIO_BANDS; i++)
  216. q->queues[i] = &noop_qdisc;
  217. if (opt == NULL) {
  218. return -EINVAL;
  219. } else {
  220. int err;
  221. if ((err= prio_tune(sch, opt)) != 0)
  222. return err;
  223. }
  224. return 0;
  225. }
  226. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  227. {
  228. struct prio_sched_data *q = qdisc_priv(sch);
  229. unsigned char *b = skb->tail;
  230. struct tc_prio_qopt opt;
  231. opt.bands = q->bands;
  232. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
  233. RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  234. return skb->len;
  235. rtattr_failure:
  236. skb_trim(skb, b - skb->data);
  237. return -1;
  238. }
  239. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  240. struct Qdisc **old)
  241. {
  242. struct prio_sched_data *q = qdisc_priv(sch);
  243. unsigned long band = arg - 1;
  244. if (band >= q->bands)
  245. return -EINVAL;
  246. if (new == NULL)
  247. new = &noop_qdisc;
  248. sch_tree_lock(sch);
  249. *old = q->queues[band];
  250. q->queues[band] = new;
  251. sch->q.qlen -= (*old)->q.qlen;
  252. qdisc_reset(*old);
  253. sch_tree_unlock(sch);
  254. return 0;
  255. }
  256. static struct Qdisc *
  257. prio_leaf(struct Qdisc *sch, unsigned long arg)
  258. {
  259. struct prio_sched_data *q = qdisc_priv(sch);
  260. unsigned long band = arg - 1;
  261. if (band >= q->bands)
  262. return NULL;
  263. return q->queues[band];
  264. }
  265. static unsigned long prio_get(struct Qdisc *sch, u32 classid)
  266. {
  267. struct prio_sched_data *q = qdisc_priv(sch);
  268. unsigned long band = TC_H_MIN(classid);
  269. if (band - 1 >= q->bands)
  270. return 0;
  271. return band;
  272. }
  273. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  274. {
  275. return prio_get(sch, classid);
  276. }
  277. static void prio_put(struct Qdisc *q, unsigned long cl)
  278. {
  279. return;
  280. }
  281. static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
  282. {
  283. unsigned long cl = *arg;
  284. struct prio_sched_data *q = qdisc_priv(sch);
  285. if (cl - 1 > q->bands)
  286. return -ENOENT;
  287. return 0;
  288. }
  289. static int prio_delete(struct Qdisc *sch, unsigned long cl)
  290. {
  291. struct prio_sched_data *q = qdisc_priv(sch);
  292. if (cl - 1 > q->bands)
  293. return -ENOENT;
  294. return 0;
  295. }
  296. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  297. struct tcmsg *tcm)
  298. {
  299. struct prio_sched_data *q = qdisc_priv(sch);
  300. if (cl - 1 > q->bands)
  301. return -ENOENT;
  302. tcm->tcm_handle |= TC_H_MIN(cl);
  303. if (q->queues[cl-1])
  304. tcm->tcm_info = q->queues[cl-1]->handle;
  305. return 0;
  306. }
  307. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  308. {
  309. struct prio_sched_data *q = qdisc_priv(sch);
  310. int prio;
  311. if (arg->stop)
  312. return;
  313. for (prio = 0; prio < q->bands; prio++) {
  314. if (arg->count < arg->skip) {
  315. arg->count++;
  316. continue;
  317. }
  318. if (arg->fn(sch, prio+1, arg) < 0) {
  319. arg->stop = 1;
  320. break;
  321. }
  322. arg->count++;
  323. }
  324. }
  325. static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
  326. {
  327. struct prio_sched_data *q = qdisc_priv(sch);
  328. if (cl)
  329. return NULL;
  330. return &q->filter_list;
  331. }
  332. static struct Qdisc_class_ops prio_class_ops = {
  333. .graft = prio_graft,
  334. .leaf = prio_leaf,
  335. .get = prio_get,
  336. .put = prio_put,
  337. .change = prio_change,
  338. .delete = prio_delete,
  339. .walk = prio_walk,
  340. .tcf_chain = prio_find_tcf,
  341. .bind_tcf = prio_bind,
  342. .unbind_tcf = prio_put,
  343. .dump = prio_dump_class,
  344. };
  345. static struct Qdisc_ops prio_qdisc_ops = {
  346. .next = NULL,
  347. .cl_ops = &prio_class_ops,
  348. .id = "prio",
  349. .priv_size = sizeof(struct prio_sched_data),
  350. .enqueue = prio_enqueue,
  351. .dequeue = prio_dequeue,
  352. .requeue = prio_requeue,
  353. .drop = prio_drop,
  354. .init = prio_init,
  355. .reset = prio_reset,
  356. .destroy = prio_destroy,
  357. .change = prio_tune,
  358. .dump = prio_dump,
  359. .owner = THIS_MODULE,
  360. };
  361. static int __init prio_module_init(void)
  362. {
  363. return register_qdisc(&prio_qdisc_ops);
  364. }
  365. static void __exit prio_module_exit(void)
  366. {
  367. unregister_qdisc(&prio_qdisc_ops);
  368. }
  369. module_init(prio_module_init)
  370. module_exit(prio_module_exit)
  371. MODULE_LICENSE("GPL");