sch_prio.c 8.9 KB

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