blob: 2ba721a590a72a9fbaa3d92a18b9db7558b54869 [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 Wang57767e782017-11-06 13:47:26 -080047static void __mall_destroy(struct cls_mall_head *head)
48{
49 tcf_exts_destroy(&head->exts);
50 tcf_exts_put_net(&head->exts);
51 kfree(head);
52}
53
Cong Wangdf2735e2017-10-26 18:24:35 -070054static void mall_destroy_work(struct work_struct *work)
55{
56 struct cls_mall_head *head = container_of(work, struct cls_mall_head,
57 work);
58 rtnl_lock();
Cong Wang57767e782017-11-06 13:47:26 -080059 __mall_destroy(head);
Cong Wangdf2735e2017-10-26 18:24:35 -070060 rtnl_unlock();
61}
62
Yotam Gigifd62d9f2017-01-31 15:14:29 +020063static void mall_destroy_rcu(struct rcu_head *rcu)
Jiri Pirkobf3994d2016-07-21 12:03:11 +020064{
Yotam Gigifd62d9f2017-01-31 15:14:29 +020065 struct cls_mall_head *head = container_of(rcu, struct cls_mall_head,
66 rcu);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020067
Cong Wangdf2735e2017-10-26 18:24:35 -070068 INIT_WORK(&head->work, mall_destroy_work);
69 tcf_queue_work(&head->work);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020070}
71
Jiri Pirko2447a962017-10-19 15:50:33 +020072static void mall_destroy_hw_filter(struct tcf_proto *tp,
73 struct cls_mall_head *head,
Jakub Kicinskib505b292018-01-24 12:54:19 -080074 unsigned long cookie,
75 struct netlink_ext_ack *extack)
Jiri Pirko2447a962017-10-19 15:50:33 +020076{
Jiri Pirko2447a962017-10-19 15:50:33 +020077 struct tc_cls_matchall_offload cls_mall = {};
78 struct tcf_block *block = tp->chain->block;
79
Jakub Kicinskib505b292018-01-24 12:54:19 -080080 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
Jiri Pirko2447a962017-10-19 15:50:33 +020081 cls_mall.command = TC_CLSMATCHALL_DESTROY;
82 cls_mall.cookie = cookie;
83
Jiri Pirko2447a962017-10-19 15:50:33 +020084 tc_setup_cb_call(block, NULL, TC_SETUP_CLSMATCHALL, &cls_mall, false);
Jiri Pirkocaa72602018-01-17 11:46:50 +010085 tcf_block_offload_dec(block, &head->flags);
Jiri Pirko2447a962017-10-19 15:50:33 +020086}
87
Yotam Gigib87f7932016-07-21 12:03:12 +020088static int mall_replace_hw_filter(struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +020089 struct cls_mall_head *head,
Quentin Monnet02798142018-01-19 17:44:44 -080090 unsigned long cookie,
91 struct netlink_ext_ack *extack)
Yotam Gigib87f7932016-07-21 12:03:12 +020092{
Jiri Pirkode4784c2017-08-07 10:15:32 +020093 struct tc_cls_matchall_offload cls_mall = {};
Jiri Pirko2447a962017-10-19 15:50:33 +020094 struct tcf_block *block = tp->chain->block;
95 bool skip_sw = tc_skip_sw(head->flags);
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +020096 int err;
Yotam Gigib87f7932016-07-21 12:03:12 +020097
Jakub Kicinski93da52b2018-01-24 12:54:18 -080098 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +020099 cls_mall.command = TC_CLSMATCHALL_REPLACE;
100 cls_mall.exts = &head->exts;
101 cls_mall.cookie = cookie;
Yotam Gigib87f7932016-07-21 12:03:12 +0200102
Jiri Pirko2447a962017-10-19 15:50:33 +0200103 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSMATCHALL,
104 &cls_mall, skip_sw);
105 if (err < 0) {
Jakub Kicinskib505b292018-01-24 12:54:19 -0800106 mall_destroy_hw_filter(tp, head, cookie, NULL);
Jiri Pirko2447a962017-10-19 15:50:33 +0200107 return err;
108 } else if (err > 0) {
Jiri Pirkocaa72602018-01-17 11:46:50 +0100109 tcf_block_offload_inc(block, &head->flags);
Jiri Pirko2447a962017-10-19 15:50:33 +0200110 }
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200111
Jiri Pirko2447a962017-10-19 15:50:33 +0200112 if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
113 return -EINVAL;
Yotam Gigib87f7932016-07-21 12:03:12 +0200114
Jiri Pirko2447a962017-10-19 15:50:33 +0200115 return 0;
Yotam Gigib87f7932016-07-21 12:03:12 +0200116}
117
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800118static void mall_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200119{
120 struct cls_mall_head *head = rtnl_dereference(tp->root);
121
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200122 if (!head)
WANG Cong763dbf62017-04-19 14:21:21 -0700123 return;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200124
Jiri Pirko2447a962017-10-19 15:50:33 +0200125 if (!tc_skip_hw(head->flags))
Jakub Kicinskib505b292018-01-24 12:54:19 -0800126 mall_destroy_hw_filter(tp, head, (unsigned long) head, extack);
Yotam Gigib87f7932016-07-21 12:03:12 +0200127
Cong Wang57767e782017-11-06 13:47:26 -0800128 if (tcf_exts_get_net(&head->exts))
129 call_rcu(&head->rcu, mall_destroy_rcu);
130 else
131 __mall_destroy(head);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200132}
133
WANG Cong8113c092017-08-04 21:31:43 -0700134static void *mall_get(struct tcf_proto *tp, u32 handle)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200135{
WANG Cong8113c092017-08-04 21:31:43 -0700136 return NULL;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200137}
138
139static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
140 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
141 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
142};
143
144static int mall_set_parms(struct net *net, struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200145 struct cls_mall_head *head,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200146 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500147 struct nlattr *est, bool ovr,
148 struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200149{
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200150 int err;
151
Alexander Aring50a56192018-01-18 11:20:52 -0500152 err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, extack);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200153 if (err < 0)
Jiri Pirkoa74cb362017-08-04 14:29:08 +0200154 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200155
156 if (tb[TCA_MATCHALL_CLASSID]) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200157 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
158 tcf_bind_filter(tp, &head->res, base);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200159 }
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200160 return 0;
161}
162
163static int mall_change(struct net *net, struct sk_buff *in_skb,
164 struct tcf_proto *tp, unsigned long base,
165 u32 handle, struct nlattr **tca,
Alexander Aring7306db32018-01-18 11:20:51 -0500166 void **arg, bool ovr, struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200167{
168 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200169 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200170 struct cls_mall_head *new;
Yotam Gigib87f7932016-07-21 12:03:12 +0200171 u32 flags = 0;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200172 int err;
173
174 if (!tca[TCA_OPTIONS])
175 return -EINVAL;
176
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200177 if (head)
178 return -EEXIST;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200179
Johannes Bergfceb6432017-04-12 14:34:07 +0200180 err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
181 mall_policy, NULL);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200182 if (err < 0)
183 return err;
184
Yotam Gigib87f7932016-07-21 12:03:12 +0200185 if (tb[TCA_MATCHALL_FLAGS]) {
186 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
187 if (!tc_flags_valid(flags))
188 return -EINVAL;
189 }
190
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200191 new = kzalloc(sizeof(*new), GFP_KERNEL);
192 if (!new)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200193 return -ENOBUFS;
194
David S. Millere2160152017-02-02 16:54:00 -0500195 err = tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200196 if (err)
197 goto err_exts_init;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200198
199 if (!handle)
200 handle = 1;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200201 new->handle = handle;
202 new->flags = flags;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200203
Alexander Aring50a56192018-01-18 11:20:52 -0500204 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr,
205 extack);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200206 if (err)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200207 goto err_set_parms;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200208
Jiri Pirko2447a962017-10-19 15:50:33 +0200209 if (!tc_skip_hw(new->flags)) {
Quentin Monnet02798142018-01-19 17:44:44 -0800210 err = mall_replace_hw_filter(tp, new, (unsigned long)new,
211 extack);
Jiri Pirko2447a962017-10-19 15:50:33 +0200212 if (err)
213 goto err_replace_hw_filter;
Yotam Gigib87f7932016-07-21 12:03:12 +0200214 }
215
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200216 if (!tc_in_hw(new->flags))
217 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
218
WANG Cong8113c092017-08-04 21:31:43 -0700219 *arg = head;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200220 rcu_assign_pointer(tp->root, new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200221 return 0;
222
Yotam Gigiec2507d2017-01-03 19:20:24 +0200223err_replace_hw_filter:
224err_set_parms:
David S. Millere2160152017-02-02 16:54:00 -0500225 tcf_exts_destroy(&new->exts);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200226err_exts_init:
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200227 kfree(new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200228 return err;
229}
230
Alexander Aring571acf22018-01-18 11:20:53 -0500231static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
232 struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200233{
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200234 return -EOPNOTSUPP;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200235}
236
237static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg)
238{
239 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200240
241 if (arg->count < arg->skip)
242 goto skip;
WANG Cong8113c092017-08-04 21:31:43 -0700243 if (arg->fn(tp, head, arg) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200244 arg->stop = 1;
245skip:
246 arg->count++;
247}
248
WANG Cong8113c092017-08-04 21:31:43 -0700249static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200250 struct sk_buff *skb, struct tcmsg *t)
251{
WANG Cong8113c092017-08-04 21:31:43 -0700252 struct cls_mall_head *head = fh;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200253 struct nlattr *nest;
254
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200255 if (!head)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200256 return skb->len;
257
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200258 t->tcm_handle = head->handle;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200259
260 nest = nla_nest_start(skb, TCA_OPTIONS);
261 if (!nest)
262 goto nla_put_failure;
263
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200264 if (head->res.classid &&
265 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200266 goto nla_put_failure;
267
Or Gerlitz7a335ad2017-02-16 10:31:11 +0200268 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
269 goto nla_put_failure;
270
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200271 if (tcf_exts_dump(skb, &head->exts))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200272 goto nla_put_failure;
273
274 nla_nest_end(skb, nest);
275
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200276 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200277 goto nla_put_failure;
278
279 return skb->len;
280
281nla_put_failure:
282 nla_nest_cancel(skb, nest);
283 return -1;
284}
285
Cong Wang07d79fc2017-08-30 14:30:36 -0700286static void mall_bind_class(void *fh, u32 classid, unsigned long cl)
287{
288 struct cls_mall_head *head = fh;
289
290 if (head && head->res.classid == classid)
291 head->res.class = cl;
292}
293
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200294static struct tcf_proto_ops cls_mall_ops __read_mostly = {
295 .kind = "matchall",
296 .classify = mall_classify,
297 .init = mall_init,
298 .destroy = mall_destroy,
299 .get = mall_get,
300 .change = mall_change,
301 .delete = mall_delete,
302 .walk = mall_walk,
303 .dump = mall_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700304 .bind_class = mall_bind_class,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200305 .owner = THIS_MODULE,
306};
307
308static int __init cls_mall_init(void)
309{
310 return register_tcf_proto_ops(&cls_mall_ops);
311}
312
313static void __exit cls_mall_exit(void)
314{
315 unregister_tcf_proto_ops(&cls_mall_ops);
316}
317
318module_init(cls_mall_init);
319module_exit(cls_mall_exit);
320
321MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
322MODULE_DESCRIPTION("Match-all classifier");
323MODULE_LICENSE("GPL v2");