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