sch_prio.c 9.1 KB

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