Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 2 | /* |
| 3 | * net/sched/cls_matchll.c Match-all classifier |
| 4 | * |
| 5 | * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com> |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/module.h> |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 11 | #include <linux/percpu.h> |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 12 | |
| 13 | #include <net/sch_generic.h> |
| 14 | #include <net/pkt_cls.h> |
| 15 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 16 | struct cls_mall_head { |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 17 | struct tcf_exts exts; |
| 18 | struct tcf_result res; |
| 19 | u32 handle; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 20 | u32 flags; |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 21 | unsigned int in_hw_count; |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 22 | struct tc_matchall_pcnt __percpu *pf; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 23 | struct rcu_work rwork; |
Jiri Pirko | f517f27 | 2019-06-17 18:02:32 +0200 | [diff] [blame] | 24 | bool deleting; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | static 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 Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 31 | |
Matteo Croce | 2542604 | 2019-05-02 10:51:05 +0200 | [diff] [blame] | 32 | if (unlikely(!head)) |
| 33 | return -1; |
| 34 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 35 | if (tc_skip_sw(head->flags)) |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 36 | return -1; |
| 37 | |
Davide Caratti | 3ff4cbe | 2017-09-16 14:02:21 +0200 | [diff] [blame] | 38 | *res = head->res; |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 39 | __this_cpu_inc(head->pf->rhit); |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 40 | return tcf_exts_exec(skb, &head->exts, res); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static int mall_init(struct tcf_proto *tp) |
| 44 | { |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
Cong Wang | 57767e78 | 2017-11-06 13:47:26 -0800 | [diff] [blame] | 48 | static void __mall_destroy(struct cls_mall_head *head) |
| 49 | { |
| 50 | tcf_exts_destroy(&head->exts); |
| 51 | tcf_exts_put_net(&head->exts); |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 52 | free_percpu(head->pf); |
Cong Wang | 57767e78 | 2017-11-06 13:47:26 -0800 | [diff] [blame] | 53 | kfree(head); |
| 54 | } |
| 55 | |
Cong Wang | df2735e | 2017-10-26 18:24:35 -0700 | [diff] [blame] | 56 | static void mall_destroy_work(struct work_struct *work) |
| 57 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 58 | struct cls_mall_head *head = container_of(to_rcu_work(work), |
| 59 | struct cls_mall_head, |
| 60 | rwork); |
Cong Wang | df2735e | 2017-10-26 18:24:35 -0700 | [diff] [blame] | 61 | rtnl_lock(); |
Cong Wang | 57767e78 | 2017-11-06 13:47:26 -0800 | [diff] [blame] | 62 | __mall_destroy(head); |
Cong Wang | df2735e | 2017-10-26 18:24:35 -0700 | [diff] [blame] | 63 | rtnl_unlock(); |
| 64 | } |
| 65 | |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 66 | static void mall_destroy_hw_filter(struct tcf_proto *tp, |
| 67 | struct cls_mall_head *head, |
Jakub Kicinski | b505b29 | 2018-01-24 12:54:19 -0800 | [diff] [blame] | 68 | unsigned long cookie, |
| 69 | struct netlink_ext_ack *extack) |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 70 | { |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 71 | struct tc_cls_matchall_offload cls_mall = {}; |
| 72 | struct tcf_block *block = tp->chain->block; |
| 73 | |
Pieter Jansen van Vuuren | d678714 | 2019-05-06 17:24:21 -0700 | [diff] [blame] | 74 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack); |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 75 | cls_mall.command = TC_CLSMATCHALL_DESTROY; |
| 76 | cls_mall.cookie = cookie; |
| 77 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 78 | tc_setup_cb_destroy(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall, false, |
| 79 | &head->flags, &head->in_hw_count, true); |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 82 | static int mall_replace_hw_filter(struct tcf_proto *tp, |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 83 | struct cls_mall_head *head, |
Quentin Monnet | 0279814 | 2018-01-19 17:44:44 -0800 | [diff] [blame] | 84 | unsigned long cookie, |
| 85 | struct netlink_ext_ack *extack) |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 86 | { |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 87 | struct tc_cls_matchall_offload cls_mall = {}; |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 88 | struct tcf_block *block = tp->chain->block; |
| 89 | bool skip_sw = tc_skip_sw(head->flags); |
Or Gerlitz | c7d2b2f | 2017-02-16 10:31:14 +0200 | [diff] [blame] | 90 | int err; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 91 | |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 92 | cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts)); |
| 93 | if (!cls_mall.rule) |
| 94 | return -ENOMEM; |
| 95 | |
Pieter Jansen van Vuuren | d678714 | 2019-05-06 17:24:21 -0700 | [diff] [blame] | 96 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack); |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 97 | cls_mall.command = TC_CLSMATCHALL_REPLACE; |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 98 | cls_mall.cookie = cookie; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 99 | |
Vlad Buslov | b15e7a6 | 2020-02-17 12:12:12 +0200 | [diff] [blame] | 100 | err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts); |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 101 | 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 Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 112 | err = tc_setup_cb_add(block, tp, TC_SETUP_CLSMATCHALL, &cls_mall, |
| 113 | skip_sw, &head->flags, &head->in_hw_count, true); |
Vlad Buslov | f2b795e | 2019-08-29 19:15:16 +0300 | [diff] [blame] | 114 | tc_cleanup_flow_action(&cls_mall.rule->action); |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 115 | kfree(cls_mall.rule); |
| 116 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 117 | if (err) { |
Jakub Kicinski | b505b29 | 2018-01-24 12:54:19 -0800 | [diff] [blame] | 118 | mall_destroy_hw_filter(tp, head, cookie, NULL); |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 119 | return err; |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 120 | } |
Or Gerlitz | c7d2b2f | 2017-02-16 10:31:14 +0200 | [diff] [blame] | 121 | |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 122 | if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW)) |
| 123 | return -EINVAL; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 124 | |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 125 | return 0; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 128 | static void mall_destroy(struct tcf_proto *tp, bool rtnl_held, |
| 129 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 130 | { |
| 131 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
| 132 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 133 | if (!head) |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 134 | return; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 135 | |
Hangbin Liu | a51c76b | 2018-08-14 17:28:26 +0800 | [diff] [blame] | 136 | tcf_unbind_filter(tp, &head->res); |
| 137 | |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 138 | if (!tc_skip_hw(head->flags)) |
Jakub Kicinski | b505b29 | 2018-01-24 12:54:19 -0800 | [diff] [blame] | 139 | mall_destroy_hw_filter(tp, head, (unsigned long) head, extack); |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 140 | |
Cong Wang | 57767e78 | 2017-11-06 13:47:26 -0800 | [diff] [blame] | 141 | if (tcf_exts_get_net(&head->exts)) |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 142 | tcf_queue_work(&head->rwork, mall_destroy_work); |
Cong Wang | 57767e78 | 2017-11-06 13:47:26 -0800 | [diff] [blame] | 143 | else |
| 144 | __mall_destroy(head); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 145 | } |
| 146 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 147 | static void *mall_get(struct tcf_proto *tp, u32 handle) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 148 | { |
Nicolas Dichtel | 0db6f8b | 2019-03-28 10:35:06 +0100 | [diff] [blame] | 149 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
| 150 | |
| 151 | if (head && head->handle == handle) |
| 152 | return head; |
| 153 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 154 | return NULL; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static 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 Caratti | 1afa3cc | 2020-02-11 19:33:39 +0100 | [diff] [blame] | 160 | [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 }, |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | static int mall_set_parms(struct net *net, struct tcf_proto *tp, |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 164 | struct cls_mall_head *head, |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 165 | unsigned long base, struct nlattr **tb, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 166 | struct nlattr *est, u32 flags, |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 167 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 168 | { |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 169 | int err; |
| 170 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 171 | err = tcf_exts_validate(net, tp, tb, est, &head->exts, flags, extack); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 172 | if (err < 0) |
Jiri Pirko | a74cb36 | 2017-08-04 14:29:08 +0200 | [diff] [blame] | 173 | return err; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 174 | |
| 175 | if (tb[TCA_MATCHALL_CLASSID]) { |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 176 | head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]); |
| 177 | tcf_bind_filter(tp, &head->res, base); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 178 | } |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int mall_change(struct net *net, struct sk_buff *in_skb, |
| 183 | struct tcf_proto *tp, unsigned long base, |
| 184 | u32 handle, struct nlattr **tca, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 185 | void **arg, u32 flags, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 186 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 187 | { |
| 188 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 189 | struct nlattr *tb[TCA_MATCHALL_MAX + 1]; |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 190 | struct cls_mall_head *new; |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 191 | u32 userflags = 0; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 192 | int err; |
| 193 | |
| 194 | if (!tca[TCA_OPTIONS]) |
| 195 | return -EINVAL; |
| 196 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 197 | if (head) |
| 198 | return -EEXIST; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 199 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 200 | err = nla_parse_nested_deprecated(tb, TCA_MATCHALL_MAX, |
| 201 | tca[TCA_OPTIONS], mall_policy, NULL); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 202 | if (err < 0) |
| 203 | return err; |
| 204 | |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 205 | if (tb[TCA_MATCHALL_FLAGS]) { |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 206 | userflags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]); |
| 207 | if (!tc_flags_valid(userflags)) |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 208 | return -EINVAL; |
| 209 | } |
| 210 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 211 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 212 | if (!new) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 213 | return -ENOBUFS; |
| 214 | |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 215 | err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0); |
Yotam Gigi | ec2507d | 2017-01-03 19:20:24 +0200 | [diff] [blame] | 216 | if (err) |
| 217 | goto err_exts_init; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 218 | |
| 219 | if (!handle) |
| 220 | handle = 1; |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 221 | new->handle = handle; |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 222 | new->flags = userflags; |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 223 | new->pf = alloc_percpu(struct tc_matchall_pcnt); |
| 224 | if (!new->pf) { |
| 225 | err = -ENOMEM; |
| 226 | goto err_alloc_percpu; |
| 227 | } |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 228 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 229 | err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], flags, |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 230 | extack); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 231 | if (err) |
Yotam Gigi | ec2507d | 2017-01-03 19:20:24 +0200 | [diff] [blame] | 232 | goto err_set_parms; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 233 | |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 234 | if (!tc_skip_hw(new->flags)) { |
Quentin Monnet | 0279814 | 2018-01-19 17:44:44 -0800 | [diff] [blame] | 235 | err = mall_replace_hw_filter(tp, new, (unsigned long)new, |
| 236 | extack); |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 237 | if (err) |
| 238 | goto err_replace_hw_filter; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Or Gerlitz | c7d2b2f | 2017-02-16 10:31:14 +0200 | [diff] [blame] | 241 | if (!tc_in_hw(new->flags)) |
| 242 | new->flags |= TCA_CLS_FLAGS_NOT_IN_HW; |
| 243 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 244 | *arg = head; |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 245 | rcu_assign_pointer(tp->root, new); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 246 | return 0; |
| 247 | |
Yotam Gigi | ec2507d | 2017-01-03 19:20:24 +0200 | [diff] [blame] | 248 | err_replace_hw_filter: |
| 249 | err_set_parms: |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 250 | free_percpu(new->pf); |
| 251 | err_alloc_percpu: |
David S. Miller | e216015 | 2017-02-02 16:54:00 -0500 | [diff] [blame] | 252 | tcf_exts_destroy(&new->exts); |
Yotam Gigi | ec2507d | 2017-01-03 19:20:24 +0200 | [diff] [blame] | 253 | err_exts_init: |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 254 | kfree(new); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 255 | return err; |
| 256 | } |
| 257 | |
Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 258 | static int mall_delete(struct tcf_proto *tp, void *arg, bool *last, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 259 | bool rtnl_held, struct netlink_ext_ack *extack) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 260 | { |
Jiri Pirko | f517f27 | 2019-06-17 18:02:32 +0200 | [diff] [blame] | 261 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
| 262 | |
| 263 | head->deleting = true; |
| 264 | *last = true; |
| 265 | return 0; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 268 | static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
| 269 | bool rtnl_held) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 270 | { |
| 271 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 272 | |
| 273 | if (arg->count < arg->skip) |
| 274 | goto skip; |
Vlad Buslov | d66022c | 2019-02-15 17:17:56 +0200 | [diff] [blame] | 275 | |
Jiri Pirko | f517f27 | 2019-06-17 18:02:32 +0200 | [diff] [blame] | 276 | if (!head || head->deleting) |
Vlad Buslov | d66022c | 2019-02-15 17:17:56 +0200 | [diff] [blame] | 277 | return; |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 278 | if (arg->fn(tp, head, arg) < 0) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 279 | arg->stop = 1; |
| 280 | skip: |
| 281 | arg->count++; |
| 282 | } |
| 283 | |
Pablo Neira Ayuso | a732331 | 2019-07-19 18:20:15 +0200 | [diff] [blame] | 284 | static int mall_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb, |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 285 | void *cb_priv, struct netlink_ext_ack *extack) |
| 286 | { |
| 287 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
| 288 | struct tc_cls_matchall_offload cls_mall = {}; |
| 289 | struct tcf_block *block = tp->chain->block; |
| 290 | int err; |
| 291 | |
| 292 | if (tc_skip_hw(head->flags)) |
| 293 | return 0; |
| 294 | |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 295 | cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts)); |
| 296 | if (!cls_mall.rule) |
| 297 | return -ENOMEM; |
| 298 | |
Pieter Jansen van Vuuren | d678714 | 2019-05-06 17:24:21 -0700 | [diff] [blame] | 299 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack); |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 300 | cls_mall.command = add ? |
| 301 | TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY; |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 302 | cls_mall.cookie = (unsigned long)head; |
| 303 | |
Vlad Buslov | b15e7a6 | 2020-02-17 12:12:12 +0200 | [diff] [blame] | 304 | err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts); |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 305 | if (err) { |
| 306 | kfree(cls_mall.rule); |
| 307 | if (add && tc_skip_sw(head->flags)) { |
| 308 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); |
| 309 | return err; |
| 310 | } |
Pieter Jansen van Vuuren | 5f05836 | 2019-05-08 15:56:07 -0700 | [diff] [blame] | 311 | return 0; |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 314 | err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSMATCHALL, |
| 315 | &cls_mall, cb_priv, &head->flags, |
| 316 | &head->in_hw_count); |
Vlad Buslov | f2b795e | 2019-08-29 19:15:16 +0300 | [diff] [blame] | 317 | tc_cleanup_flow_action(&cls_mall.rule->action); |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 318 | kfree(cls_mall.rule); |
| 319 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 320 | if (err) |
| 321 | return err; |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Pieter Jansen van Vuuren | b7fe4ab | 2019-05-04 04:46:23 -0700 | [diff] [blame] | 326 | static void mall_stats_hw_filter(struct tcf_proto *tp, |
| 327 | struct cls_mall_head *head, |
| 328 | unsigned long cookie) |
| 329 | { |
| 330 | struct tc_cls_matchall_offload cls_mall = {}; |
| 331 | struct tcf_block *block = tp->chain->block; |
| 332 | |
Pieter Jansen van Vuuren | d678714 | 2019-05-06 17:24:21 -0700 | [diff] [blame] | 333 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, NULL); |
Pieter Jansen van Vuuren | b7fe4ab | 2019-05-04 04:46:23 -0700 | [diff] [blame] | 334 | cls_mall.command = TC_CLSMATCHALL_STATS; |
| 335 | cls_mall.cookie = cookie; |
| 336 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 337 | tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false, true); |
Pieter Jansen van Vuuren | b7fe4ab | 2019-05-04 04:46:23 -0700 | [diff] [blame] | 338 | |
| 339 | tcf_exts_stats_update(&head->exts, cls_mall.stats.bytes, |
Po Liu | 4b61d3e | 2020-06-19 14:01:07 +0800 | [diff] [blame] | 340 | cls_mall.stats.pkts, cls_mall.stats.drops, |
| 341 | cls_mall.stats.lastused, |
Jiri Pirko | 93a129e | 2020-03-28 16:37:43 +0100 | [diff] [blame] | 342 | cls_mall.stats.used_hw_stats, |
| 343 | cls_mall.stats.used_hw_stats_valid); |
Pieter Jansen van Vuuren | b7fe4ab | 2019-05-04 04:46:23 -0700 | [diff] [blame] | 344 | } |
| 345 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 346 | static int mall_dump(struct net *net, struct tcf_proto *tp, void *fh, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 347 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 348 | { |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 349 | struct tc_matchall_pcnt gpf = {}; |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 350 | struct cls_mall_head *head = fh; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 351 | struct nlattr *nest; |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 352 | int cpu; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 353 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 354 | if (!head) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 355 | return skb->len; |
| 356 | |
Pieter Jansen van Vuuren | b7fe4ab | 2019-05-04 04:46:23 -0700 | [diff] [blame] | 357 | if (!tc_skip_hw(head->flags)) |
| 358 | mall_stats_hw_filter(tp, head, (unsigned long)head); |
| 359 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 360 | t->tcm_handle = head->handle; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 361 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 362 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 363 | if (!nest) |
| 364 | goto nla_put_failure; |
| 365 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 366 | if (head->res.classid && |
| 367 | nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid)) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 368 | goto nla_put_failure; |
| 369 | |
Or Gerlitz | 7a335ad | 2017-02-16 10:31:11 +0200 | [diff] [blame] | 370 | if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags)) |
| 371 | goto nla_put_failure; |
| 372 | |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 373 | for_each_possible_cpu(cpu) { |
| 374 | struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu); |
| 375 | |
| 376 | gpf.rhit += pf->rhit; |
| 377 | } |
| 378 | |
| 379 | if (nla_put_64bit(skb, TCA_MATCHALL_PCNT, |
| 380 | sizeof(struct tc_matchall_pcnt), |
| 381 | &gpf, TCA_MATCHALL_PAD)) |
| 382 | goto nla_put_failure; |
| 383 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 384 | if (tcf_exts_dump(skb, &head->exts)) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 385 | goto nla_put_failure; |
| 386 | |
| 387 | nla_nest_end(skb, nest); |
| 388 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 389 | if (tcf_exts_dump_stats(skb, &head->exts) < 0) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 390 | goto nla_put_failure; |
| 391 | |
| 392 | return skb->len; |
| 393 | |
| 394 | nla_put_failure: |
| 395 | nla_nest_cancel(skb, nest); |
| 396 | return -1; |
| 397 | } |
| 398 | |
Cong Wang | 2e24cd7 | 2020-01-23 16:26:18 -0800 | [diff] [blame] | 399 | static void mall_bind_class(void *fh, u32 classid, unsigned long cl, void *q, |
| 400 | unsigned long base) |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 401 | { |
| 402 | struct cls_mall_head *head = fh; |
| 403 | |
Cong Wang | 2e24cd7 | 2020-01-23 16:26:18 -0800 | [diff] [blame] | 404 | if (head && head->res.classid == classid) { |
| 405 | if (cl) |
| 406 | __tcf_bind_filter(q, &head->res, base); |
| 407 | else |
| 408 | __tcf_unbind_filter(q, &head->res); |
| 409 | } |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 412 | static struct tcf_proto_ops cls_mall_ops __read_mostly = { |
| 413 | .kind = "matchall", |
| 414 | .classify = mall_classify, |
| 415 | .init = mall_init, |
| 416 | .destroy = mall_destroy, |
| 417 | .get = mall_get, |
| 418 | .change = mall_change, |
| 419 | .delete = mall_delete, |
| 420 | .walk = mall_walk, |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 421 | .reoffload = mall_reoffload, |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 422 | .dump = mall_dump, |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 423 | .bind_class = mall_bind_class, |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 424 | .owner = THIS_MODULE, |
| 425 | }; |
| 426 | |
| 427 | static int __init cls_mall_init(void) |
| 428 | { |
| 429 | return register_tcf_proto_ops(&cls_mall_ops); |
| 430 | } |
| 431 | |
| 432 | static void __exit cls_mall_exit(void) |
| 433 | { |
| 434 | unregister_tcf_proto_ops(&cls_mall_ops); |
| 435 | } |
| 436 | |
| 437 | module_init(cls_mall_init); |
| 438 | module_exit(cls_mall_exit); |
| 439 | |
| 440 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); |
| 441 | MODULE_DESCRIPTION("Match-all classifier"); |
| 442 | MODULE_LICENSE("GPL v2"); |