sch_prio.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/errno.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/netdevice.h>
  21. #include <net/netlink.h>
  22. #include <net/pkt_sched.h>
  23. struct prio_sched_data {
  24. int bands;
  25. struct tcf_proto *filter_list;
  26. u8 prio2band[TC_PRIO_MAX+1];
  27. struct Qdisc *queues[TCQ_PRIO_BANDS];
  28. u8 enable_flow;
  29. };
  30. static struct Qdisc *
  31. prio_classify(struct sk_buff *skb, struct Qdisc *sch, int *qerr)
  32. {
  33. struct prio_sched_data *q = qdisc_priv(sch);
  34. u32 band = skb->priority;
  35. struct tcf_result res;
  36. int err;
  37. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  38. if (TC_H_MAJ(skb->priority) != sch->handle) {
  39. err = tc_classify(skb, q->filter_list, &res);
  40. #ifdef CONFIG_NET_CLS_ACT
  41. switch (err) {
  42. case TC_ACT_STOLEN:
  43. case TC_ACT_QUEUED:
  44. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  45. case TC_ACT_SHOT:
  46. return NULL;
  47. }
  48. #endif
  49. if (!q->filter_list || err < 0) {
  50. if (TC_H_MAJ(band))
  51. band = 0;
  52. return q->queues[q->prio2band[band & TC_PRIO_MAX]];
  53. }
  54. band = res.classid;
  55. }
  56. band = TC_H_MIN(band) - 1;
  57. if (band >= q->bands)
  58. return q->queues[q->prio2band[0]];
  59. return q->queues[band];
  60. }
  61. static int
  62. prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  63. {
  64. struct Qdisc *qdisc;
  65. int ret;
  66. qdisc = prio_classify(skb, sch, &ret);
  67. #ifdef CONFIG_NET_CLS_ACT
  68. if (qdisc == NULL) {
  69. if (ret & __NET_XMIT_BYPASS)
  70. sch->qstats.drops++;
  71. kfree_skb(skb);
  72. return ret;
  73. }
  74. #endif
  75. ret = qdisc_enqueue(skb, qdisc);
  76. if (ret == NET_XMIT_SUCCESS) {
  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 struct sk_buff *prio_peek(struct Qdisc *sch)
  85. {
  86. struct prio_sched_data *q = qdisc_priv(sch);
  87. int prio;
  88. if (!q->enable_flow)
  89. return NULL;
  90. for (prio = 0; prio < q->bands; prio++) {
  91. struct Qdisc *qdisc = q->queues[prio];
  92. struct sk_buff *skb = qdisc->ops->peek(qdisc);
  93. if (skb)
  94. return skb;
  95. }
  96. return NULL;
  97. }
  98. static struct sk_buff *prio_dequeue(struct Qdisc *sch)
  99. {
  100. struct prio_sched_data *q = qdisc_priv(sch);
  101. int prio;
  102. if (!q->enable_flow)
  103. return NULL;
  104. for (prio = 0; prio < q->bands; prio++) {
  105. struct Qdisc *qdisc = q->queues[prio];
  106. struct sk_buff *skb = qdisc_dequeue_peeked(qdisc);
  107. if (skb) {
  108. qdisc_bstats_update(sch, skb);
  109. sch->q.qlen--;
  110. return skb;
  111. }
  112. }
  113. return NULL;
  114. }
  115. static unsigned int prio_drop(struct Qdisc *sch)
  116. {
  117. struct prio_sched_data *q = qdisc_priv(sch);
  118. int prio;
  119. unsigned int len;
  120. struct Qdisc *qdisc;
  121. for (prio = q->bands-1; prio >= 0; prio--) {
  122. qdisc = q->queues[prio];
  123. if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
  124. sch->q.qlen--;
  125. return len;
  126. }
  127. }
  128. return 0;
  129. }
  130. static void
  131. prio_reset(struct Qdisc *sch)
  132. {
  133. int prio;
  134. struct prio_sched_data *q = qdisc_priv(sch);
  135. for (prio = 0; prio < q->bands; prio++)
  136. qdisc_reset(q->queues[prio]);
  137. sch->q.qlen = 0;
  138. q->enable_flow = 1;
  139. }
  140. static void
  141. prio_destroy(struct Qdisc *sch)
  142. {
  143. int prio;
  144. struct prio_sched_data *q = qdisc_priv(sch);
  145. tcf_destroy_chain(&q->filter_list);
  146. for (prio = 0; prio < q->bands; prio++)
  147. qdisc_destroy(q->queues[prio]);
  148. }
  149. static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
  150. {
  151. struct prio_sched_data *q = qdisc_priv(sch);
  152. struct tc_prio_qopt *qopt;
  153. int i;
  154. int flow_change = 0;
  155. if (nla_len(opt) < sizeof(*qopt))
  156. return -EINVAL;
  157. qopt = nla_data(opt);
  158. if (qopt->bands > TCQ_PRIO_BANDS || qopt->bands < 2)
  159. return -EINVAL;
  160. for (i = 0; i <= TC_PRIO_MAX; i++) {
  161. if (qopt->priomap[i] >= qopt->bands)
  162. return -EINVAL;
  163. }
  164. sch_tree_lock(sch);
  165. if (q->enable_flow != qopt->enable_flow) {
  166. q->enable_flow = qopt->enable_flow;
  167. flow_change = 1;
  168. }
  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 = q->queues[i];
  173. q->queues[i] = &noop_qdisc;
  174. if (child != &noop_qdisc) {
  175. qdisc_tree_decrease_qlen(child, child->q.qlen);
  176. qdisc_destroy(child);
  177. }
  178. }
  179. sch_tree_unlock(sch);
  180. for (i = 0; i < q->bands; i++) {
  181. if (q->queues[i] == &noop_qdisc) {
  182. struct Qdisc *child, *old;
  183. child = qdisc_create_dflt(sch->dev_queue,
  184. &pfifo_qdisc_ops,
  185. TC_H_MAKE(sch->handle, i + 1));
  186. if (child) {
  187. sch_tree_lock(sch);
  188. old = q->queues[i];
  189. q->queues[i] = child;
  190. if (old != &noop_qdisc) {
  191. qdisc_tree_decrease_qlen(old,
  192. old->q.qlen);
  193. qdisc_destroy(old);
  194. }
  195. sch_tree_unlock(sch);
  196. }
  197. }
  198. }
  199. /* Schedule qdisc when flow re-enabled */
  200. if (flow_change && q->enable_flow) {
  201. if (!test_bit(__QDISC_STATE_DEACTIVATED,
  202. &sch->state))
  203. __netif_schedule(qdisc_root(sch));
  204. }
  205. return 0;
  206. }
  207. static int prio_init(struct Qdisc *sch, struct nlattr *opt)
  208. {
  209. struct prio_sched_data *q = qdisc_priv(sch);
  210. int i;
  211. for (i = 0; i < TCQ_PRIO_BANDS; i++)
  212. q->queues[i] = &noop_qdisc;
  213. if (opt == NULL) {
  214. return -EINVAL;
  215. } else {
  216. int err;
  217. if ((err = prio_tune(sch, opt)) != 0)
  218. return err;
  219. }
  220. return 0;
  221. }
  222. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  223. {
  224. struct prio_sched_data *q = qdisc_priv(sch);
  225. unsigned char *b = skb_tail_pointer(skb);
  226. struct tc_prio_qopt opt;
  227. opt.bands = q->bands;
  228. opt.enable_flow = q->enable_flow;
  229. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX + 1);
  230. NLA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  231. return skb->len;
  232. nla_put_failure:
  233. nlmsg_trim(skb, b);
  234. return -1;
  235. }
  236. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  237. struct Qdisc **old)
  238. {
  239. struct prio_sched_data *q = qdisc_priv(sch);
  240. unsigned long band = arg - 1;
  241. if (new == NULL)
  242. new = &noop_qdisc;
  243. sch_tree_lock(sch);
  244. *old = q->queues[band];
  245. q->queues[band] = new;
  246. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  247. qdisc_reset(*old);
  248. sch_tree_unlock(sch);
  249. return 0;
  250. }
  251. static struct Qdisc *
  252. prio_leaf(struct Qdisc *sch, unsigned long arg)
  253. {
  254. struct prio_sched_data *q = qdisc_priv(sch);
  255. unsigned long band = arg - 1;
  256. return q->queues[band];
  257. }
  258. static unsigned long prio_get(struct Qdisc *sch, u32 classid)
  259. {
  260. struct prio_sched_data *q = qdisc_priv(sch);
  261. unsigned long band = TC_H_MIN(classid);
  262. if (band - 1 >= q->bands)
  263. return 0;
  264. return band;
  265. }
  266. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  267. {
  268. return prio_get(sch, classid);
  269. }
  270. static void prio_put(struct Qdisc *q, unsigned long cl)
  271. {
  272. }
  273. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  274. struct tcmsg *tcm)
  275. {
  276. struct prio_sched_data *q = qdisc_priv(sch);
  277. tcm->tcm_handle |= TC_H_MIN(cl);
  278. tcm->tcm_info = q->queues[cl-1]->handle;
  279. return 0;
  280. }
  281. static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  282. struct gnet_dump *d)
  283. {
  284. struct prio_sched_data *q = qdisc_priv(sch);
  285. struct Qdisc *cl_q;
  286. cl_q = q->queues[cl - 1];
  287. cl_q->qstats.qlen = cl_q->q.qlen;
  288. if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
  289. gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
  290. return -1;
  291. return 0;
  292. }
  293. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  294. {
  295. struct prio_sched_data *q = qdisc_priv(sch);
  296. int prio;
  297. if (arg->stop)
  298. return;
  299. for (prio = 0; prio < q->bands; prio++) {
  300. if (arg->count < arg->skip) {
  301. arg->count++;
  302. continue;
  303. }
  304. if (arg->fn(sch, prio + 1, arg) < 0) {
  305. arg->stop = 1;
  306. break;
  307. }
  308. arg->count++;
  309. }
  310. }
  311. static struct tcf_proto **prio_find_tcf(struct Qdisc *sch, unsigned long cl)
  312. {
  313. struct prio_sched_data *q = qdisc_priv(sch);
  314. if (cl)
  315. return NULL;
  316. return &q->filter_list;
  317. }
  318. static const struct Qdisc_class_ops prio_class_ops = {
  319. .graft = prio_graft,
  320. .leaf = prio_leaf,
  321. .get = prio_get,
  322. .put = prio_put,
  323. .walk = prio_walk,
  324. .tcf_chain = prio_find_tcf,
  325. .bind_tcf = prio_bind,
  326. .unbind_tcf = prio_put,
  327. .dump = prio_dump_class,
  328. .dump_stats = prio_dump_class_stats,
  329. };
  330. static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
  331. .next = NULL,
  332. .cl_ops = &prio_class_ops,
  333. .id = "prio",
  334. .priv_size = sizeof(struct prio_sched_data),
  335. .enqueue = prio_enqueue,
  336. .dequeue = prio_dequeue,
  337. .peek = prio_peek,
  338. .drop = prio_drop,
  339. .init = prio_init,
  340. .reset = prio_reset,
  341. .destroy = prio_destroy,
  342. .change = prio_tune,
  343. .dump = prio_dump,
  344. .owner = THIS_MODULE,
  345. };
  346. static int __init prio_module_init(void)
  347. {
  348. return register_qdisc(&prio_qdisc_ops);
  349. }
  350. static void __exit prio_module_exit(void)
  351. {
  352. unregister_qdisc(&prio_qdisc_ops);
  353. }
  354. module_init(prio_module_init)
  355. module_exit(prio_module_exit)
  356. MODULE_LICENSE("GPL");