]>
Commit | Line | Data |
---|---|---|
bf3994d2 JP |
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 | ||
fd62d9f5 | 19 | struct cls_mall_head { |
bf3994d2 JP |
20 | struct tcf_exts exts; |
21 | struct tcf_result res; | |
22 | u32 handle; | |
b87f7936 | 23 | u32 flags; |
bf3994d2 JP |
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); | |
bf3994d2 | 31 | |
fd62d9f5 | 32 | if (tc_skip_sw(head->flags)) |
b87f7936 YG |
33 | return -1; |
34 | ||
91c393ae | 35 | *res = head->res; |
fd62d9f5 | 36 | return tcf_exts_exec(skb, &head->exts, res); |
bf3994d2 JP |
37 | } |
38 | ||
39 | static int mall_init(struct tcf_proto *tp) | |
40 | { | |
bf3994d2 JP |
41 | return 0; |
42 | } | |
43 | ||
fd62d9f5 | 44 | static void mall_destroy_rcu(struct rcu_head *rcu) |
bf3994d2 | 45 | { |
fd62d9f5 YG |
46 | struct cls_mall_head *head = container_of(rcu, struct cls_mall_head, |
47 | rcu); | |
bf3994d2 | 48 | |
fd62d9f5 YG |
49 | tcf_exts_destroy(&head->exts); |
50 | kfree(head); | |
bf3994d2 JP |
51 | } |
52 | ||
b87f7936 | 53 | static int mall_replace_hw_filter(struct tcf_proto *tp, |
fd62d9f5 | 54 | struct cls_mall_head *head, |
b87f7936 YG |
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}; | |
c7d2b2f5 | 60 | int err; |
b87f7936 YG |
61 | |
62 | offload.type = TC_SETUP_MATCHALL; | |
63 | offload.cls_mall = &mall_offload; | |
64 | offload.cls_mall->command = TC_CLSMATCHALL_REPLACE; | |
fd62d9f5 | 65 | offload.cls_mall->exts = &head->exts; |
b87f7936 YG |
66 | offload.cls_mall->cookie = cookie; |
67 | ||
a5fcf8a6 JP |
68 | err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, |
69 | tp->chain->index, | |
70 | tp->protocol, &offload); | |
c7d2b2f5 OG |
71 | if (!err) |
72 | head->flags |= TCA_CLS_FLAGS_IN_HW; | |
73 | ||
74 | return err; | |
b87f7936 YG |
75 | } |
76 | ||
77 | static void mall_destroy_hw_filter(struct tcf_proto *tp, | |
fd62d9f5 | 78 | struct cls_mall_head *head, |
b87f7936 YG |
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 | ||
a5fcf8a6 JP |
91 | dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->chain->index, |
92 | tp->protocol, &offload); | |
b87f7936 YG |
93 | } |
94 | ||
763dbf63 | 95 | static void mall_destroy(struct tcf_proto *tp) |
bf3994d2 JP |
96 | { |
97 | struct cls_mall_head *head = rtnl_dereference(tp->root); | |
b87f7936 | 98 | struct net_device *dev = tp->q->dev_queue->dev; |
bf3994d2 | 99 | |
fd62d9f5 | 100 | if (!head) |
763dbf63 | 101 | return; |
bf3994d2 | 102 | |
fd62d9f5 YG |
103 | if (tc_should_offload(dev, tp, head->flags)) |
104 | mall_destroy_hw_filter(tp, head, (unsigned long) head); | |
b87f7936 | 105 | |
fd62d9f5 | 106 | call_rcu(&head->rcu, mall_destroy_rcu); |
bf3994d2 JP |
107 | } |
108 | ||
109 | static unsigned long mall_get(struct tcf_proto *tp, u32 handle) | |
110 | { | |
fd62d9f5 | 111 | return 0UL; |
bf3994d2 JP |
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, | |
fd62d9f5 | 120 | struct cls_mall_head *head, |
bf3994d2 JP |
121 | unsigned long base, struct nlattr **tb, |
122 | struct nlattr *est, bool ovr) | |
123 | { | |
124 | struct tcf_exts e; | |
125 | int err; | |
126 | ||
ec2507d2 YG |
127 | err = tcf_exts_init(&e, TCA_MATCHALL_ACT, 0); |
128 | if (err) | |
129 | return err; | |
bf3994d2 JP |
130 | err = tcf_exts_validate(net, tp, tb, est, &e, ovr); |
131 | if (err < 0) | |
ec2507d2 | 132 | goto errout; |
bf3994d2 JP |
133 | |
134 | if (tb[TCA_MATCHALL_CLASSID]) { | |
fd62d9f5 YG |
135 | head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]); |
136 | tcf_bind_filter(tp, &head->res, base); | |
bf3994d2 JP |
137 | } |
138 | ||
fd62d9f5 | 139 | tcf_exts_change(tp, &head->exts, &e); |
bf3994d2 JP |
140 | |
141 | return 0; | |
ec2507d2 YG |
142 | errout: |
143 | tcf_exts_destroy(&e); | |
144 | return err; | |
bf3994d2 JP |
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); | |
b87f7936 | 153 | struct net_device *dev = tp->q->dev_queue->dev; |
bf3994d2 | 154 | struct nlattr *tb[TCA_MATCHALL_MAX + 1]; |
fd62d9f5 | 155 | struct cls_mall_head *new; |
b87f7936 | 156 | u32 flags = 0; |
bf3994d2 JP |
157 | int err; |
158 | ||
159 | if (!tca[TCA_OPTIONS]) | |
160 | return -EINVAL; | |
161 | ||
fd62d9f5 YG |
162 | if (head) |
163 | return -EEXIST; | |
bf3994d2 | 164 | |
fceb6435 JB |
165 | err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS], |
166 | mall_policy, NULL); | |
bf3994d2 JP |
167 | if (err < 0) |
168 | return err; | |
169 | ||
b87f7936 YG |
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 | ||
fd62d9f5 YG |
176 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
177 | if (!new) | |
bf3994d2 JP |
178 | return -ENOBUFS; |
179 | ||
e2160156 | 180 | err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0); |
ec2507d2 YG |
181 | if (err) |
182 | goto err_exts_init; | |
bf3994d2 JP |
183 | |
184 | if (!handle) | |
185 | handle = 1; | |
fd62d9f5 YG |
186 | new->handle = handle; |
187 | new->flags = flags; | |
bf3994d2 | 188 | |
fd62d9f5 | 189 | err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr); |
bf3994d2 | 190 | if (err) |
ec2507d2 | 191 | goto err_set_parms; |
bf3994d2 | 192 | |
b87f7936 | 193 | if (tc_should_offload(dev, tp, flags)) { |
fd62d9f5 | 194 | err = mall_replace_hw_filter(tp, new, (unsigned long) new); |
b87f7936 YG |
195 | if (err) { |
196 | if (tc_skip_sw(flags)) | |
ec2507d2 | 197 | goto err_replace_hw_filter; |
b87f7936 YG |
198 | else |
199 | err = 0; | |
200 | } | |
201 | } | |
c7d2b2f5 OG |
202 | |
203 | if (!tc_in_hw(new->flags)) | |
204 | new->flags |= TCA_CLS_FLAGS_NOT_IN_HW; | |
b87f7936 | 205 | |
fd62d9f5 YG |
206 | *arg = (unsigned long) head; |
207 | rcu_assign_pointer(tp->root, new); | |
bf3994d2 JP |
208 | return 0; |
209 | ||
ec2507d2 YG |
210 | err_replace_hw_filter: |
211 | err_set_parms: | |
e2160156 | 212 | tcf_exts_destroy(&new->exts); |
ec2507d2 | 213 | err_exts_init: |
fd62d9f5 | 214 | kfree(new); |
bf3994d2 JP |
215 | return err; |
216 | } | |
217 | ||
763dbf63 | 218 | static int mall_delete(struct tcf_proto *tp, unsigned long arg, bool *last) |
bf3994d2 | 219 | { |
fd62d9f5 | 220 | return -EOPNOTSUPP; |
bf3994d2 JP |
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); | |
bf3994d2 JP |
226 | |
227 | if (arg->count < arg->skip) | |
228 | goto skip; | |
fd62d9f5 | 229 | if (arg->fn(tp, (unsigned long) head, arg) < 0) |
bf3994d2 JP |
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 | { | |
fd62d9f5 | 238 | struct cls_mall_head *head = (struct cls_mall_head *) fh; |
bf3994d2 JP |
239 | struct nlattr *nest; |
240 | ||
fd62d9f5 | 241 | if (!head) |
bf3994d2 JP |
242 | return skb->len; |
243 | ||
fd62d9f5 | 244 | t->tcm_handle = head->handle; |
bf3994d2 JP |
245 | |
246 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
247 | if (!nest) | |
248 | goto nla_put_failure; | |
249 | ||
fd62d9f5 YG |
250 | if (head->res.classid && |
251 | nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid)) | |
bf3994d2 JP |
252 | goto nla_put_failure; |
253 | ||
7a335ada OG |
254 | if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags)) |
255 | goto nla_put_failure; | |
256 | ||
fd62d9f5 | 257 | if (tcf_exts_dump(skb, &head->exts)) |
bf3994d2 JP |
258 | goto nla_put_failure; |
259 | ||
260 | nla_nest_end(skb, nest); | |
261 | ||
fd62d9f5 | 262 | if (tcf_exts_dump_stats(skb, &head->exts) < 0) |
bf3994d2 JP |
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"); |