sch_drr.c 11 KB

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