sch_drr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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_est 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 *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, &cl->rate_est,
  77. qdisc_root_sleeping_lock(sch),
  78. tca[TCA_RATE]);
  79. if (err)
  80. return err;
  81. }
  82. sch_tree_lock(sch);
  83. if (tb[TCA_DRR_QUANTUM])
  84. cl->quantum = quantum;
  85. sch_tree_unlock(sch);
  86. return 0;
  87. }
  88. cl = kzalloc(sizeof(struct drr_class), GFP_KERNEL);
  89. if (cl == NULL)
  90. return -ENOBUFS;
  91. cl->refcnt = 1;
  92. cl->common.classid = classid;
  93. cl->quantum = quantum;
  94. cl->qdisc = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  95. &pfifo_qdisc_ops, classid);
  96. if (cl->qdisc == NULL)
  97. cl->qdisc = &noop_qdisc;
  98. if (tca[TCA_RATE]) {
  99. err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
  100. qdisc_root_sleeping_lock(sch),
  101. tca[TCA_RATE]);
  102. if (err) {
  103. qdisc_destroy(cl->qdisc);
  104. kfree(cl);
  105. return err;
  106. }
  107. }
  108. sch_tree_lock(sch);
  109. qdisc_class_hash_insert(&q->clhash, &cl->common);
  110. sch_tree_unlock(sch);
  111. qdisc_class_hash_grow(sch, &q->clhash);
  112. *arg = (unsigned long)cl;
  113. return 0;
  114. }
  115. static void drr_destroy_class(struct Qdisc *sch, struct drr_class *cl)
  116. {
  117. gen_kill_estimator(&cl->bstats, &cl->rate_est);
  118. qdisc_destroy(cl->qdisc);
  119. kfree(cl);
  120. }
  121. static int drr_delete_class(struct Qdisc *sch, unsigned long arg)
  122. {
  123. struct drr_sched *q = qdisc_priv(sch);
  124. struct drr_class *cl = (struct drr_class *)arg;
  125. if (cl->filter_cnt > 0)
  126. return -EBUSY;
  127. sch_tree_lock(sch);
  128. drr_purge_queue(cl);
  129. qdisc_class_hash_remove(&q->clhash, &cl->common);
  130. BUG_ON(--cl->refcnt == 0);
  131. /*
  132. * This shouldn't happen: we "hold" one cops->get() when called
  133. * from tc_ctl_tclass; the destroy method is done from cops->put().
  134. */
  135. sch_tree_unlock(sch);
  136. return 0;
  137. }
  138. static unsigned long drr_get_class(struct Qdisc *sch, u32 classid)
  139. {
  140. struct drr_class *cl = drr_find_class(sch, classid);
  141. if (cl != NULL)
  142. cl->refcnt++;
  143. return (unsigned long)cl;
  144. }
  145. static void drr_put_class(struct Qdisc *sch, unsigned long arg)
  146. {
  147. struct drr_class *cl = (struct drr_class *)arg;
  148. if (--cl->refcnt == 0)
  149. drr_destroy_class(sch, cl);
  150. }
  151. static struct tcf_proto **drr_tcf_chain(struct Qdisc *sch, unsigned long cl)
  152. {
  153. struct drr_sched *q = qdisc_priv(sch);
  154. if (cl)
  155. return NULL;
  156. return &q->filter_list;
  157. }
  158. static unsigned long drr_bind_tcf(struct Qdisc *sch, unsigned long parent,
  159. u32 classid)
  160. {
  161. struct drr_class *cl = drr_find_class(sch, classid);
  162. if (cl != NULL)
  163. cl->filter_cnt++;
  164. return (unsigned long)cl;
  165. }
  166. static void drr_unbind_tcf(struct Qdisc *sch, unsigned long arg)
  167. {
  168. struct drr_class *cl = (struct drr_class *)arg;
  169. cl->filter_cnt--;
  170. }
  171. static int drr_graft_class(struct Qdisc *sch, unsigned long arg,
  172. struct Qdisc *new, struct Qdisc **old)
  173. {
  174. struct drr_class *cl = (struct drr_class *)arg;
  175. if (new == NULL) {
  176. new = qdisc_create_dflt(qdisc_dev(sch), sch->dev_queue,
  177. &pfifo_qdisc_ops, cl->common.classid);
  178. if (new == NULL)
  179. new = &noop_qdisc;
  180. }
  181. sch_tree_lock(sch);
  182. drr_purge_queue(cl);
  183. *old = cl->qdisc;
  184. cl->qdisc = new;
  185. sch_tree_unlock(sch);
  186. return 0;
  187. }
  188. static struct Qdisc *drr_class_leaf(struct Qdisc *sch, unsigned long arg)
  189. {
  190. struct drr_class *cl = (struct drr_class *)arg;
  191. return cl->qdisc;
  192. }
  193. static void drr_qlen_notify(struct Qdisc *csh, unsigned long arg)
  194. {
  195. struct drr_class *cl = (struct drr_class *)arg;
  196. if (cl->qdisc->q.qlen == 0)
  197. list_del(&cl->alist);
  198. }
  199. static int drr_dump_class(struct Qdisc *sch, unsigned long arg,
  200. struct sk_buff *skb, struct tcmsg *tcm)
  201. {
  202. struct drr_class *cl = (struct drr_class *)arg;
  203. struct nlattr *nest;
  204. tcm->tcm_parent = TC_H_ROOT;
  205. tcm->tcm_handle = cl->common.classid;
  206. tcm->tcm_info = cl->qdisc->handle;
  207. nest = nla_nest_start(skb, TCA_OPTIONS);
  208. if (nest == NULL)
  209. goto nla_put_failure;
  210. NLA_PUT_U32(skb, TCA_DRR_QUANTUM, cl->quantum);
  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. struct tc_drr_stats xstats;
  221. memset(&xstats, 0, sizeof(xstats));
  222. if (cl->qdisc->q.qlen) {
  223. xstats.deficit = cl->deficit;
  224. cl->qdisc->qstats.qlen = cl->qdisc->q.qlen;
  225. }
  226. if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
  227. gnet_stats_copy_rate_est(d, &cl->bstats, &cl->rate_est) < 0 ||
  228. gnet_stats_copy_queue(d, &cl->qdisc->qstats) < 0)
  229. return -1;
  230. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  231. }
  232. static void drr_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  233. {
  234. struct drr_sched *q = qdisc_priv(sch);
  235. struct drr_class *cl;
  236. struct hlist_node *n;
  237. unsigned int i;
  238. if (arg->stop)
  239. return;
  240. for (i = 0; i < q->clhash.hashsize; i++) {
  241. hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
  242. if (arg->count < arg->skip) {
  243. arg->count++;
  244. continue;
  245. }
  246. if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
  247. arg->stop = 1;
  248. return;
  249. }
  250. arg->count++;
  251. }
  252. }
  253. }
  254. static struct drr_class *drr_classify(struct sk_buff *skb, struct Qdisc *sch,
  255. int *qerr)
  256. {
  257. struct drr_sched *q = qdisc_priv(sch);
  258. struct drr_class *cl;
  259. struct tcf_result res;
  260. int result;
  261. if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
  262. cl = drr_find_class(sch, skb->priority);
  263. if (cl != NULL)
  264. return cl;
  265. }
  266. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  267. result = tc_classify(skb, q->filter_list, &res);
  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. unsigned int len;
  290. int err;
  291. cl = drr_classify(skb, sch, &err);
  292. if (cl == NULL) {
  293. if (err & __NET_XMIT_BYPASS)
  294. sch->qstats.drops++;
  295. kfree_skb(skb);
  296. return err;
  297. }
  298. len = qdisc_pkt_len(skb);
  299. err = qdisc_enqueue(skb, cl->qdisc);
  300. if (unlikely(err != NET_XMIT_SUCCESS)) {
  301. if (net_xmit_drop_count(err)) {
  302. cl->qstats.drops++;
  303. sch->qstats.drops++;
  304. }
  305. return err;
  306. }
  307. if (cl->qdisc->q.qlen == 1) {
  308. list_add_tail(&cl->alist, &q->active);
  309. cl->deficit = cl->quantum;
  310. }
  311. cl->bstats.packets++;
  312. cl->bstats.bytes += len;
  313. sch->bstats.packets++;
  314. sch->bstats.bytes += len;
  315. sch->q.qlen++;
  316. return err;
  317. }
  318. static struct sk_buff *drr_dequeue(struct Qdisc *sch)
  319. {
  320. struct drr_sched *q = qdisc_priv(sch);
  321. struct drr_class *cl;
  322. struct sk_buff *skb;
  323. unsigned int len;
  324. if (list_empty(&q->active))
  325. goto out;
  326. while (1) {
  327. cl = list_first_entry(&q->active, struct drr_class, alist);
  328. skb = cl->qdisc->ops->peek(cl->qdisc);
  329. if (skb == NULL)
  330. goto out;
  331. len = qdisc_pkt_len(skb);
  332. if (len <= cl->deficit) {
  333. cl->deficit -= len;
  334. skb = qdisc_dequeue_peeked(cl->qdisc);
  335. if (cl->qdisc->q.qlen == 0)
  336. list_del(&cl->alist);
  337. sch->q.qlen--;
  338. return skb;
  339. }
  340. cl->deficit += cl->quantum;
  341. list_move_tail(&cl->alist, &q->active);
  342. }
  343. out:
  344. return NULL;
  345. }
  346. static unsigned int drr_drop(struct Qdisc *sch)
  347. {
  348. struct drr_sched *q = qdisc_priv(sch);
  349. struct drr_class *cl;
  350. unsigned int len;
  351. list_for_each_entry(cl, &q->active, alist) {
  352. if (cl->qdisc->ops->drop) {
  353. len = cl->qdisc->ops->drop(cl->qdisc);
  354. if (len > 0) {
  355. sch->q.qlen--;
  356. if (cl->qdisc->q.qlen == 0)
  357. list_del(&cl->alist);
  358. return len;
  359. }
  360. }
  361. }
  362. return 0;
  363. }
  364. static int drr_init_qdisc(struct Qdisc *sch, struct nlattr *opt)
  365. {
  366. struct drr_sched *q = qdisc_priv(sch);
  367. int err;
  368. err = qdisc_class_hash_init(&q->clhash);
  369. if (err < 0)
  370. return err;
  371. INIT_LIST_HEAD(&q->active);
  372. return 0;
  373. }
  374. static void drr_reset_qdisc(struct Qdisc *sch)
  375. {
  376. struct drr_sched *q = qdisc_priv(sch);
  377. struct drr_class *cl;
  378. struct hlist_node *n;
  379. unsigned int i;
  380. for (i = 0; i < q->clhash.hashsize; i++) {
  381. hlist_for_each_entry(cl, n, &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 *n, *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, n, 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");