sch_ingress.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. struct tcf_block_ext_info block_info;
  21. struct mini_Qdisc_pair miniqp;
  22. };
  23. static struct Qdisc *ingress_leaf(struct Qdisc *sch, unsigned long arg)
  24. {
  25. return NULL;
  26. }
  27. static unsigned long ingress_find(struct Qdisc *sch, u32 classid)
  28. {
  29. return TC_H_MIN(classid) + 1;
  30. }
  31. static unsigned long ingress_bind_filter(struct Qdisc *sch,
  32. unsigned long parent, u32 classid)
  33. {
  34. return ingress_find(sch, classid);
  35. }
  36. static void ingress_unbind_filter(struct Qdisc *sch, unsigned long cl)
  37. {
  38. }
  39. static void ingress_walk(struct Qdisc *sch, struct qdisc_walker *walker)
  40. {
  41. }
  42. static struct tcf_block *ingress_tcf_block(struct Qdisc *sch, unsigned long cl,
  43. struct netlink_ext_ack *extack)
  44. {
  45. struct ingress_sched_data *q = qdisc_priv(sch);
  46. return q->block;
  47. }
  48. static void clsact_chain_head_change(struct tcf_proto *tp_head, void *priv)
  49. {
  50. struct mini_Qdisc_pair *miniqp = priv;
  51. mini_qdisc_pair_swap(miniqp, tp_head);
  52. };
  53. static void ingress_ingress_block_set(struct Qdisc *sch, u32 block_index)
  54. {
  55. struct ingress_sched_data *q = qdisc_priv(sch);
  56. q->block_info.block_index = block_index;
  57. }
  58. static u32 ingress_ingress_block_get(struct Qdisc *sch)
  59. {
  60. struct ingress_sched_data *q = qdisc_priv(sch);
  61. return q->block_info.block_index;
  62. }
  63. static int ingress_init(struct Qdisc *sch, struct nlattr *opt,
  64. struct netlink_ext_ack *extack)
  65. {
  66. struct ingress_sched_data *q = qdisc_priv(sch);
  67. struct net_device *dev = qdisc_dev(sch);
  68. net_inc_ingress_queue();
  69. mini_qdisc_pair_init(&q->miniqp, sch, &dev->miniq_ingress);
  70. q->block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
  71. q->block_info.chain_head_change = clsact_chain_head_change;
  72. q->block_info.chain_head_change_priv = &q->miniqp;
  73. return tcf_block_get_ext(&q->block, sch, &q->block_info, extack);
  74. }
  75. static void ingress_destroy(struct Qdisc *sch)
  76. {
  77. struct ingress_sched_data *q = qdisc_priv(sch);
  78. tcf_block_put_ext(q->block, sch, &q->block_info);
  79. net_dec_ingress_queue();
  80. }
  81. static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
  82. {
  83. struct nlattr *nest;
  84. nest = nla_nest_start(skb, TCA_OPTIONS);
  85. if (nest == NULL)
  86. goto nla_put_failure;
  87. return nla_nest_end(skb, nest);
  88. nla_put_failure:
  89. nla_nest_cancel(skb, nest);
  90. return -1;
  91. }
  92. static const struct Qdisc_class_ops ingress_class_ops = {
  93. .leaf = ingress_leaf,
  94. .find = ingress_find,
  95. .walk = ingress_walk,
  96. .tcf_block = ingress_tcf_block,
  97. .bind_tcf = ingress_bind_filter,
  98. .unbind_tcf = ingress_unbind_filter,
  99. };
  100. static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
  101. .cl_ops = &ingress_class_ops,
  102. .id = "ingress",
  103. .priv_size = sizeof(struct ingress_sched_data),
  104. .static_flags = TCQ_F_CPUSTATS,
  105. .init = ingress_init,
  106. .destroy = ingress_destroy,
  107. .dump = ingress_dump,
  108. .ingress_block_set = ingress_ingress_block_set,
  109. .ingress_block_get = ingress_ingress_block_get,
  110. .owner = THIS_MODULE,
  111. };
  112. struct clsact_sched_data {
  113. struct tcf_block *ingress_block;
  114. struct tcf_block *egress_block;
  115. struct tcf_block_ext_info ingress_block_info;
  116. struct tcf_block_ext_info egress_block_info;
  117. struct mini_Qdisc_pair miniqp_ingress;
  118. struct mini_Qdisc_pair miniqp_egress;
  119. };
  120. static unsigned long clsact_find(struct Qdisc *sch, u32 classid)
  121. {
  122. switch (TC_H_MIN(classid)) {
  123. case TC_H_MIN(TC_H_MIN_INGRESS):
  124. case TC_H_MIN(TC_H_MIN_EGRESS):
  125. return TC_H_MIN(classid);
  126. default:
  127. return 0;
  128. }
  129. }
  130. static unsigned long clsact_bind_filter(struct Qdisc *sch,
  131. unsigned long parent, u32 classid)
  132. {
  133. return clsact_find(sch, classid);
  134. }
  135. static struct tcf_block *clsact_tcf_block(struct Qdisc *sch, unsigned long cl,
  136. struct netlink_ext_ack *extack)
  137. {
  138. struct clsact_sched_data *q = qdisc_priv(sch);
  139. switch (cl) {
  140. case TC_H_MIN(TC_H_MIN_INGRESS):
  141. return q->ingress_block;
  142. case TC_H_MIN(TC_H_MIN_EGRESS):
  143. return q->egress_block;
  144. default:
  145. return NULL;
  146. }
  147. }
  148. static void clsact_ingress_block_set(struct Qdisc *sch, u32 block_index)
  149. {
  150. struct clsact_sched_data *q = qdisc_priv(sch);
  151. q->ingress_block_info.block_index = block_index;
  152. }
  153. static void clsact_egress_block_set(struct Qdisc *sch, u32 block_index)
  154. {
  155. struct clsact_sched_data *q = qdisc_priv(sch);
  156. q->egress_block_info.block_index = block_index;
  157. }
  158. static u32 clsact_ingress_block_get(struct Qdisc *sch)
  159. {
  160. struct clsact_sched_data *q = qdisc_priv(sch);
  161. return q->ingress_block_info.block_index;
  162. }
  163. static u32 clsact_egress_block_get(struct Qdisc *sch)
  164. {
  165. struct clsact_sched_data *q = qdisc_priv(sch);
  166. return q->egress_block_info.block_index;
  167. }
  168. static int clsact_init(struct Qdisc *sch, struct nlattr *opt,
  169. struct netlink_ext_ack *extack)
  170. {
  171. struct clsact_sched_data *q = qdisc_priv(sch);
  172. struct net_device *dev = qdisc_dev(sch);
  173. int err;
  174. net_inc_ingress_queue();
  175. net_inc_egress_queue();
  176. mini_qdisc_pair_init(&q->miniqp_ingress, sch, &dev->miniq_ingress);
  177. q->ingress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
  178. q->ingress_block_info.chain_head_change = clsact_chain_head_change;
  179. q->ingress_block_info.chain_head_change_priv = &q->miniqp_ingress;
  180. err = tcf_block_get_ext(&q->ingress_block, sch, &q->ingress_block_info,
  181. extack);
  182. if (err)
  183. return err;
  184. mini_qdisc_pair_init(&q->miniqp_egress, sch, &dev->miniq_egress);
  185. q->egress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS;
  186. q->egress_block_info.chain_head_change = clsact_chain_head_change;
  187. q->egress_block_info.chain_head_change_priv = &q->miniqp_egress;
  188. return tcf_block_get_ext(&q->egress_block, sch, &q->egress_block_info, extack);
  189. }
  190. static void clsact_destroy(struct Qdisc *sch)
  191. {
  192. struct clsact_sched_data *q = qdisc_priv(sch);
  193. tcf_block_put_ext(q->egress_block, sch, &q->egress_block_info);
  194. tcf_block_put_ext(q->ingress_block, sch, &q->ingress_block_info);
  195. net_dec_ingress_queue();
  196. net_dec_egress_queue();
  197. }
  198. static const struct Qdisc_class_ops clsact_class_ops = {
  199. .leaf = ingress_leaf,
  200. .find = clsact_find,
  201. .walk = ingress_walk,
  202. .tcf_block = clsact_tcf_block,
  203. .bind_tcf = clsact_bind_filter,
  204. .unbind_tcf = ingress_unbind_filter,
  205. };
  206. static struct Qdisc_ops clsact_qdisc_ops __read_mostly = {
  207. .cl_ops = &clsact_class_ops,
  208. .id = "clsact",
  209. .priv_size = sizeof(struct clsact_sched_data),
  210. .static_flags = TCQ_F_CPUSTATS,
  211. .init = clsact_init,
  212. .destroy = clsact_destroy,
  213. .dump = ingress_dump,
  214. .ingress_block_set = clsact_ingress_block_set,
  215. .egress_block_set = clsact_egress_block_set,
  216. .ingress_block_get = clsact_ingress_block_get,
  217. .egress_block_get = clsact_egress_block_get,
  218. .owner = THIS_MODULE,
  219. };
  220. static int __init ingress_module_init(void)
  221. {
  222. int ret;
  223. ret = register_qdisc(&ingress_qdisc_ops);
  224. if (!ret) {
  225. ret = register_qdisc(&clsact_qdisc_ops);
  226. if (ret)
  227. unregister_qdisc(&ingress_qdisc_ops);
  228. }
  229. return ret;
  230. }
  231. static void __exit ingress_module_exit(void)
  232. {
  233. unregister_qdisc(&ingress_qdisc_ops);
  234. unregister_qdisc(&clsact_qdisc_ops);
  235. }
  236. module_init(ingress_module_init);
  237. module_exit(ingress_module_exit);
  238. MODULE_ALIAS("sch_clsact");
  239. MODULE_LICENSE("GPL");