sch_prio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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(qdisc_dev(sch),
  123. (q->mq ? prio : 0))) {
  124. qdisc = q->queues[prio];
  125. skb = qdisc->dequeue(qdisc);
  126. if (skb) {
  127. sch->q.qlen--;
  128. return skb;
  129. }
  130. }
  131. }
  132. return NULL;
  133. }
  134. static struct sk_buff *rr_dequeue(struct Qdisc* sch)
  135. {
  136. struct sk_buff *skb;
  137. struct prio_sched_data *q = qdisc_priv(sch);
  138. struct Qdisc *qdisc;
  139. int bandcount;
  140. /* Only take one pass through the queues. If nothing is available,
  141. * return nothing.
  142. */
  143. for (bandcount = 0; bandcount < q->bands; bandcount++) {
  144. /* Check if the target subqueue is available before
  145. * pulling an skb. This way we avoid excessive requeues
  146. * for slower queues. If the queue is stopped, try the
  147. * next queue.
  148. */
  149. if (!__netif_subqueue_stopped(qdisc_dev(sch),
  150. (q->mq ? q->curband : 0))) {
  151. qdisc = q->queues[q->curband];
  152. skb = qdisc->dequeue(qdisc);
  153. if (skb) {
  154. sch->q.qlen--;
  155. q->curband++;
  156. if (q->curband >= q->bands)
  157. q->curband = 0;
  158. return skb;
  159. }
  160. }
  161. q->curband++;
  162. if (q->curband >= q->bands)
  163. q->curband = 0;
  164. }
  165. return NULL;
  166. }
  167. static unsigned int prio_drop(struct Qdisc* sch)
  168. {
  169. struct prio_sched_data *q = qdisc_priv(sch);
  170. int prio;
  171. unsigned int len;
  172. struct Qdisc *qdisc;
  173. for (prio = q->bands-1; prio >= 0; prio--) {
  174. qdisc = q->queues[prio];
  175. if (qdisc->ops->drop && (len = qdisc->ops->drop(qdisc)) != 0) {
  176. sch->q.qlen--;
  177. return len;
  178. }
  179. }
  180. return 0;
  181. }
  182. static void
  183. prio_reset(struct Qdisc* sch)
  184. {
  185. int prio;
  186. struct prio_sched_data *q = qdisc_priv(sch);
  187. for (prio=0; prio<q->bands; prio++)
  188. qdisc_reset(q->queues[prio]);
  189. sch->q.qlen = 0;
  190. }
  191. static void
  192. prio_destroy(struct Qdisc* sch)
  193. {
  194. int prio;
  195. struct prio_sched_data *q = qdisc_priv(sch);
  196. tcf_destroy_chain(&q->filter_list);
  197. for (prio=0; prio<q->bands; prio++)
  198. qdisc_destroy(q->queues[prio]);
  199. }
  200. static int prio_tune(struct Qdisc *sch, struct nlattr *opt)
  201. {
  202. struct prio_sched_data *q = qdisc_priv(sch);
  203. struct tc_prio_qopt *qopt;
  204. struct nlattr *tb[TCA_PRIO_MAX + 1];
  205. int err;
  206. int i;
  207. err = nla_parse_nested_compat(tb, TCA_PRIO_MAX, opt, NULL, qopt,
  208. sizeof(*qopt));
  209. if (err < 0)
  210. return err;
  211. q->bands = qopt->bands;
  212. /* If we're multiqueue, make sure the number of incoming bands
  213. * matches the number of queues on the device we're associating with.
  214. * If the number of bands requested is zero, then set q->bands to
  215. * dev->egress_subqueue_count. Also, the root qdisc must be the
  216. * only one that is enabled for multiqueue, since it's the only one
  217. * that interacts with the underlying device.
  218. */
  219. q->mq = nla_get_flag(tb[TCA_PRIO_MQ]);
  220. if (q->mq) {
  221. if (sch->parent != TC_H_ROOT)
  222. return -EINVAL;
  223. if (netif_is_multiqueue(qdisc_dev(sch))) {
  224. if (q->bands == 0)
  225. q->bands = qdisc_dev(sch)->egress_subqueue_count;
  226. else if (q->bands != qdisc_dev(sch)->egress_subqueue_count)
  227. return -EINVAL;
  228. } else
  229. return -EOPNOTSUPP;
  230. }
  231. if (q->bands > TCQ_PRIO_BANDS || q->bands < 2)
  232. return -EINVAL;
  233. for (i=0; i<=TC_PRIO_MAX; i++) {
  234. if (qopt->priomap[i] >= q->bands)
  235. return -EINVAL;
  236. }
  237. sch_tree_lock(sch);
  238. memcpy(q->prio2band, qopt->priomap, TC_PRIO_MAX+1);
  239. for (i=q->bands; i<TCQ_PRIO_BANDS; i++) {
  240. struct Qdisc *child = xchg(&q->queues[i], &noop_qdisc);
  241. if (child != &noop_qdisc) {
  242. qdisc_tree_decrease_qlen(child, child->q.qlen);
  243. qdisc_destroy(child);
  244. }
  245. }
  246. sch_tree_unlock(sch);
  247. for (i=0; i<q->bands; i++) {
  248. if (q->queues[i] == &noop_qdisc) {
  249. struct Qdisc *child;
  250. child = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  251. &pfifo_qdisc_ops,
  252. TC_H_MAKE(sch->handle, i + 1));
  253. if (child) {
  254. sch_tree_lock(sch);
  255. child = xchg(&q->queues[i], child);
  256. if (child != &noop_qdisc) {
  257. qdisc_tree_decrease_qlen(child,
  258. child->q.qlen);
  259. qdisc_destroy(child);
  260. }
  261. sch_tree_unlock(sch);
  262. }
  263. }
  264. }
  265. return 0;
  266. }
  267. static int prio_init(struct Qdisc *sch, struct nlattr *opt)
  268. {
  269. struct prio_sched_data *q = qdisc_priv(sch);
  270. int i;
  271. for (i=0; i<TCQ_PRIO_BANDS; i++)
  272. q->queues[i] = &noop_qdisc;
  273. if (opt == NULL) {
  274. return -EINVAL;
  275. } else {
  276. int err;
  277. if ((err= prio_tune(sch, opt)) != 0)
  278. return err;
  279. }
  280. return 0;
  281. }
  282. static int prio_dump(struct Qdisc *sch, struct sk_buff *skb)
  283. {
  284. struct prio_sched_data *q = qdisc_priv(sch);
  285. unsigned char *b = skb_tail_pointer(skb);
  286. struct nlattr *nest;
  287. struct tc_prio_qopt opt;
  288. opt.bands = q->bands;
  289. memcpy(&opt.priomap, q->prio2band, TC_PRIO_MAX+1);
  290. nest = nla_nest_compat_start(skb, TCA_OPTIONS, sizeof(opt), &opt);
  291. if (nest == NULL)
  292. goto nla_put_failure;
  293. if (q->mq) {
  294. if (nla_put_flag(skb, TCA_PRIO_MQ) < 0)
  295. goto nla_put_failure;
  296. }
  297. nla_nest_compat_end(skb, nest);
  298. return skb->len;
  299. nla_put_failure:
  300. nlmsg_trim(skb, b);
  301. return -1;
  302. }
  303. static int prio_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
  304. struct Qdisc **old)
  305. {
  306. struct prio_sched_data *q = qdisc_priv(sch);
  307. unsigned long band = arg - 1;
  308. if (band >= q->bands)
  309. return -EINVAL;
  310. if (new == NULL)
  311. new = &noop_qdisc;
  312. sch_tree_lock(sch);
  313. *old = q->queues[band];
  314. q->queues[band] = new;
  315. qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
  316. qdisc_reset(*old);
  317. sch_tree_unlock(sch);
  318. return 0;
  319. }
  320. static struct Qdisc *
  321. prio_leaf(struct Qdisc *sch, unsigned long arg)
  322. {
  323. struct prio_sched_data *q = qdisc_priv(sch);
  324. unsigned long band = arg - 1;
  325. if (band >= q->bands)
  326. return NULL;
  327. return q->queues[band];
  328. }
  329. static unsigned long prio_get(struct Qdisc *sch, u32 classid)
  330. {
  331. struct prio_sched_data *q = qdisc_priv(sch);
  332. unsigned long band = TC_H_MIN(classid);
  333. if (band - 1 >= q->bands)
  334. return 0;
  335. return band;
  336. }
  337. static unsigned long prio_bind(struct Qdisc *sch, unsigned long parent, u32 classid)
  338. {
  339. return prio_get(sch, classid);
  340. }
  341. static void prio_put(struct Qdisc *q, unsigned long cl)
  342. {
  343. return;
  344. }
  345. static int prio_change(struct Qdisc *sch, u32 handle, u32 parent, struct nlattr **tca, unsigned long *arg)
  346. {
  347. unsigned long cl = *arg;
  348. struct prio_sched_data *q = qdisc_priv(sch);
  349. if (cl - 1 > q->bands)
  350. return -ENOENT;
  351. return 0;
  352. }
  353. static int prio_delete(struct Qdisc *sch, unsigned long cl)
  354. {
  355. struct prio_sched_data *q = qdisc_priv(sch);
  356. if (cl - 1 > q->bands)
  357. return -ENOENT;
  358. return 0;
  359. }
  360. static int prio_dump_class(struct Qdisc *sch, unsigned long cl, struct sk_buff *skb,
  361. struct tcmsg *tcm)
  362. {
  363. struct prio_sched_data *q = qdisc_priv(sch);
  364. if (cl - 1 > q->bands)
  365. return -ENOENT;
  366. tcm->tcm_handle |= TC_H_MIN(cl);
  367. if (q->queues[cl-1])
  368. tcm->tcm_info = q->queues[cl-1]->handle;
  369. return 0;
  370. }
  371. static int prio_dump_class_stats(struct Qdisc *sch, unsigned long cl,
  372. struct gnet_dump *d)
  373. {
  374. struct prio_sched_data *q = qdisc_priv(sch);
  375. struct Qdisc *cl_q;
  376. cl_q = q->queues[cl - 1];
  377. if (gnet_stats_copy_basic(d, &cl_q->bstats) < 0 ||
  378. gnet_stats_copy_queue(d, &cl_q->qstats) < 0)
  379. return -1;
  380. return 0;
  381. }
  382. static void prio_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  383. {
  384. struct prio_sched_data *q = qdisc_priv(sch);
  385. int prio;
  386. if (arg->stop)
  387. return;
  388. for (prio = 0; prio < q->bands; prio++) {
  389. if (arg->count < arg->skip) {
  390. arg->count++;
  391. continue;
  392. }
  393. if (arg->fn(sch, prio+1, arg) < 0) {
  394. arg->stop = 1;
  395. break;
  396. }
  397. arg->count++;
  398. }
  399. }
  400. static struct tcf_proto ** prio_find_tcf(struct Qdisc *sch, unsigned long cl)
  401. {
  402. struct prio_sched_data *q = qdisc_priv(sch);
  403. if (cl)
  404. return NULL;
  405. return &q->filter_list;
  406. }
  407. static const struct Qdisc_class_ops prio_class_ops = {
  408. .graft = prio_graft,
  409. .leaf = prio_leaf,
  410. .get = prio_get,
  411. .put = prio_put,
  412. .change = prio_change,
  413. .delete = prio_delete,
  414. .walk = prio_walk,
  415. .tcf_chain = prio_find_tcf,
  416. .bind_tcf = prio_bind,
  417. .unbind_tcf = prio_put,
  418. .dump = prio_dump_class,
  419. .dump_stats = prio_dump_class_stats,
  420. };
  421. static struct Qdisc_ops prio_qdisc_ops __read_mostly = {
  422. .next = NULL,
  423. .cl_ops = &prio_class_ops,
  424. .id = "prio",
  425. .priv_size = sizeof(struct prio_sched_data),
  426. .enqueue = prio_enqueue,
  427. .dequeue = prio_dequeue,
  428. .requeue = prio_requeue,
  429. .drop = prio_drop,
  430. .init = prio_init,
  431. .reset = prio_reset,
  432. .destroy = prio_destroy,
  433. .change = prio_tune,
  434. .dump = prio_dump,
  435. .owner = THIS_MODULE,
  436. };
  437. static struct Qdisc_ops rr_qdisc_ops __read_mostly = {
  438. .next = NULL,
  439. .cl_ops = &prio_class_ops,
  440. .id = "rr",
  441. .priv_size = sizeof(struct prio_sched_data),
  442. .enqueue = prio_enqueue,
  443. .dequeue = rr_dequeue,
  444. .requeue = prio_requeue,
  445. .drop = prio_drop,
  446. .init = prio_init,
  447. .reset = prio_reset,
  448. .destroy = prio_destroy,
  449. .change = prio_tune,
  450. .dump = prio_dump,
  451. .owner = THIS_MODULE,
  452. };
  453. static int __init prio_module_init(void)
  454. {
  455. int err;
  456. err = register_qdisc(&prio_qdisc_ops);
  457. if (err < 0)
  458. return err;
  459. err = register_qdisc(&rr_qdisc_ops);
  460. if (err < 0)
  461. unregister_qdisc(&prio_qdisc_ops);
  462. return err;
  463. }
  464. static void __exit prio_module_exit(void)
  465. {
  466. unregister_qdisc(&prio_qdisc_ops);
  467. unregister_qdisc(&rr_qdisc_ops);
  468. }
  469. module_init(prio_module_init)
  470. module_exit(prio_module_exit)
  471. MODULE_LICENSE("GPL");
  472. MODULE_ALIAS("sch_rr");