sch_prio.c 12 KB

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