sch_drr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * net/sched/sch_drr.c Deficit Round Robin scheduler
  3. *
  4. * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/pkt_sched.h>
  15. #include <net/sch_generic.h>
  16. #include <net/pkt_sched.h>
  17. #include <net/pkt_cls.h>
  18. struct drr_class {
  19. struct Qdisc_class_common common;
  20. unsigned int refcnt;
  21. unsigned int filter_cnt;
  22. struct gnet_stats_basic bstats;
  23. struct gnet_stats_queue qstats;
  24. struct gnet_stats_rate_est rate_est;
  25. struct list_head alist;
  26. struct Qdisc *qdisc;
  27. u32 quantum;
  28. u32 deficit;
  29. };
  30. struct drr_sched {
  31. struct list_head active;
  32. struct tcf_proto *filter_list;
  33. struct Qdisc_class_hash clhash;
  34. };
  35. static struct drr_class *drr_find_class(struct Qdisc *sch, u32 classid)
  36. {
  37. struct drr_sched *q = qdisc_priv(sch);
  38. struct Qdisc_class_common *clc;
  39. clc = qdisc_class_find(&q->clhash, classid);
  40. if (clc == NULL)
  41. return NULL;
  42. return container_of(clc, struct drr_class, common);
  43. }
  44. static void drr_purge_queue(struct drr_class *cl)
  45. {
  46. unsigned int len = cl->qdisc->q.qlen;
  47. qdisc_reset(cl->qdisc);
  48. qdisc_tree_decrease_qlen(cl->qdisc, len);
  49. }
  50. static const struct nla_policy drr_policy[TCA_DRR_MAX + 1] = {
  51. [TCA_DRR_QUANTUM] = { .type = NLA_U32 },
  52. };
  53. static int drr_change_class(struct Qdisc *sch, u32 classid, u32 parentid,
  54. struct nlattr **tca, unsigned long *arg)
  55. {
  56. struct drr_sched *q = qdisc_priv(sch);
  57. struct drr_class *cl = (struct drr_class *)*arg;
  58. struct nlattr *opt = tca[TCA_OPTIONS];
  59. struct nlattr *tb[TCA_DRR_MAX + 1];
  60. u32 quantum;
  61. int err;
  62. if (!opt)
  63. return -EINVAL;
  64. err = nla_parse_nested(tb, TCA_DRR_MAX, opt, drr_policy);
  65. if (err < 0)
  66. return err;
  67. if (tb[TCA_DRR_QUANTUM]) {
  68. quantum = nla_get_u32(tb[TCA_DRR_QUANTUM]);
  69. if (quantum == 0)
  70. return -EINVAL;
  71. } else
  72. quantum = psched_mtu(qdisc_dev(sch));
  73. if (cl != NULL) {
  74. if (tca[TCA_RATE]) {
  75. err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
  76. qdisc_root_sleeping_lock(sch),
  77. tca[TCA_RATE]);
  78. if (err)
  79. return err;
  80. }
  81. sch_tree_lock(sch);
  82. if (tb[TCA_DRR_QUANTUM])
  83. cl->quantum = quantum;
  84. sch_tree_unlock(sch);
  85. return 0;
  86. }
  87. cl = kzalloc(sizeof(struct drr_class), GFP_KERNEL);
  88. if (cl == NULL)
  89. return -ENOBUFS;
  90. cl->refcnt = 1;
  91. cl->common.classid = classid;
  92. cl->quantum = quantum;
  93. cl->qdisc = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  94. &pfifo_qdisc_ops, classid);
  95. if (cl->qdisc == NULL)
  96. cl->qdisc = &noop_qdisc;
  97. if (tca[TCA_RATE]) {
  98. err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
  99. qdisc_root_sleeping_lock(sch),
  100. tca[TCA_RATE]);
  101. if (err) {
  102. qdisc_destroy(cl->qdisc);
  103. kfree(cl);
  104. return err;
  105. }
  106. }
  107. sch_tree_lock(sch);
  108. qdisc_class_hash_insert(&q->clhash, &cl->common);
  109. sch_tree_unlock(sch);
  110. qdisc_class_hash_grow(sch, &q->clhash);
  111. *arg = (unsigned long)cl;
  112. return 0;
  113. }
  114. static void drr_destroy_class(struct Qdisc *sch, struct drr_class *cl)
  115. {
  116. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  117. qdisc_destroy(cl->qdisc);
  118. kfree(cl);
  119. }
  120. static int drr_delete_class(struct Qdisc *sch, unsigned long arg)
  121. {
  122. struct drr_sched *q = qdisc_priv(sch);
  123. struct drr_class *cl = (struct drr_class *)arg;
  124. if (cl->filter_cnt > 0)
  125. return -EBUSY;
  126. sch_tree_lock(sch);
  127. drr_purge_queue(cl);
  128. qdisc_class_hash_remove(&q->clhash, &cl->common);
  129. BUG_ON(--cl->refcnt == 0);
  130. /*
  131. * This shouldn't happen: we "hold" one cops->get() when called
  132. * from tc_ctl_tclass; the destroy method is done from cops->put().
  133. */
  134. sch_tree_unlock(sch);
  135. return 0;
  136. }
  137. static unsigned long drr_get_class(struct Qdisc *sch, u32 classid)
  138. {
  139. struct drr_class *cl = drr_find_class(sch, classid);
  140. if (cl != NULL)
  141. cl->refcnt++;
  142. return (unsigned long)cl;
  143. }
  144. static void drr_put_class(struct Qdisc *sch, unsigned long arg)
  145. {
  146. struct drr_class *cl = (struct drr_class *)arg;
  147. if (--cl->refcnt == 0)
  148. drr_destroy_class(sch, cl);
  149. }
  150. static struct tcf_proto **drr_tcf_chain(struct Qdisc *sch, unsigned long cl)
  151. {
  152. struct drr_sched *q = qdisc_priv(sch);
  153. if (cl)
  154. return NULL;
  155. return &q->filter_list;
  156. }
  157. static unsigned long drr_bind_tcf(struct Qdisc *sch, unsigned long parent,
  158. u32 classid)
  159. {
  160. struct drr_class *cl = drr_find_class(sch, classid);
  161. if (cl != NULL)
  162. cl->filter_cnt++;
  163. return (unsigned long)cl;
  164. }
  165. static void drr_unbind_tcf(struct Qdisc *sch, unsigned long arg)
  166. {
  167. struct drr_class *cl = (struct drr_class *)arg;
  168. cl->filter_cnt--;
  169. }
  170. static int drr_graft_class(struct Qdisc *sch, unsigned long arg,
  171. struct Qdisc *new, struct Qdisc **old)
  172. {
  173. struct drr_class *cl = (struct drr_class *)arg;
  174. if (new == NULL) {
  175. new = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  176. &pfifo_qdisc_ops, cl->common.classid);
  177. if (new == NULL)
  178. new = &noop_qdisc;
  179. }
  180. sch_tree_lock(sch);
  181. drr_purge_queue(cl);
  182. *old = cl->qdisc;
  183. cl->qdisc = new;
  184. sch_tree_unlock(sch);
  185. return 0;
  186. }
  187. static struct Qdisc *drr_class_leaf(struct Qdisc *sch, unsigned long arg)
  188. {
  189. struct drr_class *cl = (struct drr_class *)arg;
  190. return cl->qdisc;
  191. }
  192. static void drr_qlen_notify(struct Qdisc *csh, unsigned long arg)
  193. {
  194. struct drr_class *cl = (struct drr_class *)arg;
  195. if (cl->qdisc->q.qlen == 0)
  196. list_del(&cl->alist);
  197. }
  198. static int drr_dump_class(struct Qdisc *sch, unsigned long arg,
  199. struct sk_buff *skb, struct tcmsg *tcm)
  200. {
  201. struct drr_class *cl = (struct drr_class *)arg;
  202. struct nlattr *nest;
  203. tcm->tcm_parent = TC_H_ROOT;
  204. tcm->tcm_handle = cl->common.classid;
  205. tcm->tcm_info = cl->qdisc->handle;
  206. nest = nla_nest_start(skb, TCA_OPTIONS);
  207. if (nest == NULL)
  208. goto nla_put_failure;
  209. NLA_PUT_U32(skb, TCA_DRR_QUANTUM, cl->quantum);
  210. return nla_nest_end(skb, nest);
  211. nla_put_failure:
  212. nla_nest_cancel(skb, nest);
  213. return -EMSGSIZE;
  214. }
  215. static int drr_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  216. struct gnet_dump *d)
  217. {
  218. struct drr_class *cl = (struct drr_class *)arg;
  219. struct tc_drr_stats xstats;
  220. memset(&xstats, 0, sizeof(xstats));
  221. if (cl->qdisc->q.qlen)
  222. xstats.deficit = cl->deficit;
  223. if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
  224. gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
  225. gnet_stats_copy_queue(d, &cl->qdisc->qstats) < 0)
  226. return -1;
  227. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  228. }
  229. static void drr_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  230. {
  231. struct drr_sched *q = qdisc_priv(sch);
  232. struct drr_class *cl;
  233. struct hlist_node *n;
  234. unsigned int i;
  235. if (arg->stop)
  236. return;
  237. for (i = 0; i < q->clhash.hashsize; i++) {
  238. hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
  239. if (arg->count < arg->skip) {
  240. arg->count++;
  241. continue;
  242. }
  243. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  244. arg->stop = 1;
  245. return;
  246. }
  247. arg->count++;
  248. }
  249. }
  250. }
  251. static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch,
  252. int *qerr)
  253. {
  254. struct drr_sched *q = qdisc_priv(sch);
  255. struct drr_class *cl;
  256. struct tcf_result res;
  257. int result;
  258. if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
  259. cl = drr_find_class(sch, skb->priority);
  260. if (cl != NULL)
  261. return cl;
  262. }
  263. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  264. result = tc_classify(skb, q->filter_list, &res);
  265. if (result >= 0) {
  266. #ifdef CONFIG_NET_CLS_ACT
  267. switch (result) {
  268. case TC_ACT_QUEUED:
  269. case TC_ACT_STOLEN:
  270. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  271. case TC_ACT_SHOT:
  272. return NULL;
  273. }
  274. #endif
  275. cl = (struct drr_class *)res.class;
  276. if (cl == NULL)
  277. cl = drr_find_class(sch, res.classid);
  278. return cl;
  279. }
  280. return NULL;
  281. }
  282. static int drr_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  283. {
  284. struct drr_sched *q = qdisc_priv(sch);
  285. struct drr_class *cl;
  286. unsigned int len;
  287. int err;
  288. cl = drr_classify(skb, sch, &err);
  289. if (cl == NULL) {
  290. if (err & __NET_XMIT_BYPASS)
  291. sch->qstats.drops++;
  292. kfree_skb(skb);
  293. return err;
  294. }
  295. len = qdisc_pkt_len(skb);
  296. err = qdisc_enqueue(skb, cl->qdisc);
  297. if (unlikely(err != NET_XMIT_SUCCESS)) {
  298. if (net_xmit_drop_count(err)) {
  299. cl->qstats.drops++;
  300. sch->qstats.drops++;
  301. }
  302. return err;
  303. }
  304. if (cl->qdisc->q.qlen == 1) {
  305. list_add_tail(&cl->alist, &q->active);
  306. cl->deficit = cl->quantum;
  307. }
  308. cl->bstats.packets++;
  309. cl->bstats.bytes += len;
  310. sch->bstats.packets++;
  311. sch->bstats.bytes += len;
  312. sch->q.qlen++;
  313. return err;
  314. }
  315. static struct sk_buff *drr_dequeue(struct Qdisc *sch)
  316. {
  317. struct drr_sched *q = qdisc_priv(sch);
  318. struct drr_class *cl;
  319. struct sk_buff *skb;
  320. unsigned int len;
  321. if (list_empty(&q->active))
  322. goto out;
  323. while (1) {
  324. cl = list_first_entry(&q->active, struct drr_class, alist);
  325. skb = cl->qdisc->ops->peek(cl->qdisc);
  326. if (skb == NULL)
  327. goto out;
  328. len = qdisc_pkt_len(skb);
  329. if (len <= cl->deficit) {
  330. cl->deficit -= len;
  331. skb = qdisc_dequeue_peeked(cl->qdisc);
  332. if (cl->qdisc->q.qlen == 0)
  333. list_del(&cl->alist);
  334. sch->q.qlen--;
  335. return skb;
  336. }
  337. cl->deficit += cl->quantum;
  338. list_move_tail(&cl->alist, &q->active);
  339. }
  340. out:
  341. return NULL;
  342. }
  343. static unsigned int drr_drop(struct Qdisc *sch)
  344. {
  345. struct drr_sched *q = qdisc_priv(sch);
  346. struct drr_class *cl;
  347. unsigned int len;
  348. list_for_each_entry(cl, &q->active, alist) {
  349. if (cl->qdisc->ops->drop) {
  350. len = cl->qdisc->ops->drop(cl->qdisc);
  351. if (len > 0) {
  352. sch->q.qlen--;
  353. if (cl->qdisc->q.qlen == 0)
  354. list_del(&cl->alist);
  355. return len;
  356. }
  357. }
  358. }
  359. return 0;
  360. }
  361. static int drr_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
  362. {
  363. struct drr_sched *q = qdisc_priv(sch);
  364. int err;
  365. err = qdisc_class_hash_init(&q->clhash);
  366. if (err < 0)
  367. return err;
  368. INIT_LIST_HEAD(&q->active);
  369. return 0;
  370. }
  371. static void drr_reset_qdisc(struct Qdisc *sch)
  372. {
  373. struct drr_sched *q = qdisc_priv(sch);
  374. struct drr_class *cl;
  375. struct hlist_node *n;
  376. unsigned int i;
  377. for (i = 0; i < q->clhash.hashsize; i++) {
  378. hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
  379. if (cl->qdisc->q.qlen)
  380. list_del(&cl->alist);
  381. qdisc_reset(cl->qdisc);
  382. }
  383. }
  384. sch->q.qlen = 0;
  385. }
  386. static void drr_destroy_qdisc(struct Qdisc *sch)
  387. {
  388. struct drr_sched *q = qdisc_priv(sch);
  389. struct drr_class *cl;
  390. struct hlist_node *n, *next;
  391. unsigned int i;
  392. tcf_destroy_chain(&q->filter_list);
  393. for (i = 0; i < q->clhash.hashsize; i++) {
  394. hlist_for_each_entry_safe(cl, n, next, &q->clhash.hash[i],
  395. common.hnode)
  396. drr_destroy_class(sch, cl);
  397. }
  398. qdisc_class_hash_destroy(&q->clhash);
  399. }
  400. static const struct Qdisc_class_ops drr_class_ops = {
  401. .change = drr_change_class,
  402. .delete = drr_delete_class,
  403. .get = drr_get_class,
  404. .put = drr_put_class,
  405. .tcf_chain = drr_tcf_chain,
  406. .bind_tcf = drr_bind_tcf,
  407. .unbind_tcf = drr_unbind_tcf,
  408. .graft = drr_graft_class,
  409. .leaf = drr_class_leaf,
  410. .qlen_notify = drr_qlen_notify,
  411. .dump = drr_dump_class,
  412. .dump_stats = drr_dump_class_stats,
  413. .walk = drr_walk,
  414. };
  415. static struct Qdisc_ops drr_qdisc_ops __read_mostly = {
  416. .cl_ops = &drr_class_ops,
  417. .id = "drr",
  418. .priv_size = sizeof(struct drr_sched),
  419. .enqueue = drr_enqueue,
  420. .dequeue = drr_dequeue,
  421. .peek = qdisc_peek_dequeued,
  422. .drop = drr_drop,
  423. .init = drr_init_qdisc,
  424. .reset = drr_reset_qdisc,
  425. .destroy = drr_destroy_qdisc,
  426. .owner = THIS_MODULE,
  427. };
  428. static int __init drr_init(void)
  429. {
  430. return register_qdisc(&drr_qdisc_ops);
  431. }
  432. static void __exit drr_exit(void)
  433. {
  434. unregister_qdisc(&drr_qdisc_ops);
  435. }
  436. module_init(drr_init);
  437. module_exit(drr_exit);
  438. MODULE_LICENSE("GPL");