blob: f2141cb5556235b7520606cb88384c062f3ffbd2 [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
Yotam Gigifd62d9f2017-01-31 15:14:29 +020035 return tcf_exts_exec(skb, &head->exts, res);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020036}
37
38static int mall_init(struct tcf_proto *tp)
39{
Jiri Pirkobf3994d2016-07-21 12:03:11 +020040 return 0;
41}
42
Yotam Gigifd62d9f2017-01-31 15:14:29 +020043static void mall_destroy_rcu(struct rcu_head *rcu)
Jiri Pirkobf3994d2016-07-21 12:03:11 +020044{
Yotam Gigifd62d9f2017-01-31 15:14:29 +020045 struct cls_mall_head *head = container_of(rcu, struct cls_mall_head,
46 rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020047
Yotam Gigifd62d9f2017-01-31 15:14:29 +020048 tcf_exts_destroy(&head->exts);
49 kfree(head);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020050}
51
Yotam Gigib87f7932016-07-21 12:03:12 +020052static int mall_replace_hw_filter(struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +020053 struct cls_mall_head *head,
Yotam Gigib87f7932016-07-21 12:03:12 +020054 unsigned long cookie)
55{
56 struct net_device *dev = tp->q->dev_queue->dev;
57 struct tc_to_netdev offload;
58 struct tc_cls_matchall_offload mall_offload = {0};
59
60 offload.type = TC_SETUP_MATCHALL;
61 offload.cls_mall = &mall_offload;
62 offload.cls_mall->command = TC_CLSMATCHALL_REPLACE;
Yotam Gigifd62d9f2017-01-31 15:14:29 +020063 offload.cls_mall->exts = &head->exts;
Yotam Gigib87f7932016-07-21 12:03:12 +020064 offload.cls_mall->cookie = cookie;
65
66 return dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
67 &offload);
68}
69
70static void mall_destroy_hw_filter(struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +020071 struct cls_mall_head *head,
Yotam Gigib87f7932016-07-21 12:03:12 +020072 unsigned long cookie)
73{
74 struct net_device *dev = tp->q->dev_queue->dev;
75 struct tc_to_netdev offload;
76 struct tc_cls_matchall_offload mall_offload = {0};
77
78 offload.type = TC_SETUP_MATCHALL;
79 offload.cls_mall = &mall_offload;
80 offload.cls_mall->command = TC_CLSMATCHALL_DESTROY;
81 offload.cls_mall->exts = NULL;
82 offload.cls_mall->cookie = cookie;
83
84 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
85 &offload);
86}
87
Jiri Pirkobf3994d2016-07-21 12:03:11 +020088static bool mall_destroy(struct tcf_proto *tp, bool force)
89{
90 struct cls_mall_head *head = rtnl_dereference(tp->root);
Yotam Gigib87f7932016-07-21 12:03:12 +020091 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkobf3994d2016-07-21 12:03:11 +020092
Yotam Gigifd62d9f2017-01-31 15:14:29 +020093 if (!head)
94 return true;
Jiri Pirkobf3994d2016-07-21 12:03:11 +020095
Yotam Gigifd62d9f2017-01-31 15:14:29 +020096 if (tc_should_offload(dev, tp, head->flags))
97 mall_destroy_hw_filter(tp, head, (unsigned long) head);
Yotam Gigib87f7932016-07-21 12:03:12 +020098
Yotam Gigifd62d9f2017-01-31 15:14:29 +020099 call_rcu(&head->rcu, mall_destroy_rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200100 return true;
101}
102
103static unsigned long mall_get(struct tcf_proto *tp, u32 handle)
104{
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200105 return 0UL;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200106}
107
108static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
109 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
110 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
111};
112
113static int mall_set_parms(struct net *net, struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200114 struct cls_mall_head *head,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200115 unsigned long base, struct nlattr **tb,
116 struct nlattr *est, bool ovr)
117{
118 struct tcf_exts e;
119 int err;
120
Yotam Gigiec2507d2017-01-03 19:20:24 +0200121 err = tcf_exts_init(&e, TCA_MATCHALL_ACT, 0);
122 if (err)
123 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200124 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
125 if (err < 0)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200126 goto errout;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200127
128 if (tb[TCA_MATCHALL_CLASSID]) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200129 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
130 tcf_bind_filter(tp, &head->res, base);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200131 }
132
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200133 tcf_exts_change(tp, &head->exts, &e);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200134
135 return 0;
Yotam Gigiec2507d2017-01-03 19:20:24 +0200136errout:
137 tcf_exts_destroy(&e);
138 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200139}
140
141static int mall_change(struct net *net, struct sk_buff *in_skb,
142 struct tcf_proto *tp, unsigned long base,
143 u32 handle, struct nlattr **tca,
144 unsigned long *arg, bool ovr)
145{
146 struct cls_mall_head *head = rtnl_dereference(tp->root);
Yotam Gigib87f7932016-07-21 12:03:12 +0200147 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200148 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200149 struct cls_mall_head *new;
Yotam Gigib87f7932016-07-21 12:03:12 +0200150 u32 flags = 0;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200151 int err;
152
153 if (!tca[TCA_OPTIONS])
154 return -EINVAL;
155
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200156 if (head)
157 return -EEXIST;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200158
159 err = nla_parse_nested(tb, TCA_MATCHALL_MAX,
160 tca[TCA_OPTIONS], mall_policy);
161 if (err < 0)
162 return err;
163
Yotam Gigib87f7932016-07-21 12:03:12 +0200164 if (tb[TCA_MATCHALL_FLAGS]) {
165 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
166 if (!tc_flags_valid(flags))
167 return -EINVAL;
168 }
169
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200170 new = kzalloc(sizeof(*new), GFP_KERNEL);
171 if (!new)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200172 return -ENOBUFS;
173
David S. Millere2160152017-02-02 16:54:00 -0500174 err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200175 if (err)
176 goto err_exts_init;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200177
178 if (!handle)
179 handle = 1;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200180 new->handle = handle;
181 new->flags = flags;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200182
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200183 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200184 if (err)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200185 goto err_set_parms;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200186
Yotam Gigib87f7932016-07-21 12:03:12 +0200187 if (tc_should_offload(dev, tp, flags)) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200188 err = mall_replace_hw_filter(tp, new, (unsigned long) new);
Yotam Gigib87f7932016-07-21 12:03:12 +0200189 if (err) {
190 if (tc_skip_sw(flags))
Yotam Gigiec2507d2017-01-03 19:20:24 +0200191 goto err_replace_hw_filter;
Yotam Gigib87f7932016-07-21 12:03:12 +0200192 else
193 err = 0;
194 }
195 }
196
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200197 *arg = (unsigned long) head;
198 rcu_assign_pointer(tp->root, new);
199 if (head)
200 call_rcu(&head->rcu, mall_destroy_rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200201 return 0;
202
Yotam Gigiec2507d2017-01-03 19:20:24 +0200203err_replace_hw_filter:
204err_set_parms:
David S. Millere2160152017-02-02 16:54:00 -0500205 tcf_exts_destroy(&new->exts);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200206err_exts_init:
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200207 kfree(new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200208 return err;
209}
210
211static int mall_delete(struct tcf_proto *tp, unsigned long arg)
212{
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200213 return -EOPNOTSUPP;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200214}
215
216static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
217{
218 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200219
220 if (arg->count < arg->skip)
221 goto skip;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200222 if (arg->fn(tp, (unsigned long) head, arg) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200223 arg->stop = 1;
224skip:
225 arg->count++;
226}
227
228static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
229 struct sk_buff *skb, struct tcmsg *t)
230{
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200231 struct cls_mall_head *head = (struct cls_mall_head *) fh;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200232 struct nlattr *nest;
233
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200234 if (!head)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200235 return skb->len;
236
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200237 t->tcm_handle = head->handle;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200238
239 nest = nla_nest_start(skb, TCA_OPTIONS);
240 if (!nest)
241 goto nla_put_failure;
242
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200243 if (head->res.classid &&
244 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200245 goto nla_put_failure;
246
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200247 if (tcf_exts_dump(skb, &head->exts))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200248 goto nla_put_failure;
249
250 nla_nest_end(skb, nest);
251
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200252 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200253 goto nla_put_failure;
254
255 return skb->len;
256
257nla_put_failure:
258 nla_nest_cancel(skb, nest);
259 return -1;
260}
261
262static struct tcf_proto_ops cls_mall_ops __read_mostly = {
263 .kind = "matchall",
264 .classify = mall_classify,
265 .init = mall_init,
266 .destroy = mall_destroy,
267 .get = mall_get,
268 .change = mall_change,
269 .delete = mall_delete,
270 .walk = mall_walk,
271 .dump = mall_dump,
272 .owner = THIS_MODULE,
273};
274
275static int __init cls_mall_init(void)
276{
277 return register_tcf_proto_ops(&cls_mall_ops);
278}
279
280static void __exit cls_mall_exit(void)
281{
282 unregister_tcf_proto_ops(&cls_mall_ops);
283}
284
285module_init(cls_mall_init);
286module_exit(cls_mall_exit);
287
288MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
289MODULE_DESCRIPTION("Match-all classifier");
290MODULE_LICENSE("GPL v2");