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/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 refcnt;
  22. unsigned int filter_cnt;
  23. struct gnet_stats_basic_packed bstats;
  24. struct gnet_stats_queue qstats;
  25. struct gnet_stats_rate_est64 rate_est;
  26. struct list_head alist;
  27. struct Qdisc *qdisc;
  28. u32 quantum;
  29. u32 deficit;
  30. };
  31. struct drr_sched {
  32. struct list_head active;
  33. struct tcf_proto __rcu *filter_list;
  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);
  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. qdisc_root_sleeping_lock(sch),
  80. tca[TCA_RATE]);
  81. if (err)
  82. return err;
  83. }
  84. sch_tree_lock(sch);
  85. if (tb[TCA_DRR_QUANTUM])
  86. cl->quantum = quantum;
  87. sch_tree_unlock(sch);
  88. return 0;
  89. }
  90. cl = kzalloc(sizeof(struct drr_class), GFP_KERNEL);
  91. if (cl == NULL)
  92. return -ENOBUFS;
  93. cl->refcnt = 1;
  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. if (tca[TCA_RATE]) {
  101. err = gen_replace_estimator(&cl->bstats, NULL, &cl->rate_est,
  102. qdisc_root_sleeping_lock(sch),
  103. tca[TCA_RATE]);
  104. if (err) {
  105. qdisc_destroy(cl->qdisc);
  106. kfree(cl);
  107. return err;
  108. }
  109. }
  110. sch_tree_lock(sch);
  111. qdisc_class_hash_insert(&q->clhash, &cl->common);
  112. sch_tree_unlock(sch);
  113. qdisc_class_hash_grow(sch, &q->clhash);
  114. *arg = (unsigned long)cl;
  115. return 0;
  116. }
  117. static void drr_destroy_class(struct Qdisc *sch, struct drr_class *cl)
  118. {
  119. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  120. qdisc_destroy(cl->qdisc);
  121. kfree(cl);
  122. }
  123. static int drr_delete_class(struct Qdisc *sch, unsigned long arg)
  124. {
  125. struct drr_sched *q = qdisc_priv(sch);
  126. struct drr_class *cl = (struct drr_class *)arg;
  127. if (cl->filter_cnt > 0)
  128. return -EBUSY;
  129. sch_tree_lock(sch);
  130. drr_purge_queue(cl);
  131. qdisc_class_hash_remove(&q->clhash, &cl->common);
  132. BUG_ON(--cl->refcnt == 0);
  133. /*
  134. * This shouldn't happen: we "hold" one cops->get() when called
  135. * from tc_ctl_tclass; the destroy method is done from cops->put().
  136. */
  137. sch_tree_unlock(sch);
  138. return 0;
  139. }
  140. static unsigned long drr_get_class(struct Qdisc *sch, u32 classid)
  141. {
  142. struct drr_class *cl = drr_find_class(sch, classid);
  143. if (cl != NULL)
  144. cl->refcnt++;
  145. return (unsigned long)cl;
  146. }
  147. static void drr_put_class(struct Qdisc *sch, unsigned long arg)
  148. {
  149. struct drr_class *cl = (struct drr_class *)arg;
  150. if (--cl->refcnt == 0)
  151. drr_destroy_class(sch, cl);
  152. }
  153. static struct tcf_proto __rcu **drr_tcf_chain(struct Qdisc *sch,
  154. unsigned long cl)
  155. {
  156. struct drr_sched *q = qdisc_priv(sch);
  157. if (cl)
  158. return NULL;
  159. return &q->filter_list;
  160. }
  161. static unsigned long drr_bind_tcf(struct Qdisc *sch, unsigned long parent,
  162. u32 classid)
  163. {
  164. struct drr_class *cl = drr_find_class(sch, classid);
  165. if (cl != NULL)
  166. cl->filter_cnt++;
  167. return (unsigned long)cl;
  168. }
  169. static void drr_unbind_tcf(struct Qdisc *sch, unsigned long arg)
  170. {
  171. struct drr_class *cl = (struct drr_class *)arg;
  172. cl->filter_cnt--;
  173. }
  174. static int drr_graft_class(struct Qdisc *sch, unsigned long arg,
  175. struct Qdisc *new, struct Qdisc **old)
  176. {
  177. struct drr_class *cl = (struct drr_class *)arg;
  178. if (new == NULL) {
  179. new = qdisc_create_dflt(sch->dev_queue,
  180. &pfifo_qdisc_ops, cl->common.classid);
  181. if (new == NULL)
  182. new = &noop_qdisc;
  183. }
  184. *old = qdisc_replace(sch, new, &cl->qdisc);
  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. if (nla_put_u32(skb, TCA_DRR_QUANTUM, cl->quantum))
  210. goto nla_put_failure;
  211. return nla_nest_end(skb, nest);
  212. nla_put_failure:
  213. nla_nest_cancel(skb, nest);
  214. return -EMSGSIZE;
  215. }
  216. static int drr_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  217. struct gnet_dump *d)
  218. {
  219. struct drr_class *cl = (struct drr_class *)arg;
  220. __u32 qlen = cl->qdisc->q.qlen;
  221. struct tc_drr_stats xstats;
  222. memset(&xstats, 0, sizeof(xstats));
  223. if (qlen)
  224. xstats.deficit = cl->deficit;
  225. if (gnet_stats_copy_basic(d, NULL, &cl->bstats) < 0 ||
  226. gnet_stats_copy_rate_est(d, &cl->bstats, &cl->rate_est) < 0 ||
  227. gnet_stats_copy_queue(d, NULL, &cl->qdisc->qstats, qlen) < 0)
  228. return -1;
  229. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  230. }
  231. static void drr_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  232. {
  233. struct drr_sched *q = qdisc_priv(sch);
  234. struct drr_class *cl;
  235. unsigned int i;
  236. if (arg->stop)
  237. return;
  238. for (i = 0; i < q->clhash.hashsize; i++) {
  239. hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
  240. if (arg->count < arg->skip) {
  241. arg->count++;
  242. continue;
  243. }
  244. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  245. arg->stop = 1;
  246. return;
  247. }
  248. arg->count++;
  249. }
  250. }
  251. }
  252. static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch,
  253. int *qerr)
  254. {
  255. struct drr_sched *q = qdisc_priv(sch);
  256. struct drr_class *cl;
  257. struct tcf_result res;
  258. struct tcf_proto *fl;
  259. int result;
  260. if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
  261. cl = drr_find_class(sch, skb->priority);
  262. if (cl != NULL)
  263. return cl;
  264. }
  265. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  266. fl = rcu_dereference_bh(q->filter_list);
  267. result = tc_classify(skb, fl, &res, false);
  268. if (result >= 0) {
  269. #ifdef CONFIG_NET_CLS_ACT
  270. switch (result) {
  271. case TC_ACT_QUEUED:
  272. case TC_ACT_STOLEN:
  273. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  274. case TC_ACT_SHOT:
  275. return NULL;
  276. }
  277. #endif
  278. cl = (struct drr_class *)res.class;
  279. if (cl == NULL)
  280. cl = drr_find_class(sch, res.classid);
  281. return cl;
  282. }
  283. return NULL;
  284. }
  285. static int drr_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  286. {
  287. struct drr_sched *q = qdisc_priv(sch);
  288. struct drr_class *cl;
  289. int err = 0;
  290. cl = drr_classify(skb, sch, &err);
  291. if (cl == NULL) {
  292. if (err & __NET_XMIT_BYPASS)
  293. qdisc_qstats_drop(sch);
  294. kfree_skb(skb);
  295. return err;
  296. }
  297. err = qdisc_enqueue(skb, cl->qdisc);
  298. if (unlikely(err != NET_XMIT_SUCCESS)) {
  299. if (net_xmit_drop_count(err)) {
  300. cl->qstats.drops++;
  301. qdisc_qstats_drop(sch);
  302. }
  303. return err;
  304. }
  305. if (cl->qdisc->q.qlen == 1) {
  306. list_add_tail(&cl->alist, &q->active);
  307. cl->deficit = cl->quantum;
  308. }
  309. sch->q.qlen++;
  310. return err;
  311. }
  312. static struct sk_buff *drr_dequeue(struct Qdisc *sch)
  313. {
  314. struct drr_sched *q = qdisc_priv(sch);
  315. struct drr_class *cl;
  316. struct sk_buff *skb;
  317. unsigned int len;
  318. if (list_empty(&q->active))
  319. goto out;
  320. while (1) {
  321. cl = list_first_entry(&q->active, struct drr_class, alist);
  322. skb = cl->qdisc->ops->peek(cl->qdisc);
  323. if (skb == NULL) {
  324. qdisc_warn_nonwc(__func__, cl->qdisc);
  325. goto out;
  326. }
  327. len = qdisc_pkt_len(skb);
  328. if (len <= cl->deficit) {
  329. cl->deficit -= len;
  330. skb = qdisc_dequeue_peeked(cl->qdisc);
  331. if (cl->qdisc->q.qlen == 0)
  332. list_del(&cl->alist);
  333. bstats_update(&cl->bstats, skb);
  334. qdisc_bstats_update(sch, skb);
  335. sch->q.qlen--;
  336. return skb;
  337. }
  338. cl->deficit += cl->quantum;
  339. list_move_tail(&cl->alist, &q->active);
  340. }
  341. out:
  342. return NULL;
  343. }
  344. static unsigned int drr_drop(struct Qdisc *sch)
  345. {
  346. struct drr_sched *q = qdisc_priv(sch);
  347. struct drr_class *cl;
  348. unsigned int len;
  349. list_for_each_entry(cl, &q->active, alist) {
  350. if (cl->qdisc->ops->drop) {
  351. len = cl->qdisc->ops->drop(cl->qdisc);
  352. if (len > 0) {
  353. sch->q.qlen--;
  354. if (cl->qdisc->q.qlen == 0)
  355. list_del(&cl->alist);
  356. return len;
  357. }
  358. }
  359. }
  360. return 0;
  361. }
  362. static int drr_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
  363. {
  364. struct drr_sched *q = qdisc_priv(sch);
  365. int err;
  366. err = qdisc_class_hash_init(&q->clhash);
  367. if (err < 0)
  368. return err;
  369. INIT_LIST_HEAD(&q->active);
  370. return 0;
  371. }
  372. static void drr_reset_qdisc(struct Qdisc *sch)
  373. {
  374. struct drr_sched *q = qdisc_priv(sch);
  375. struct drr_class *cl;
  376. unsigned int i;
  377. for (i = 0; i < q->clhash.hashsize; i++) {
  378. hlist_for_each_entry(cl, &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 *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, 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");