sch_prio.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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_SUCCESS | __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 | __NET_XMIT_STOLEN;
  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. if (net_xmit_drop_count(ret))
  81. sch->qstats.drops++;
  82. return ret;
  83. }
  84. static int
  85. prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
  86. {
  87. struct Qdisc *qdisc;
  88. int ret;
  89. qdisc = prio_classify(skb, sch, &ret);
  90. #ifdef CONFIG_NET_CLS_ACT
  91. if (qdisc == NULL) {
  92. if (ret & __NET_XMIT_BYPASS)
  93. sch->qstats.drops++;
  94. kfree_skb(skb);
  95. return ret;
  96. }
  97. #endif
  98. if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
  99. sch->q.qlen++;
  100. sch->qstats.requeues++;
  101. return NET_XMIT_SUCCESS;
  102. }
  103. if (net_xmit_drop_count(ret))
  104. sch->qstats.drops++;
  105. return ret;
  106. }
  107. static struct sk_buff *prio_dequeue(struct Qdisc* sch)
  108. {
  109. struct prio_sched_data *q = qdisc_priv(sch);
  110. int prio;
  111. for (prio = 0; prio < q->bands; prio++) {
  112. struct Qdisc *qdisc = q->queues[prio];
  113. struct sk_buff *skb = qdisc->dequeue(qdisc);
  114. if (skb) {
  115. sch->q.qlen--;
  116. return skb;
  117. }
  118. }
  119. return NULL;
  120. }
  121. static unsigned int prio_drop(struct Qdisc* sch)
  122. {
  123. struct prio_sched_data *q = qdisc_priv(sch);
  124. int prio;
  125. unsigned int len;
  126. struct Qdisc *qdisc;
  127. for (prio = q->bands-1; prio >= 0; prio--) {
  128. qdisc = q->queues[prio];
  129. if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
  130. sch->q.qlen--;
  131. return len;
  132. }
  133. }
  134. return 0;
  135. }
  136. static void
  137. prio_reset(struct Qdisc* sch)
  138. {
  139. int prio;
  140. struct prio_sched_data *q = qdisc_priv(sch);
  141. for (prio=0; prio<q->bands; prio++)
  142. qdisc_reset(q->queues[prio]);
  143. sch->q.qlen = 0;
  144. }
  145. static void
  146. prio_destroy(struct Qdisc* sch)
  147. {
  148. int prio;
  149. struct prio_sched_data *q = qdisc_priv(sch);
  150. tcf_destroy_chain(&q->filter_list);
  151. for (prio=0; prio<q->bands; prio++)
  152. qdisc_destroy(q->queues[prio]);
  153. }
  154. static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
  155. {
  156. struct prio_sched_data *q = qdisc_priv(sch);
  157. struct tc_prio_qopt *qopt;
  158. int i;
  159. if (nla_len(opt) < sizeof(*qopt))
  160. return -EINVAL;
  161. qopt = nla_data(opt);
  162. if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
  163. return -EINVAL;
  164. for (i=0; i<=TC_PRIO_MAX; i++) {
  165. if (qopt->priomap[i] >= qopt->bands)
  166. return -EINVAL;
  167. }
  168. sch_tree_lock(sch);
  169. q->bands = qopt->bands;
  170. memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
  171. for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
  172. struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
  173. if (child != &noop_qdisc) {
  174. qdisc_tree_decrease_qlen(child, child->q.qlen);
  175. qdisc_destroy(child);
  176. }
  177. }
  178. sch_tree_unlock(sch);
  179. for (i=0; i<q->bands; i++) {
  180. if (q->queues[i] == &noop_qdisc) {
  181. struct Qdisc *child;
  182. child = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  183. &pfifo_qdisc_ops,
  184. TC_H_MAKE(sch->handle, i + 1));
  185. if (child) {
  186. sch_tree_lock(sch);
  187. child = xchg(&q->queues[i], child);
  188. if (child != &noop_qdisc) {
  189. qdisc_tree_decrease_qlen(child,
  190. child->q.qlen);
  191. qdisc_destroy(child);
  192. }
  193. sch_tree_unlock(sch);
  194. }
  195. }
  196. }
  197. return 0;
  198. }
  199. static int prio_init(struct Qdisc *sch, struct nlattr *opt)
  200. {
  201. struct prio_sched_data *q = qdisc_priv(sch);
  202. int i;
  203. for (i=0; i<TCQ_PRIO_BANDS; i++)
  204. q->queues[i] = &noop_qdisc;
  205. if (opt == NULL) {
  206. return -EINVAL;
  207. } else {
  208. int err;
  209. if ((err= prio_tune(sch, opt)) != 0)
  210. return err;
  211. }
  212. return 0;
  213. }
  214. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  215. {
  216. struct prio_sched_data *q = qdisc_priv(sch);
  217. unsigned char *b = skb_tail_pointer(skb);
  218. struct tc_prio_qopt opt;
  219. opt.bands = q->bands;
  220. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
  221. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  222. return skb->len;
  223. nla_put_failure:
  224. nlmsg_trim(skb, b);
  225. return -1;
  226. }
  227. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  228. struct Qdisc **old)
  229. {
  230. struct prio_sched_data *q = qdisc_priv(sch);
  231. unsigned long band = arg - 1;
  232. if (band >= q->bands)
  233. return -EINVAL;
  234. if (new == NULL)
  235. new = &noop_qdisc;
  236. sch_tree_lock(sch);
  237. *old = q->queues[band];
  238. q->queues[band] = new;
  239. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  240. qdisc_reset(*old);
  241. sch_tree_unlock(sch);
  242. return 0;
  243. }
  244. static struct Qdisc *
  245. prio_leaf(struct Qdisc *sch, unsigned long arg)
  246. {
  247. struct prio_sched_data *q = qdisc_priv(sch);
  248. unsigned long band = arg - 1;
  249. if (band >= q->bands)
  250. return NULL;
  251. return q->queues[band];
  252. }
  253. static unsigned long prio_get(struct Qdisc *sch, u32 classid)
  254. {
  255. struct prio_sched_data *q = qdisc_priv(sch);
  256. unsigned long band = TC_H_MIN(classid);
  257. if (band - 1 >= q->bands)
  258. return 0;
  259. return band;
  260. }
  261. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  262. {
  263. return prio_get(sch, classid);
  264. }
  265. static void prio_put(struct Qdisc *q, unsigned long cl)
  266. {
  267. return;
  268. }
  269. static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct nlattr **tca, unsigned long *arg)
  270. {
  271. unsigned long cl = *arg;
  272. struct prio_sched_data *q = qdisc_priv(sch);
  273. if (cl - 1 > q->bands)
  274. return -ENOENT;
  275. return 0;
  276. }
  277. static int prio_delete(struct Qdisc *sch, unsigned long cl)
  278. {
  279. struct prio_sched_data *q = qdisc_priv(sch);
  280. if (cl - 1 > q->bands)
  281. return -ENOENT;
  282. return 0;
  283. }
  284. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  285. struct tcmsg *tcm)
  286. {
  287. struct prio_sched_data *q = qdisc_priv(sch);
  288. if (cl - 1 > q->bands)
  289. return -ENOENT;
  290. tcm->tcm_handle |= TC_H_MIN(cl);
  291. if (q->queues[cl-1])
  292. tcm->tcm_info = q->queues[cl-1]->handle;
  293. return 0;
  294. }
  295. static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  296. struct gnet_dump *d)
  297. {
  298. struct prio_sched_data *q = qdisc_priv(sch);
  299. struct Qdisc *cl_q;
  300. cl_q = q->queues[cl - 1];
  301. if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
  302. gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
  303. return -1;
  304. return 0;
  305. }
  306. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  307. {
  308. struct prio_sched_data *q = qdisc_priv(sch);
  309. int prio;
  310. if (arg->stop)
  311. return;
  312. for (prio = 0; prio < q->bands; prio++) {
  313. if (arg->count < arg->skip) {
  314. arg->count++;
  315. continue;
  316. }
  317. if (arg->fn(sch, prio+1, arg) < 0) {
  318. arg->stop = 1;
  319. break;
  320. }
  321. arg->count++;
  322. }
  323. }
  324. static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
  325. {
  326. struct prio_sched_data *q = qdisc_priv(sch);
  327. if (cl)
  328. return NULL;
  329. return &q->filter_list;
  330. }
  331. static const struct Qdisc_class_ops prio_class_ops = {
  332. .graft = prio_graft,
  333. .leaf = prio_leaf,
  334. .get = prio_get,
  335. .put = prio_put,
  336. .change = prio_change,
  337. .delete = prio_delete,
  338. .walk = prio_walk,
  339. .tcf_chain = prio_find_tcf,
  340. .bind_tcf = prio_bind,
  341. .unbind_tcf = prio_put,
  342. .dump = prio_dump_class,
  343. .dump_stats = prio_dump_class_stats,
  344. };
  345. static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
  346. .next = NULL,
  347. .cl_ops = &prio_class_ops,
  348. .id = "prio",
  349. .priv_size = sizeof(struct prio_sched_data),
  350. .enqueue = prio_enqueue,
  351. .dequeue = prio_dequeue,
  352. .requeue = prio_requeue,
  353. .drop = prio_drop,
  354. .init = prio_init,
  355. .reset = prio_reset,
  356. .destroy = prio_destroy,
  357. .change = prio_tune,
  358. .dump = prio_dump,
  359. .owner = THIS_MODULE,
  360. };
  361. static int __init prio_module_init(void)
  362. {
  363. return register_qdisc(&prio_qdisc_ops);
  364. }
  365. static void __exit prio_module_exit(void)
  366. {
  367. unregister_qdisc(&prio_qdisc_ops);
  368. }
  369. module_init(prio_module_init)
  370. module_exit(prio_module_exit)
  371. MODULE_LICENSE("GPL");