sch_prio.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/skbuff.h>
  19. #include <net/netlink.h>
  20. #include <net/pkt_sched.h>
  21. struct prio_sched_data
  22. {
  23. int bands;
  24. struct tcf_proto *filter_list;
  25. u8 prio2band[TC_PRIO_MAX+1];
  26. struct Qdisc *queues[TCQ_PRIO_BANDS];
  27. };
  28. static struct Qdisc *
  29. prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  30. {
  31. struct prio_sched_data *q = qdisc_priv(sch);
  32. u32 band = skb->priority;
  33. struct tcf_result res;
  34. int err;
  35. *qerr = NET_XMIT_BYPASS;
  36. if (TC_H_MAJ(skb->priority) != sch->handle) {
  37. err = tc_classify(skb, q->filter_list, &res);
  38. #ifdef CONFIG_NET_CLS_ACT
  39. switch (err) {
  40. case TC_ACT_STOLEN:
  41. case TC_ACT_QUEUED:
  42. *qerr = NET_XMIT_SUCCESS;
  43. case TC_ACT_SHOT:
  44. return NULL;
  45. }
  46. #endif
  47. if (!q->filter_list || err < 0) {
  48. if (TC_H_MAJ(band))
  49. band = 0;
  50. return q->queues[q->prio2band[band&TC_PRIO_MAX]];
  51. }
  52. band = res.classid;
  53. }
  54. band = TC_H_MIN(band) - 1;
  55. if (band >= q->bands)
  56. return q->queues[q->prio2band[0]];
  57. return q->queues[band];
  58. }
  59. static int
  60. prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  61. {
  62. struct Qdisc *qdisc;
  63. int ret;
  64. qdisc = prio_classify(skb, sch, &ret);
  65. #ifdef CONFIG_NET_CLS_ACT
  66. if (qdisc == NULL) {
  67. if (ret == NET_XMIT_BYPASS)
  68. sch->qstats.drops++;
  69. kfree_skb(skb);
  70. return ret;
  71. }
  72. #endif
  73. if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
  74. sch->bstats.bytes += skb->len;
  75. sch->bstats.packets++;
  76. sch->q.qlen++;
  77. return NET_XMIT_SUCCESS;
  78. }
  79. sch->qstats.drops++;
  80. return ret;
  81. }
  82. static int
  83. prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
  84. {
  85. struct Qdisc *qdisc;
  86. int ret;
  87. qdisc = prio_classify(skb, sch, &ret);
  88. #ifdef CONFIG_NET_CLS_ACT
  89. if (qdisc == NULL) {
  90. if (ret == NET_XMIT_BYPASS)
  91. sch->qstats.drops++;
  92. kfree_skb(skb);
  93. return ret;
  94. }
  95. #endif
  96. if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
  97. sch->q.qlen++;
  98. sch->qstats.requeues++;
  99. return 0;
  100. }
  101. sch->qstats.drops++;
  102. return NET_XMIT_DROP;
  103. }
  104. static struct sk_buff *prio_dequeue(struct Qdisc* sch)
  105. {
  106. struct prio_sched_data *q = qdisc_priv(sch);
  107. int prio;
  108. for (prio = 0; prio < q->bands; prio++) {
  109. struct Qdisc *qdisc = q->queues[prio];
  110. struct sk_buff *skb = qdisc->dequeue(qdisc);
  111. if (skb) {
  112. sch->q.qlen--;
  113. return skb;
  114. }
  115. }
  116. return NULL;
  117. }
  118. static unsigned int prio_drop(struct Qdisc* sch)
  119. {
  120. struct prio_sched_data *q = qdisc_priv(sch);
  121. int prio;
  122. unsigned int len;
  123. struct Qdisc *qdisc;
  124. for (prio = q->bands-1; prio >= 0; prio--) {
  125. qdisc = q->queues[prio];
  126. if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
  127. sch->q.qlen--;
  128. return len;
  129. }
  130. }
  131. return 0;
  132. }
  133. static void
  134. prio_reset(struct Qdisc* sch)
  135. {
  136. int prio;
  137. struct prio_sched_data *q = qdisc_priv(sch);
  138. for (prio=0; prio<q->bands; prio++)
  139. qdisc_reset(q->queues[prio]);
  140. sch->q.qlen = 0;
  141. }
  142. static void
  143. prio_destroy(struct Qdisc* sch)
  144. {
  145. int prio;
  146. struct prio_sched_data *q = qdisc_priv(sch);
  147. tcf_destroy_chain(&q->filter_list);
  148. for (prio=0; prio<q->bands; prio++)
  149. qdisc_destroy(q->queues[prio]);
  150. }
  151. static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
  152. {
  153. struct prio_sched_data *q = qdisc_priv(sch);
  154. struct tc_prio_qopt *qopt;
  155. int i;
  156. if (nla_len(opt) < sizeof(*qopt))
  157. return -EINVAL;
  158. qopt = nla_data(opt);
  159. if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
  160. return -EINVAL;
  161. for (i=0; i<=TC_PRIO_MAX; i++) {
  162. if (qopt->priomap[i] >= qopt->bands)
  163. return -EINVAL;
  164. }
  165. sch_tree_lock(sch);
  166. q->bands = qopt->bands;
  167. memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
  168. for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
  169. struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
  170. if (child != &noop_qdisc) {
  171. qdisc_tree_decrease_qlen(child, child->q.qlen);
  172. qdisc_destroy(child);
  173. }
  174. }
  175. sch_tree_unlock(sch);
  176. for (i=0; i<q->bands; i++) {
  177. if (q->queues[i] == &noop_qdisc) {
  178. struct Qdisc *child;
  179. child = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  180. &pfifo_qdisc_ops,
  181. TC_H_MAKE(sch->handle, i + 1));
  182. if (child) {
  183. sch_tree_lock(sch);
  184. child = xchg(&q->queues[i], child);
  185. if (child != &noop_qdisc) {
  186. qdisc_tree_decrease_qlen(child,
  187. child->q.qlen);
  188. qdisc_destroy(child);
  189. }
  190. sch_tree_unlock(sch);
  191. }
  192. }
  193. }
  194. return 0;
  195. }
  196. static int prio_init(struct Qdisc *sch, struct nlattr *opt)
  197. {
  198. struct prio_sched_data *q = qdisc_priv(sch);
  199. int i;
  200. for (i=0; i<TCQ_PRIO_BANDS; i++)
  201. q->queues[i] = &noop_qdisc;
  202. if (opt == NULL) {
  203. return -EINVAL;
  204. } else {
  205. int err;
  206. if ((err= prio_tune(sch, opt)) != 0)
  207. return err;
  208. }
  209. return 0;
  210. }
  211. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  212. {
  213. struct prio_sched_data *q = qdisc_priv(sch);
  214. unsigned char *b = skb_tail_pointer(skb);
  215. struct nlattr *nest;
  216. struct tc_prio_qopt opt;
  217. opt.bands = q->bands;
  218. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
  219. nest = nla_nest_compat_start(skb, TCA_OPTIONS, sizeof(opt), &opt);
  220. if (nest == NULL)
  221. goto nla_put_failure;
  222. nla_nest_compat_end(skb, nest);
  223. return skb->len;
  224. nla_put_failure:
  225. nlmsg_trim(skb, b);
  226. return -1;
  227. }
  228. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  229. struct Qdisc **old)
  230. {
  231. struct prio_sched_data *q = qdisc_priv(sch);
  232. unsigned long band = arg - 1;
  233. if (band >= q->bands)
  234. return -EINVAL;
  235. if (new == NULL)
  236. new = &noop_qdisc;
  237. sch_tree_lock(sch);
  238. *old = q->queues[band];
  239. q->queues[band] = new;
  240. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  241. qdisc_reset(*old);
  242. sch_tree_unlock(sch);
  243. return 0;
  244. }
  245. static struct Qdisc *
  246. prio_leaf(struct Qdisc *sch, unsigned long arg)
  247. {
  248. struct prio_sched_data *q = qdisc_priv(sch);
  249. unsigned long band = arg - 1;
  250. if (band >= q->bands)
  251. return NULL;
  252. return q->queues[band];
  253. }
  254. static unsigned long prio_get(struct Qdisc *sch, u32 classid)
  255. {
  256. struct prio_sched_data *q = qdisc_priv(sch);
  257. unsigned long band = TC_H_MIN(classid);
  258. if (band - 1 >= q->bands)
  259. return 0;
  260. return band;
  261. }
  262. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  263. {
  264. return prio_get(sch, classid);
  265. }
  266. static void prio_put(struct Qdisc *q, unsigned long cl)
  267. {
  268. return;
  269. }
  270. static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct nlattr **tca, unsigned long *arg)
  271. {
  272. unsigned long cl = *arg;
  273. struct prio_sched_data *q = qdisc_priv(sch);
  274. if (cl - 1 > q->bands)
  275. return -ENOENT;
  276. return 0;
  277. }
  278. static int prio_delete(struct Qdisc *sch, unsigned long cl)
  279. {
  280. struct prio_sched_data *q = qdisc_priv(sch);
  281. if (cl - 1 > q->bands)
  282. return -ENOENT;
  283. return 0;
  284. }
  285. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  286. struct tcmsg *tcm)
  287. {
  288. struct prio_sched_data *q = qdisc_priv(sch);
  289. if (cl - 1 > q->bands)
  290. return -ENOENT;
  291. tcm->tcm_handle |= TC_H_MIN(cl);
  292. if (q->queues[cl-1])
  293. tcm->tcm_info = q->queues[cl-1]->handle;
  294. return 0;
  295. }
  296. static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  297. struct gnet_dump *d)
  298. {
  299. struct prio_sched_data *q = qdisc_priv(sch);
  300. struct Qdisc *cl_q;
  301. cl_q = q->queues[cl - 1];
  302. if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
  303. gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
  304. return -1;
  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 const 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. .dump_stats = prio_dump_class_stats,
  345. };
  346. static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
  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");