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