blob: 459921bd3d87b5a563d3725015f8765b20566aa0 [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>
Cong Wangf88c19a2019-01-17 12:44:25 -080015#include <linux/percpu.h>
Jiri Pirkobf3994d2016-07-21 12:03:11 +020016
17#include <net/sch_generic.h>
18#include <net/pkt_cls.h>
19
Yotam Gigifd62d9f2017-01-31 15:14:29 +020020struct cls_mall_head {
Jiri Pirkobf3994d2016-07-21 12:03:11 +020021 struct tcf_exts exts;
22 struct tcf_result res;
23 u32 handle;
Yotam Gigib87f7932016-07-21 12:03:12 +020024 u32 flags;
John Hurley0efd1b32018-06-25 14:30:07 -070025 unsigned int in_hw_count;
Cong Wangf88c19a2019-01-17 12:44:25 -080026 struct tc_matchall_pcnt __percpu *pf;
Cong Wangaaa908f2018-05-23 15:26:53 -070027 struct rcu_work rwork;
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;
Cong Wangf88c19a2019-01-17 12:44:25 -080039 __this_cpu_inc(head->pf->rhit);
Yotam Gigifd62d9f2017-01-31 15:14:29 +020040 return tcf_exts_exec(skb, &head->exts, res);
Jiri Pirkobf3994d2016-07-21 12:03:11 +020041}
42
43static int mall_init(struct tcf_proto *tp)
44{
Jiri Pirkobf3994d2016-07-21 12:03:11 +020045 return 0;
46}
47
Cong Wang57767e782017-11-06 13:47:26 -080048static void __mall_destroy(struct cls_mall_head *head)
49{
50 tcf_exts_destroy(&head->exts);
51 tcf_exts_put_net(&head->exts);
Cong Wangf88c19a2019-01-17 12:44:25 -080052 free_percpu(head->pf);
Cong Wang57767e782017-11-06 13:47:26 -080053 kfree(head);
54}
55
Cong Wangdf2735e2017-10-26 18:24:35 -070056static void mall_destroy_work(struct work_struct *work)
57{
Cong Wangaaa908f2018-05-23 15:26:53 -070058 struct cls_mall_head *head = container_of(to_rcu_work(work),
59 struct cls_mall_head,
60 rwork);
Cong Wangdf2735e2017-10-26 18:24:35 -070061 rtnl_lock();
Cong Wang57767e782017-11-06 13:47:26 -080062 __mall_destroy(head);
Cong Wangdf2735e2017-10-26 18:24:35 -070063 rtnl_unlock();
64}
65
Jiri Pirko2447a962017-10-19 15:50:33 +020066static void mall_destroy_hw_filter(struct tcf_proto *tp,
67 struct cls_mall_head *head,
Jakub Kicinskib505b292018-01-24 12:54:19 -080068 unsigned long cookie,
69 struct netlink_ext_ack *extack)
Jiri Pirko2447a962017-10-19 15:50:33 +020070{
Jiri Pirko2447a962017-10-19 15:50:33 +020071 struct tc_cls_matchall_offload cls_mall = {};
72 struct tcf_block *block = tp->chain->block;
73
Jakub Kicinskib505b292018-01-24 12:54:19 -080074 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
Jiri Pirko2447a962017-10-19 15:50:33 +020075 cls_mall.command = TC_CLSMATCHALL_DESTROY;
76 cls_mall.cookie = cookie;
77
Cong Wangaeb3fec2018-12-11 11:15:46 -080078 tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false);
Jiri Pirkocaa72602018-01-17 11:46:50 +010079 tcf_block_offload_dec(block, &head->flags);
Jiri Pirko2447a962017-10-19 15:50:33 +020080}
81
Yotam Gigib87f7932016-07-21 12:03:12 +020082static int mall_replace_hw_filter(struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +020083 struct cls_mall_head *head,
Quentin Monnet02798142018-01-19 17:44:44 -080084 unsigned long cookie,
85 struct netlink_ext_ack *extack)
Yotam Gigib87f7932016-07-21 12:03:12 +020086{
Jiri Pirkode4784c2017-08-07 10:15:32 +020087 struct tc_cls_matchall_offload cls_mall = {};
Jiri Pirko2447a962017-10-19 15:50:33 +020088 struct tcf_block *block = tp->chain->block;
89 bool skip_sw = tc_skip_sw(head->flags);
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +020090 int err;
Yotam Gigib87f7932016-07-21 12:03:12 +020091
Jakub Kicinski93da52b2018-01-24 12:54:18 -080092 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +020093 cls_mall.command = TC_CLSMATCHALL_REPLACE;
94 cls_mall.exts = &head->exts;
95 cls_mall.cookie = cookie;
Yotam Gigib87f7932016-07-21 12:03:12 +020096
Cong Wangaeb3fec2018-12-11 11:15:46 -080097 err = tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, skip_sw);
Jiri Pirko2447a962017-10-19 15:50:33 +020098 if (err < 0) {
Jakub Kicinskib505b292018-01-24 12:54:19 -080099 mall_destroy_hw_filter(tp, head, cookie, NULL);
Jiri Pirko2447a962017-10-19 15:50:33 +0200100 return err;
101 } else if (err > 0) {
John Hurley0efd1b32018-06-25 14:30:07 -0700102 head->in_hw_count = err;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100103 tcf_block_offload_inc(block, &head->flags);
Jiri Pirko2447a962017-10-19 15:50:33 +0200104 }
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200105
Jiri Pirko2447a962017-10-19 15:50:33 +0200106 if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
107 return -EINVAL;
Yotam Gigib87f7932016-07-21 12:03:12 +0200108
Jiri Pirko2447a962017-10-19 15:50:33 +0200109 return 0;
Yotam Gigib87f7932016-07-21 12:03:12 +0200110}
111
Vlad Buslov12db03b2019-02-11 10:55:45 +0200112static void mall_destroy(struct tcf_proto *tp, bool rtnl_held,
113 struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200114{
115 struct cls_mall_head *head = rtnl_dereference(tp->root);
116
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200117 if (!head)
WANG Cong763dbf62017-04-19 14:21:21 -0700118 return;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200119
Hangbin Liua51c76b2018-08-14 17:28:26 +0800120 tcf_unbind_filter(tp, &head->res);
121
Jiri Pirko2447a962017-10-19 15:50:33 +0200122 if (!tc_skip_hw(head->flags))
Jakub Kicinskib505b292018-01-24 12:54:19 -0800123 mall_destroy_hw_filter(tp, head, (unsigned long) head, extack);
Yotam Gigib87f7932016-07-21 12:03:12 +0200124
Cong Wang57767e782017-11-06 13:47:26 -0800125 if (tcf_exts_get_net(&head->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700126 tcf_queue_work(&head->rwork, mall_destroy_work);
Cong Wang57767e782017-11-06 13:47:26 -0800127 else
128 __mall_destroy(head);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200129}
130
WANG Cong8113c092017-08-04 21:31:43 -0700131static void *mall_get(struct tcf_proto *tp, u32 handle)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200132{
WANG Cong8113c092017-08-04 21:31:43 -0700133 return NULL;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200134}
135
136static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
137 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
138 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
139};
140
141static int mall_set_parms(struct net *net, struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200142 struct cls_mall_head *head,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200143 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500144 struct nlattr *est, bool ovr,
145 struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200146{
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200147 int err;
148
Vlad Buslovec6743a2019-02-11 10:55:43 +0200149 err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, true,
150 extack);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200151 if (err < 0)
Jiri Pirkoa74cb362017-08-04 14:29:08 +0200152 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200153
154 if (tb[TCA_MATCHALL_CLASSID]) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200155 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
156 tcf_bind_filter(tp, &head->res, base);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200157 }
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200158 return 0;
159}
160
161static int mall_change(struct net *net, struct sk_buff *in_skb,
162 struct tcf_proto *tp, unsigned long base,
163 u32 handle, struct nlattr **tca,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200164 void **arg, bool ovr, bool rtnl_held,
165 struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200166{
167 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200168 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200169 struct cls_mall_head *new;
Yotam Gigib87f7932016-07-21 12:03:12 +0200170 u32 flags = 0;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200171 int err;
172
173 if (!tca[TCA_OPTIONS])
174 return -EINVAL;
175
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200176 if (head)
177 return -EEXIST;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200178
Johannes Bergfceb6432017-04-12 14:34:07 +0200179 err = nla_parse_nested(tb, TCA_MATCHALL_MAX, tca[TCA_OPTIONS],
180 mall_policy, NULL);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200181 if (err < 0)
182 return err;
183
Yotam Gigib87f7932016-07-21 12:03:12 +0200184 if (tb[TCA_MATCHALL_FLAGS]) {
185 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
186 if (!tc_flags_valid(flags))
187 return -EINVAL;
188 }
189
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200190 new = kzalloc(sizeof(*new), GFP_KERNEL);
191 if (!new)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200192 return -ENOBUFS;
193
Cong Wang14215102019-02-20 21:37:42 -0800194 err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200195 if (err)
196 goto err_exts_init;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200197
198 if (!handle)
199 handle = 1;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200200 new->handle = handle;
201 new->flags = flags;
Cong Wangf88c19a2019-01-17 12:44:25 -0800202 new->pf = alloc_percpu(struct tc_matchall_pcnt);
203 if (!new->pf) {
204 err = -ENOMEM;
205 goto err_alloc_percpu;
206 }
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200207
Alexander Aring50a56192018-01-18 11:20:52 -0500208 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr,
209 extack);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200210 if (err)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200211 goto err_set_parms;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200212
Jiri Pirko2447a962017-10-19 15:50:33 +0200213 if (!tc_skip_hw(new->flags)) {
Quentin Monnet02798142018-01-19 17:44:44 -0800214 err = mall_replace_hw_filter(tp, new, (unsigned long)new,
215 extack);
Jiri Pirko2447a962017-10-19 15:50:33 +0200216 if (err)
217 goto err_replace_hw_filter;
Yotam Gigib87f7932016-07-21 12:03:12 +0200218 }
219
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200220 if (!tc_in_hw(new->flags))
221 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
222
WANG Cong8113c092017-08-04 21:31:43 -0700223 *arg = head;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200224 rcu_assign_pointer(tp->root, new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200225 return 0;
226
Yotam Gigiec2507d2017-01-03 19:20:24 +0200227err_replace_hw_filter:
228err_set_parms:
Cong Wangf88c19a2019-01-17 12:44:25 -0800229 free_percpu(new->pf);
230err_alloc_percpu:
David S. Millere2160152017-02-02 16:54:00 -0500231 tcf_exts_destroy(&new->exts);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200232err_exts_init:
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200233 kfree(new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200234 return err;
235}
236
Alexander Aring571acf22018-01-18 11:20:53 -0500237static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200238 bool rtnl_held, struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200239{
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200240 return -EOPNOTSUPP;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200241}
242
Vlad Buslov12db03b2019-02-11 10:55:45 +0200243static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg,
244 bool rtnl_held)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200245{
246 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200247
248 if (arg->count < arg->skip)
249 goto skip;
Vlad Buslovd66022c2019-02-15 17:17:56 +0200250
251 if (!head)
252 return;
WANG Cong8113c092017-08-04 21:31:43 -0700253 if (arg->fn(tp, head, arg) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200254 arg->stop = 1;
255skip:
256 arg->count++;
257}
258
John Hurley0efd1b32018-06-25 14:30:07 -0700259static int mall_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
260 void *cb_priv, struct netlink_ext_ack *extack)
261{
262 struct cls_mall_head *head = rtnl_dereference(tp->root);
263 struct tc_cls_matchall_offload cls_mall = {};
264 struct tcf_block *block = tp->chain->block;
265 int err;
266
267 if (tc_skip_hw(head->flags))
268 return 0;
269
270 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
271 cls_mall.command = add ?
272 TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY;
273 cls_mall.exts = &head->exts;
274 cls_mall.cookie = (unsigned long)head;
275
276 err = cb(TC_SETUP_CLSMATCHALL, &cls_mall, cb_priv);
277 if (err) {
278 if (add && tc_skip_sw(head->flags))
279 return err;
280 return 0;
281 }
282
283 tc_cls_offload_cnt_update(block, &head->in_hw_count, &head->flags, add);
284
285 return 0;
286}
287
WANG Cong8113c092017-08-04 21:31:43 -0700288static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200289 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200290{
Cong Wangf88c19a2019-01-17 12:44:25 -0800291 struct tc_matchall_pcnt gpf = {};
WANG Cong8113c092017-08-04 21:31:43 -0700292 struct cls_mall_head *head = fh;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200293 struct nlattr *nest;
Cong Wangf88c19a2019-01-17 12:44:25 -0800294 int cpu;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200295
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200296 if (!head)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200297 return skb->len;
298
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200299 t->tcm_handle = head->handle;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200300
301 nest = nla_nest_start(skb, TCA_OPTIONS);
302 if (!nest)
303 goto nla_put_failure;
304
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200305 if (head->res.classid &&
306 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200307 goto nla_put_failure;
308
Or Gerlitz7a335ad2017-02-16 10:31:11 +0200309 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
310 goto nla_put_failure;
311
Cong Wangf88c19a2019-01-17 12:44:25 -0800312 for_each_possible_cpu(cpu) {
313 struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu);
314
315 gpf.rhit += pf->rhit;
316 }
317
318 if (nla_put_64bit(skb, TCA_MATCHALL_PCNT,
319 sizeof(struct tc_matchall_pcnt),
320 &gpf, TCA_MATCHALL_PAD))
321 goto nla_put_failure;
322
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200323 if (tcf_exts_dump(skb, &head->exts))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200324 goto nla_put_failure;
325
326 nla_nest_end(skb, nest);
327
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200328 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200329 goto nla_put_failure;
330
331 return skb->len;
332
333nla_put_failure:
334 nla_nest_cancel(skb, nest);
335 return -1;
336}
337
Cong Wang07d79fc2017-08-30 14:30:36 -0700338static void mall_bind_class(void *fh, u32 classid, unsigned long cl)
339{
340 struct cls_mall_head *head = fh;
341
342 if (head && head->res.classid == classid)
343 head->res.class = cl;
344}
345
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200346static struct tcf_proto_ops cls_mall_ops __read_mostly = {
347 .kind = "matchall",
348 .classify = mall_classify,
349 .init = mall_init,
350 .destroy = mall_destroy,
351 .get = mall_get,
352 .change = mall_change,
353 .delete = mall_delete,
354 .walk = mall_walk,
John Hurley0efd1b32018-06-25 14:30:07 -0700355 .reoffload = mall_reoffload,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200356 .dump = mall_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700357 .bind_class = mall_bind_class,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200358 .owner = THIS_MODULE,
359};
360
361static int __init cls_mall_init(void)
362{
363 return register_tcf_proto_ops(&cls_mall_ops);
364}
365
366static void __exit cls_mall_exit(void)
367{
368 unregister_tcf_proto_ops(&cls_mall_ops);
369}
370
371module_init(cls_mall_init);
372module_exit(cls_mall_exit);
373
374MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
375MODULE_DESCRIPTION("Match-all classifier");
376MODULE_LICENSE("GPL v2");