blob: fcecf5aac666829a8a00b10de31f61f6231e3cca [file] [log] [blame]
Jiri Pirkobf3994d2016-07-21 12:03:11 +02001/*
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
19struct cls_mall_filter {
20 struct tcf_exts exts;
21 struct tcf_result res;
22 u32 handle;
23 struct rcu_head rcu;
Yotam Gigib87f7932016-07-21 12:03:12 +020024 u32 flags;
Jiri Pirkobf3994d2016-07-21 12:03:11 +020025};
26
27struct cls_mall_head {
28 struct cls_mall_filter *filter;
29 struct rcu_head rcu;
30};
31
32static int mall_classify(struct sk_buff *skb, const struct tcf_proto *tp,
33 struct tcf_result *res)
34{
35 struct cls_mall_head *head = rcu_dereference_bh(tp->root);
36 struct cls_mall_filter *f = head->filter;
37
Yotam Gigib87f7932016-07-21 12:03:12 +020038 if (tc_skip_sw(f->flags))
39 return -1;
40
Jiri Pirkobf3994d2016-07-21 12:03:11 +020041 return tcf_exts_exec(skb, &f->exts, res);
42}
43
44static int mall_init(struct tcf_proto *tp)
45{
46 struct cls_mall_head *head;
47
48 head = kzalloc(sizeof(*head), GFP_KERNEL);
49 if (!head)
50 return -ENOBUFS;
51
52 rcu_assign_pointer(tp->root, head);
53
54 return 0;
55}
56
57static void mall_destroy_filter(struct rcu_head *head)
58{
59 struct cls_mall_filter *f = container_of(head, struct cls_mall_filter, rcu);
60
61 tcf_exts_destroy(&f->exts);
Yotam Gigib87f7932016-07-21 12:03:12 +020062
Jiri Pirkobf3994d2016-07-21 12:03:11 +020063 kfree(f);
64}
65
Yotam Gigib87f7932016-07-21 12:03:12 +020066static int mall_replace_hw_filter(struct tcf_proto *tp,
67 struct cls_mall_filter *f,
68 unsigned long cookie)
69{
70 struct net_device *dev = tp->q->dev_queue->dev;
71 struct tc_to_netdev offload;
72 struct tc_cls_matchall_offload mall_offload = {0};
73
74 offload.type = TC_SETUP_MATCHALL;
75 offload.cls_mall = &mall_offload;
76 offload.cls_mall->command = TC_CLSMATCHALL_REPLACE;
77 offload.cls_mall->exts = &f->exts;
78 offload.cls_mall->cookie = cookie;
79
80 return dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
81 &offload);
82}
83
84static void mall_destroy_hw_filter(struct tcf_proto *tp,
85 struct cls_mall_filter *f,
86 unsigned long cookie)
87{
88 struct net_device *dev = tp->q->dev_queue->dev;
89 struct tc_to_netdev offload;
90 struct tc_cls_matchall_offload mall_offload = {0};
91
92 offload.type = TC_SETUP_MATCHALL;
93 offload.cls_mall = &mall_offload;
94 offload.cls_mall->command = TC_CLSMATCHALL_DESTROY;
95 offload.cls_mall->exts = NULL;
96 offload.cls_mall->cookie = cookie;
97
98 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
99 &offload);
100}
101
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200102static bool mall_destroy(struct tcf_proto *tp, bool force)
103{
104 struct cls_mall_head *head = rtnl_dereference(tp->root);
Yotam Gigib87f7932016-07-21 12:03:12 +0200105 struct net_device *dev = tp->q->dev_queue->dev;
106 struct cls_mall_filter *f = head->filter;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200107
Yotam Gigib87f7932016-07-21 12:03:12 +0200108 if (!force && f)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200109 return false;
110
Yotam Gigib87f7932016-07-21 12:03:12 +0200111 if (f) {
112 if (tc_should_offload(dev, tp, f->flags))
113 mall_destroy_hw_filter(tp, f, (unsigned long) f);
114
115 call_rcu(&f->rcu, mall_destroy_filter);
116 }
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200117 kfree_rcu(head, rcu);
118 return true;
119}
120
121static unsigned long mall_get(struct tcf_proto *tp, u32 handle)
122{
123 struct cls_mall_head *head = rtnl_dereference(tp->root);
124 struct cls_mall_filter *f = head->filter;
125
126 if (f && f->handle == handle)
127 return (unsigned long) f;
128 return 0;
129}
130
131static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
132 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
133 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
134};
135
136static int mall_set_parms(struct net *net, struct tcf_proto *tp,
137 struct cls_mall_filter *f,
138 unsigned long base, struct nlattr **tb,
139 struct nlattr *est, bool ovr)
140{
141 struct tcf_exts e;
142 int err;
143
Yotam Gigiec2507d2017-01-03 19:20:24 +0200144 err = tcf_exts_init(&e, TCA_MATCHALL_ACT, 0);
145 if (err)
146 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200147 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
148 if (err < 0)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200149 goto errout;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200150
151 if (tb[TCA_MATCHALL_CLASSID]) {
152 f->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
153 tcf_bind_filter(tp, &f->res, base);
154 }
155
156 tcf_exts_change(tp, &f->exts, &e);
157
158 return 0;
Yotam Gigiec2507d2017-01-03 19:20:24 +0200159errout:
160 tcf_exts_destroy(&e);
161 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200162}
163
164static int mall_change(struct net *net, struct sk_buff *in_skb,
165 struct tcf_proto *tp, unsigned long base,
166 u32 handle, struct nlattr **tca,
167 unsigned long *arg, bool ovr)
168{
169 struct cls_mall_head *head = rtnl_dereference(tp->root);
170 struct cls_mall_filter *fold = (struct cls_mall_filter *) *arg;
Yotam Gigib87f7932016-07-21 12:03:12 +0200171 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200172 struct cls_mall_filter *f;
173 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
Yotam Gigib87f7932016-07-21 12:03:12 +0200174 u32 flags = 0;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200175 int err;
176
177 if (!tca[TCA_OPTIONS])
178 return -EINVAL;
179
180 if (head->filter)
181 return -EBUSY;
182
183 if (fold)
184 return -EINVAL;
185
186 err = nla_parse_nested(tb, TCA_MATCHALL_MAX,
187 tca[TCA_OPTIONS], mall_policy);
188 if (err < 0)
189 return err;
190
Yotam Gigib87f7932016-07-21 12:03:12 +0200191 if (tb[TCA_MATCHALL_FLAGS]) {
192 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
193 if (!tc_flags_valid(flags))
194 return -EINVAL;
195 }
196
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200197 f = kzalloc(sizeof(*f), GFP_KERNEL);
198 if (!f)
199 return -ENOBUFS;
200
Yotam Gigiec2507d2017-01-03 19:20:24 +0200201 err = tcf_exts_init(&f->exts, TCA_MATCHALL_ACT, 0);
202 if (err)
203 goto err_exts_init;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200204
205 if (!handle)
206 handle = 1;
207 f->handle = handle;
Yotam Gigib87f7932016-07-21 12:03:12 +0200208 f->flags = flags;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200209
210 err = mall_set_parms(net, tp, f, base, tb, tca[TCA_RATE], ovr);
211 if (err)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200212 goto err_set_parms;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200213
Yotam Gigib87f7932016-07-21 12:03:12 +0200214 if (tc_should_offload(dev, tp, flags)) {
215 err = mall_replace_hw_filter(tp, f, (unsigned long) f);
216 if (err) {
217 if (tc_skip_sw(flags))
Yotam Gigiec2507d2017-01-03 19:20:24 +0200218 goto err_replace_hw_filter;
Yotam Gigib87f7932016-07-21 12:03:12 +0200219 else
220 err = 0;
221 }
222 }
223
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200224 *arg = (unsigned long) f;
225 rcu_assign_pointer(head->filter, f);
226
227 return 0;
228
Yotam Gigiec2507d2017-01-03 19:20:24 +0200229err_replace_hw_filter:
230err_set_parms:
231 tcf_exts_destroy(&f->exts);
232err_exts_init:
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200233 kfree(f);
234 return err;
235}
236
237static int mall_delete(struct tcf_proto *tp, unsigned long arg)
238{
239 struct cls_mall_head *head = rtnl_dereference(tp->root);
240 struct cls_mall_filter *f = (struct cls_mall_filter *) arg;
Yotam Gigib87f7932016-07-21 12:03:12 +0200241 struct net_device *dev = tp->q->dev_queue->dev;
242
243 if (tc_should_offload(dev, tp, f->flags))
244 mall_destroy_hw_filter(tp, f, (unsigned long) f);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200245
246 RCU_INIT_POINTER(head->filter, NULL);
247 tcf_unbind_filter(tp, &f->res);
248 call_rcu(&f->rcu, mall_destroy_filter);
249 return 0;
250}
251
252static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
253{
254 struct cls_mall_head *head = rtnl_dereference(tp->root);
255 struct cls_mall_filter *f = head->filter;
256
257 if (arg->count < arg->skip)
258 goto skip;
259 if (arg->fn(tp, (unsigned long) f, arg) < 0)
260 arg->stop = 1;
261skip:
262 arg->count++;
263}
264
265static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
266 struct sk_buff *skb, struct tcmsg *t)
267{
268 struct cls_mall_filter *f = (struct cls_mall_filter *) fh;
269 struct nlattr *nest;
270
271 if (!f)
272 return skb->len;
273
274 t->tcm_handle = f->handle;
275
276 nest = nla_nest_start(skb, TCA_OPTIONS);
277 if (!nest)
278 goto nla_put_failure;
279
280 if (f->res.classid &&
281 nla_put_u32(skb, TCA_MATCHALL_CLASSID, f->res.classid))
282 goto nla_put_failure;
283
284 if (tcf_exts_dump(skb, &f->exts))
285 goto nla_put_failure;
286
287 nla_nest_end(skb, nest);
288
289 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
290 goto nla_put_failure;
291
292 return skb->len;
293
294nla_put_failure:
295 nla_nest_cancel(skb, nest);
296 return -1;
297}
298
299static struct tcf_proto_ops cls_mall_ops __read_mostly = {
300 .kind = "matchall",
301 .classify = mall_classify,
302 .init = mall_init,
303 .destroy = mall_destroy,
304 .get = mall_get,
305 .change = mall_change,
306 .delete = mall_delete,
307 .walk = mall_walk,
308 .dump = mall_dump,
309 .owner = THIS_MODULE,
310};
311
312static int __init cls_mall_init(void)
313{
314 return register_tcf_proto_ops(&cls_mall_ops);
315}
316
317static void __exit cls_mall_exit(void)
318{
319 unregister_tcf_proto_ops(&cls_mall_ops);
320}
321
322module_init(cls_mall_init);
323module_exit(cls_mall_exit);
324
325MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
326MODULE_DESCRIPTION("Match-all classifier");
327MODULE_LICENSE("GPL v2");