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