blob: c33f711b90198ab8b77bd314c289d09cbfe2d170 [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;
Cong Wangdf2735e2017-10-26 18:24:35 -070024 union {
25 struct work_struct work;
26 struct rcu_head rcu;
27 };
Jiri Pirkobf3994d2016-07-21 12:03:11 +020028};
29
30static 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);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020034
Yotam Gigifd62d9f2017-01-31 15:14:29 +020035 if (tc_skip_sw(head->flags))
Yotam Gigib87f7932016-07-21 12:03:12 +020036 return -1;
37
Davide Caratti3ff4cbe2017-09-16 14:02:21 +020038 *res = head->res;
Yotam Gigifd62d9f2017-01-31 15:14:29 +020039 return tcf_exts_exec(skb, &head->exts, res);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020040}
41
42static int mall_init(struct tcf_proto *tp)
43{
Jiri Pirkobf3994d2016-07-21 12:03:11 +020044 return 0;
45}
46
Cong Wangdf2735e2017-10-26 18:24:35 -070047static 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
Yotam Gigifd62d9f2017-01-31 15:14:29 +020057static void mall_destroy_rcu(struct rcu_head *rcu)
Jiri Pirkobf3994d2016-07-21 12:03:11 +020058{
Yotam Gigifd62d9f2017-01-31 15:14:29 +020059 struct cls_mall_head *head = container_of(rcu, struct cls_mall_head,
60 rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020061
Cong Wangdf2735e2017-10-26 18:24:35 -070062 INIT_WORK(&head->work, mall_destroy_work);
63 tcf_queue_work(&head->work);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020064}
65
Yotam Gigib87f7932016-07-21 12:03:12 +020066static int mall_replace_hw_filter(struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +020067 struct cls_mall_head *head,
Yotam Gigib87f7932016-07-21 12:03:12 +020068 unsigned long cookie)
69{
70 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkode4784c2017-08-07 10:15:32 +020071 struct tc_cls_matchall_offload cls_mall = {};
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +020072 int err;
Yotam Gigib87f7932016-07-21 12:03:12 +020073
Jiri Pirkode4784c2017-08-07 10:15:32 +020074 tc_cls_common_offload_init(&cls_mall.common, tp);
75 cls_mall.command = TC_CLSMATCHALL_REPLACE;
76 cls_mall.exts = &head->exts;
77 cls_mall.cookie = cookie;
Yotam Gigib87f7932016-07-21 12:03:12 +020078
Jiri Pirkoade9b652017-08-07 10:15:18 +020079 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSMATCHALL,
Jiri Pirkode4784c2017-08-07 10:15:32 +020080 &cls_mall);
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +020081 if (!err)
82 head->flags |= TCA_CLS_FLAGS_IN_HW;
83
84 return err;
Yotam Gigib87f7932016-07-21 12:03:12 +020085}
86
87static void mall_destroy_hw_filter(struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +020088 struct cls_mall_head *head,
Yotam Gigib87f7932016-07-21 12:03:12 +020089 unsigned long cookie)
90{
91 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkode4784c2017-08-07 10:15:32 +020092 struct tc_cls_matchall_offload cls_mall = {};
Yotam Gigib87f7932016-07-21 12:03:12 +020093
Jiri Pirkode4784c2017-08-07 10:15:32 +020094 tc_cls_common_offload_init(&cls_mall.common, tp);
95 cls_mall.command = TC_CLSMATCHALL_DESTROY;
96 cls_mall.cookie = cookie;
Yotam Gigib87f7932016-07-21 12:03:12 +020097
Jiri Pirkode4784c2017-08-07 10:15:32 +020098 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSMATCHALL, &cls_mall);
Yotam Gigib87f7932016-07-21 12:03:12 +020099}
100
WANG Cong763dbf62017-04-19 14:21:21 -0700101static void mall_destroy(struct tcf_proto *tp)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200102{
103 struct cls_mall_head *head = rtnl_dereference(tp->root);
Yotam Gigib87f7932016-07-21 12:03:12 +0200104 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200105
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200106 if (!head)
WANG Cong763dbf62017-04-19 14:21:21 -0700107 return;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200108
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200109 if (tc_should_offload(dev, head->flags))
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200110 mall_destroy_hw_filter(tp, head, (unsigned long) head);
Yotam Gigib87f7932016-07-21 12:03:12 +0200111
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200112 call_rcu(&head->rcu, mall_destroy_rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200113}
114
WANG Cong8113c092017-08-04 21:31:43 -0700115static void *mall_get(struct tcf_proto *tp, u32 handle)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200116{
WANG Cong8113c092017-08-04 21:31:43 -0700117 return NULL;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200118}
119
120static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
121 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
122 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
123};
124
125static int mall_set_parms(struct net *net, struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200126 struct cls_mall_head *head,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200127 unsigned long base, struct nlattr **tb,
128 struct nlattr *est, bool ovr)
129{
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200130 int err;
131
Jiri Pirkoa74cb362017-08-04 14:29:08 +0200132 err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200133 if (err < 0)
Jiri Pirkoa74cb362017-08-04 14:29:08 +0200134 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200135
136 if (tb[TCA_MATCHALL_CLASSID]) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200137 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
138 tcf_bind_filter(tp, &head->res, base);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200139 }
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200140 return 0;
141}
142
143static int mall_change(struct net *net, struct sk_buff *in_skb,
144 struct tcf_proto *tp, unsigned long base,
145 u32 handle, struct nlattr **tca,
WANG Cong8113c092017-08-04 21:31:43 -0700146 void **arg, bool ovr)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200147{
148 struct cls_mall_head *head = rtnl_dereference(tp->root);
Yotam Gigib87f7932016-07-21 12:03:12 +0200149 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200150 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200151 struct cls_mall_head *new;
Yotam Gigib87f7932016-07-21 12:03:12 +0200152 u32 flags = 0;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200153 int err;
154
155 if (!tca[TCA_OPTIONS])
156 return -EINVAL;
157
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200158 if (head)
159 return -EEXIST;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200160
Johannes Bergfceb6432017-04-12 14:34:07 +0200161 err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
162 mall_policy, NULL);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200163 if (err < 0)
164 return err;
165
Yotam Gigib87f7932016-07-21 12:03:12 +0200166 if (tb[TCA_MATCHALL_FLAGS]) {
167 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
168 if (!tc_flags_valid(flags))
169 return -EINVAL;
170 }
171
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200172 new = kzalloc(sizeof(*new), GFP_KERNEL);
173 if (!new)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200174 return -ENOBUFS;
175
David S. Millere2160152017-02-02 16:54:00 -0500176 err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200177 if (err)
178 goto err_exts_init;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200179
180 if (!handle)
181 handle = 1;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200182 new->handle = handle;
183 new->flags = flags;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200184
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200185 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200186 if (err)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200187 goto err_set_parms;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200188
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200189 if (tc_should_offload(dev, flags)) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200190 err = mall_replace_hw_filter(tp, new, (unsigned long) new);
Yotam Gigib87f7932016-07-21 12:03:12 +0200191 if (err) {
192 if (tc_skip_sw(flags))
Yotam Gigiec2507d2017-01-03 19:20:24 +0200193 goto err_replace_hw_filter;
Yotam Gigib87f7932016-07-21 12:03:12 +0200194 else
195 err = 0;
196 }
197 }
198
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200199 if (!tc_in_hw(new->flags))
200 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
201
WANG Cong8113c092017-08-04 21:31:43 -0700202 *arg = head;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200203 rcu_assign_pointer(tp->root, new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200204 return 0;
205
Yotam Gigiec2507d2017-01-03 19:20:24 +0200206err_replace_hw_filter:
207err_set_parms:
David S. Millere2160152017-02-02 16:54:00 -0500208 tcf_exts_destroy(&new->exts);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200209err_exts_init:
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200210 kfree(new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200211 return err;
212}
213
WANG Cong8113c092017-08-04 21:31:43 -0700214static int mall_delete(struct tcf_proto *tp, void *arg, bool *last)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200215{
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200216 return -EOPNOTSUPP;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200217}
218
219static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
220{
221 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200222
223 if (arg->count < arg->skip)
224 goto skip;
WANG Cong8113c092017-08-04 21:31:43 -0700225 if (arg->fn(tp, head, arg) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200226 arg->stop = 1;
227skip:
228 arg->count++;
229}
230
WANG Cong8113c092017-08-04 21:31:43 -0700231static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200232 struct sk_buff *skb, struct tcmsg *t)
233{
WANG Cong8113c092017-08-04 21:31:43 -0700234 struct cls_mall_head *head = fh;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200235 struct nlattr *nest;
236
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200237 if (!head)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200238 return skb->len;
239
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200240 t->tcm_handle = head->handle;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200241
242 nest = nla_nest_start(skb, TCA_OPTIONS);
243 if (!nest)
244 goto nla_put_failure;
245
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200246 if (head->res.classid &&
247 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200248 goto nla_put_failure;
249
Or Gerlitz7a335ad2017-02-16 10:31:11 +0200250 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
251 goto nla_put_failure;
252
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200253 if (tcf_exts_dump(skb, &head->exts))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200254 goto nla_put_failure;
255
256 nla_nest_end(skb, nest);
257
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200258 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200259 goto nla_put_failure;
260
261 return skb->len;
262
263nla_put_failure:
264 nla_nest_cancel(skb, nest);
265 return -1;
266}
267
Cong Wang07d79fc2017-08-30 14:30:36 -0700268static void mall_bind_class(void *fh, u32 classid, unsigned long cl)
269{
270 struct cls_mall_head *head = fh;
271
272 if (head && head->res.classid == classid)
273 head->res.class = cl;
274}
275
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200276static struct tcf_proto_ops cls_mall_ops __read_mostly = {
277 .kind = "matchall",
278 .classify = mall_classify,
279 .init = mall_init,
280 .destroy = mall_destroy,
281 .get = mall_get,
282 .change = mall_change,
283 .delete = mall_delete,
284 .walk = mall_walk,
285 .dump = mall_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700286 .bind_class = mall_bind_class,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200287 .owner = THIS_MODULE,
288};
289
290static int __init cls_mall_init(void)
291{
292 return register_tcf_proto_ops(&cls_mall_ops);
293}
294
295static void __exit cls_mall_exit(void)
296{
297 unregister_tcf_proto_ops(&cls_mall_ops);
298}
299
300module_init(cls_mall_init);
301module_exit(cls_mall_exit);
302
303MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
304MODULE_DESCRIPTION("Match-all classifier");
305MODULE_LICENSE("GPL v2");