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