GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / sched / sch_ingress.c
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 void ingress_ingress_block_set(struct Qdisc *sch, u32 block_index)
67 {
68         struct ingress_sched_data *q = qdisc_priv(sch);
69
70         q->block_info.block_index = block_index;
71 }
72
73 static u32 ingress_ingress_block_get(struct Qdisc *sch)
74 {
75         struct ingress_sched_data *q = qdisc_priv(sch);
76
77         return q->block_info.block_index;
78 }
79
80 static int ingress_init(struct Qdisc *sch, struct nlattr *opt,
81                         struct netlink_ext_ack *extack)
82 {
83         struct ingress_sched_data *q = qdisc_priv(sch);
84         struct net_device *dev = qdisc_dev(sch);
85
86         if (sch->parent != TC_H_INGRESS)
87                 return -EOPNOTSUPP;
88
89         net_inc_ingress_queue();
90
91         mini_qdisc_pair_init(&q->miniqp, sch, &dev->miniq_ingress);
92
93         q->block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
94         q->block_info.chain_head_change = clsact_chain_head_change;
95         q->block_info.chain_head_change_priv = &q->miniqp;
96
97         return tcf_block_get_ext(&q->block, sch, &q->block_info, extack);
98 }
99
100 static void ingress_destroy(struct Qdisc *sch)
101 {
102         struct ingress_sched_data *q = qdisc_priv(sch);
103
104         if (sch->parent != TC_H_INGRESS)
105                 return;
106
107         tcf_block_put_ext(q->block, sch, &q->block_info);
108         net_dec_ingress_queue();
109 }
110
111 static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
112 {
113         struct nlattr *nest;
114
115         nest = nla_nest_start(skb, TCA_OPTIONS);
116         if (nest == NULL)
117                 goto nla_put_failure;
118
119         return nla_nest_end(skb, nest);
120
121 nla_put_failure:
122         nla_nest_cancel(skb, nest);
123         return -1;
124 }
125
126 static const struct Qdisc_class_ops ingress_class_ops = {
127         .leaf           =       ingress_leaf,
128         .find           =       ingress_find,
129         .walk           =       ingress_walk,
130         .tcf_block      =       ingress_tcf_block,
131         .bind_tcf       =       ingress_bind_filter,
132         .unbind_tcf     =       ingress_unbind_filter,
133 };
134
135 static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
136         .cl_ops                 =       &ingress_class_ops,
137         .id                     =       "ingress",
138         .priv_size              =       sizeof(struct ingress_sched_data),
139         .static_flags           =       TCQ_F_INGRESS | TCQ_F_CPUSTATS,
140         .init                   =       ingress_init,
141         .destroy                =       ingress_destroy,
142         .dump                   =       ingress_dump,
143         .ingress_block_set      =       ingress_ingress_block_set,
144         .ingress_block_get      =       ingress_ingress_block_get,
145         .owner                  =       THIS_MODULE,
146 };
147
148 struct clsact_sched_data {
149         struct tcf_block *ingress_block;
150         struct tcf_block *egress_block;
151         struct tcf_block_ext_info ingress_block_info;
152         struct tcf_block_ext_info egress_block_info;
153         struct mini_Qdisc_pair miniqp_ingress;
154         struct mini_Qdisc_pair miniqp_egress;
155 };
156
157 static unsigned long clsact_find(struct Qdisc *sch, u32 classid)
158 {
159         switch (TC_H_MIN(classid)) {
160         case TC_H_MIN(TC_H_MIN_INGRESS):
161         case TC_H_MIN(TC_H_MIN_EGRESS):
162                 return TC_H_MIN(classid);
163         default:
164                 return 0;
165         }
166 }
167
168 static unsigned long clsact_bind_filter(struct Qdisc *sch,
169                                         unsigned long parent, u32 classid)
170 {
171         return clsact_find(sch, classid);
172 }
173
174 static struct tcf_block *clsact_tcf_block(struct Qdisc *sch, unsigned long cl,
175                                           struct netlink_ext_ack *extack)
176 {
177         struct clsact_sched_data *q = qdisc_priv(sch);
178
179         switch (cl) {
180         case TC_H_MIN(TC_H_MIN_INGRESS):
181                 return q->ingress_block;
182         case TC_H_MIN(TC_H_MIN_EGRESS):
183                 return q->egress_block;
184         default:
185                 return NULL;
186         }
187 }
188
189 static void clsact_ingress_block_set(struct Qdisc *sch, u32 block_index)
190 {
191         struct clsact_sched_data *q = qdisc_priv(sch);
192
193         q->ingress_block_info.block_index = block_index;
194 }
195
196 static void clsact_egress_block_set(struct Qdisc *sch, u32 block_index)
197 {
198         struct clsact_sched_data *q = qdisc_priv(sch);
199
200         q->egress_block_info.block_index = block_index;
201 }
202
203 static u32 clsact_ingress_block_get(struct Qdisc *sch)
204 {
205         struct clsact_sched_data *q = qdisc_priv(sch);
206
207         return q->ingress_block_info.block_index;
208 }
209
210 static u32 clsact_egress_block_get(struct Qdisc *sch)
211 {
212         struct clsact_sched_data *q = qdisc_priv(sch);
213
214         return q->egress_block_info.block_index;
215 }
216
217 static int clsact_init(struct Qdisc *sch, struct nlattr *opt,
218                        struct netlink_ext_ack *extack)
219 {
220         struct clsact_sched_data *q = qdisc_priv(sch);
221         struct net_device *dev = qdisc_dev(sch);
222         int err;
223
224         if (sch->parent != TC_H_CLSACT)
225                 return -EOPNOTSUPP;
226
227         net_inc_ingress_queue();
228         net_inc_egress_queue();
229
230         mini_qdisc_pair_init(&q->miniqp_ingress, sch, &dev->miniq_ingress);
231
232         q->ingress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
233         q->ingress_block_info.chain_head_change = clsact_chain_head_change;
234         q->ingress_block_info.chain_head_change_priv = &q->miniqp_ingress;
235
236         err = tcf_block_get_ext(&q->ingress_block, sch, &q->ingress_block_info,
237                                 extack);
238         if (err)
239                 return err;
240
241         mini_qdisc_pair_init(&q->miniqp_egress, sch, &dev->miniq_egress);
242
243         q->egress_block_info.binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS;
244         q->egress_block_info.chain_head_change = clsact_chain_head_change;
245         q->egress_block_info.chain_head_change_priv = &q->miniqp_egress;
246
247         return tcf_block_get_ext(&q->egress_block, sch, &q->egress_block_info, extack);
248 }
249
250 static void clsact_destroy(struct Qdisc *sch)
251 {
252         struct clsact_sched_data *q = qdisc_priv(sch);
253
254         if (sch->parent != TC_H_CLSACT)
255                 return;
256
257         tcf_block_put_ext(q->egress_block, sch, &q->egress_block_info);
258         tcf_block_put_ext(q->ingress_block, sch, &q->ingress_block_info);
259
260         net_dec_ingress_queue();
261         net_dec_egress_queue();
262 }
263
264 static const struct Qdisc_class_ops clsact_class_ops = {
265         .leaf           =       ingress_leaf,
266         .find           =       clsact_find,
267         .walk           =       ingress_walk,
268         .tcf_block      =       clsact_tcf_block,
269         .bind_tcf       =       clsact_bind_filter,
270         .unbind_tcf     =       ingress_unbind_filter,
271 };
272
273 static struct Qdisc_ops clsact_qdisc_ops __read_mostly = {
274         .cl_ops                 =       &clsact_class_ops,
275         .id                     =       "clsact",
276         .priv_size              =       sizeof(struct clsact_sched_data),
277         .static_flags           =       TCQ_F_INGRESS | TCQ_F_CPUSTATS,
278         .init                   =       clsact_init,
279         .destroy                =       clsact_destroy,
280         .dump                   =       ingress_dump,
281         .ingress_block_set      =       clsact_ingress_block_set,
282         .egress_block_set       =       clsact_egress_block_set,
283         .ingress_block_get      =       clsact_ingress_block_get,
284         .egress_block_get       =       clsact_egress_block_get,
285         .owner                  =       THIS_MODULE,
286 };
287
288 static int __init ingress_module_init(void)
289 {
290         int ret;
291
292         ret = register_qdisc(&ingress_qdisc_ops);
293         if (!ret) {
294                 ret = register_qdisc(&clsact_qdisc_ops);
295                 if (ret)
296                         unregister_qdisc(&ingress_qdisc_ops);
297         }
298
299         return ret;
300 }
301
302 static void __exit ingress_module_exit(void)
303 {
304         unregister_qdisc(&ingress_qdisc_ops);
305         unregister_qdisc(&clsact_qdisc_ops);
306 }
307
308 module_init(ingress_module_init);
309 module_exit(ingress_module_exit);
310
311 MODULE_ALIAS("sch_clsact");
312 MODULE_LICENSE("GPL");