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