sch_prio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. int curband; /* for round-robin */
  25. struct tcf_proto *filter_list;
  26. u8 prio2band[TC_PRIO_MAX+1];
  27. struct Qdisc *queues[TCQ_PRIO_BANDS];
  28. int mq;
  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. *qerr = NET_XMIT_BYPASS;
  37. if (TC_H_MAJ(skb->priority) != sch->handle) {
  38. #ifdef CONFIG_NET_CLS_ACT
  39. switch (tc_classify(skb, q->filter_list, &res)) {
  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. if (!q->filter_list ) {
  47. #else
  48. if (!q->filter_list || tc_classify(skb, q->filter_list, &res)) {
  49. #endif
  50. if (TC_H_MAJ(band))
  51. band = 0;
  52. band = q->prio2band[band&TC_PRIO_MAX];
  53. goto out;
  54. }
  55. band = res.classid;
  56. }
  57. band = TC_H_MIN(band) - 1;
  58. if (band >= q->bands)
  59. band = q->prio2band[0];
  60. out:
  61. if (q->mq)
  62. skb_set_queue_mapping(skb, band);
  63. return q->queues[band];
  64. }
  65. static int
  66. prio_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  67. {
  68. struct Qdisc *qdisc;
  69. int ret;
  70. qdisc = prio_classify(skb, sch, &ret);
  71. #ifdef CONFIG_NET_CLS_ACT
  72. if (qdisc == NULL) {
  73. if (ret == NET_XMIT_BYPASS)
  74. sch->qstats.drops++;
  75. kfree_skb(skb);
  76. return ret;
  77. }
  78. #endif
  79. if ((ret = qdisc->enqueue(skb, qdisc)) == NET_XMIT_SUCCESS) {
  80. sch->bstats.bytes += skb->len;
  81. sch->bstats.packets++;
  82. sch->q.qlen++;
  83. return NET_XMIT_SUCCESS;
  84. }
  85. sch->qstats.drops++;
  86. return ret;
  87. }
  88. static int
  89. prio_requeue(struct sk_buff *skb, struct Qdisc* sch)
  90. {
  91. struct Qdisc *qdisc;
  92. int ret;
  93. qdisc = prio_classify(skb, sch, &ret);
  94. #ifdef CONFIG_NET_CLS_ACT
  95. if (qdisc == NULL) {
  96. if (ret == NET_XMIT_BYPASS)
  97. sch->qstats.drops++;
  98. kfree_skb(skb);
  99. return ret;
  100. }
  101. #endif
  102. if ((ret = qdisc->ops->requeue(skb, qdisc)) == NET_XMIT_SUCCESS) {
  103. sch->q.qlen++;
  104. sch->qstats.requeues++;
  105. return 0;
  106. }
  107. sch->qstats.drops++;
  108. return NET_XMIT_DROP;
  109. }
  110. static struct sk_buff *
  111. prio_dequeue(struct Qdisc* sch)
  112. {
  113. struct sk_buff *skb;
  114. struct prio_sched_data *q = qdisc_priv(sch);
  115. int prio;
  116. struct Qdisc *qdisc;
  117. for (prio = 0; prio < q->bands; prio++) {
  118. /* Check if the target subqueue is available before
  119. * pulling an skb. This way we avoid excessive requeues
  120. * for slower queues.
  121. */
  122. if (!netif_subqueue_stopped(sch->dev, (q->mq ? prio : 0))) {
  123. qdisc = q->queues[prio];
  124. skb = qdisc->dequeue(qdisc);
  125. if (skb) {
  126. sch->q.qlen--;
  127. return skb;
  128. }
  129. }
  130. }
  131. return NULL;
  132. }
  133. static struct sk_buff *rr_dequeue(struct Qdisc* sch)
  134. {
  135. struct sk_buff *skb;
  136. struct prio_sched_data *q = qdisc_priv(sch);
  137. struct Qdisc *qdisc;
  138. int bandcount;
  139. /* Only take one pass through the queues. If nothing is available,
  140. * return nothing.
  141. */
  142. for (bandcount = 0; bandcount < q->bands; bandcount++) {
  143. /* Check if the target subqueue is available before
  144. * pulling an skb. This way we avoid excessive requeues
  145. * for slower queues. If the queue is stopped, try the
  146. * next queue.
  147. */
  148. if (!netif_subqueue_stopped(sch->dev,
  149. (q->mq ? q->curband : 0))) {
  150. qdisc = q->queues[q->curband];
  151. skb = qdisc->dequeue(qdisc);
  152. if (skb) {
  153. sch->q.qlen--;
  154. q->curband++;
  155. if (q->curband >= q->bands)
  156. q->curband = 0;
  157. return skb;
  158. }
  159. }
  160. q->curband++;
  161. if (q->curband >= q->bands)
  162. q->curband = 0;
  163. }
  164. return NULL;
  165. }
  166. static unsigned int prio_drop(struct Qdisc* sch)
  167. {
  168. struct prio_sched_data *q = qdisc_priv(sch);
  169. int prio;
  170. unsigned int len;
  171. struct Qdisc *qdisc;
  172. for (prio = q->bands-1; prio >= 0; prio--) {
  173. qdisc = q->queues[prio];
  174. if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
  175. sch->q.qlen--;
  176. return len;
  177. }
  178. }
  179. return 0;
  180. }
  181. static void
  182. prio_reset(struct Qdisc* sch)
  183. {
  184. int prio;
  185. struct prio_sched_data *q = qdisc_priv(sch);
  186. for (prio=0; prio<q->bands; prio++)
  187. qdisc_reset(q->queues[prio]);
  188. sch->q.qlen = 0;
  189. }
  190. static void
  191. prio_destroy(struct Qdisc* sch)
  192. {
  193. int prio;
  194. struct prio_sched_data *q = qdisc_priv(sch);
  195. tcf_destroy_chain(q->filter_list);
  196. for (prio=0; prio<q->bands; prio++)
  197. qdisc_destroy(q->queues[prio]);
  198. }
  199. static int prio_tune(struct Qdisc *sch, struct rtattr *opt)
  200. {
  201. struct prio_sched_data *q = qdisc_priv(sch);
  202. struct tc_prio_qopt *qopt;
  203. struct rtattr *tb[TCA_PRIO_MAX];
  204. int i;
  205. if (rtattr_parse_nested_compat(tb, TCA_PRIO_MAX, opt, qopt,
  206. sizeof(*qopt)))
  207. return -EINVAL;
  208. q->bands = qopt->bands;
  209. /* If we're multiqueue, make sure the number of incoming bands
  210. * matches the number of queues on the device we're associating with.
  211. * If the number of bands requested is zero, then set q->bands to
  212. * dev->egress_subqueue_count.
  213. */
  214. q->mq = RTA_GET_FLAG(tb[TCA_PRIO_MQ - 1]);
  215. if (q->mq) {
  216. if (sch->handle != TC_H_ROOT)
  217. return -EINVAL;
  218. if (netif_is_multiqueue(sch->dev)) {
  219. if (q->bands == 0)
  220. q->bands = sch->dev->egress_subqueue_count;
  221. else if (q->bands != sch->dev->egress_subqueue_count)
  222. return -EINVAL;
  223. } else
  224. return -EOPNOTSUPP;
  225. }
  226. if (q->bands > TCQ_PRIO_BANDS || q->bands < 2)
  227. return -EINVAL;
  228. for (i=0; i<=TC_PRIO_MAX; i++) {
  229. if (qopt->priomap[i] >= q->bands)
  230. return -EINVAL;
  231. }
  232. sch_tree_lock(sch);
  233. memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
  234. for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
  235. struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
  236. if (child != &noop_qdisc) {
  237. qdisc_tree_decrease_qlen(child, child->q.qlen);
  238. qdisc_destroy(child);
  239. }
  240. }
  241. sch_tree_unlock(sch);
  242. for (i=0; i<q->bands; i++) {
  243. if (q->queues[i] == &noop_qdisc) {
  244. struct Qdisc *child;
  245. child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
  246. TC_H_MAKE(sch->handle, i + 1));
  247. if (child) {
  248. sch_tree_lock(sch);
  249. child = xchg(&q->queues[i], child);
  250. if (child != &noop_qdisc) {
  251. qdisc_tree_decrease_qlen(child,
  252. child->q.qlen);
  253. qdisc_destroy(child);
  254. }
  255. sch_tree_unlock(sch);
  256. }
  257. }
  258. }
  259. return 0;
  260. }
  261. static int prio_init(struct Qdisc *sch, struct rtattr *opt)
  262. {
  263. struct prio_sched_data *q = qdisc_priv(sch);
  264. int i;
  265. for (i=0; i<TCQ_PRIO_BANDS; i++)
  266. q->queues[i] = &noop_qdisc;
  267. if (opt == NULL) {
  268. return -EINVAL;
  269. } else {
  270. int err;
  271. if ((err= prio_tune(sch, opt)) != 0)
  272. return err;
  273. }
  274. return 0;
  275. }
  276. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  277. {
  278. struct prio_sched_data *q = qdisc_priv(sch);
  279. unsigned char *b = skb_tail_pointer(skb);
  280. struct rtattr *nest;
  281. struct tc_prio_qopt opt;
  282. opt.bands = q->bands;
  283. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
  284. nest = RTA_NEST_COMPAT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  285. if (q->mq)
  286. RTA_PUT_FLAG(skb, TCA_PRIO_MQ);
  287. RTA_NEST_COMPAT_END(skb, nest);
  288. return skb->len;
  289. rtattr_failure:
  290. nlmsg_trim(skb, b);
  291. return -1;
  292. }
  293. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  294. struct Qdisc **old)
  295. {
  296. struct prio_sched_data *q = qdisc_priv(sch);
  297. unsigned long band = arg - 1;
  298. if (band >= q->bands)
  299. return -EINVAL;
  300. if (new == NULL)
  301. new = &noop_qdisc;
  302. sch_tree_lock(sch);
  303. *old = q->queues[band];
  304. q->queues[band] = new;
  305. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  306. qdisc_reset(*old);
  307. sch_tree_unlock(sch);
  308. return 0;
  309. }
  310. static struct Qdisc *
  311. prio_leaf(struct Qdisc *sch, unsigned long arg)
  312. {
  313. struct prio_sched_data *q = qdisc_priv(sch);
  314. unsigned long band = arg - 1;
  315. if (band >= q->bands)
  316. return NULL;
  317. return q->queues[band];
  318. }
  319. static unsigned long prio_get(struct Qdisc *sch, u32 classid)
  320. {
  321. struct prio_sched_data *q = qdisc_priv(sch);
  322. unsigned long band = TC_H_MIN(classid);
  323. if (band - 1 >= q->bands)
  324. return 0;
  325. return band;
  326. }
  327. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  328. {
  329. return prio_get(sch, classid);
  330. }
  331. static void prio_put(struct Qdisc *q, unsigned long cl)
  332. {
  333. return;
  334. }
  335. static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
  336. {
  337. unsigned long cl = *arg;
  338. struct prio_sched_data *q = qdisc_priv(sch);
  339. if (cl - 1 > q->bands)
  340. return -ENOENT;
  341. return 0;
  342. }
  343. static int prio_delete(struct Qdisc *sch, unsigned long cl)
  344. {
  345. struct prio_sched_data *q = qdisc_priv(sch);
  346. if (cl - 1 > q->bands)
  347. return -ENOENT;
  348. return 0;
  349. }
  350. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  351. struct tcmsg *tcm)
  352. {
  353. struct prio_sched_data *q = qdisc_priv(sch);
  354. if (cl - 1 > q->bands)
  355. return -ENOENT;
  356. tcm->tcm_handle |= TC_H_MIN(cl);
  357. if (q->queues[cl-1])
  358. tcm->tcm_info = q->queues[cl-1]->handle;
  359. return 0;
  360. }
  361. static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  362. struct gnet_dump *d)
  363. {
  364. struct prio_sched_data *q = qdisc_priv(sch);
  365. struct Qdisc *cl_q;
  366. cl_q = q->queues[cl - 1];
  367. if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
  368. gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
  369. return -1;
  370. return 0;
  371. }
  372. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  373. {
  374. struct prio_sched_data *q = qdisc_priv(sch);
  375. int prio;
  376. if (arg->stop)
  377. return;
  378. for (prio = 0; prio < q->bands; prio++) {
  379. if (arg->count < arg->skip) {
  380. arg->count++;
  381. continue;
  382. }
  383. if (arg->fn(sch, prio+1, arg) < 0) {
  384. arg->stop = 1;
  385. break;
  386. }
  387. arg->count++;
  388. }
  389. }
  390. static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
  391. {
  392. struct prio_sched_data *q = qdisc_priv(sch);
  393. if (cl)
  394. return NULL;
  395. return &q->filter_list;
  396. }
  397. static struct Qdisc_class_ops prio_class_ops = {
  398. .graft = prio_graft,
  399. .leaf = prio_leaf,
  400. .get = prio_get,
  401. .put = prio_put,
  402. .change = prio_change,
  403. .delete = prio_delete,
  404. .walk = prio_walk,
  405. .tcf_chain = prio_find_tcf,
  406. .bind_tcf = prio_bind,
  407. .unbind_tcf = prio_put,
  408. .dump = prio_dump_class,
  409. .dump_stats = prio_dump_class_stats,
  410. };
  411. static struct Qdisc_ops prio_qdisc_ops = {
  412. .next = NULL,
  413. .cl_ops = &prio_class_ops,
  414. .id = "prio",
  415. .priv_size = sizeof(struct prio_sched_data),
  416. .enqueue = prio_enqueue,
  417. .dequeue = prio_dequeue,
  418. .requeue = prio_requeue,
  419. .drop = prio_drop,
  420. .init = prio_init,
  421. .reset = prio_reset,
  422. .destroy = prio_destroy,
  423. .change = prio_tune,
  424. .dump = prio_dump,
  425. .owner = THIS_MODULE,
  426. };
  427. static struct Qdisc_ops rr_qdisc_ops = {
  428. .next = NULL,
  429. .cl_ops = &prio_class_ops,
  430. .id = "rr",
  431. .priv_size = sizeof(struct prio_sched_data),
  432. .enqueue = prio_enqueue,
  433. .dequeue = rr_dequeue,
  434. .requeue = prio_requeue,
  435. .drop = prio_drop,
  436. .init = prio_init,
  437. .reset = prio_reset,
  438. .destroy = prio_destroy,
  439. .change = prio_tune,
  440. .dump = prio_dump,
  441. .owner = THIS_MODULE,
  442. };
  443. static int __init prio_module_init(void)
  444. {
  445. int err;
  446. err = register_qdisc(&prio_qdisc_ops);
  447. if (err < 0)
  448. return err;
  449. err = register_qdisc(&rr_qdisc_ops);
  450. if (err < 0)
  451. unregister_qdisc(&prio_qdisc_ops);
  452. return err;
  453. }
  454. static void __exit prio_module_exit(void)
  455. {
  456. unregister_qdisc(&prio_qdisc_ops);
  457. unregister_qdisc(&rr_qdisc_ops);
  458. }
  459. module_init(prio_module_init)
  460. module_exit(prio_module_exit)
  461. MODULE_LICENSE("GPL");
  462. MODULE_ALIAS("sch_rr");