sch_drr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. struct netlink_ext_ack *extack)
  58. {
  59. struct drr_sched *q = qdisc_priv(sch);
  60. struct drr_class *cl = (struct drr_class *)*arg;
  61. struct nlattr *opt = tca[TCA_OPTIONS];
  62. struct nlattr *tb[TCA_DRR_MAX + 1];
  63. u32 quantum;
  64. int err;
  65. if (!opt) {
  66. NL_SET_ERR_MSG(extack, "DRR options are required for this operation");
  67. return -EINVAL;
  68. }
  69. err = nla_parse_nested(tb, TCA_DRR_MAX, opt, drr_policy, extack);
  70. if (err < 0)
  71. return err;
  72. if (tb[TCA_DRR_QUANTUM]) {
  73. quantum = nla_get_u32(tb[TCA_DRR_QUANTUM]);
  74. if (quantum == 0) {
  75. NL_SET_ERR_MSG(extack, "Specified DRR quantum cannot be zero");
  76. return -EINVAL;
  77. }
  78. } else
  79. quantum = psched_mtu(qdisc_dev(sch));
  80. if (cl != NULL) {
  81. if (tca[TCA_RATE]) {
  82. err = gen_replace_estimator(&cl->bstats, NULL,
  83. &cl->rate_est,
  84. NULL,
  85. qdisc_root_sleeping_running(sch),
  86. tca[TCA_RATE]);
  87. if (err) {
  88. NL_SET_ERR_MSG(extack, "Failed to replace estimator");
  89. return err;
  90. }
  91. }
  92. sch_tree_lock(sch);
  93. if (tb[TCA_DRR_QUANTUM])
  94. cl->quantum = quantum;
  95. sch_tree_unlock(sch);
  96. return 0;
  97. }
  98. cl = kzalloc(sizeof(struct drr_class), GFP_KERNEL);
  99. if (cl == NULL)
  100. return -ENOBUFS;
  101. cl->common.classid = classid;
  102. cl->quantum = quantum;
  103. cl->qdisc = qdisc_create_dflt(sch->dev_queue,
  104. &pfifo_qdisc_ops, classid,
  105. NULL);
  106. if (cl->qdisc == NULL)
  107. cl->qdisc = &noop_qdisc;
  108. else
  109. qdisc_hash_add(cl->qdisc, true);
  110. if (tca[TCA_RATE]) {
  111. err = gen_replace_estimator(&cl->bstats, NULL, &cl->rate_est,
  112. NULL,
  113. qdisc_root_sleeping_running(sch),
  114. tca[TCA_RATE]);
  115. if (err) {
  116. NL_SET_ERR_MSG(extack, "Failed to replace estimator");
  117. qdisc_destroy(cl->qdisc);
  118. kfree(cl);
  119. return err;
  120. }
  121. }
  122. sch_tree_lock(sch);
  123. qdisc_class_hash_insert(&q->clhash, &cl->common);
  124. sch_tree_unlock(sch);
  125. qdisc_class_hash_grow(sch, &q->clhash);
  126. *arg = (unsigned long)cl;
  127. return 0;
  128. }
  129. static void drr_destroy_class(struct Qdisc *sch, struct drr_class *cl)
  130. {
  131. gen_kill_estimator(&cl->rate_est);
  132. qdisc_destroy(cl->qdisc);
  133. kfree(cl);
  134. }
  135. static int drr_delete_class(struct Qdisc *sch, unsigned long arg)
  136. {
  137. struct drr_sched *q = qdisc_priv(sch);
  138. struct drr_class *cl = (struct drr_class *)arg;
  139. if (cl->filter_cnt > 0)
  140. return -EBUSY;
  141. sch_tree_lock(sch);
  142. drr_purge_queue(cl);
  143. qdisc_class_hash_remove(&q->clhash, &cl->common);
  144. sch_tree_unlock(sch);
  145. drr_destroy_class(sch, cl);
  146. return 0;
  147. }
  148. static unsigned long drr_search_class(struct Qdisc *sch, u32 classid)
  149. {
  150. return (unsigned long)drr_find_class(sch, classid);
  151. }
  152. static struct tcf_block *drr_tcf_block(struct Qdisc *sch, unsigned long cl,
  153. struct netlink_ext_ack *extack)
  154. {
  155. struct drr_sched *q = qdisc_priv(sch);
  156. if (cl) {
  157. NL_SET_ERR_MSG(extack, "DRR classid must be zero");
  158. return NULL;
  159. }
  160. return q->block;
  161. }
  162. static unsigned long drr_bind_tcf(struct Qdisc *sch, unsigned long parent,
  163. u32 classid)
  164. {
  165. struct drr_class *cl = drr_find_class(sch, classid);
  166. if (cl != NULL)
  167. cl->filter_cnt++;
  168. return (unsigned long)cl;
  169. }
  170. static void drr_unbind_tcf(struct Qdisc *sch, unsigned long arg)
  171. {
  172. struct drr_class *cl = (struct drr_class *)arg;
  173. cl->filter_cnt--;
  174. }
  175. static int drr_graft_class(struct Qdisc *sch, unsigned long arg,
  176. struct Qdisc *new, struct Qdisc **old,
  177. struct netlink_ext_ack *extack)
  178. {
  179. struct drr_class *cl = (struct drr_class *)arg;
  180. if (new == NULL) {
  181. new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
  182. cl->common.classid, NULL);
  183. if (new == NULL)
  184. new = &noop_qdisc;
  185. }
  186. *old = qdisc_replace(sch, new, &cl->qdisc);
  187. return 0;
  188. }
  189. static struct Qdisc *drr_class_leaf(struct Qdisc *sch, unsigned long arg)
  190. {
  191. struct drr_class *cl = (struct drr_class *)arg;
  192. return cl->qdisc;
  193. }
  194. static void drr_qlen_notify(struct Qdisc *csh, unsigned long arg)
  195. {
  196. struct drr_class *cl = (struct drr_class *)arg;
  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. if (nla_put_u32(skb, TCA_DRR_QUANTUM, cl->quantum))
  211. goto nla_put_failure;
  212. return nla_nest_end(skb, nest);
  213. nla_put_failure:
  214. nla_nest_cancel(skb, nest);
  215. return -EMSGSIZE;
  216. }
  217. static int drr_dump_class_stats(struct Qdisc *sch, unsigned long arg,
  218. struct gnet_dump *d)
  219. {
  220. struct drr_class *cl = (struct drr_class *)arg;
  221. __u32 qlen = cl->qdisc->q.qlen;
  222. struct tc_drr_stats xstats;
  223. memset(&xstats, 0, sizeof(xstats));
  224. if (qlen)
  225. xstats.deficit = cl->deficit;
  226. if (gnet_stats_copy_basic(qdisc_root_sleeping_running(sch),
  227. d, NULL, &cl->bstats) < 0 ||
  228. gnet_stats_copy_rate_est(d, &cl->rate_est) < 0 ||
  229. gnet_stats_copy_queue(d, NULL, &cl->qdisc->qstats, qlen) < 0)
  230. return -1;
  231. return gnet_stats_copy_app(d, &xstats, sizeof(xstats));
  232. }
  233. static void drr_walk(struct Qdisc *sch, struct qdisc_walker *arg)
  234. {
  235. struct drr_sched *q = qdisc_priv(sch);
  236. struct drr_class *cl;
  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, &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. struct tcf_proto *fl;
  261. int result;
  262. if (TC_H_MAJ(skb->priority ^ sch->handle) == 0) {
  263. cl = drr_find_class(sch, skb->priority);
  264. if (cl != NULL)
  265. return cl;
  266. }
  267. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
  268. fl = rcu_dereference_bh(q->filter_list);
  269. result = tcf_classify(skb, fl, &res, false);
  270. if (result >= 0) {
  271. #ifdef CONFIG_NET_CLS_ACT
  272. switch (result) {
  273. case TC_ACT_QUEUED:
  274. case TC_ACT_STOLEN:
  275. case TC_ACT_TRAP:
  276. *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
  277. /* fall through */
  278. case TC_ACT_SHOT:
  279. return NULL;
  280. }
  281. #endif
  282. cl = (struct drr_class *)res.class;
  283. if (cl == NULL)
  284. cl = drr_find_class(sch, res.classid);
  285. return cl;
  286. }
  287. return NULL;
  288. }
  289. static int drr_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  290. struct sk_buff **to_free)
  291. {
  292. struct drr_sched *q = qdisc_priv(sch);
  293. struct drr_class *cl;
  294. int err = 0;
  295. cl = drr_classify(skb, sch, &err);
  296. if (cl == NULL) {
  297. if (err & __NET_XMIT_BYPASS)
  298. qdisc_qstats_drop(sch);
  299. __qdisc_drop(skb, to_free);
  300. return err;
  301. }
  302. err = qdisc_enqueue(skb, cl->qdisc, to_free);
  303. if (unlikely(err != NET_XMIT_SUCCESS)) {
  304. if (net_xmit_drop_count(err)) {
  305. cl->qstats.drops++;
  306. qdisc_qstats_drop(sch);
  307. }
  308. return err;
  309. }
  310. if (cl->qdisc->q.qlen == 1) {
  311. list_add_tail(&cl->alist, &q->active);
  312. cl->deficit = cl->quantum;
  313. }
  314. qdisc_qstats_backlog_inc(sch, skb);
  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. qdisc_warn_nonwc(__func__, cl->qdisc);
  331. goto out;
  332. }
  333. len = qdisc_pkt_len(skb);
  334. if (len <= cl->deficit) {
  335. cl->deficit -= len;
  336. skb = qdisc_dequeue_peeked(cl->qdisc);
  337. if (unlikely(skb == NULL))
  338. goto out;
  339. if (cl->qdisc->q.qlen == 0)
  340. list_del(&cl->alist);
  341. bstats_update(&cl->bstats, skb);
  342. qdisc_bstats_update(sch, skb);
  343. qdisc_qstats_backlog_dec(sch, skb);
  344. sch->q.qlen--;
  345. return skb;
  346. }
  347. cl->deficit += cl->quantum;
  348. list_move_tail(&cl->alist, &q->active);
  349. }
  350. out:
  351. return NULL;
  352. }
  353. static int drr_init_qdisc(struct Qdisc *sch, struct nlattr *opt,
  354. struct netlink_ext_ack *extack)
  355. {
  356. struct drr_sched *q = qdisc_priv(sch);
  357. int err;
  358. err = tcf_block_get(&q->block, &q->filter_list, sch, extack);
  359. if (err)
  360. return err;
  361. err = qdisc_class_hash_init(&q->clhash);
  362. if (err < 0)
  363. return err;
  364. INIT_LIST_HEAD(&q->active);
  365. return 0;
  366. }
  367. static void drr_reset_qdisc(struct Qdisc *sch)
  368. {
  369. struct drr_sched *q = qdisc_priv(sch);
  370. struct drr_class *cl;
  371. unsigned int i;
  372. for (i = 0; i < q->clhash.hashsize; i++) {
  373. hlist_for_each_entry(cl, &q->clhash.hash[i], common.hnode) {
  374. if (cl->qdisc->q.qlen)
  375. list_del(&cl->alist);
  376. qdisc_reset(cl->qdisc);
  377. }
  378. }
  379. sch->qstats.backlog = 0;
  380. sch->q.qlen = 0;
  381. }
  382. static void drr_destroy_qdisc(struct Qdisc *sch)
  383. {
  384. struct drr_sched *q = qdisc_priv(sch);
  385. struct drr_class *cl;
  386. struct hlist_node *next;
  387. unsigned int i;
  388. tcf_block_put(q->block);
  389. for (i = 0; i < q->clhash.hashsize; i++) {
  390. hlist_for_each_entry_safe(cl, next, &q->clhash.hash[i],
  391. common.hnode)
  392. drr_destroy_class(sch, cl);
  393. }
  394. qdisc_class_hash_destroy(&q->clhash);
  395. }
  396. static const struct Qdisc_class_ops drr_class_ops = {
  397. .change = drr_change_class,
  398. .delete = drr_delete_class,
  399. .find = drr_search_class,
  400. .tcf_block = drr_tcf_block,
  401. .bind_tcf = drr_bind_tcf,
  402. .unbind_tcf = drr_unbind_tcf,
  403. .graft = drr_graft_class,
  404. .leaf = drr_class_leaf,
  405. .qlen_notify = drr_qlen_notify,
  406. .dump = drr_dump_class,
  407. .dump_stats = drr_dump_class_stats,
  408. .walk = drr_walk,
  409. };
  410. static struct Qdisc_ops drr_qdisc_ops __read_mostly = {
  411. .cl_ops = &drr_class_ops,
  412. .id = "drr",
  413. .priv_size = sizeof(struct drr_sched),
  414. .enqueue = drr_enqueue,
  415. .dequeue = drr_dequeue,
  416. .peek = qdisc_peek_dequeued,
  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");