sch_prio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. int err;
  37. *qerr = 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;
  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. 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. Also, the root qdisc must be the
  213. * only one that is enabled for multiqueue, since it's the only one
  214. * that interacts with the underlying device.
  215. */
  216. q->mq = RTA_GET_FLAG(tb[TCA_PRIO_MQ - 1]);
  217. if (q->mq) {
  218. if (sch->parent != TC_H_ROOT)
  219. return -EINVAL;
  220. if (netif_is_multiqueue(sch->dev)) {
  221. if (q->bands == 0)
  222. q->bands = sch->dev->egress_subqueue_count;
  223. else if (q->bands != sch->dev->egress_subqueue_count)
  224. return -EINVAL;
  225. } else
  226. return -EOPNOTSUPP;
  227. }
  228. if (q->bands > TCQ_PRIO_BANDS || q->bands < 2)
  229. return -EINVAL;
  230. for (i=0; i<=TC_PRIO_MAX; i++) {
  231. if (qopt->priomap[i] >= q->bands)
  232. return -EINVAL;
  233. }
  234. sch_tree_lock(sch);
  235. memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
  236. for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
  237. struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
  238. if (child != &noop_qdisc) {
  239. qdisc_tree_decrease_qlen(child, child->q.qlen);
  240. qdisc_destroy(child);
  241. }
  242. }
  243. sch_tree_unlock(sch);
  244. for (i=0; i<q->bands; i++) {
  245. if (q->queues[i] == &noop_qdisc) {
  246. struct Qdisc *child;
  247. child = qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops,
  248. TC_H_MAKE(sch->handle, i + 1));
  249. if (child) {
  250. sch_tree_lock(sch);
  251. child = xchg(&q->queues[i], child);
  252. if (child != &noop_qdisc) {
  253. qdisc_tree_decrease_qlen(child,
  254. child->q.qlen);
  255. qdisc_destroy(child);
  256. }
  257. sch_tree_unlock(sch);
  258. }
  259. }
  260. }
  261. return 0;
  262. }
  263. static int prio_init(struct Qdisc *sch, struct rtattr *opt)
  264. {
  265. struct prio_sched_data *q = qdisc_priv(sch);
  266. int i;
  267. for (i=0; i<TCQ_PRIO_BANDS; i++)
  268. q->queues[i] = &noop_qdisc;
  269. if (opt == NULL) {
  270. return -EINVAL;
  271. } else {
  272. int err;
  273. if ((err= prio_tune(sch, opt)) != 0)
  274. return err;
  275. }
  276. return 0;
  277. }
  278. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  279. {
  280. struct prio_sched_data *q = qdisc_priv(sch);
  281. unsigned char *b = skb_tail_pointer(skb);
  282. struct rtattr *nest;
  283. struct tc_prio_qopt opt;
  284. opt.bands = q->bands;
  285. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
  286. nest = RTA_NEST_COMPAT(skb, TCA_OPTIONS, sizeof(opt), &opt);
  287. if (q->mq)
  288. RTA_PUT_FLAG(skb, TCA_PRIO_MQ);
  289. RTA_NEST_COMPAT_END(skb, nest);
  290. return skb->len;
  291. rtattr_failure:
  292. nlmsg_trim(skb, b);
  293. return -1;
  294. }
  295. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  296. struct Qdisc **old)
  297. {
  298. struct prio_sched_data *q = qdisc_priv(sch);
  299. unsigned long band = arg - 1;
  300. if (band >= q->bands)
  301. return -EINVAL;
  302. if (new == NULL)
  303. new = &noop_qdisc;
  304. sch_tree_lock(sch);
  305. *old = q->queues[band];
  306. q->queues[band] = new;
  307. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  308. qdisc_reset(*old);
  309. sch_tree_unlock(sch);
  310. return 0;
  311. }
  312. static struct Qdisc *
  313. prio_leaf(struct Qdisc *sch, unsigned long arg)
  314. {
  315. struct prio_sched_data *q = qdisc_priv(sch);
  316. unsigned long band = arg - 1;
  317. if (band >= q->bands)
  318. return NULL;
  319. return q->queues[band];
  320. }
  321. static unsigned long prio_get(struct Qdisc *sch, u32 classid)
  322. {
  323. struct prio_sched_data *q = qdisc_priv(sch);
  324. unsigned long band = TC_H_MIN(classid);
  325. if (band - 1 >= q->bands)
  326. return 0;
  327. return band;
  328. }
  329. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  330. {
  331. return prio_get(sch, classid);
  332. }
  333. static void prio_put(struct Qdisc *q, unsigned long cl)
  334. {
  335. return;
  336. }
  337. static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct rtattr **tca, unsigned long *arg)
  338. {
  339. unsigned long cl = *arg;
  340. struct prio_sched_data *q = qdisc_priv(sch);
  341. if (cl - 1 > q->bands)
  342. return -ENOENT;
  343. return 0;
  344. }
  345. static int prio_delete(struct Qdisc *sch, unsigned long cl)
  346. {
  347. struct prio_sched_data *q = qdisc_priv(sch);
  348. if (cl - 1 > q->bands)
  349. return -ENOENT;
  350. return 0;
  351. }
  352. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  353. struct tcmsg *tcm)
  354. {
  355. struct prio_sched_data *q = qdisc_priv(sch);
  356. if (cl - 1 > q->bands)
  357. return -ENOENT;
  358. tcm->tcm_handle |= TC_H_MIN(cl);
  359. if (q->queues[cl-1])
  360. tcm->tcm_info = q->queues[cl-1]->handle;
  361. return 0;
  362. }
  363. static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  364. struct gnet_dump *d)
  365. {
  366. struct prio_sched_data *q = qdisc_priv(sch);
  367. struct Qdisc *cl_q;
  368. cl_q = q->queues[cl - 1];
  369. if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
  370. gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
  371. return -1;
  372. return 0;
  373. }
  374. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  375. {
  376. struct prio_sched_data *q = qdisc_priv(sch);
  377. int prio;
  378. if (arg->stop)
  379. return;
  380. for (prio = 0; prio < q->bands; prio++) {
  381. if (arg->count < arg->skip) {
  382. arg->count++;
  383. continue;
  384. }
  385. if (arg->fn(sch, prio+1, arg) < 0) {
  386. arg->stop = 1;
  387. break;
  388. }
  389. arg->count++;
  390. }
  391. }
  392. static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
  393. {
  394. struct prio_sched_data *q = qdisc_priv(sch);
  395. if (cl)
  396. return NULL;
  397. return &q->filter_list;
  398. }
  399. static struct Qdisc_class_ops prio_class_ops = {
  400. .graft = prio_graft,
  401. .leaf = prio_leaf,
  402. .get = prio_get,
  403. .put = prio_put,
  404. .change = prio_change,
  405. .delete = prio_delete,
  406. .walk = prio_walk,
  407. .tcf_chain = prio_find_tcf,
  408. .bind_tcf = prio_bind,
  409. .unbind_tcf = prio_put,
  410. .dump = prio_dump_class,
  411. .dump_stats = prio_dump_class_stats,
  412. };
  413. static struct Qdisc_ops prio_qdisc_ops = {
  414. .next = NULL,
  415. .cl_ops = &prio_class_ops,
  416. .id = "prio",
  417. .priv_size = sizeof(struct prio_sched_data),
  418. .enqueue = prio_enqueue,
  419. .dequeue = prio_dequeue,
  420. .requeue = prio_requeue,
  421. .drop = prio_drop,
  422. .init = prio_init,
  423. .reset = prio_reset,
  424. .destroy = prio_destroy,
  425. .change = prio_tune,
  426. .dump = prio_dump,
  427. .owner = THIS_MODULE,
  428. };
  429. static struct Qdisc_ops rr_qdisc_ops = {
  430. .next = NULL,
  431. .cl_ops = &prio_class_ops,
  432. .id = "rr",
  433. .priv_size = sizeof(struct prio_sched_data),
  434. .enqueue = prio_enqueue,
  435. .dequeue = rr_dequeue,
  436. .requeue = prio_requeue,
  437. .drop = prio_drop,
  438. .init = prio_init,
  439. .reset = prio_reset,
  440. .destroy = prio_destroy,
  441. .change = prio_tune,
  442. .dump = prio_dump,
  443. .owner = THIS_MODULE,
  444. };
  445. static int __init prio_module_init(void)
  446. {
  447. int err;
  448. err = register_qdisc(&prio_qdisc_ops);
  449. if (err < 0)
  450. return err;
  451. err = register_qdisc(&rr_qdisc_ops);
  452. if (err < 0)
  453. unregister_qdisc(&prio_qdisc_ops);
  454. return err;
  455. }
  456. static void __exit prio_module_exit(void)
  457. {
  458. unregister_qdisc(&prio_qdisc_ops);
  459. unregister_qdisc(&rr_qdisc_ops);
  460. }
  461. module_init(prio_module_init)
  462. module_exit(prio_module_exit)
  463. MODULE_LICENSE("GPL");
  464. MODULE_ALIAS("sch_rr");