blob: 5278534c7e87f3a4502a0542713123ada7eb3d15 [file] [log] [blame]
Jiri Pirkobf3994d2016-07-21 12:03:11 +02001/*
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
Yotam Gigifd62d9f2017-01-31 15:14:29 +020019struct cls_mall_head {
Jiri Pirkobf3994d2016-07-21 12:03:11 +020020 struct tcf_exts exts;
21 struct tcf_result res;
22 u32 handle;
Yotam Gigib87f7932016-07-21 12:03:12 +020023 u32 flags;
Jiri Pirkobf3994d2016-07-21 12:03:11 +020024 struct rcu_head rcu;
25};
26
27static int mall_classify(struct sk_buff *skb, const struct tcf_proto *tp,
28 struct tcf_result *res)
29{
30 struct cls_mall_head *head = rcu_dereference_bh(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020031
Yotam Gigifd62d9f2017-01-31 15:14:29 +020032 if (tc_skip_sw(head->flags))
Yotam Gigib87f7932016-07-21 12:03:12 +020033 return -1;
34
Davide Caratti3ff4cbe2017-09-16 14:02:21 +020035 *res = head->res;
Yotam Gigifd62d9f2017-01-31 15:14:29 +020036 return tcf_exts_exec(skb, &head->exts, res);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020037}
38
39static int mall_init(struct tcf_proto *tp)
40{
Jiri Pirkobf3994d2016-07-21 12:03:11 +020041 return 0;
42}
43
Yotam Gigifd62d9f2017-01-31 15:14:29 +020044static void mall_destroy_rcu(struct rcu_head *rcu)
Jiri Pirkobf3994d2016-07-21 12:03:11 +020045{
Yotam Gigifd62d9f2017-01-31 15:14:29 +020046 struct cls_mall_head *head = container_of(rcu, struct cls_mall_head,
47 rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020048
Yotam Gigifd62d9f2017-01-31 15:14:29 +020049 tcf_exts_destroy(&head->exts);
50 kfree(head);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020051}
52
Jiri Pirko2447a962017-10-19 15:50:33 +020053static void mall_destroy_hw_filter(struct tcf_proto *tp,
54 struct cls_mall_head *head,
55 unsigned long cookie)
56{
57 struct net_device *dev = tp->q->dev_queue->dev;
58 struct tc_cls_matchall_offload cls_mall = {};
59 struct tcf_block *block = tp->chain->block;
60
61 tc_cls_common_offload_init(&cls_mall.common, tp);
62 cls_mall.command = TC_CLSMATCHALL_DESTROY;
63 cls_mall.cookie = cookie;
64
65 if (tc_can_offload(dev))
66 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSMATCHALL,
67 &cls_mall);
68 tc_setup_cb_call(block, NULL, TC_SETUP_CLSMATCHALL, &cls_mall, false);
69}
70
Yotam Gigib87f7932016-07-21 12:03:12 +020071static int mall_replace_hw_filter(struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +020072 struct cls_mall_head *head,
Yotam Gigib87f7932016-07-21 12:03:12 +020073 unsigned long cookie)
74{
75 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkode4784c2017-08-07 10:15:32 +020076 struct tc_cls_matchall_offload cls_mall = {};
Jiri Pirko2447a962017-10-19 15:50:33 +020077 struct tcf_block *block = tp->chain->block;
78 bool skip_sw = tc_skip_sw(head->flags);
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +020079 int err;
Yotam Gigib87f7932016-07-21 12:03:12 +020080
Jiri Pirkode4784c2017-08-07 10:15:32 +020081 tc_cls_common_offload_init(&cls_mall.common, tp);
82 cls_mall.command = TC_CLSMATCHALL_REPLACE;
83 cls_mall.exts = &head->exts;
84 cls_mall.cookie = cookie;
Yotam Gigib87f7932016-07-21 12:03:12 +020085
Jiri Pirko2447a962017-10-19 15:50:33 +020086 if (tc_can_offload(dev)) {
87 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSMATCHALL,
88 &cls_mall);
89 if (err) {
90 if (skip_sw)
91 return err;
92 } else {
93 head->flags |= TCA_CLS_FLAGS_IN_HW;
94 }
95 }
96
97 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSMATCHALL,
98 &cls_mall, skip_sw);
99 if (err < 0) {
100 mall_destroy_hw_filter(tp, head, cookie);
101 return err;
102 } else if (err > 0) {
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200103 head->flags |= TCA_CLS_FLAGS_IN_HW;
Jiri Pirko2447a962017-10-19 15:50:33 +0200104 }
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200105
Jiri Pirko2447a962017-10-19 15:50:33 +0200106 if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
107 return -EINVAL;
Yotam Gigib87f7932016-07-21 12:03:12 +0200108
Jiri Pirko2447a962017-10-19 15:50:33 +0200109 return 0;
Yotam Gigib87f7932016-07-21 12:03:12 +0200110}
111
WANG Cong763dbf62017-04-19 14:21:21 -0700112static void mall_destroy(struct tcf_proto *tp)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200113{
114 struct cls_mall_head *head = rtnl_dereference(tp->root);
115
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200116 if (!head)
WANG Cong763dbf62017-04-19 14:21:21 -0700117 return;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200118
Jiri Pirko2447a962017-10-19 15:50:33 +0200119 if (!tc_skip_hw(head->flags))
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200120 mall_destroy_hw_filter(tp, head, (unsigned long) head);
Yotam Gigib87f7932016-07-21 12:03:12 +0200121
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200122 call_rcu(&head->rcu, mall_destroy_rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200123}
124
WANG Cong8113c092017-08-04 21:31:43 -0700125static void *mall_get(struct tcf_proto *tp, u32 handle)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200126{
WANG Cong8113c092017-08-04 21:31:43 -0700127 return NULL;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200128}
129
130static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
131 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
132 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
133};
134
135static int mall_set_parms(struct net *net, struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200136 struct cls_mall_head *head,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200137 unsigned long base, struct nlattr **tb,
138 struct nlattr *est, bool ovr)
139{
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200140 int err;
141
Jiri Pirkoa74cb362017-08-04 14:29:08 +0200142 err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200143 if (err < 0)
Jiri Pirkoa74cb362017-08-04 14:29:08 +0200144 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200145
146 if (tb[TCA_MATCHALL_CLASSID]) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200147 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
148 tcf_bind_filter(tp, &head->res, base);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200149 }
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200150 return 0;
151}
152
153static int mall_change(struct net *net, struct sk_buff *in_skb,
154 struct tcf_proto *tp, unsigned long base,
155 u32 handle, struct nlattr **tca,
WANG Cong8113c092017-08-04 21:31:43 -0700156 void **arg, bool ovr)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200157{
158 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200159 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200160 struct cls_mall_head *new;
Yotam Gigib87f7932016-07-21 12:03:12 +0200161 u32 flags = 0;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200162 int err;
163
164 if (!tca[TCA_OPTIONS])
165 return -EINVAL;
166
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200167 if (head)
168 return -EEXIST;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200169
Johannes Bergfceb6432017-04-12 14:34:07 +0200170 err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
171 mall_policy, NULL);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200172 if (err < 0)
173 return err;
174
Yotam Gigib87f7932016-07-21 12:03:12 +0200175 if (tb[TCA_MATCHALL_FLAGS]) {
176 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
177 if (!tc_flags_valid(flags))
178 return -EINVAL;
179 }
180
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200181 new = kzalloc(sizeof(*new), GFP_KERNEL);
182 if (!new)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200183 return -ENOBUFS;
184
David S. Millere2160152017-02-02 16:54:00 -0500185 err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200186 if (err)
187 goto err_exts_init;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200188
189 if (!handle)
190 handle = 1;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200191 new->handle = handle;
192 new->flags = flags;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200193
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200194 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200195 if (err)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200196 goto err_set_parms;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200197
Jiri Pirko2447a962017-10-19 15:50:33 +0200198 if (!tc_skip_hw(new->flags)) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200199 err = mall_replace_hw_filter(tp, new, (unsigned long) new);
Jiri Pirko2447a962017-10-19 15:50:33 +0200200 if (err)
201 goto err_replace_hw_filter;
Yotam Gigib87f7932016-07-21 12:03:12 +0200202 }
203
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200204 if (!tc_in_hw(new->flags))
205 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
206
WANG Cong8113c092017-08-04 21:31:43 -0700207 *arg = head;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200208 rcu_assign_pointer(tp->root, new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200209 return 0;
210
Yotam Gigiec2507d2017-01-03 19:20:24 +0200211err_replace_hw_filter:
212err_set_parms:
David S. Millere2160152017-02-02 16:54:00 -0500213 tcf_exts_destroy(&new->exts);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200214err_exts_init:
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200215 kfree(new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200216 return err;
217}
218
WANG Cong8113c092017-08-04 21:31:43 -0700219static int mall_delete(struct tcf_proto *tp, void *arg, bool *last)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200220{
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200221 return -EOPNOTSUPP;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200222}
223
224static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
225{
226 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200227
228 if (arg->count < arg->skip)
229 goto skip;
WANG Cong8113c092017-08-04 21:31:43 -0700230 if (arg->fn(tp, head, arg) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200231 arg->stop = 1;
232skip:
233 arg->count++;
234}
235
WANG Cong8113c092017-08-04 21:31:43 -0700236static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200237 struct sk_buff *skb, struct tcmsg *t)
238{
WANG Cong8113c092017-08-04 21:31:43 -0700239 struct cls_mall_head *head = fh;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200240 struct nlattr *nest;
241
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200242 if (!head)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200243 return skb->len;
244
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200245 t->tcm_handle = head->handle;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200246
247 nest = nla_nest_start(skb, TCA_OPTIONS);
248 if (!nest)
249 goto nla_put_failure;
250
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200251 if (head->res.classid &&
252 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200253 goto nla_put_failure;
254
Or Gerlitz7a335ad2017-02-16 10:31:11 +0200255 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
256 goto nla_put_failure;
257
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200258 if (tcf_exts_dump(skb, &head->exts))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200259 goto nla_put_failure;
260
261 nla_nest_end(skb, nest);
262
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200263 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200264 goto nla_put_failure;
265
266 return skb->len;
267
268nla_put_failure:
269 nla_nest_cancel(skb, nest);
270 return -1;
271}
272
Cong Wang07d79fc2017-08-30 14:30:36 -0700273static void mall_bind_class(void *fh, u32 classid, unsigned long cl)
274{
275 struct cls_mall_head *head = fh;
276
277 if (head && head->res.classid == classid)
278 head->res.class = cl;
279}
280
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200281static struct tcf_proto_ops cls_mall_ops __read_mostly = {
282 .kind = "matchall",
283 .classify = mall_classify,
284 .init = mall_init,
285 .destroy = mall_destroy,
286 .get = mall_get,
287 .change = mall_change,
288 .delete = mall_delete,
289 .walk = mall_walk,
290 .dump = mall_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700291 .bind_class = mall_bind_class,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200292 .owner = THIS_MODULE,
293};
294
295static int __init cls_mall_init(void)
296{
297 return register_tcf_proto_ops(&cls_mall_ops);
298}
299
300static void __exit cls_mall_exit(void)
301{
302 unregister_tcf_proto_ops(&cls_mall_ops);
303}
304
305module_init(cls_mall_init);
306module_exit(cls_mall_exit);
307
308MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
309MODULE_DESCRIPTION("Match-all classifier");
310MODULE_LICENSE("GPL v2");