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