Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 1 | /* |
| 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 Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 15 | #include <linux/percpu.h> |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 16 | |
| 17 | #include <net/sch_generic.h> |
| 18 | #include <net/pkt_cls.h> |
| 19 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 20 | struct cls_mall_head { |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 21 | struct tcf_exts exts; |
| 22 | struct tcf_result res; |
| 23 | u32 handle; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 24 | u32 flags; |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 25 | unsigned int in_hw_count; |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 26 | struct tc_matchall_pcnt __percpu *pf; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 27 | struct rcu_work rwork; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | static 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 Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 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 | |
Jakub Kicinski | b505b29 | 2018-01-24 12:54:19 -0800 | [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 | |
Cong Wang | aeb3fec | 2018-12-11 11:15:46 -0800 | [diff] [blame] | 78 | tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 79 | tcf_block_offload_dec(block, &head->flags); |
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 | |
Jakub Kicinski | 93da52b | 2018-01-24 12:54:18 -0800 | [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 | |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 100 | err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts); |
| 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 | |
Cong Wang | aeb3fec | 2018-12-11 11:15:46 -0800 | [diff] [blame] | 112 | err = tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, skip_sw); |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 113 | kfree(cls_mall.rule); |
| 114 | |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 115 | if (err < 0) { |
Jakub Kicinski | b505b29 | 2018-01-24 12:54:19 -0800 | [diff] [blame] | 116 | mall_destroy_hw_filter(tp, head, cookie, NULL); |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 117 | return err; |
| 118 | } else if (err > 0) { |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 119 | head->in_hw_count = err; |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 120 | tcf_block_offload_inc(block, &head->flags); |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 121 | } |
Or Gerlitz | c7d2b2f | 2017-02-16 10:31:14 +0200 | [diff] [blame] | 122 | |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 123 | if (skip_sw && !(head->flags & TCA_CLS_FLAGS_IN_HW)) |
| 124 | return -EINVAL; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 125 | |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 126 | return 0; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 129 | static void mall_destroy(struct tcf_proto *tp, bool rtnl_held, |
| 130 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 131 | { |
| 132 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
| 133 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 134 | if (!head) |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 135 | return; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 136 | |
Hangbin Liu | a51c76b | 2018-08-14 17:28:26 +0800 | [diff] [blame] | 137 | tcf_unbind_filter(tp, &head->res); |
| 138 | |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 139 | if (!tc_skip_hw(head->flags)) |
Jakub Kicinski | b505b29 | 2018-01-24 12:54:19 -0800 | [diff] [blame] | 140 | mall_destroy_hw_filter(tp, head, (unsigned long) head, extack); |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 141 | |
Cong Wang | 57767e78 | 2017-11-06 13:47:26 -0800 | [diff] [blame] | 142 | if (tcf_exts_get_net(&head->exts)) |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 143 | tcf_queue_work(&head->rwork, mall_destroy_work); |
Cong Wang | 57767e78 | 2017-11-06 13:47:26 -0800 | [diff] [blame] | 144 | else |
| 145 | __mall_destroy(head); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 146 | } |
| 147 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 148 | static void *mall_get(struct tcf_proto *tp, u32 handle) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 149 | { |
Nicolas Dichtel | 0db6f8b | 2019-03-28 10:35:06 +0100 | [diff] [blame] | 150 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
| 151 | |
| 152 | if (head && head->handle == handle) |
| 153 | return head; |
| 154 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 155 | return NULL; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = { |
| 159 | [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC }, |
| 160 | [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 }, |
| 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, |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 166 | struct nlattr *est, bool ovr, |
| 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 | |
Vlad Buslov | ec6743a | 2019-02-11 10:55:43 +0200 | [diff] [blame] | 171 | err = tcf_exts_validate(net, tp, tb, est, &head->exts, ovr, true, |
| 172 | extack); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 173 | if (err < 0) |
Jiri Pirko | a74cb36 | 2017-08-04 14:29:08 +0200 | [diff] [blame] | 174 | return err; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 175 | |
| 176 | if (tb[TCA_MATCHALL_CLASSID]) { |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 177 | head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]); |
| 178 | tcf_bind_filter(tp, &head->res, base); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 179 | } |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static 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 Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 186 | void **arg, bool ovr, bool rtnl_held, |
| 187 | struct netlink_ext_ack *extack) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 188 | { |
| 189 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 190 | struct nlattr *tb[TCA_MATCHALL_MAX + 1]; |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 191 | struct cls_mall_head *new; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 192 | u32 flags = 0; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 193 | int err; |
| 194 | |
| 195 | if (!tca[TCA_OPTIONS]) |
| 196 | return -EINVAL; |
| 197 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 198 | if (head) |
| 199 | return -EEXIST; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 200 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 201 | err = nla_parse_nested_deprecated(tb, TCA_MATCHALL_MAX, |
| 202 | tca[TCA_OPTIONS], mall_policy, NULL); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 203 | if (err < 0) |
| 204 | return err; |
| 205 | |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 206 | 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 Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 212 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 213 | if (!new) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 214 | return -ENOBUFS; |
| 215 | |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 216 | err = tcf_exts_init(&new->exts, net, TCA_MATCHALL_ACT, 0); |
Yotam Gigi | ec2507d | 2017-01-03 19:20:24 +0200 | [diff] [blame] | 217 | if (err) |
| 218 | goto err_exts_init; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 219 | |
| 220 | if (!handle) |
| 221 | handle = 1; |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 222 | new->handle = handle; |
| 223 | new->flags = flags; |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 224 | new->pf = alloc_percpu(struct tc_matchall_pcnt); |
| 225 | if (!new->pf) { |
| 226 | err = -ENOMEM; |
| 227 | goto err_alloc_percpu; |
| 228 | } |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 229 | |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 230 | err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr, |
| 231 | extack); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 232 | if (err) |
Yotam Gigi | ec2507d | 2017-01-03 19:20:24 +0200 | [diff] [blame] | 233 | goto err_set_parms; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 234 | |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 235 | if (!tc_skip_hw(new->flags)) { |
Quentin Monnet | 0279814 | 2018-01-19 17:44:44 -0800 | [diff] [blame] | 236 | err = mall_replace_hw_filter(tp, new, (unsigned long)new, |
| 237 | extack); |
Jiri Pirko | 2447a96 | 2017-10-19 15:50:33 +0200 | [diff] [blame] | 238 | if (err) |
| 239 | goto err_replace_hw_filter; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Or Gerlitz | c7d2b2f | 2017-02-16 10:31:14 +0200 | [diff] [blame] | 242 | if (!tc_in_hw(new->flags)) |
| 243 | new->flags |= TCA_CLS_FLAGS_NOT_IN_HW; |
| 244 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 245 | *arg = head; |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 246 | rcu_assign_pointer(tp->root, new); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 247 | return 0; |
| 248 | |
Yotam Gigi | ec2507d | 2017-01-03 19:20:24 +0200 | [diff] [blame] | 249 | err_replace_hw_filter: |
| 250 | err_set_parms: |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 251 | free_percpu(new->pf); |
| 252 | err_alloc_percpu: |
David S. Miller | e216015 | 2017-02-02 16:54:00 -0500 | [diff] [blame] | 253 | tcf_exts_destroy(&new->exts); |
Yotam Gigi | ec2507d | 2017-01-03 19:20:24 +0200 | [diff] [blame] | 254 | err_exts_init: |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 255 | kfree(new); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 256 | return err; |
| 257 | } |
| 258 | |
Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 259 | static int mall_delete(struct tcf_proto *tp, void *arg, bool *last, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 260 | bool rtnl_held, struct netlink_ext_ack *extack) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 261 | { |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 262 | return -EOPNOTSUPP; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 265 | static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
| 266 | bool rtnl_held) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 267 | { |
| 268 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 269 | |
| 270 | if (arg->count < arg->skip) |
| 271 | goto skip; |
Vlad Buslov | d66022c | 2019-02-15 17:17:56 +0200 | [diff] [blame] | 272 | |
| 273 | if (!head) |
| 274 | return; |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 275 | if (arg->fn(tp, head, arg) < 0) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 276 | arg->stop = 1; |
| 277 | skip: |
| 278 | arg->count++; |
| 279 | } |
| 280 | |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 281 | static int mall_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb, |
| 282 | void *cb_priv, struct netlink_ext_ack *extack) |
| 283 | { |
| 284 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
| 285 | struct tc_cls_matchall_offload cls_mall = {}; |
| 286 | struct tcf_block *block = tp->chain->block; |
| 287 | int err; |
| 288 | |
| 289 | if (tc_skip_hw(head->flags)) |
| 290 | return 0; |
| 291 | |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 292 | cls_mall.rule = flow_rule_alloc(tcf_exts_num_actions(&head->exts)); |
| 293 | if (!cls_mall.rule) |
| 294 | return -ENOMEM; |
| 295 | |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 296 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, extack); |
| 297 | cls_mall.command = add ? |
| 298 | TC_CLSMATCHALL_REPLACE : TC_CLSMATCHALL_DESTROY; |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 299 | cls_mall.cookie = (unsigned long)head; |
| 300 | |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 301 | err = tc_setup_flow_action(&cls_mall.rule->action, &head->exts); |
| 302 | if (err) { |
| 303 | kfree(cls_mall.rule); |
| 304 | if (add && tc_skip_sw(head->flags)) { |
| 305 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); |
| 306 | return err; |
| 307 | } |
| 308 | } |
| 309 | |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 310 | err = cb(TC_SETUP_CLSMATCHALL, &cls_mall, cb_priv); |
Pieter Jansen van Vuuren | f00cbf19 | 2019-05-04 04:46:17 -0700 | [diff] [blame] | 311 | kfree(cls_mall.rule); |
| 312 | |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 313 | if (err) { |
| 314 | if (add && tc_skip_sw(head->flags)) |
| 315 | return err; |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | tc_cls_offload_cnt_update(block, &head->in_hw_count, &head->flags, add); |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
Pieter Jansen van Vuuren | b7fe4ab | 2019-05-04 04:46:23 -0700 | [diff] [blame^] | 324 | static void mall_stats_hw_filter(struct tcf_proto *tp, |
| 325 | struct cls_mall_head *head, |
| 326 | unsigned long cookie) |
| 327 | { |
| 328 | struct tc_cls_matchall_offload cls_mall = {}; |
| 329 | struct tcf_block *block = tp->chain->block; |
| 330 | |
| 331 | tc_cls_common_offload_init(&cls_mall.common, tp, head->flags, NULL); |
| 332 | cls_mall.command = TC_CLSMATCHALL_STATS; |
| 333 | cls_mall.cookie = cookie; |
| 334 | |
| 335 | tc_setup_cb_call(block, TC_SETUP_CLSMATCHALL, &cls_mall, false); |
| 336 | |
| 337 | tcf_exts_stats_update(&head->exts, cls_mall.stats.bytes, |
| 338 | cls_mall.stats.pkts, cls_mall.stats.lastused); |
| 339 | } |
| 340 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 341 | 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] | 342 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 343 | { |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 344 | struct tc_matchall_pcnt gpf = {}; |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 345 | struct cls_mall_head *head = fh; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 346 | struct nlattr *nest; |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 347 | int cpu; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 348 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 349 | if (!head) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 350 | return skb->len; |
| 351 | |
Pieter Jansen van Vuuren | b7fe4ab | 2019-05-04 04:46:23 -0700 | [diff] [blame^] | 352 | if (!tc_skip_hw(head->flags)) |
| 353 | mall_stats_hw_filter(tp, head, (unsigned long)head); |
| 354 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 355 | t->tcm_handle = head->handle; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 356 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 357 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 358 | if (!nest) |
| 359 | goto nla_put_failure; |
| 360 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 361 | if (head->res.classid && |
| 362 | nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid)) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 363 | goto nla_put_failure; |
| 364 | |
Or Gerlitz | 7a335ad | 2017-02-16 10:31:11 +0200 | [diff] [blame] | 365 | if (head->flags && nla_put_u32(skb, TCA_MATCHALL_FLAGS, head->flags)) |
| 366 | goto nla_put_failure; |
| 367 | |
Cong Wang | f88c19a | 2019-01-17 12:44:25 -0800 | [diff] [blame] | 368 | for_each_possible_cpu(cpu) { |
| 369 | struct tc_matchall_pcnt *pf = per_cpu_ptr(head->pf, cpu); |
| 370 | |
| 371 | gpf.rhit += pf->rhit; |
| 372 | } |
| 373 | |
| 374 | if (nla_put_64bit(skb, TCA_MATCHALL_PCNT, |
| 375 | sizeof(struct tc_matchall_pcnt), |
| 376 | &gpf, TCA_MATCHALL_PAD)) |
| 377 | goto nla_put_failure; |
| 378 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 379 | if (tcf_exts_dump(skb, &head->exts)) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 380 | goto nla_put_failure; |
| 381 | |
| 382 | nla_nest_end(skb, nest); |
| 383 | |
Yotam Gigi | fd62d9f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 384 | if (tcf_exts_dump_stats(skb, &head->exts) < 0) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 385 | goto nla_put_failure; |
| 386 | |
| 387 | return skb->len; |
| 388 | |
| 389 | nla_put_failure: |
| 390 | nla_nest_cancel(skb, nest); |
| 391 | return -1; |
| 392 | } |
| 393 | |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 394 | static void mall_bind_class(void *fh, u32 classid, unsigned long cl) |
| 395 | { |
| 396 | struct cls_mall_head *head = fh; |
| 397 | |
| 398 | if (head && head->res.classid == classid) |
| 399 | head->res.class = cl; |
| 400 | } |
| 401 | |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 402 | static struct tcf_proto_ops cls_mall_ops __read_mostly = { |
| 403 | .kind = "matchall", |
| 404 | .classify = mall_classify, |
| 405 | .init = mall_init, |
| 406 | .destroy = mall_destroy, |
| 407 | .get = mall_get, |
| 408 | .change = mall_change, |
| 409 | .delete = mall_delete, |
| 410 | .walk = mall_walk, |
John Hurley | 0efd1b3 | 2018-06-25 14:30:07 -0700 | [diff] [blame] | 411 | .reoffload = mall_reoffload, |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 412 | .dump = mall_dump, |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 413 | .bind_class = mall_bind_class, |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 414 | .owner = THIS_MODULE, |
| 415 | }; |
| 416 | |
| 417 | static int __init cls_mall_init(void) |
| 418 | { |
| 419 | return register_tcf_proto_ops(&cls_mall_ops); |
| 420 | } |
| 421 | |
| 422 | static void __exit cls_mall_exit(void) |
| 423 | { |
| 424 | unregister_tcf_proto_ops(&cls_mall_ops); |
| 425 | } |
| 426 | |
| 427 | module_init(cls_mall_init); |
| 428 | module_exit(cls_mall_exit); |
| 429 | |
| 430 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); |
| 431 | MODULE_DESCRIPTION("Match-all classifier"); |
| 432 | MODULE_LICENSE("GPL v2"); |