xref: /openbmc/linux/net/sched/cls_matchall.c (revision 7fc38225363dd8f19e667ad7c77b63bc4a5c065d)
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 #include <linux/percpu.h>
16 
17 #include <net/sch_generic.h>
18 #include <net/pkt_cls.h>
19 
20 struct cls_mall_head {
21 	struct tcf_exts exts;
22 	struct tcf_result res;
23 	u32 handle;
24 	u32 flags;
25 	unsigned int in_hw_count;
26 	struct tc_matchall_pcnt __percpu *pf;
27 	struct rcu_work rwork;
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 	__this_cpu_inc(head->pf->rhit);
40 	return tcf_exts_exec(skb, &head->exts, res);
41 }
42 
43 static int mall_init(struct tcf_proto *tp)
44 {
45 	return 0;
46 }
47 
48 static void __mall_destroy(struct cls_mall_head *head)
49 {
50 	tcf_exts_destroy(&head->exts);
51 	tcf_exts_put_net(&head->exts);
52 	free_percpu(head->pf);
53 	kfree(head);
54 }
55 
56 static void mall_destroy_work(struct work_struct *work)
57 {
58 	struct cls_mall_head *head = container_of(to_rcu_work(work),
59 						  struct cls_mall_head,
60 						  rwork);
61 	rtnl_lock();
62 	__mall_destroy(head);
63 	rtnl_unlock();
64 }
65 
66 static void mall_destroy_hw_filter(struct tcf_proto *tp,
67 				   struct cls_mall_head *head,
68 				   unsigned long cookie,
69 				   struct netlink_ext_ack *extack)
70 {
71 	struct tc_cls_matchall_offload cls_mall = {};
72 	struct tcf_block *block = tp->chain->block;
73 
74 	tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
75 	cls_mall.command = TC_CLSMATCHALL_DESTROY;
76 	cls_mall.cookie = cookie;
77 
78 	tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false);
79 	tcf_block_offload_dec(block, &head->flags);
80 }
81 
82 static int mall_replace_hw_filter(struct tcf_proto *tp,
83 				  struct cls_mall_head *head,
84 				  unsigned long cookie,
85 				  struct netlink_ext_ack *extack)
86 {
87 	struct tc_cls_matchall_offload cls_mall = {};
88 	struct tcf_block *block = tp->chain->block;
89 	bool skip_sw = tc_skip_sw(head->flags);
90 	int err;
91 
92 	tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
93 	cls_mall.command = TC_CLSMATCHALL_REPLACE;
94 	cls_mall.exts = &head->exts;
95 	cls_mall.cookie = cookie;
96 
97 	err = tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, skip_sw);
98 	if (err < 0) {
99 		mall_destroy_hw_filter(tp, head, cookie, NULL);
100 		return err;
101 	} else if (err > 0) {
102 		head->in_hw_count = err;
103 		tcf_block_offload_inc(block, &head->flags);
104 	}
105 
106 	if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
107 		return -EINVAL;
108 
109 	return 0;
110 }
111 
112 static void mall_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
113 {
114 	struct cls_mall_head *head = rtnl_dereference(tp->root);
115 
116 	if (!head)
117 		return;
118 
119 	tcf_unbind_filter(tp, &head->res);
120 
121 	if (!tc_skip_hw(head->flags))
122 		mall_destroy_hw_filter(tp, head, (unsigned long) head, extack);
123 
124 	if (tcf_exts_get_net(&head->exts))
125 		tcf_queue_work(&head->rwork, mall_destroy_work);
126 	else
127 		__mall_destroy(head);
128 }
129 
130 static void *mall_get(struct tcf_proto *tp, u32 handle)
131 {
132 	return NULL;
133 }
134 
135 static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
136 	[TCA_MATCHALL_UNSPEC]		= { .type = NLA_UNSPEC },
137 	[TCA_MATCHALL_CLASSID]		= { .type = NLA_U32 },
138 };
139 
140 static int mall_set_parms(struct net *net, struct tcf_proto *tp,
141 			  struct cls_mall_head *head,
142 			  unsigned long base, struct nlattr **tb,
143 			  struct nlattr *est, bool ovr,
144 			  struct netlink_ext_ack *extack)
145 {
146 	int err;
147 
148 	err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, extack);
149 	if (err < 0)
150 		return err;
151 
152 	if (tb[TCA_MATCHALL_CLASSID]) {
153 		head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
154 		tcf_bind_filter(tp, &head->res, base);
155 	}
156 	return 0;
157 }
158 
159 static int mall_change(struct net *net, struct sk_buff *in_skb,
160 		       struct tcf_proto *tp, unsigned long base,
161 		       u32 handle, struct nlattr **tca,
162 		       void **arg, bool ovr, struct netlink_ext_ack *extack)
163 {
164 	struct cls_mall_head *head = rtnl_dereference(tp->root);
165 	struct nlattr *tb[TCA_MATCHALL_MAX + 1];
166 	struct cls_mall_head *new;
167 	u32 flags = 0;
168 	int err;
169 
170 	if (!tca[TCA_OPTIONS])
171 		return -EINVAL;
172 
173 	if (head)
174 		return -EEXIST;
175 
176 	err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
177 			       mall_policy, NULL);
178 	if (err < 0)
179 		return err;
180 
181 	if (tb[TCA_MATCHALL_FLAGS]) {
182 		flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
183 		if (!tc_flags_valid(flags))
184 			return -EINVAL;
185 	}
186 
187 	new = kzalloc(sizeof(*new), GFP_KERNEL);
188 	if (!new)
189 		return -ENOBUFS;
190 
191 	err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
192 	if (err)
193 		goto err_exts_init;
194 
195 	if (!handle)
196 		handle = 1;
197 	new->handle = handle;
198 	new->flags = flags;
199 	new->pf = alloc_percpu(struct tc_matchall_pcnt);
200 	if (!new->pf) {
201 		err = -ENOMEM;
202 		goto err_alloc_percpu;
203 	}
204 
205 	err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr,
206 			     extack);
207 	if (err)
208 		goto err_set_parms;
209 
210 	if (!tc_skip_hw(new->flags)) {
211 		err = mall_replace_hw_filter(tp, new, (unsigned long)new,
212 					     extack);
213 		if (err)
214 			goto err_replace_hw_filter;
215 	}
216 
217 	if (!tc_in_hw(new->flags))
218 		new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
219 
220 	*arg = head;
221 	rcu_assign_pointer(tp->root, new);
222 	return 0;
223 
224 err_replace_hw_filter:
225 err_set_parms:
226 	free_percpu(new->pf);
227 err_alloc_percpu:
228 	tcf_exts_destroy(&new->exts);
229 err_exts_init:
230 	kfree(new);
231 	return err;
232 }
233 
234 static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
235 		       struct netlink_ext_ack *extack)
236 {
237 	return -EOPNOTSUPP;
238 }
239 
240 static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
241 {
242 	struct cls_mall_head *head = rtnl_dereference(tp->root);
243 
244 	if (arg->count < arg->skip)
245 		goto skip;
246 	if (arg->fn(tp, head, arg) < 0)
247 		arg->stop = 1;
248 skip:
249 	arg->count++;
250 }
251 
252 static int mall_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
253 			  void *cb_priv, struct netlink_ext_ack *extack)
254 {
255 	struct cls_mall_head *head = rtnl_dereference(tp->root);
256 	struct tc_cls_matchall_offload cls_mall = {};
257 	struct tcf_block *block = tp->chain->block;
258 	int err;
259 
260 	if (tc_skip_hw(head->flags))
261 		return 0;
262 
263 	tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
264 	cls_mall.command = add ?
265 		TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY;
266 	cls_mall.exts = &head->exts;
267 	cls_mall.cookie = (unsigned long)head;
268 
269 	err = cb(TC_SETUP_CLSMATCHALL, &cls_mall, cb_priv);
270 	if (err) {
271 		if (add && tc_skip_sw(head->flags))
272 			return err;
273 		return 0;
274 	}
275 
276 	tc_cls_offload_cnt_update(block, &head->in_hw_count, &head->flags, add);
277 
278 	return 0;
279 }
280 
281 static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
282 		     struct sk_buff *skb, struct tcmsg *t)
283 {
284 	struct tc_matchall_pcnt gpf = {};
285 	struct cls_mall_head *head = fh;
286 	struct nlattr *nest;
287 	int cpu;
288 
289 	if (!head)
290 		return skb->len;
291 
292 	t->tcm_handle = head->handle;
293 
294 	nest = nla_nest_start(skb, TCA_OPTIONS);
295 	if (!nest)
296 		goto nla_put_failure;
297 
298 	if (head->res.classid &&
299 	    nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
300 		goto nla_put_failure;
301 
302 	if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
303 		goto nla_put_failure;
304 
305 	for_each_possible_cpu(cpu) {
306 		struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu);
307 
308 		gpf.rhit += pf->rhit;
309 	}
310 
311 	if (nla_put_64bit(skb, TCA_MATCHALL_PCNT,
312 			  sizeof(struct tc_matchall_pcnt),
313 			  &gpf, TCA_MATCHALL_PAD))
314 		goto nla_put_failure;
315 
316 	if (tcf_exts_dump(skb, &head->exts))
317 		goto nla_put_failure;
318 
319 	nla_nest_end(skb, nest);
320 
321 	if (tcf_exts_dump_stats(skb, &head->exts) < 0)
322 		goto nla_put_failure;
323 
324 	return skb->len;
325 
326 nla_put_failure:
327 	nla_nest_cancel(skb, nest);
328 	return -1;
329 }
330 
331 static void mall_bind_class(void *fh, u32 classid, unsigned long cl)
332 {
333 	struct cls_mall_head *head = fh;
334 
335 	if (head && head->res.classid == classid)
336 		head->res.class = cl;
337 }
338 
339 static struct tcf_proto_ops cls_mall_ops __read_mostly = {
340 	.kind		= "matchall",
341 	.classify	= mall_classify,
342 	.init		= mall_init,
343 	.destroy	= mall_destroy,
344 	.get		= mall_get,
345 	.change		= mall_change,
346 	.delete		= mall_delete,
347 	.walk		= mall_walk,
348 	.reoffload	= mall_reoffload,
349 	.dump		= mall_dump,
350 	.bind_class	= mall_bind_class,
351 	.owner		= THIS_MODULE,
352 };
353 
354 static int __init cls_mall_init(void)
355 {
356 	return register_tcf_proto_ops(&cls_mall_ops);
357 }
358 
359 static void __exit cls_mall_exit(void)
360 {
361 	unregister_tcf_proto_ops(&cls_mall_ops);
362 }
363 
364 module_init(cls_mall_init);
365 module_exit(cls_mall_exit);
366 
367 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
368 MODULE_DESCRIPTION("Match-all classifier");
369 MODULE_LICENSE("GPL v2");
370