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