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_BYPASS;
  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_BYPASS)
  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_BYPASS)
  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 (qdisc->ops->drop && (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<q->bands; i++) {
  198. if (q->queues[i] == &noop_qdisc) {
  199. struct Qdisc *child;
  200. child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops);
  201. if (child) {
  202. sch_tree_lock(sch);
  203. child = xchg(&q->queues[i], child);
  204. if (child != &noop_qdisc)
  205. qdisc_destroy(child);
  206. sch_tree_unlock(sch);
  207. }
  208. }
  209. }
  210. return 0;
  211. }
  212. static int prio_init(struct Qdisc *sch, struct rtattr *opt)
  213. {
  214. struct prio_sched_data *q = qdisc_priv(sch);
  215. int i;
  216. for (i=0; i<TCQ_PRIO_BANDS; i++)
  217. q->queues[i] = &noop_qdisc;
  218. if (opt == NULL) {
  219. return -EINVAL;
  220. } else {
  221. int err;
  222. if ((err= prio_tune(sch, opt)) != 0)
  223. return err;
  224. }
  225. return 0;
  226. }
  227. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  228. {
  229. struct prio_sched_data *q = qdisc_priv(sch);
  230. unsigned char *b = skb->tail;
  231. struct tc_prio_qopt opt;
  232. opt.bands = q->bands;
  233. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
  234. RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  235. return skb->len;
  236. rtattr_failure:
  237. skb_trim(skb, b - skb->data);
  238. return -1;
  239. }
  240. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  241. struct Qdisc **old)
  242. {
  243. struct prio_sched_data *q = qdisc_priv(sch);
  244. unsigned long band = arg - 1;
  245. if (band >= q->bands)
  246. return -EINVAL;
  247. if (new == NULL)
  248. new = &noop_qdisc;
  249. sch_tree_lock(sch);
  250. *old = q->queues[band];
  251. q->queues[band] = new;
  252. sch->q.qlen -= (*old)->q.qlen;
  253. qdisc_reset(*old);
  254. sch_tree_unlock(sch);
  255. return 0;
  256. }
  257. static struct Qdisc *
  258. prio_leaf(struct Qdisc *sch, unsigned long arg)
  259. {
  260. struct prio_sched_data *q = qdisc_priv(sch);
  261. unsigned long band = arg - 1;
  262. if (band >= q->bands)
  263. return NULL;
  264. return q->queues[band];
  265. }
  266. static unsigned long prio_get(struct Qdisc *sch, u32 classid)
  267. {
  268. struct prio_sched_data *q = qdisc_priv(sch);
  269. unsigned long band = TC_H_MIN(classid);
  270. if (band - 1 >= q->bands)
  271. return 0;
  272. return band;
  273. }
  274. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  275. {
  276. return prio_get(sch, classid);
  277. }
  278. static void prio_put(struct Qdisc *q, unsigned long cl)
  279. {
  280. return;
  281. }
  282. static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
  283. {
  284. unsigned long cl = *arg;
  285. struct prio_sched_data *q = qdisc_priv(sch);
  286. if (cl - 1 > q->bands)
  287. return -ENOENT;
  288. return 0;
  289. }
  290. static int prio_delete(struct Qdisc *sch, unsigned long cl)
  291. {
  292. struct prio_sched_data *q = qdisc_priv(sch);
  293. if (cl - 1 > q->bands)
  294. return -ENOENT;
  295. return 0;
  296. }
  297. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  298. struct tcmsg *tcm)
  299. {
  300. struct prio_sched_data *q = qdisc_priv(sch);
  301. if (cl - 1 > q->bands)
  302. return -ENOENT;
  303. tcm->tcm_handle |= TC_H_MIN(cl);
  304. if (q->queues[cl-1])
  305. tcm->tcm_info = q->queues[cl-1]->handle;
  306. return 0;
  307. }
  308. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  309. {
  310. struct prio_sched_data *q = qdisc_priv(sch);
  311. int prio;
  312. if (arg->stop)
  313. return;
  314. for (prio = 0; prio < q->bands; prio++) {
  315. if (arg->count < arg->skip) {
  316. arg->count++;
  317. continue;
  318. }
  319. if (arg->fn(sch, prio+1, arg) < 0) {
  320. arg->stop = 1;
  321. break;
  322. }
  323. arg->count++;
  324. }
  325. }
  326. static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
  327. {
  328. struct prio_sched_data *q = qdisc_priv(sch);
  329. if (cl)
  330. return NULL;
  331. return &q->filter_list;
  332. }
  333. static struct Qdisc_class_ops prio_class_ops = {
  334. .graft = prio_graft,
  335. .leaf = prio_leaf,
  336. .get = prio_get,
  337. .put = prio_put,
  338. .change = prio_change,
  339. .delete = prio_delete,
  340. .walk = prio_walk,
  341. .tcf_chain = prio_find_tcf,
  342. .bind_tcf = prio_bind,
  343. .unbind_tcf = prio_put,
  344. .dump = prio_dump_class,
  345. };
  346. static struct Qdisc_ops prio_qdisc_ops = {
  347. .next = NULL,
  348. .cl_ops = &prio_class_ops,
  349. .id = "prio",
  350. .priv_size = sizeof(struct prio_sched_data),
  351. .enqueue = prio_enqueue,
  352. .dequeue = prio_dequeue,
  353. .requeue = prio_requeue,
  354. .drop = prio_drop,
  355. .init = prio_init,
  356. .reset = prio_reset,
  357. .destroy = prio_destroy,
  358. .change = prio_tune,
  359. .dump = prio_dump,
  360. .owner = THIS_MODULE,
  361. };
  362. static int __init prio_module_init(void)
  363. {
  364. return register_qdisc(&prio_qdisc_ops);
  365. }
  366. static void __exit prio_module_exit(void)
  367. {
  368. unregister_qdisc(&prio_qdisc_ops);
  369. }
  370. module_init(prio_module_init)
  371. module_exit(prio_module_exit)
  372. MODULE_LICENSE("GPL");