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