sch_drr.c 11 KB

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