blob: e75fb65037d7fff1e89049ca609f50c2069703be [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 Gigi6c8556f2017-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 Gigi6c8556f2017-01-31 15:14:29 +020032 if (tc_skip_sw(head->flags))
Yotam Gigib87f7932016-07-21 12:03:12 +020033 return -1;
34
Davide Carattib13bc542017-09-16 14:02:21 +020035 *res = head->res;
Yotam Gigi6c8556f2017-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 Gigi6c8556f2017-01-31 15:14:29 +020044static void mall_destroy_rcu(struct rcu_head *rcu)
Jiri Pirkobf3994d2016-07-21 12:03:11 +020045{
Yotam Gigi6c8556f2017-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 Gigi6c8556f2017-01-31 15:14:29 +020049 tcf_exts_destroy(&head->exts);
50 kfree(head);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020051}
52
Yotam Gigib87f7932016-07-21 12:03:12 +020053static int mall_replace_hw_filter(struct tcf_proto *tp,
Yotam Gigi6c8556f2017-01-31 15:14:29 +020054 struct cls_mall_head *head,
Yotam Gigib87f7932016-07-21 12:03:12 +020055 unsigned long cookie)
56{
57 struct net_device *dev = tp->q->dev_queue->dev;
58 struct tc_to_netdev offload;
59 struct tc_cls_matchall_offload mall_offload = {0};
60
61 offload.type = TC_SETUP_MATCHALL;
62 offload.cls_mall = &mall_offload;
63 offload.cls_mall->command = TC_CLSMATCHALL_REPLACE;
Yotam Gigi6c8556f2017-01-31 15:14:29 +020064 offload.cls_mall->exts = &head->exts;
Yotam Gigib87f7932016-07-21 12:03:12 +020065 offload.cls_mall->cookie = cookie;
66
67 return dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
68 &offload);
69}
70
71static void mall_destroy_hw_filter(struct tcf_proto *tp,
Yotam Gigi6c8556f2017-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;
76 struct tc_to_netdev offload;
77 struct tc_cls_matchall_offload mall_offload = {0};
78
79 offload.type = TC_SETUP_MATCHALL;
80 offload.cls_mall = &mall_offload;
81 offload.cls_mall->command = TC_CLSMATCHALL_DESTROY;
82 offload.cls_mall->exts = NULL;
83 offload.cls_mall->cookie = cookie;
84
85 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
86 &offload);
87}
88
Jiri Pirkobf3994d2016-07-21 12:03:11 +020089static bool mall_destroy(struct tcf_proto *tp, bool force)
90{
91 struct cls_mall_head *head = rtnl_dereference(tp->root);
Yotam Gigib87f7932016-07-21 12:03:12 +020092 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkobf3994d2016-07-21 12:03:11 +020093
Yotam Gigi6c8556f2017-01-31 15:14:29 +020094 if (!head)
95 return true;
Jiri Pirkobf3994d2016-07-21 12:03:11 +020096
Yotam Gigi6c8556f2017-01-31 15:14:29 +020097 if (tc_should_offload(dev, tp, head->flags))
98 mall_destroy_hw_filter(tp, head, (unsigned long) head);
Yotam Gigib87f7932016-07-21 12:03:12 +020099
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200100 call_rcu(&head->rcu, mall_destroy_rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200101 return true;
102}
103
104static unsigned long mall_get(struct tcf_proto *tp, u32 handle)
105{
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200106 return 0UL;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200107}
108
109static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
110 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
111 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
112};
113
114static int mall_set_parms(struct net *net, struct tcf_proto *tp,
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200115 struct cls_mall_head *head,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200116 unsigned long base, struct nlattr **tb,
117 struct nlattr *est, bool ovr)
118{
119 struct tcf_exts e;
120 int err;
121
122 tcf_exts_init(&e, TCA_MATCHALL_ACT, 0);
123 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
124 if (err < 0)
125 return err;
126
127 if (tb[TCA_MATCHALL_CLASSID]) {
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200128 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
129 tcf_bind_filter(tp, &head->res, base);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200130 }
131
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200132 tcf_exts_change(tp, &head->exts, &e);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200133
134 return 0;
135}
136
137static int mall_change(struct net *net, struct sk_buff *in_skb,
138 struct tcf_proto *tp, unsigned long base,
139 u32 handle, struct nlattr **tca,
140 unsigned long *arg, bool ovr)
141{
142 struct cls_mall_head *head = rtnl_dereference(tp->root);
Yotam Gigib87f7932016-07-21 12:03:12 +0200143 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200144 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200145 struct cls_mall_head *new;
Yotam Gigib87f7932016-07-21 12:03:12 +0200146 u32 flags = 0;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200147 int err;
148
149 if (!tca[TCA_OPTIONS])
150 return -EINVAL;
151
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200152 if (head)
153 return -EEXIST;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200154
155 err = nla_parse_nested(tb, TCA_MATCHALL_MAX,
156 tca[TCA_OPTIONS], mall_policy);
157 if (err < 0)
158 return err;
159
Yotam Gigib87f7932016-07-21 12:03:12 +0200160 if (tb[TCA_MATCHALL_FLAGS]) {
161 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
162 if (!tc_flags_valid(flags))
163 return -EINVAL;
164 }
165
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200166 new = kzalloc(sizeof(*new), GFP_KERNEL);
167 if (!new)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200168 return -ENOBUFS;
169
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200170 tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200171
172 if (!handle)
173 handle = 1;
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200174 new->handle = handle;
175 new->flags = flags;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200176
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200177 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200178 if (err)
179 goto errout;
180
Yotam Gigib87f7932016-07-21 12:03:12 +0200181 if (tc_should_offload(dev, tp, flags)) {
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200182 err = mall_replace_hw_filter(tp, new, (unsigned long) new);
Yotam Gigib87f7932016-07-21 12:03:12 +0200183 if (err) {
184 if (tc_skip_sw(flags))
185 goto errout;
186 else
187 err = 0;
188 }
189 }
190
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200191 *arg = (unsigned long) head;
192 rcu_assign_pointer(tp->root, new);
193 if (head)
194 call_rcu(&head->rcu, mall_destroy_rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200195 return 0;
196
197errout:
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200198 kfree(new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200199 return err;
200}
201
202static int mall_delete(struct tcf_proto *tp, unsigned long arg)
203{
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200204 return -EOPNOTSUPP;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200205}
206
207static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
208{
209 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200210
211 if (arg->count < arg->skip)
212 goto skip;
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200213 if (arg->fn(tp, (unsigned long) head, arg) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200214 arg->stop = 1;
215skip:
216 arg->count++;
217}
218
219static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
220 struct sk_buff *skb, struct tcmsg *t)
221{
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200222 struct cls_mall_head *head = (struct cls_mall_head *) fh;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200223 struct nlattr *nest;
224
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200225 if (!head)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200226 return skb->len;
227
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200228 t->tcm_handle = head->handle;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200229
230 nest = nla_nest_start(skb, TCA_OPTIONS);
231 if (!nest)
232 goto nla_put_failure;
233
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200234 if (head->res.classid &&
235 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200236 goto nla_put_failure;
237
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200238 if (tcf_exts_dump(skb, &head->exts))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200239 goto nla_put_failure;
240
241 nla_nest_end(skb, nest);
242
Yotam Gigi6c8556f2017-01-31 15:14:29 +0200243 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200244 goto nla_put_failure;
245
246 return skb->len;
247
248nla_put_failure:
249 nla_nest_cancel(skb, nest);
250 return -1;
251}
252
253static struct tcf_proto_ops cls_mall_ops __read_mostly = {
254 .kind = "matchall",
255 .classify = mall_classify,
256 .init = mall_init,
257 .destroy = mall_destroy,
258 .get = mall_get,
259 .change = mall_change,
260 .delete = mall_delete,
261 .walk = mall_walk,
262 .dump = mall_dump,
263 .owner = THIS_MODULE,
264};
265
266static int __init cls_mall_init(void)
267{
268 return register_tcf_proto_ops(&cls_mall_ops);
269}
270
271static void __exit cls_mall_exit(void)
272{
273 unregister_tcf_proto_ops(&cls_mall_ops);
274}
275
276module_init(cls_mall_init);
277module_exit(cls_mall_exit);
278
279MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
280MODULE_DESCRIPTION("Match-all classifier");
281MODULE_LICENSE("GPL v2");