sch_prio.c 12 KB

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