sch_prio.c 9.4 KB

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