sch_drr.c 11 KB

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