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