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