xref: /openbmc/linux/net/sched/cls_matchall.c (revision 8ee90c5c)
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 	union {
25 		struct work_struct work;
26 		struct rcu_head	rcu;
27 	};
28 };
29 
30 static 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);
34 
35 	if (tc_skip_sw(head->flags))
36 		return -1;
37 
38 	*res = head->res;
39 	return tcf_exts_exec(skb, &head->exts, res);
40 }
41 
42 static int mall_init(struct tcf_proto *tp)
43 {
44 	return 0;
45 }
46 
47 static 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 
57 static void mall_destroy_rcu(struct rcu_head *rcu)
58 {
59 	struct cls_mall_head *head = container_of(rcu, struct cls_mall_head,
60 						  rcu);
61 
62 	INIT_WORK(&head->work, mall_destroy_work);
63 	tcf_queue_work(&head->work);
64 }
65 
66 static int mall_replace_hw_filter(struct tcf_proto *tp,
67 				  struct cls_mall_head *head,
68 				  unsigned long cookie)
69 {
70 	struct net_device *dev = tp->q->dev_queue->dev;
71 	struct tc_cls_matchall_offload cls_mall = {};
72 	int err;
73 
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;
78 
79 	err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSMATCHALL,
80 					    &cls_mall);
81 	if (!err)
82 		head->flags |= TCA_CLS_FLAGS_IN_HW;
83 
84 	return err;
85 }
86 
87 static void mall_destroy_hw_filter(struct tcf_proto *tp,
88 				   struct cls_mall_head *head,
89 				   unsigned long cookie)
90 {
91 	struct net_device *dev = tp->q->dev_queue->dev;
92 	struct tc_cls_matchall_offload cls_mall = {};
93 
94 	tc_cls_common_offload_init(&cls_mall.common, tp);
95 	cls_mall.command = TC_CLSMATCHALL_DESTROY;
96 	cls_mall.cookie = cookie;
97 
98 	dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSMATCHALL, &cls_mall);
99 }
100 
101 static void mall_destroy(struct tcf_proto *tp)
102 {
103 	struct cls_mall_head *head = rtnl_dereference(tp->root);
104 	struct net_device *dev = tp->q->dev_queue->dev;
105 
106 	if (!head)
107 		return;
108 
109 	if (tc_should_offload(dev, head->flags))
110 		mall_destroy_hw_filter(tp, head, (unsigned long) head);
111 
112 	call_rcu(&head->rcu, mall_destroy_rcu);
113 }
114 
115 static void *mall_get(struct tcf_proto *tp, u32 handle)
116 {
117 	return NULL;
118 }
119 
120 static 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 
125 static int mall_set_parms(struct net *net, struct tcf_proto *tp,
126 			  struct cls_mall_head *head,
127 			  unsigned long base, struct nlattr **tb,
128 			  struct nlattr *est, bool ovr)
129 {
130 	int err;
131 
132 	err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr);
133 	if (err < 0)
134 		return err;
135 
136 	if (tb[TCA_MATCHALL_CLASSID]) {
137 		head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
138 		tcf_bind_filter(tp, &head->res, base);
139 	}
140 	return 0;
141 }
142 
143 static 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,
146 		       void **arg, bool ovr)
147 {
148 	struct cls_mall_head *head = rtnl_dereference(tp->root);
149 	struct net_device *dev = tp->q->dev_queue->dev;
150 	struct nlattr *tb[TCA_MATCHALL_MAX + 1];
151 	struct cls_mall_head *new;
152 	u32 flags = 0;
153 	int err;
154 
155 	if (!tca[TCA_OPTIONS])
156 		return -EINVAL;
157 
158 	if (head)
159 		return -EEXIST;
160 
161 	err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
162 			       mall_policy, NULL);
163 	if (err < 0)
164 		return err;
165 
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 
172 	new = kzalloc(sizeof(*new), GFP_KERNEL);
173 	if (!new)
174 		return -ENOBUFS;
175 
176 	err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
177 	if (err)
178 		goto err_exts_init;
179 
180 	if (!handle)
181 		handle = 1;
182 	new->handle = handle;
183 	new->flags = flags;
184 
185 	err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
186 	if (err)
187 		goto err_set_parms;
188 
189 	if (tc_should_offload(dev, flags)) {
190 		err = mall_replace_hw_filter(tp, new, (unsigned long) new);
191 		if (err) {
192 			if (tc_skip_sw(flags))
193 				goto err_replace_hw_filter;
194 			else
195 				err = 0;
196 		}
197 	}
198 
199 	if (!tc_in_hw(new->flags))
200 		new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
201 
202 	*arg = head;
203 	rcu_assign_pointer(tp->root, new);
204 	return 0;
205 
206 err_replace_hw_filter:
207 err_set_parms:
208 	tcf_exts_destroy(&new->exts);
209 err_exts_init:
210 	kfree(new);
211 	return err;
212 }
213 
214 static int mall_delete(struct tcf_proto *tp, void *arg, bool *last)
215 {
216 	return -EOPNOTSUPP;
217 }
218 
219 static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
220 {
221 	struct cls_mall_head *head = rtnl_dereference(tp->root);
222 
223 	if (arg->count < arg->skip)
224 		goto skip;
225 	if (arg->fn(tp, head, arg) < 0)
226 		arg->stop = 1;
227 skip:
228 	arg->count++;
229 }
230 
231 static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
232 		     struct sk_buff *skb, struct tcmsg *t)
233 {
234 	struct cls_mall_head *head = fh;
235 	struct nlattr *nest;
236 
237 	if (!head)
238 		return skb->len;
239 
240 	t->tcm_handle = head->handle;
241 
242 	nest = nla_nest_start(skb, TCA_OPTIONS);
243 	if (!nest)
244 		goto nla_put_failure;
245 
246 	if (head->res.classid &&
247 	    nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
248 		goto nla_put_failure;
249 
250 	if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
251 		goto nla_put_failure;
252 
253 	if (tcf_exts_dump(skb, &head->exts))
254 		goto nla_put_failure;
255 
256 	nla_nest_end(skb, nest);
257 
258 	if (tcf_exts_dump_stats(skb, &head->exts) < 0)
259 		goto nla_put_failure;
260 
261 	return skb->len;
262 
263 nla_put_failure:
264 	nla_nest_cancel(skb, nest);
265 	return -1;
266 }
267 
268 static 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 
276 static 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,
286 	.bind_class	= mall_bind_class,
287 	.owner		= THIS_MODULE,
288 };
289 
290 static int __init cls_mall_init(void)
291 {
292 	return register_tcf_proto_ops(&cls_mall_ops);
293 }
294 
295 static void __exit cls_mall_exit(void)
296 {
297 	unregister_tcf_proto_ops(&cls_mall_ops);
298 }
299 
300 module_init(cls_mall_init);
301 module_exit(cls_mall_exit);
302 
303 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
304 MODULE_DESCRIPTION("Match-all classifier");
305 MODULE_LICENSE("GPL v2");
306