blob: 9dc26c32cf321e475a8f783cf423ff67562479fa [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};
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +020059 int err;
Yotam Gigib87f7932016-07-21 12:03:12 +020060
61 offload.type = TC_SETUP_MATCHALL;
62 offload.cls_mall = &mall_offload;
63 offload.cls_mall->command = TC_CLSMATCHALL_REPLACE;
Yotam Gigifd62d9f2017-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
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +020067 err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle,
68 tp->chain->index,
69 tp->protocol, &offload);
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +020070 if (!err)
71 head->flags |= TCA_CLS_FLAGS_IN_HW;
72
73 return err;
Yotam Gigib87f7932016-07-21 12:03:12 +020074}
75
76static void mall_destroy_hw_filter(struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +020077 struct cls_mall_head *head,
Yotam Gigib87f7932016-07-21 12:03:12 +020078 unsigned long cookie)
79{
80 struct net_device *dev = tp->q->dev_queue->dev;
81 struct tc_to_netdev offload;
82 struct tc_cls_matchall_offload mall_offload = {0};
83
84 offload.type = TC_SETUP_MATCHALL;
85 offload.cls_mall = &mall_offload;
86 offload.cls_mall->command = TC_CLSMATCHALL_DESTROY;
87 offload.cls_mall->exts = NULL;
88 offload.cls_mall->cookie = cookie;
89
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +020090 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->chain->index,
91 tp->protocol, &offload);
Yotam Gigib87f7932016-07-21 12:03:12 +020092}
93
WANG Cong763dbf62017-04-19 14:21:21 -070094static void mall_destroy(struct tcf_proto *tp)
Jiri Pirkobf3994d2016-07-21 12:03:11 +020095{
96 struct cls_mall_head *head = rtnl_dereference(tp->root);
Yotam Gigib87f7932016-07-21 12:03:12 +020097 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkobf3994d2016-07-21 12:03:11 +020098
Yotam Gigifd62d9f2017-01-31 15:14:29 +020099 if (!head)
WANG Cong763dbf62017-04-19 14:21:21 -0700100 return;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200101
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200102 if (tc_should_offload(dev, tp, head->flags))
103 mall_destroy_hw_filter(tp, head, (unsigned long) head);
Yotam Gigib87f7932016-07-21 12:03:12 +0200104
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200105 call_rcu(&head->rcu, mall_destroy_rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200106}
107
108static unsigned long mall_get(struct tcf_proto *tp, u32 handle)
109{
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200110 return 0UL;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200111}
112
113static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
114 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
115 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
116};
117
118static int mall_set_parms(struct net *net, struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200119 struct cls_mall_head *head,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200120 unsigned long base, struct nlattr **tb,
121 struct nlattr *est, bool ovr)
122{
123 struct tcf_exts e;
124 int err;
125
Yotam Gigiec2507d2017-01-03 19:20:24 +0200126 err = tcf_exts_init(&e, TCA_MATCHALL_ACT, 0);
127 if (err)
128 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200129 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
130 if (err < 0)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200131 goto errout;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200132
133 if (tb[TCA_MATCHALL_CLASSID]) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200134 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
135 tcf_bind_filter(tp, &head->res, base);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200136 }
137
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200138 tcf_exts_change(tp, &head->exts, &e);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200139
140 return 0;
Yotam Gigiec2507d2017-01-03 19:20:24 +0200141errout:
142 tcf_exts_destroy(&e);
143 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200144}
145
146static int mall_change(struct net *net, struct sk_buff *in_skb,
147 struct tcf_proto *tp, unsigned long base,
148 u32 handle, struct nlattr **tca,
149 unsigned long *arg, bool ovr)
150{
151 struct cls_mall_head *head = rtnl_dereference(tp->root);
Yotam Gigib87f7932016-07-21 12:03:12 +0200152 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200153 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200154 struct cls_mall_head *new;
Yotam Gigib87f7932016-07-21 12:03:12 +0200155 u32 flags = 0;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200156 int err;
157
158 if (!tca[TCA_OPTIONS])
159 return -EINVAL;
160
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200161 if (head)
162 return -EEXIST;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200163
Johannes Bergfceb6432017-04-12 14:34:07 +0200164 err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
165 mall_policy, NULL);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200166 if (err < 0)
167 return err;
168
Yotam Gigib87f7932016-07-21 12:03:12 +0200169 if (tb[TCA_MATCHALL_FLAGS]) {
170 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
171 if (!tc_flags_valid(flags))
172 return -EINVAL;
173 }
174
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200175 new = kzalloc(sizeof(*new), GFP_KERNEL);
176 if (!new)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200177 return -ENOBUFS;
178
David S. Millere2160152017-02-02 16:54:00 -0500179 err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200180 if (err)
181 goto err_exts_init;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200182
183 if (!handle)
184 handle = 1;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200185 new->handle = handle;
186 new->flags = flags;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200187
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200188 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200189 if (err)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200190 goto err_set_parms;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200191
Yotam Gigib87f7932016-07-21 12:03:12 +0200192 if (tc_should_offload(dev, tp, flags)) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200193 err = mall_replace_hw_filter(tp, new, (unsigned long) new);
Yotam Gigib87f7932016-07-21 12:03:12 +0200194 if (err) {
195 if (tc_skip_sw(flags))
Yotam Gigiec2507d2017-01-03 19:20:24 +0200196 goto err_replace_hw_filter;
Yotam Gigib87f7932016-07-21 12:03:12 +0200197 else
198 err = 0;
199 }
200 }
201
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200202 if (!tc_in_hw(new->flags))
203 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
204
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200205 *arg = (unsigned long) head;
206 rcu_assign_pointer(tp->root, new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200207 return 0;
208
Yotam Gigiec2507d2017-01-03 19:20:24 +0200209err_replace_hw_filter:
210err_set_parms:
David S. Millere2160152017-02-02 16:54:00 -0500211 tcf_exts_destroy(&new->exts);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200212err_exts_init:
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200213 kfree(new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200214 return err;
215}
216
WANG Cong763dbf62017-04-19 14:21:21 -0700217static int mall_delete(struct tcf_proto *tp, unsigned long arg, bool *last)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200218{
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200219 return -EOPNOTSUPP;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200220}
221
222static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
223{
224 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200225
226 if (arg->count < arg->skip)
227 goto skip;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200228 if (arg->fn(tp, (unsigned long) head, arg) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200229 arg->stop = 1;
230skip:
231 arg->count++;
232}
233
234static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
235 struct sk_buff *skb, struct tcmsg *t)
236{
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200237 struct cls_mall_head *head = (struct cls_mall_head *) fh;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200238 struct nlattr *nest;
239
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200240 if (!head)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200241 return skb->len;
242
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200243 t->tcm_handle = head->handle;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200244
245 nest = nla_nest_start(skb, TCA_OPTIONS);
246 if (!nest)
247 goto nla_put_failure;
248
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200249 if (head->res.classid &&
250 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200251 goto nla_put_failure;
252
Or Gerlitz7a335ad2017-02-16 10:31:11 +0200253 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
254 goto nla_put_failure;
255
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200256 if (tcf_exts_dump(skb, &head->exts))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200257 goto nla_put_failure;
258
259 nla_nest_end(skb, nest);
260
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200261 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200262 goto nla_put_failure;
263
264 return skb->len;
265
266nla_put_failure:
267 nla_nest_cancel(skb, nest);
268 return -1;
269}
270
271static struct tcf_proto_ops cls_mall_ops __read_mostly = {
272 .kind = "matchall",
273 .classify = mall_classify,
274 .init = mall_init,
275 .destroy = mall_destroy,
276 .get = mall_get,
277 .change = mall_change,
278 .delete = mall_delete,
279 .walk = mall_walk,
280 .dump = mall_dump,
281 .owner = THIS_MODULE,
282};
283
284static int __init cls_mall_init(void)
285{
286 return register_tcf_proto_ops(&cls_mall_ops);
287}
288
289static void __exit cls_mall_exit(void)
290{
291 unregister_tcf_proto_ops(&cls_mall_ops);
292}
293
294module_init(cls_mall_init);
295module_exit(cls_mall_exit);
296
297MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
298MODULE_DESCRIPTION("Match-all classifier");
299MODULE_LICENSE("GPL v2");