a8853ada22f6b3d1bd314e3b153694033a7dadd7
[platform/kernel/linux-rpi.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         return tcf_exts_exec(skb, &head->exts, res);
36 }
37
38 static int mall_init(struct tcf_proto *tp)
39 {
40         return 0;
41 }
42
43 static void mall_destroy_rcu(struct rcu_head *rcu)
44 {
45         struct cls_mall_head *head = container_of(rcu, struct cls_mall_head,
46                                                   rcu);
47
48         tcf_exts_destroy(&head->exts);
49         kfree(head);
50 }
51
52 static int mall_replace_hw_filter(struct tcf_proto *tp,
53                                   struct cls_mall_head *head,
54                                   unsigned long cookie)
55 {
56         struct net_device *dev = tp->q->dev_queue->dev;
57         struct tc_to_netdev offload;
58         struct tc_cls_matchall_offload mall_offload = {0};
59         int err;
60
61         offload.cls_mall = &mall_offload;
62         offload.cls_mall->command = TC_CLSMATCHALL_REPLACE;
63         offload.cls_mall->exts = &head->exts;
64         offload.cls_mall->cookie = cookie;
65
66         err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSMATCHALL,
67                                             tp->q->handle, tp->chain->index,
68                                             tp->protocol, &offload);
69         if (!err)
70                 head->flags |= TCA_CLS_FLAGS_IN_HW;
71
72         return err;
73 }
74
75 static void mall_destroy_hw_filter(struct tcf_proto *tp,
76                                    struct cls_mall_head *head,
77                                    unsigned long cookie)
78 {
79         struct net_device *dev = tp->q->dev_queue->dev;
80         struct tc_to_netdev offload;
81         struct tc_cls_matchall_offload mall_offload = {0};
82
83         offload.cls_mall = &mall_offload;
84         offload.cls_mall->command = TC_CLSMATCHALL_DESTROY;
85         offload.cls_mall->exts = NULL;
86         offload.cls_mall->cookie = cookie;
87
88         dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSMATCHALL, tp->q->handle,
89                                       tp->chain->index, tp->protocol, &offload);
90 }
91
92 static void mall_destroy(struct tcf_proto *tp)
93 {
94         struct cls_mall_head *head = rtnl_dereference(tp->root);
95         struct net_device *dev = tp->q->dev_queue->dev;
96
97         if (!head)
98                 return;
99
100         if (tc_should_offload(dev, tp, head->flags))
101                 mall_destroy_hw_filter(tp, head, (unsigned long) head);
102
103         call_rcu(&head->rcu, mall_destroy_rcu);
104 }
105
106 static unsigned long mall_get(struct tcf_proto *tp, u32 handle)
107 {
108         return 0UL;
109 }
110
111 static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
112         [TCA_MATCHALL_UNSPEC]           = { .type = NLA_UNSPEC },
113         [TCA_MATCHALL_CLASSID]          = { .type = NLA_U32 },
114 };
115
116 static int mall_set_parms(struct net *net, struct tcf_proto *tp,
117                           struct cls_mall_head *head,
118                           unsigned long base, struct nlattr **tb,
119                           struct nlattr *est, bool ovr)
120 {
121         int err;
122
123         err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr);
124         if (err < 0)
125                 return err;
126
127         if (tb[TCA_MATCHALL_CLASSID]) {
128                 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
129                 tcf_bind_filter(tp, &head->res, base);
130         }
131         return 0;
132 }
133
134 static int mall_change(struct net *net, struct sk_buff *in_skb,
135                        struct tcf_proto *tp, unsigned long base,
136                        u32 handle, struct nlattr **tca,
137                        unsigned long *arg, bool ovr)
138 {
139         struct cls_mall_head *head = rtnl_dereference(tp->root);
140         struct net_device *dev = tp->q->dev_queue->dev;
141         struct nlattr *tb[TCA_MATCHALL_MAX + 1];
142         struct cls_mall_head *new;
143         u32 flags = 0;
144         int err;
145
146         if (!tca[TCA_OPTIONS])
147                 return -EINVAL;
148
149         if (head)
150                 return -EEXIST;
151
152         err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
153                                mall_policy, NULL);
154         if (err < 0)
155                 return err;
156
157         if (tb[TCA_MATCHALL_FLAGS]) {
158                 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
159                 if (!tc_flags_valid(flags))
160                         return -EINVAL;
161         }
162
163         new = kzalloc(sizeof(*new), GFP_KERNEL);
164         if (!new)
165                 return -ENOBUFS;
166
167         err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
168         if (err)
169                 goto err_exts_init;
170
171         if (!handle)
172                 handle = 1;
173         new->handle = handle;
174         new->flags = flags;
175
176         err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
177         if (err)
178                 goto err_set_parms;
179
180         if (tc_should_offload(dev, tp, flags)) {
181                 err = mall_replace_hw_filter(tp, new, (unsigned long) new);
182                 if (err) {
183                         if (tc_skip_sw(flags))
184                                 goto err_replace_hw_filter;
185                         else
186                                 err = 0;
187                 }
188         }
189
190         if (!tc_in_hw(new->flags))
191                 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
192
193         *arg = (unsigned long) head;
194         rcu_assign_pointer(tp->root, new);
195         return 0;
196
197 err_replace_hw_filter:
198 err_set_parms:
199         tcf_exts_destroy(&new->exts);
200 err_exts_init:
201         kfree(new);
202         return err;
203 }
204
205 static int mall_delete(struct tcf_proto *tp, unsigned long arg, bool *last)
206 {
207         return -EOPNOTSUPP;
208 }
209
210 static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
211 {
212         struct cls_mall_head *head = rtnl_dereference(tp->root);
213
214         if (arg->count < arg->skip)
215                 goto skip;
216         if (arg->fn(tp, (unsigned long) head, arg) < 0)
217                 arg->stop = 1;
218 skip:
219         arg->count++;
220 }
221
222 static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
223                      struct sk_buff *skb, struct tcmsg *t)
224 {
225         struct cls_mall_head *head = (struct cls_mall_head *) fh;
226         struct nlattr *nest;
227
228         if (!head)
229                 return skb->len;
230
231         t->tcm_handle = head->handle;
232
233         nest = nla_nest_start(skb, TCA_OPTIONS);
234         if (!nest)
235                 goto nla_put_failure;
236
237         if (head->res.classid &&
238             nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
239                 goto nla_put_failure;
240
241         if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
242                 goto nla_put_failure;
243
244         if (tcf_exts_dump(skb, &head->exts))
245                 goto nla_put_failure;
246
247         nla_nest_end(skb, nest);
248
249         if (tcf_exts_dump_stats(skb, &head->exts) < 0)
250                 goto nla_put_failure;
251
252         return skb->len;
253
254 nla_put_failure:
255         nla_nest_cancel(skb, nest);
256         return -1;
257 }
258
259 static struct tcf_proto_ops cls_mall_ops __read_mostly = {
260         .kind           = "matchall",
261         .classify       = mall_classify,
262         .init           = mall_init,
263         .destroy        = mall_destroy,
264         .get            = mall_get,
265         .change         = mall_change,
266         .delete         = mall_delete,
267         .walk           = mall_walk,
268         .dump           = mall_dump,
269         .owner          = THIS_MODULE,
270 };
271
272 static int __init cls_mall_init(void)
273 {
274         return register_tcf_proto_ops(&cls_mall_ops);
275 }
276
277 static void __exit cls_mall_exit(void)
278 {
279         unregister_tcf_proto_ops(&cls_mall_ops);
280 }
281
282 module_init(cls_mall_init);
283 module_exit(cls_mall_exit);
284
285 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
286 MODULE_DESCRIPTION("Match-all classifier");
287 MODULE_LICENSE("GPL v2");