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> |
| 15 | |
| 16 | #include <net/sch_generic.h> |
| 17 | #include <net/pkt_cls.h> |
| 18 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 19 | struct cls_mall_head { |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 20 | struct tcf_exts exts; |
| 21 | struct tcf_result res; |
| 22 | u32 handle; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 23 | u32 flags; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 24 | struct rcu_head rcu; |
| 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 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 32 | if (tc_skip_sw(head->flags)) |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 33 | return -1; |
| 34 | |
Davide Caratti | b13bc54 | 2017-09-16 14:02:21 +0200 | [diff] [blame] | 35 | *res = head->res; |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 36 | return tcf_exts_exec(skb, &head->exts, res); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static int mall_init(struct tcf_proto *tp) |
| 40 | { |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 44 | static void mall_destroy_rcu(struct rcu_head *rcu) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 45 | { |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 46 | struct cls_mall_head *head = container_of(rcu, struct cls_mall_head, |
| 47 | rcu); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 48 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 49 | tcf_exts_destroy(&head->exts); |
| 50 | kfree(head); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 53 | static int mall_replace_hw_filter(struct tcf_proto *tp, |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 54 | struct cls_mall_head *head, |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 55 | unsigned long cookie) |
| 56 | { |
| 57 | struct net_device *dev = tp->q->dev_queue->dev; |
| 58 | struct tc_to_netdev offload; |
| 59 | struct tc_cls_matchall_offload mall_offload = {0}; |
| 60 | |
| 61 | offload.type = TC_SETUP_MATCHALL; |
| 62 | offload.cls_mall = &mall_offload; |
| 63 | offload.cls_mall->command = TC_CLSMATCHALL_REPLACE; |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 64 | offload.cls_mall->exts = &head->exts; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 65 | offload.cls_mall->cookie = cookie; |
| 66 | |
| 67 | return dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, |
| 68 | &offload); |
| 69 | } |
| 70 | |
| 71 | static void mall_destroy_hw_filter(struct tcf_proto *tp, |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 72 | struct cls_mall_head *head, |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 73 | unsigned long cookie) |
| 74 | { |
| 75 | struct net_device *dev = tp->q->dev_queue->dev; |
| 76 | struct tc_to_netdev offload; |
| 77 | struct tc_cls_matchall_offload mall_offload = {0}; |
| 78 | |
| 79 | offload.type = TC_SETUP_MATCHALL; |
| 80 | offload.cls_mall = &mall_offload; |
| 81 | offload.cls_mall->command = TC_CLSMATCHALL_DESTROY; |
| 82 | offload.cls_mall->exts = NULL; |
| 83 | offload.cls_mall->cookie = cookie; |
| 84 | |
| 85 | dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, |
| 86 | &offload); |
| 87 | } |
| 88 | |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 89 | static bool mall_destroy(struct tcf_proto *tp, bool force) |
| 90 | { |
| 91 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 92 | struct net_device *dev = tp->q->dev_queue->dev; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 93 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 94 | if (!head) |
| 95 | return true; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 96 | |
Hangbin Liu | c4db09a | 2018-08-14 17:28:26 +0800 | [diff] [blame] | 97 | tcf_unbind_filter(tp, &head->res); |
| 98 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 99 | if (tc_should_offload(dev, tp, head->flags)) |
| 100 | mall_destroy_hw_filter(tp, head, (unsigned long) head); |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 101 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 102 | call_rcu(&head->rcu, mall_destroy_rcu); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | static unsigned long mall_get(struct tcf_proto *tp, u32 handle) |
| 107 | { |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 108 | return 0UL; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = { |
| 112 | [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC }, |
| 113 | [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 }, |
| 114 | }; |
| 115 | |
| 116 | static int mall_set_parms(struct net *net, struct tcf_proto *tp, |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 117 | struct cls_mall_head *head, |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 118 | unsigned long base, struct nlattr **tb, |
| 119 | struct nlattr *est, bool ovr) |
| 120 | { |
| 121 | struct tcf_exts e; |
| 122 | int err; |
| 123 | |
| 124 | tcf_exts_init(&e, TCA_MATCHALL_ACT, 0); |
| 125 | err = tcf_exts_validate(net, tp, tb, est, &e, ovr); |
| 126 | if (err < 0) |
| 127 | return err; |
| 128 | |
| 129 | if (tb[TCA_MATCHALL_CLASSID]) { |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 130 | head->res.classid = nla_get_u32(tb[TCA_MATCHALL_CLASSID]); |
| 131 | tcf_bind_filter(tp, &head->res, base); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 134 | tcf_exts_change(tp, &head->exts, &e); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int mall_change(struct net *net, struct sk_buff *in_skb, |
| 140 | struct tcf_proto *tp, unsigned long base, |
| 141 | u32 handle, struct nlattr **tca, |
| 142 | unsigned long *arg, bool ovr) |
| 143 | { |
| 144 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 145 | struct net_device *dev = tp->q->dev_queue->dev; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 146 | struct nlattr *tb[TCA_MATCHALL_MAX + 1]; |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 147 | struct cls_mall_head *new; |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 148 | u32 flags = 0; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 149 | int err; |
| 150 | |
| 151 | if (!tca[TCA_OPTIONS]) |
| 152 | return -EINVAL; |
| 153 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 154 | if (head) |
| 155 | return -EEXIST; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 156 | |
| 157 | err = nla_parse_nested(tb, TCA_MATCHALL_MAX, |
| 158 | tca[TCA_OPTIONS], mall_policy); |
| 159 | if (err < 0) |
| 160 | return err; |
| 161 | |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 162 | if (tb[TCA_MATCHALL_FLAGS]) { |
| 163 | flags = nla_get_u32(tb[TCA_MATCHALL_FLAGS]); |
| 164 | if (!tc_flags_valid(flags)) |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 168 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
| 169 | if (!new) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 170 | return -ENOBUFS; |
| 171 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 172 | tcf_exts_init(&new->exts, TCA_MATCHALL_ACT, 0); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 173 | |
| 174 | if (!handle) |
| 175 | handle = 1; |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 176 | new->handle = handle; |
| 177 | new->flags = flags; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 178 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 179 | err = mall_set_parms(net, tp, new, base, tb, tca[TCA_RATE], ovr); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 180 | if (err) |
| 181 | goto errout; |
| 182 | |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 183 | if (tc_should_offload(dev, tp, flags)) { |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 184 | err = mall_replace_hw_filter(tp, new, (unsigned long) new); |
Yotam Gigi | b87f793 | 2016-07-21 12:03:12 +0200 | [diff] [blame] | 185 | if (err) { |
| 186 | if (tc_skip_sw(flags)) |
| 187 | goto errout; |
| 188 | else |
| 189 | err = 0; |
| 190 | } |
| 191 | } |
| 192 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 193 | *arg = (unsigned long) head; |
| 194 | rcu_assign_pointer(tp->root, new); |
| 195 | if (head) |
| 196 | call_rcu(&head->rcu, mall_destroy_rcu); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 197 | return 0; |
| 198 | |
| 199 | errout: |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 200 | kfree(new); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 201 | return err; |
| 202 | } |
| 203 | |
| 204 | static int mall_delete(struct tcf_proto *tp, unsigned long arg) |
| 205 | { |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 206 | return -EOPNOTSUPP; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void mall_walk(struct tcf_proto *tp, struct tcf_walker *arg) |
| 210 | { |
| 211 | struct cls_mall_head *head = rtnl_dereference(tp->root); |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 212 | |
| 213 | if (arg->count < arg->skip) |
| 214 | goto skip; |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 215 | if (arg->fn(tp, (unsigned long) head, arg) < 0) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 216 | arg->stop = 1; |
| 217 | skip: |
| 218 | arg->count++; |
| 219 | } |
| 220 | |
| 221 | static int mall_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, |
| 222 | struct sk_buff *skb, struct tcmsg *t) |
| 223 | { |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 224 | struct cls_mall_head *head = (struct cls_mall_head *) fh; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 225 | struct nlattr *nest; |
| 226 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 227 | if (!head) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 228 | return skb->len; |
| 229 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 230 | t->tcm_handle = head->handle; |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 231 | |
| 232 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 233 | if (!nest) |
| 234 | goto nla_put_failure; |
| 235 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 236 | if (head->res.classid && |
| 237 | nla_put_u32(skb, TCA_MATCHALL_CLASSID, head->res.classid)) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 238 | goto nla_put_failure; |
| 239 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 240 | if (tcf_exts_dump(skb, &head->exts)) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 241 | goto nla_put_failure; |
| 242 | |
| 243 | nla_nest_end(skb, nest); |
| 244 | |
Yotam Gigi | 6c8556f | 2017-01-31 15:14:29 +0200 | [diff] [blame] | 245 | if (tcf_exts_dump_stats(skb, &head->exts) < 0) |
Jiri Pirko | bf3994d | 2016-07-21 12:03:11 +0200 | [diff] [blame] | 246 | goto nla_put_failure; |
| 247 | |
| 248 | return skb->len; |
| 249 | |
| 250 | nla_put_failure: |
| 251 | nla_nest_cancel(skb, nest); |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | static struct tcf_proto_ops cls_mall_ops __read_mostly = { |
| 256 | .kind = "matchall", |
| 257 | .classify = mall_classify, |
| 258 | .init = mall_init, |
| 259 | .destroy = mall_destroy, |
| 260 | .get = mall_get, |
| 261 | .change = mall_change, |
| 262 | .delete = mall_delete, |
| 263 | .walk = mall_walk, |
| 264 | .dump = mall_dump, |
| 265 | .owner = THIS_MODULE, |
| 266 | }; |
| 267 | |
| 268 | static int __init cls_mall_init(void) |
| 269 | { |
| 270 | return register_tcf_proto_ops(&cls_mall_ops); |
| 271 | } |
| 272 | |
| 273 | static void __exit cls_mall_exit(void) |
| 274 | { |
| 275 | unregister_tcf_proto_ops(&cls_mall_ops); |
| 276 | } |
| 277 | |
| 278 | module_init(cls_mall_init); |
| 279 | module_exit(cls_mall_exit); |
| 280 | |
| 281 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); |
| 282 | MODULE_DESCRIPTION("Match-all classifier"); |
| 283 | MODULE_LICENSE("GPL v2"); |