blob: cafb84480bab8c431ee4dba2bbddbdf701042836 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jiri Pirkobf3994d2016-07-21 12:03:11 +02002/*
3 * net/sched/cls_matchll.c Match-all classifier
4 *
5 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
Jiri Pirkobf3994d2016-07-21 12:03:11 +02006 */
7
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/module.h>
Cong Wangf88c19a2019-01-17 12:44:25 -080011#include <linux/percpu.h>
Jiri Pirkobf3994d2016-07-21 12:03:11 +020012
13#include <net/sch_generic.h>
14#include <net/pkt_cls.h>
15
Yotam Gigifd62d9f2017-01-31 15:14:29 +020016struct cls_mall_head {
Jiri Pirkobf3994d2016-07-21 12:03:11 +020017 struct tcf_exts exts;
18 struct tcf_result res;
19 u32 handle;
Yotam Gigib87f7932016-07-21 12:03:12 +020020 u32 flags;
John Hurley0efd1b32018-06-25 14:30:07 -070021 unsigned int in_hw_count;
Cong Wangf88c19a2019-01-17 12:44:25 -080022 struct tc_matchall_pcnt __percpu *pf;
Cong Wangaaa908f2018-05-23 15:26:53 -070023 struct rcu_work rwork;
Jiri Pirkof517f272019-06-17 18:02:32 +020024 bool deleting;
Jiri Pirkobf3994d2016-07-21 12:03:11 +020025};
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
Matteo Croce25426042019-05-02 10:51:05 +020032 if (unlikely(!head))
33 return -1;
34
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
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -070074 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
Vlad Buslov40119212019-08-26 16:44:59 +030078 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall, false,
79 &head->flags, &head->in_hw_count, true);
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
Pieter Jansen van Vuurenf00cbf192019-05-04 04:46:17 -070092 cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts));
93 if (!cls_mall.rule)
94 return -ENOMEM;
95
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -070096 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +020097 cls_mall.command = TC_CLSMATCHALL_REPLACE;
Jiri Pirkode4784c2017-08-07 10:15:32 +020098 cls_mall.cookie = cookie;
Yotam Gigib87f7932016-07-21 12:03:12 +020099
Vlad Buslovb15e7a62020-02-17 12:12:12 +0200100 err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts);
Pieter Jansen van Vuurenf00cbf192019-05-04 04:46:17 -0700101 if (err) {
102 kfree(cls_mall.rule);
103 mall_destroy_hw_filter(tp, head, cookie, NULL);
104 if (skip_sw)
105 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
106 else
107 err = 0;
108
109 return err;
110 }
111
Vlad Buslov40119212019-08-26 16:44:59 +0300112 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall,
113 skip_sw, &head->flags, &head->in_hw_count, true);
Vlad Buslovf2b795e2019-08-29 19:15:16 +0300114 tc_cleanup_flow_action(&cls_mall.rule->action);
Pieter Jansen van Vuurenf00cbf192019-05-04 04:46:17 -0700115 kfree(cls_mall.rule);
116
Vlad Buslov40119212019-08-26 16:44:59 +0300117 if (err) {
Jakub Kicinskib505b292018-01-24 12:54:19 -0800118 mall_destroy_hw_filter(tp, head, cookie, NULL);
Jiri Pirko2447a962017-10-19 15:50:33 +0200119 return err;
Jiri Pirko2447a962017-10-19 15:50:33 +0200120 }
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200121
Jiri Pirko2447a962017-10-19 15:50:33 +0200122 if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW))
123 return -EINVAL;
Yotam Gigib87f7932016-07-21 12:03:12 +0200124
Jiri Pirko2447a962017-10-19 15:50:33 +0200125 return 0;
Yotam Gigib87f7932016-07-21 12:03:12 +0200126}
127
Vlad Buslov12db03b2019-02-11 10:55:45 +0200128static void mall_destroy(struct tcf_proto *tp, bool rtnl_held,
129 struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200130{
131 struct cls_mall_head *head = rtnl_dereference(tp->root);
132
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200133 if (!head)
WANG Cong763dbf62017-04-19 14:21:21 -0700134 return;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200135
Hangbin Liua51c76b2018-08-14 17:28:26 +0800136 tcf_unbind_filter(tp, &head->res);
137
Jiri Pirko2447a962017-10-19 15:50:33 +0200138 if (!tc_skip_hw(head->flags))
Jakub Kicinskib505b292018-01-24 12:54:19 -0800139 mall_destroy_hw_filter(tp, head, (unsigned long) head, extack);
Yotam Gigib87f7932016-07-21 12:03:12 +0200140
Cong Wang57767e782017-11-06 13:47:26 -0800141 if (tcf_exts_get_net(&head->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700142 tcf_queue_work(&head->rwork, mall_destroy_work);
Cong Wang57767e782017-11-06 13:47:26 -0800143 else
144 __mall_destroy(head);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200145}
146
WANG Cong8113c092017-08-04 21:31:43 -0700147static void *mall_get(struct tcf_proto *tp, u32 handle)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200148{
Nicolas Dichtel0db6f8b2019-03-28 10:35:06 +0100149 struct cls_mall_head *head = rtnl_dereference(tp->root);
150
151 if (head && head->handle == handle)
152 return head;
153
WANG Cong8113c092017-08-04 21:31:43 -0700154 return NULL;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200155}
156
157static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
158 [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
159 [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
Davide Caratti1afa3cc2020-02-11 19:33:39 +0100160 [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 },
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200161};
162
163static int mall_set_parms(struct net *net, struct tcf_proto *tp,
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200164 struct cls_mall_head *head,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200165 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500166 struct nlattr *est, bool ovr,
167 struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200168{
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200169 int err;
170
Vlad Buslovec6743a2019-02-11 10:55:43 +0200171 err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, true,
172 extack);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200173 if (err < 0)
Jiri Pirkoa74cb362017-08-04 14:29:08 +0200174 return err;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200175
176 if (tb[TCA_MATCHALL_CLASSID]) {
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200177 head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]);
178 tcf_bind_filter(tp, &head->res, base);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200179 }
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200180 return 0;
181}
182
183static int mall_change(struct net *net, struct sk_buff *in_skb,
184 struct tcf_proto *tp, unsigned long base,
185 u32 handle, struct nlattr **tca,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200186 void **arg, bool ovr, bool rtnl_held,
187 struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200188{
189 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200190 struct nlattr *tb[TCA_MATCHALL_MAX + 1];
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200191 struct cls_mall_head *new;
Yotam Gigib87f7932016-07-21 12:03:12 +0200192 u32 flags = 0;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200193 int err;
194
195 if (!tca[TCA_OPTIONS])
196 return -EINVAL;
197
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200198 if (head)
199 return -EEXIST;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200200
Johannes Berg8cb08172019-04-26 14:07:28 +0200201 err = nla_parse_nested_deprecated(tb, TCA_MATCHALL_MAX,
202 tca[TCA_OPTIONS], mall_policy, NULL);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200203 if (err < 0)
204 return err;
205
Yotam Gigib87f7932016-07-21 12:03:12 +0200206 if (tb[TCA_MATCHALL_FLAGS]) {
207 flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]);
208 if (!tc_flags_valid(flags))
209 return -EINVAL;
210 }
211
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200212 new = kzalloc(sizeof(*new), GFP_KERNEL);
213 if (!new)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200214 return -ENOBUFS;
215
Cong Wang14215102019-02-20 21:37:42 -0800216 err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200217 if (err)
218 goto err_exts_init;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200219
220 if (!handle)
221 handle = 1;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200222 new->handle = handle;
223 new->flags = flags;
Cong Wangf88c19a2019-01-17 12:44:25 -0800224 new->pf = alloc_percpu(struct tc_matchall_pcnt);
225 if (!new->pf) {
226 err = -ENOMEM;
227 goto err_alloc_percpu;
228 }
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200229
Alexander Aring50a56192018-01-18 11:20:52 -0500230 err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr,
231 extack);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200232 if (err)
Yotam Gigiec2507d2017-01-03 19:20:24 +0200233 goto err_set_parms;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200234
Jiri Pirko2447a962017-10-19 15:50:33 +0200235 if (!tc_skip_hw(new->flags)) {
Quentin Monnet02798142018-01-19 17:44:44 -0800236 err = mall_replace_hw_filter(tp, new, (unsigned long)new,
237 extack);
Jiri Pirko2447a962017-10-19 15:50:33 +0200238 if (err)
239 goto err_replace_hw_filter;
Yotam Gigib87f7932016-07-21 12:03:12 +0200240 }
241
Or Gerlitzc7d2b2f2017-02-16 10:31:14 +0200242 if (!tc_in_hw(new->flags))
243 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
244
WANG Cong8113c092017-08-04 21:31:43 -0700245 *arg = head;
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200246 rcu_assign_pointer(tp->root, new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200247 return 0;
248
Yotam Gigiec2507d2017-01-03 19:20:24 +0200249err_replace_hw_filter:
250err_set_parms:
Cong Wangf88c19a2019-01-17 12:44:25 -0800251 free_percpu(new->pf);
252err_alloc_percpu:
David S. Millere2160152017-02-02 16:54:00 -0500253 tcf_exts_destroy(&new->exts);
Yotam Gigiec2507d2017-01-03 19:20:24 +0200254err_exts_init:
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200255 kfree(new);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200256 return err;
257}
258
Alexander Aring571acf22018-01-18 11:20:53 -0500259static int mall_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200260 bool rtnl_held, struct netlink_ext_ack *extack)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200261{
Jiri Pirkof517f272019-06-17 18:02:32 +0200262 struct cls_mall_head *head = rtnl_dereference(tp->root);
263
264 head->deleting = true;
265 *last = true;
266 return 0;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200267}
268
Vlad Buslov12db03b2019-02-11 10:55:45 +0200269static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg,
270 bool rtnl_held)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200271{
272 struct cls_mall_head *head = rtnl_dereference(tp->root);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200273
274 if (arg->count < arg->skip)
275 goto skip;
Vlad Buslovd66022c2019-02-15 17:17:56 +0200276
Jiri Pirkof517f272019-06-17 18:02:32 +0200277 if (!head || head->deleting)
Vlad Buslovd66022c2019-02-15 17:17:56 +0200278 return;
WANG Cong8113c092017-08-04 21:31:43 -0700279 if (arg->fn(tp, head, arg) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200280 arg->stop = 1;
281skip:
282 arg->count++;
283}
284
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200285static int mall_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
John Hurley0efd1b32018-06-25 14:30:07 -0700286 void *cb_priv, struct netlink_ext_ack *extack)
287{
288 struct cls_mall_head *head = rtnl_dereference(tp->root);
289 struct tc_cls_matchall_offload cls_mall = {};
290 struct tcf_block *block = tp->chain->block;
291 int err;
292
293 if (tc_skip_hw(head->flags))
294 return 0;
295
Pieter Jansen van Vuurenf00cbf192019-05-04 04:46:17 -0700296 cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts));
297 if (!cls_mall.rule)
298 return -ENOMEM;
299
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -0700300 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack);
John Hurley0efd1b32018-06-25 14:30:07 -0700301 cls_mall.command = add ?
302 TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY;
John Hurley0efd1b32018-06-25 14:30:07 -0700303 cls_mall.cookie = (unsigned long)head;
304
Vlad Buslovb15e7a62020-02-17 12:12:12 +0200305 err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts);
Pieter Jansen van Vuurenf00cbf192019-05-04 04:46:17 -0700306 if (err) {
307 kfree(cls_mall.rule);
308 if (add && tc_skip_sw(head->flags)) {
309 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
310 return err;
311 }
Pieter Jansen van Vuuren5f058362019-05-08 15:56:07 -0700312 return 0;
Pieter Jansen van Vuurenf00cbf192019-05-04 04:46:17 -0700313 }
314
Vlad Buslov40119212019-08-26 16:44:59 +0300315 err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSMATCHALL,
316 &cls_mall, cb_priv, &head->flags,
317 &head->in_hw_count);
Vlad Buslovf2b795e2019-08-29 19:15:16 +0300318 tc_cleanup_flow_action(&cls_mall.rule->action);
Pieter Jansen van Vuurenf00cbf192019-05-04 04:46:17 -0700319 kfree(cls_mall.rule);
320
Vlad Buslov40119212019-08-26 16:44:59 +0300321 if (err)
322 return err;
John Hurley0efd1b32018-06-25 14:30:07 -0700323
324 return 0;
325}
326
Pieter Jansen van Vuurenb7fe4ab2019-05-04 04:46:23 -0700327static void mall_stats_hw_filter(struct tcf_proto *tp,
328 struct cls_mall_head *head,
329 unsigned long cookie)
330{
331 struct tc_cls_matchall_offload cls_mall = {};
332 struct tcf_block *block = tp->chain->block;
333
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -0700334 tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, NULL);
Pieter Jansen van Vuurenb7fe4ab2019-05-04 04:46:23 -0700335 cls_mall.command = TC_CLSMATCHALL_STATS;
336 cls_mall.cookie = cookie;
337
Vlad Buslov40119212019-08-26 16:44:59 +0300338 tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false, true);
Pieter Jansen van Vuurenb7fe4ab2019-05-04 04:46:23 -0700339
340 tcf_exts_stats_update(&head->exts, cls_mall.stats.bytes,
Po Liu4b61d3e2020-06-19 14:01:07 +0800341 cls_mall.stats.pkts, cls_mall.stats.drops,
342 cls_mall.stats.lastused,
Jiri Pirko93a129e2020-03-28 16:37:43 +0100343 cls_mall.stats.used_hw_stats,
344 cls_mall.stats.used_hw_stats_valid);
Pieter Jansen van Vuurenb7fe4ab2019-05-04 04:46:23 -0700345}
346
WANG Cong8113c092017-08-04 21:31:43 -0700347static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200348 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200349{
Cong Wangf88c19a2019-01-17 12:44:25 -0800350 struct tc_matchall_pcnt gpf = {};
WANG Cong8113c092017-08-04 21:31:43 -0700351 struct cls_mall_head *head = fh;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200352 struct nlattr *nest;
Cong Wangf88c19a2019-01-17 12:44:25 -0800353 int cpu;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200354
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200355 if (!head)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200356 return skb->len;
357
Pieter Jansen van Vuurenb7fe4ab2019-05-04 04:46:23 -0700358 if (!tc_skip_hw(head->flags))
359 mall_stats_hw_filter(tp, head, (unsigned long)head);
360
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200361 t->tcm_handle = head->handle;
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200362
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200363 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200364 if (!nest)
365 goto nla_put_failure;
366
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200367 if (head->res.classid &&
368 nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200369 goto nla_put_failure;
370
Or Gerlitz7a335ad2017-02-16 10:31:11 +0200371 if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags))
372 goto nla_put_failure;
373
Cong Wangf88c19a2019-01-17 12:44:25 -0800374 for_each_possible_cpu(cpu) {
375 struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu);
376
377 gpf.rhit += pf->rhit;
378 }
379
380 if (nla_put_64bit(skb, TCA_MATCHALL_PCNT,
381 sizeof(struct tc_matchall_pcnt),
382 &gpf, TCA_MATCHALL_PAD))
383 goto nla_put_failure;
384
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200385 if (tcf_exts_dump(skb, &head->exts))
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200386 goto nla_put_failure;
387
388 nla_nest_end(skb, nest);
389
Yotam Gigifd62d9f2017-01-31 15:14:29 +0200390 if (tcf_exts_dump_stats(skb, &head->exts) < 0)
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200391 goto nla_put_failure;
392
393 return skb->len;
394
395nla_put_failure:
396 nla_nest_cancel(skb, nest);
397 return -1;
398}
399
Cong Wang2e24cd72020-01-23 16:26:18 -0800400static void mall_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
401 unsigned long base)
Cong Wang07d79fc2017-08-30 14:30:36 -0700402{
403 struct cls_mall_head *head = fh;
404
Cong Wang2e24cd72020-01-23 16:26:18 -0800405 if (head && head->res.classid == classid) {
406 if (cl)
407 __tcf_bind_filter(q, &head->res, base);
408 else
409 __tcf_unbind_filter(q, &head->res);
410 }
Cong Wang07d79fc2017-08-30 14:30:36 -0700411}
412
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200413static struct tcf_proto_ops cls_mall_ops __read_mostly = {
414 .kind = "matchall",
415 .classify = mall_classify,
416 .init = mall_init,
417 .destroy = mall_destroy,
418 .get = mall_get,
419 .change = mall_change,
420 .delete = mall_delete,
421 .walk = mall_walk,
John Hurley0efd1b32018-06-25 14:30:07 -0700422 .reoffload = mall_reoffload,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200423 .dump = mall_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700424 .bind_class = mall_bind_class,
Jiri Pirkobf3994d2016-07-21 12:03:11 +0200425 .owner = THIS_MODULE,
426};
427
428static int __init cls_mall_init(void)
429{
430 return register_tcf_proto_ops(&cls_mall_ops);
431}
432
433static void __exit cls_mall_exit(void)
434{
435 unregister_tcf_proto_ops(&cls_mall_ops);
436}
437
438module_init(cls_mall_init);
439module_exit(cls_mall_exit);
440
441MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
442MODULE_DESCRIPTION("Match-all classifier");
443MODULE_LICENSE("GPL v2");