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