]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/sched/cls_matchall.c
x86/bugs: Make cpu_show_common() static
[mirror_ubuntu-artful-kernel.git] / net / sched / cls_matchall.c
1 /*
2 * net/sched/cls_matchll.c Match-all classifier
3 *
4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15
16 #include <net/sch_generic.h>
17 #include <net/pkt_cls.h>
18
19 struct cls_mall_head {
20 struct tcf_exts exts;
21 struct tcf_result res;
22 u32 handle;
23 u32 flags;
24 struct rcu_head rcu;
25 };
26
27 static int mall_classify(struct sk_buff *skb, const struct tcf_proto *tp,
28 struct tcf_result *res)
29 {
30 struct cls_mall_head *head = rcu_dereference_bh(tp->root);
31
32 if (tc_skip_sw(head->flags))
33 return -1;
34
35 *res = head->res;
36 return tcf_exts_exec(skb, &head->exts, res);
37 }
38
39 static int mall_init(struct tcf_proto *tp)
40 {
41 return 0;
42 }
43
44 static void mall_destroy_rcu(struct rcu_head *rcu)
45 {
46 struct cls_mall_head *head = container_of(rcu, struct cls_mall_head,
47 rcu);
48
49 tcf_exts_destroy(&head->exts);
50 kfree(head);
51 }
52
53 static int mall_replace_hw_filter(struct tcf_proto *tp,
54 struct cls_mall_head *head,
55 unsigned long cookie)
56 {
57 struct net_device *dev = tp->q->dev_queue->dev;
58 struct tc_to_netdev offload;
59 struct tc_cls_matchall_offload mall_offload = {0};
60 int err;
61
62 offload.type = TC_SETUP_MATCHALL;
63 offload.cls_mall = &mall_offload;
64 offload.cls_mall->command = TC_CLSMATCHALL_REPLACE;
65 offload.cls_mall->exts = &head->exts;
66 offload.cls_mall->cookie = cookie;
67
68 err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
69 tp->chain->index,
70 tp->protocol, &offload);
71 if (!err)
72 head->flags |= TCA_CLS_FLAGS_IN_HW;
73
74 return err;
75 }
76
77 static void mall_destroy_hw_filter(struct tcf_proto *tp,
78 struct cls_mall_head *head,
79 unsigned long cookie)
80 {
81 struct net_device *dev = tp->q->dev_queue->dev;
82 struct tc_to_netdev offload;
83 struct tc_cls_matchall_offload mall_offload = {0};
84
85 offload.type = TC_SETUP_MATCHALL;
86 offload.cls_mall = &mall_offload;
87 offload.cls_mall->command = TC_CLSMATCHALL_DESTROY;
88 offload.cls_mall->exts = NULL;
89 offload.cls_mall->cookie = cookie;
90
91 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->chain->index,
92 tp->protocol, &offload);
93 }
94
95 static void mall_destroy(struct tcf_proto *tp)
96 {
97 struct cls_mall_head *head = rtnl_dereference(tp->root);
98 struct net_device *dev = tp->q->dev_queue->dev;
99
100 if (!head)
101 return;
102
103 if (tc_should_offload(dev, tp, head->flags))
104 mall_destroy_hw_filter(tp, head, (unsigned long) head);
105
106 call_rcu(&head->rcu, mall_destroy_rcu);
107 }
108
109 static unsigned long mall_get(struct tcf_proto *tp, u32 handle)
110 {
111 return 0UL;
112 }
113
114 static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
115 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
116 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
117 };
118
119 static int mall_set_parms(struct net *net, struct tcf_proto *tp,
120 struct cls_mall_head *head,
121 unsigned long base, struct nlattr **tb,
122 struct nlattr *est, bool ovr)
123 {
124 struct tcf_exts e;
125 int err;
126
127 err = tcf_exts_init(&e, TCA_MATCHALL_ACT, 0);
128 if (err)
129 return err;
130 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
131 if (err < 0)
132 goto errout;
133
134 if (tb[TCA_MATCHALL_CLASSID]) {
135 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
136 tcf_bind_filter(tp, &head->res, base);
137 }
138
139 tcf_exts_change(tp, &head->exts, &e);
140
141 return 0;
142 errout:
143 tcf_exts_destroy(&e);
144 return err;
145 }
146
147 static int mall_change(struct net *net, struct sk_buff *in_skb,
148 struct tcf_proto *tp, unsigned long base,
149 u32 handle, struct nlattr **tca,
150 unsigned long *arg, bool ovr)
151 {
152 struct cls_mall_head *head = rtnl_dereference(tp->root);
153 struct net_device *dev = tp->q->dev_queue->dev;
154 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
155 struct cls_mall_head *new;
156 u32 flags = 0;
157 int err;
158
159 if (!tca[TCA_OPTIONS])
160 return -EINVAL;
161
162 if (head)
163 return -EEXIST;
164
165 err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
166 mall_policy, NULL);
167 if (err < 0)
168 return err;
169
170 if (tb[TCA_MATCHALL_FLAGS]) {
171 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
172 if (!tc_flags_valid(flags))
173 return -EINVAL;
174 }
175
176 new = kzalloc(sizeof(*new), GFP_KERNEL);
177 if (!new)
178 return -ENOBUFS;
179
180 err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
181 if (err)
182 goto err_exts_init;
183
184 if (!handle)
185 handle = 1;
186 new->handle = handle;
187 new->flags = flags;
188
189 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
190 if (err)
191 goto err_set_parms;
192
193 if (tc_should_offload(dev, tp, flags)) {
194 err = mall_replace_hw_filter(tp, new, (unsigned long) new);
195 if (err) {
196 if (tc_skip_sw(flags))
197 goto err_replace_hw_filter;
198 else
199 err = 0;
200 }
201 }
202
203 if (!tc_in_hw(new->flags))
204 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
205
206 *arg = (unsigned long) head;
207 rcu_assign_pointer(tp->root, new);
208 return 0;
209
210 err_replace_hw_filter:
211 err_set_parms:
212 tcf_exts_destroy(&new->exts);
213 err_exts_init:
214 kfree(new);
215 return err;
216 }
217
218 static int mall_delete(struct tcf_proto *tp, unsigned long arg, bool *last)
219 {
220 return -EOPNOTSUPP;
221 }
222
223 static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
224 {
225 struct cls_mall_head *head = rtnl_dereference(tp->root);
226
227 if (arg->count < arg->skip)
228 goto skip;
229 if (arg->fn(tp, (unsigned long) head, arg) < 0)
230 arg->stop = 1;
231 skip:
232 arg->count++;
233 }
234
235 static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
236 struct sk_buff *skb, struct tcmsg *t)
237 {
238 struct cls_mall_head *head = (struct cls_mall_head *) fh;
239 struct nlattr *nest;
240
241 if (!head)
242 return skb->len;
243
244 t->tcm_handle = head->handle;
245
246 nest = nla_nest_start(skb, TCA_OPTIONS);
247 if (!nest)
248 goto nla_put_failure;
249
250 if (head->res.classid &&
251 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
252 goto nla_put_failure;
253
254 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
255 goto nla_put_failure;
256
257 if (tcf_exts_dump(skb, &head->exts))
258 goto nla_put_failure;
259
260 nla_nest_end(skb, nest);
261
262 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
263 goto nla_put_failure;
264
265 return skb->len;
266
267 nla_put_failure:
268 nla_nest_cancel(skb, nest);
269 return -1;
270 }
271
272 static struct tcf_proto_ops cls_mall_ops __read_mostly = {
273 .kind = "matchall",
274 .classify = mall_classify,
275 .init = mall_init,
276 .destroy = mall_destroy,
277 .get = mall_get,
278 .change = mall_change,
279 .delete = mall_delete,
280 .walk = mall_walk,
281 .dump = mall_dump,
282 .owner = THIS_MODULE,
283 };
284
285 static int __init cls_mall_init(void)
286 {
287 return register_tcf_proto_ops(&cls_mall_ops);
288 }
289
290 static void __exit cls_mall_exit(void)
291 {
292 unregister_tcf_proto_ops(&cls_mall_ops);
293 }
294
295 module_init(cls_mall_init);
296 module_exit(cls_mall_exit);
297
298 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
299 MODULE_DESCRIPTION("Match-all classifier");
300 MODULE_LICENSE("GPL v2");