sch_prio.c 9.4 KB

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