sch_ingress.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* net/sched/sch_ingress.c - Ingress and clsact qdisc
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version
  6. * 2 of the License, or (at your option) any later version.
  7. *
  8. * Authors: Jamal Hadi Salim 1999
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/list.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/rtnetlink.h>
  15. #include <net/netlink.h>
  16. #include <net/pkt_sched.h>
  17. #include <net/pkt_cls.h>
  18. struct ingress_sched_data {
  19. struct tcf_block *block;
  20. };
  21. static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
  22. {
  23. return NULL;
  24. }
  25. static unsigned long ingress_get(struct Qdisc *sch, u32 classid)
  26. {
  27. return TC_H_MIN(classid) + 1;
  28. }
  29. static bool ingress_cl_offload(u32 classid)
  30. {
  31. return true;
  32. }
  33. static unsigned long ingress_bind_filter(struct Qdisc *sch,
  34. unsigned long parent, u32 classid)
  35. {
  36. return ingress_get(sch, classid);
  37. }
  38. static void ingress_put(struct Qdisc *sch, unsigned long cl)
  39. {
  40. }
  41. static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  42. {
  43. }
  44. static struct tcf_block *ingress_tcf_block(struct Qdisc *sch, unsigned long cl)
  45. {
  46. struct ingress_sched_data *q = qdisc_priv(sch);
  47. return q->block;
  48. }
  49. static int ingress_init(struct Qdisc *sch, struct nlattr *opt)
  50. {
  51. struct ingress_sched_data *q = qdisc_priv(sch);
  52. struct net_device *dev = qdisc_dev(sch);
  53. int err;
  54. err = tcf_block_get(&q->block, &dev->ingress_cl_list);
  55. if (err)
  56. return err;
  57. net_inc_ingress_queue();
  58. sch->flags |= TCQ_F_CPUSTATS;
  59. return 0;
  60. }
  61. static void ingress_destroy(struct Qdisc *sch)
  62. {
  63. struct ingress_sched_data *q = qdisc_priv(sch);
  64. tcf_block_put(q->block);
  65. net_dec_ingress_queue();
  66. }
  67. static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
  68. {
  69. struct nlattr *nest;
  70. nest = nla_nest_start(skb, TCA_OPTIONS);
  71. if (nest == NULL)
  72. goto nla_put_failure;
  73. return nla_nest_end(skb, nest);
  74. nla_put_failure:
  75. nla_nest_cancel(skb, nest);
  76. return -1;
  77. }
  78. static const struct Qdisc_class_ops ingress_class_ops = {
  79. .leaf = ingress_leaf,
  80. .get = ingress_get,
  81. .put = ingress_put,
  82. .walk = ingress_walk,
  83. .tcf_block = ingress_tcf_block,
  84. .tcf_cl_offload = ingress_cl_offload,
  85. .bind_tcf = ingress_bind_filter,
  86. .unbind_tcf = ingress_put,
  87. };
  88. static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
  89. .cl_ops = &ingress_class_ops,
  90. .id = "ingress",
  91. .priv_size = sizeof(struct ingress_sched_data),
  92. .init = ingress_init,
  93. .destroy = ingress_destroy,
  94. .dump = ingress_dump,
  95. .owner = THIS_MODULE,
  96. };
  97. struct clsact_sched_data {
  98. struct tcf_block *ingress_block;
  99. struct tcf_block *egress_block;
  100. };
  101. static unsigned long clsact_get(struct Qdisc *sch, u32 classid)
  102. {
  103. switch (TC_H_MIN(classid)) {
  104. case TC_H_MIN(TC_H_MIN_INGRESS):
  105. case TC_H_MIN(TC_H_MIN_EGRESS):
  106. return TC_H_MIN(classid);
  107. default:
  108. return 0;
  109. }
  110. }
  111. static bool clsact_cl_offload(u32 classid)
  112. {
  113. return TC_H_MIN(classid) == TC_H_MIN(TC_H_MIN_INGRESS);
  114. }
  115. static unsigned long clsact_bind_filter(struct Qdisc *sch,
  116. unsigned long parent, u32 classid)
  117. {
  118. return clsact_get(sch, classid);
  119. }
  120. static struct tcf_block *clsact_tcf_block(struct Qdisc *sch, unsigned long cl)
  121. {
  122. struct clsact_sched_data *q = qdisc_priv(sch);
  123. switch (cl) {
  124. case TC_H_MIN(TC_H_MIN_INGRESS):
  125. return q->ingress_block;
  126. case TC_H_MIN(TC_H_MIN_EGRESS):
  127. return q->egress_block;
  128. default:
  129. return NULL;
  130. }
  131. }
  132. static int clsact_init(struct Qdisc *sch, struct nlattr *opt)
  133. {
  134. struct clsact_sched_data *q = qdisc_priv(sch);
  135. struct net_device *dev = qdisc_dev(sch);
  136. int err;
  137. err = tcf_block_get(&q->ingress_block, &dev->ingress_cl_list);
  138. if (err)
  139. return err;
  140. err = tcf_block_get(&q->egress_block, &dev->egress_cl_list);
  141. if (err)
  142. return err;
  143. net_inc_ingress_queue();
  144. net_inc_egress_queue();
  145. sch->flags |= TCQ_F_CPUSTATS;
  146. return 0;
  147. }
  148. static void clsact_destroy(struct Qdisc *sch)
  149. {
  150. struct clsact_sched_data *q = qdisc_priv(sch);
  151. tcf_block_put(q->egress_block);
  152. tcf_block_put(q->ingress_block);
  153. net_dec_ingress_queue();
  154. net_dec_egress_queue();
  155. }
  156. static const struct Qdisc_class_ops clsact_class_ops = {
  157. .leaf = ingress_leaf,
  158. .get = clsact_get,
  159. .put = ingress_put,
  160. .walk = ingress_walk,
  161. .tcf_block = clsact_tcf_block,
  162. .tcf_cl_offload = clsact_cl_offload,
  163. .bind_tcf = clsact_bind_filter,
  164. .unbind_tcf = ingress_put,
  165. };
  166. static struct Qdisc_ops clsact_qdisc_ops __read_mostly = {
  167. .cl_ops = &clsact_class_ops,
  168. .id = "clsact",
  169. .priv_size = sizeof(struct clsact_sched_data),
  170. .init = clsact_init,
  171. .destroy = clsact_destroy,
  172. .dump = ingress_dump,
  173. .owner = THIS_MODULE,
  174. };
  175. static int __init ingress_module_init(void)
  176. {
  177. int ret;
  178. ret = register_qdisc(&ingress_qdisc_ops);
  179. if (!ret) {
  180. ret = register_qdisc(&clsact_qdisc_ops);
  181. if (ret)
  182. unregister_qdisc(&ingress_qdisc_ops);
  183. }
  184. return ret;
  185. }
  186. static void __exit ingress_module_exit(void)
  187. {
  188. unregister_qdisc(&ingress_qdisc_ops);
  189. unregister_qdisc(&clsact_qdisc_ops);
  190. }
  191. module_init(ingress_module_init);
  192. module_exit(ingress_module_exit);
  193. MODULE_ALIAS("sch_clsact");
  194. MODULE_LICENSE("GPL");