Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * net/sched/cls_tcindex.c Packet classifier for skb->tc_index |
| 4 | * |
| 5 | * Written 1998,1999 by Werner Almesberger, EPFL ICA |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/errno.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 14 | #include <linux/refcount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <net/act_api.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 16 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <net/pkt_cls.h> |
Jiri Pirko | 1abf272 | 2017-10-13 14:01:03 +0200 | [diff] [blame] | 18 | #include <net/sch_generic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | /* |
| 21 | * Passing parameters to the root seems to be done more awkwardly than really |
| 22 | * necessary. At least, u32 doesn't seem to use such dirty hacks. To be |
| 23 | * verified. FIXME. |
| 24 | */ |
| 25 | |
| 26 | #define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */ |
| 27 | #define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */ |
| 28 | |
| 29 | |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 30 | struct tcindex_data; |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | struct tcindex_filter_result { |
| 33 | struct tcf_exts exts; |
| 34 | struct tcf_result res; |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 35 | struct tcindex_data *p; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 36 | struct rcu_work rwork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct tcindex_filter { |
| 40 | u16 key; |
| 41 | struct tcindex_filter_result result; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 42 | struct tcindex_filter __rcu *next; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 43 | struct rcu_work rwork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | |
| 47 | struct tcindex_data { |
| 48 | struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */ |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 49 | struct tcindex_filter __rcu **h; /* imperfect hash; */ |
| 50 | struct tcf_proto *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | u16 mask; /* AND key with mask */ |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 52 | u32 shift; /* shift ANDed key to the right */ |
| 53 | u32 hash; /* hash table size; 0 if undefined */ |
| 54 | u32 alloc_hash; /* allocated size */ |
| 55 | u32 fall_through; /* 0: only classify if explicit match */ |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 56 | refcount_t refcnt; /* a temporary refcnt for perfect hash */ |
Cong Wang | 3d21053 | 2019-02-16 10:58:26 -0800 | [diff] [blame] | 57 | struct rcu_work rwork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 60 | static inline int tcindex_filter_is_set(struct tcindex_filter_result *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Jiri Pirko | 6fc6d06 | 2017-08-04 14:29:00 +0200 | [diff] [blame] | 62 | return tcf_exts_has_actions(&r->exts) || r->res.classid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 65 | static void tcindex_data_get(struct tcindex_data *p) |
| 66 | { |
| 67 | refcount_inc(&p->refcnt); |
| 68 | } |
| 69 | |
| 70 | static void tcindex_data_put(struct tcindex_data *p) |
| 71 | { |
| 72 | if (refcount_dec_and_test(&p->refcnt)) { |
| 73 | kfree(p->perfect); |
| 74 | kfree(p->h); |
| 75 | kfree(p); |
| 76 | } |
| 77 | } |
| 78 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 79 | static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p, |
| 80 | u16 key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 82 | if (p->perfect) { |
| 83 | struct tcindex_filter_result *f = p->perfect + key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 85 | return tcindex_filter_is_set(f) ? f : NULL; |
| 86 | } else if (p->h) { |
| 87 | struct tcindex_filter __rcu **fp; |
| 88 | struct tcindex_filter *f; |
| 89 | |
| 90 | fp = &p->h[key % p->hash]; |
| 91 | for (f = rcu_dereference_bh_rtnl(*fp); |
| 92 | f; |
| 93 | fp = &f->next, f = rcu_dereference_bh_rtnl(*fp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (f->key == key) |
| 95 | return &f->result; |
| 96 | } |
| 97 | |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 102 | static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | struct tcf_result *res) |
| 104 | { |
WANG Cong | 2f9a220 | 2014-09-15 14:06:48 -0700 | [diff] [blame] | 105 | struct tcindex_data *p = rcu_dereference_bh(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | struct tcindex_filter_result *f; |
| 107 | int key = (skb->tc_index & p->mask) >> p->shift; |
| 108 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 109 | pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n", |
| 110 | skb, tp, res, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | f = tcindex_lookup(p, key); |
| 113 | if (!f) { |
Jiri Pirko | 1abf272 | 2017-10-13 14:01:03 +0200 | [diff] [blame] | 114 | struct Qdisc *q = tcf_block_q(tp->chain->block); |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (!p->fall_through) |
| 117 | return -1; |
Jiri Pirko | 1abf272 | 2017-10-13 14:01:03 +0200 | [diff] [blame] | 118 | res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | res->class = 0; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 120 | pr_debug("alg 0x%x\n", res->classid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | *res = f->res; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 124 | pr_debug("map 0x%x\n", res->classid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | return tcf_exts_exec(skb, &f->exts, res); |
| 127 | } |
| 128 | |
| 129 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 130 | static void *tcindex_get(struct tcf_proto *tp, u32 handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 132 | struct tcindex_data *p = rtnl_dereference(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | struct tcindex_filter_result *r; |
| 134 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 135 | pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (p->perfect && handle >= p->alloc_hash) |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 137 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | r = tcindex_lookup(p, handle); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 139 | return r && tcindex_filter_is_set(r) ? r : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | static int tcindex_init(struct tcf_proto *tp) |
| 143 | { |
| 144 | struct tcindex_data *p; |
| 145 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 146 | pr_debug("tcindex_init(tp %p)\n", tp); |
| 147 | p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | if (!p) |
| 149 | return -ENOMEM; |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | p->mask = 0xffff; |
| 152 | p->hash = DEFAULT_HASH_SIZE; |
| 153 | p->fall_through = 1; |
Cong Wang | a8eab6d | 2020-04-02 20:58:51 -0700 | [diff] [blame] | 154 | refcount_set(&p->refcnt, 1); /* Paired with tcindex_destroy_work() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 156 | rcu_assign_pointer(tp->root, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
Cong Wang | f2b7510 | 2017-11-06 13:47:29 -0800 | [diff] [blame] | 160 | static void __tcindex_destroy_rexts(struct tcindex_filter_result *r) |
| 161 | { |
| 162 | tcf_exts_destroy(&r->exts); |
| 163 | tcf_exts_put_net(&r->exts); |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 164 | tcindex_data_put(r->p); |
Cong Wang | f2b7510 | 2017-11-06 13:47:29 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Cong Wang | 27ce4f0 | 2017-10-26 18:24:39 -0700 | [diff] [blame] | 167 | static void tcindex_destroy_rexts_work(struct work_struct *work) |
| 168 | { |
| 169 | struct tcindex_filter_result *r; |
| 170 | |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 171 | r = container_of(to_rcu_work(work), |
| 172 | struct tcindex_filter_result, |
| 173 | rwork); |
Cong Wang | 27ce4f0 | 2017-10-26 18:24:39 -0700 | [diff] [blame] | 174 | rtnl_lock(); |
Cong Wang | f2b7510 | 2017-11-06 13:47:29 -0800 | [diff] [blame] | 175 | __tcindex_destroy_rexts(r); |
Cong Wang | 27ce4f0 | 2017-10-26 18:24:39 -0700 | [diff] [blame] | 176 | rtnl_unlock(); |
| 177 | } |
| 178 | |
Cong Wang | f2b7510 | 2017-11-06 13:47:29 -0800 | [diff] [blame] | 179 | static void __tcindex_destroy_fexts(struct tcindex_filter *f) |
| 180 | { |
| 181 | tcf_exts_destroy(&f->result.exts); |
| 182 | tcf_exts_put_net(&f->result.exts); |
| 183 | kfree(f); |
| 184 | } |
| 185 | |
Cong Wang | 27ce4f0 | 2017-10-26 18:24:39 -0700 | [diff] [blame] | 186 | static void tcindex_destroy_fexts_work(struct work_struct *work) |
| 187 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 188 | struct tcindex_filter *f = container_of(to_rcu_work(work), |
| 189 | struct tcindex_filter, |
| 190 | rwork); |
Cong Wang | 27ce4f0 | 2017-10-26 18:24:39 -0700 | [diff] [blame] | 191 | |
| 192 | rtnl_lock(); |
Cong Wang | f2b7510 | 2017-11-06 13:47:29 -0800 | [diff] [blame] | 193 | __tcindex_destroy_fexts(f); |
Cong Wang | 27ce4f0 | 2017-10-26 18:24:39 -0700 | [diff] [blame] | 194 | rtnl_unlock(); |
Alexei Starovoitov | ed7aa87 | 2015-08-25 20:06:33 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 197 | static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 198 | bool rtnl_held, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 200 | struct tcindex_data *p = rtnl_dereference(tp->root); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 201 | struct tcindex_filter_result *r = arg; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 202 | struct tcindex_filter __rcu **walk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | struct tcindex_filter *f = NULL; |
| 204 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 205 | pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (p->perfect) { |
| 207 | if (!r->res.class) |
| 208 | return -ENOENT; |
| 209 | } else { |
| 210 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 212 | for (i = 0; i < p->hash; i++) { |
| 213 | walk = p->h + i; |
| 214 | for (f = rtnl_dereference(*walk); f; |
| 215 | walk = &f->next, f = rtnl_dereference(*walk)) { |
| 216 | if (&f->result == r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | goto found; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return -ENOENT; |
| 221 | |
| 222 | found: |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 223 | rcu_assign_pointer(*walk, rtnl_dereference(f->next)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | tcf_unbind_filter(tp, &r->res); |
Alexei Starovoitov | ed7aa87 | 2015-08-25 20:06:33 -0700 | [diff] [blame] | 226 | /* all classifiers are required to call tcf_exts_destroy() after rcu |
| 227 | * grace period, since converted-to-rcu actions are relying on that |
| 228 | * in cleanup() callback |
| 229 | */ |
Cong Wang | f2b7510 | 2017-11-06 13:47:29 -0800 | [diff] [blame] | 230 | if (f) { |
| 231 | if (tcf_exts_get_net(&f->result.exts)) |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 232 | tcf_queue_work(&f->rwork, tcindex_destroy_fexts_work); |
Cong Wang | f2b7510 | 2017-11-06 13:47:29 -0800 | [diff] [blame] | 233 | else |
| 234 | __tcindex_destroy_fexts(f); |
| 235 | } else { |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 236 | tcindex_data_get(p); |
| 237 | |
Cong Wang | f2b7510 | 2017-11-06 13:47:29 -0800 | [diff] [blame] | 238 | if (tcf_exts_get_net(&r->exts)) |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 239 | tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work); |
Cong Wang | f2b7510 | 2017-11-06 13:47:29 -0800 | [diff] [blame] | 240 | else |
| 241 | __tcindex_destroy_rexts(r); |
| 242 | } |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 243 | |
| 244 | *last = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Cong Wang | 3d21053 | 2019-02-16 10:58:26 -0800 | [diff] [blame] | 248 | static void tcindex_destroy_work(struct work_struct *work) |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 249 | { |
Cong Wang | 3d21053 | 2019-02-16 10:58:26 -0800 | [diff] [blame] | 250 | struct tcindex_data *p = container_of(to_rcu_work(work), |
| 251 | struct tcindex_data, |
| 252 | rwork); |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 253 | |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 254 | tcindex_data_put(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static inline int |
| 258 | valid_perfect_hash(struct tcindex_data *p) |
| 259 | { |
| 260 | return p->hash > (p->mask >> p->shift); |
| 261 | } |
| 262 | |
Patrick McHardy | 6fa8c01 | 2008-01-23 20:36:12 -0800 | [diff] [blame] | 263 | static const struct nla_policy tcindex_policy[TCA_TCINDEX_MAX + 1] = { |
| 264 | [TCA_TCINDEX_HASH] = { .type = NLA_U32 }, |
| 265 | [TCA_TCINDEX_MASK] = { .type = NLA_U16 }, |
| 266 | [TCA_TCINDEX_SHIFT] = { .type = NLA_U32 }, |
| 267 | [TCA_TCINDEX_FALL_THROUGH] = { .type = NLA_U32 }, |
| 268 | [TCA_TCINDEX_CLASSID] = { .type = NLA_U32 }, |
| 269 | }; |
| 270 | |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 271 | static int tcindex_filter_result_init(struct tcindex_filter_result *r, |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 272 | struct tcindex_data *p, |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 273 | struct net *net) |
Cong Wang | bf63ac7 | 2014-05-19 12:15:49 -0700 | [diff] [blame] | 274 | { |
| 275 | memset(r, 0, sizeof(*r)); |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 276 | r->p = p; |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 277 | return tcf_exts_init(&r->exts, net, TCA_TCINDEX_ACT, |
| 278 | TCA_TCINDEX_POLICE); |
Cong Wang | bf63ac7 | 2014-05-19 12:15:49 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Pavel Skripkin | f5051bc | 2021-07-17 14:29:33 +0300 | [diff] [blame] | 281 | static void tcindex_free_perfect_hash(struct tcindex_data *cp); |
| 282 | |
Cong Wang | 3d21053 | 2019-02-16 10:58:26 -0800 | [diff] [blame] | 283 | static void tcindex_partial_destroy_work(struct work_struct *work) |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 284 | { |
Cong Wang | 3d21053 | 2019-02-16 10:58:26 -0800 | [diff] [blame] | 285 | struct tcindex_data *p = container_of(to_rcu_work(work), |
| 286 | struct tcindex_data, |
| 287 | rwork); |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 288 | |
Cong Wang | b1be2e8 | 2020-03-11 22:42:27 -0700 | [diff] [blame] | 289 | rtnl_lock(); |
Pavel Skripkin | f5051bc | 2021-07-17 14:29:33 +0300 | [diff] [blame] | 290 | if (p->perfect) |
| 291 | tcindex_free_perfect_hash(p); |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 292 | kfree(p); |
Cong Wang | b1be2e8 | 2020-03-11 22:42:27 -0700 | [diff] [blame] | 293 | rtnl_unlock(); |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 294 | } |
| 295 | |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 296 | static void tcindex_free_perfect_hash(struct tcindex_data *cp) |
| 297 | { |
| 298 | int i; |
| 299 | |
| 300 | for (i = 0; i < cp->hash; i++) |
| 301 | tcf_exts_destroy(&cp->perfect[i].exts); |
| 302 | kfree(cp->perfect); |
| 303 | } |
| 304 | |
Cong Wang | 033b228 | 2019-02-11 13:06:15 -0800 | [diff] [blame] | 305 | static int tcindex_alloc_perfect_hash(struct net *net, struct tcindex_data *cp) |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 306 | { |
| 307 | int i, err = 0; |
| 308 | |
| 309 | cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result), |
Pavel Skripkin | 3f2db25 | 2021-06-25 23:23:48 +0300 | [diff] [blame] | 310 | GFP_KERNEL | __GFP_NOWARN); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 311 | if (!cp->perfect) |
| 312 | return -ENOMEM; |
| 313 | |
| 314 | for (i = 0; i < cp->hash; i++) { |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 315 | err = tcf_exts_init(&cp->perfect[i].exts, net, |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 316 | TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE); |
| 317 | if (err < 0) |
| 318 | goto errout; |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 319 | cp->perfect[i].p = cp; |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | return 0; |
| 323 | |
| 324 | errout: |
| 325 | tcindex_free_perfect_hash(cp); |
| 326 | return err; |
| 327 | } |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | static int |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 330 | tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base, |
| 331 | u32 handle, struct tcindex_data *p, |
| 332 | struct tcindex_filter_result *r, struct nlattr **tb, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 333 | struct nlattr *est, u32 flags, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | struct tcindex_filter_result new_filter_result, *old_r = r; |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 336 | struct tcindex_data *cp = NULL, *oldp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | struct tcindex_filter *f = NULL; /* make gcc behave */ |
Cong Wang | 1db817e7 | 2019-02-11 13:06:16 -0800 | [diff] [blame] | 338 | struct tcf_result cr = {}; |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 339 | int err, balloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | struct tcf_exts e; |
| 341 | |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 342 | err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | if (err < 0) |
| 344 | return err; |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 345 | err = tcf_exts_validate(net, tp, tb, est, &e, flags, extack); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 346 | if (err < 0) |
| 347 | goto errout; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 348 | |
WANG Cong | 02c5e84 | 2014-09-25 12:06:04 -0700 | [diff] [blame] | 349 | err = -ENOMEM; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 350 | /* tcindex_data attributes must look atomic to classifier/lookup so |
| 351 | * allocate new tcindex data and RCU assign it onto root. Keeping |
| 352 | * perfect hash and hash pointers from old data. |
| 353 | */ |
WANG Cong | a57a65b | 2014-09-15 14:06:46 -0700 | [diff] [blame] | 354 | cp = kzalloc(sizeof(*cp), GFP_KERNEL); |
WANG Cong | 02c5e84 | 2014-09-25 12:06:04 -0700 | [diff] [blame] | 355 | if (!cp) |
WANG Cong | 44b75e4 | 2014-09-15 16:43:42 -0700 | [diff] [blame] | 356 | goto errout; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 357 | |
| 358 | cp->mask = p->mask; |
| 359 | cp->shift = p->shift; |
| 360 | cp->hash = p->hash; |
| 361 | cp->alloc_hash = p->alloc_hash; |
| 362 | cp->fall_through = p->fall_through; |
| 363 | cp->tp = tp; |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 364 | refcount_set(&cp->refcnt, 1); /* Paired with tcindex_destroy_work() */ |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 365 | |
Cong Wang | 599be01 | 2020-02-02 21:14:35 -0800 | [diff] [blame] | 366 | if (tb[TCA_TCINDEX_HASH]) |
| 367 | cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]); |
| 368 | |
| 369 | if (tb[TCA_TCINDEX_MASK]) |
| 370 | cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]); |
| 371 | |
Eric Dumazet | bcd0cf1 | 2021-01-14 10:52:29 -0800 | [diff] [blame] | 372 | if (tb[TCA_TCINDEX_SHIFT]) { |
Cong Wang | 599be01 | 2020-02-02 21:14:35 -0800 | [diff] [blame] | 373 | cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]); |
Eric Dumazet | bcd0cf1 | 2021-01-14 10:52:29 -0800 | [diff] [blame] | 374 | if (cp->shift > 16) { |
| 375 | err = -EINVAL; |
| 376 | goto errout; |
| 377 | } |
| 378 | } |
Cong Wang | 599be01 | 2020-02-02 21:14:35 -0800 | [diff] [blame] | 379 | if (!cp->hash) { |
| 380 | /* Hash not specified, use perfect hash if the upper limit |
| 381 | * of the hashing index is below the threshold. |
| 382 | */ |
| 383 | if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD) |
| 384 | cp->hash = (cp->mask >> cp->shift) + 1; |
| 385 | else |
| 386 | cp->hash = DEFAULT_HASH_SIZE; |
| 387 | } |
| 388 | |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 389 | if (p->perfect) { |
WANG Cong | 6e05656 | 2014-09-30 16:07:23 -0700 | [diff] [blame] | 390 | int i; |
| 391 | |
Cong Wang | 033b228 | 2019-02-11 13:06:15 -0800 | [diff] [blame] | 392 | if (tcindex_alloc_perfect_hash(net, cp) < 0) |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 393 | goto errout; |
Cong Wang | 0d1c353 | 2020-03-11 22:42:28 -0700 | [diff] [blame] | 394 | cp->alloc_hash = cp->hash; |
Cong Wang | 599be01 | 2020-02-02 21:14:35 -0800 | [diff] [blame] | 395 | for (i = 0; i < min(cp->hash, p->hash); i++) |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 396 | cp->perfect[i].res = p->perfect[i].res; |
WANG Cong | 44b75e4 | 2014-09-15 16:43:42 -0700 | [diff] [blame] | 397 | balloc = 1; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 398 | } |
| 399 | cp->h = p->h; |
| 400 | |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 401 | err = tcindex_filter_result_init(&new_filter_result, cp, net); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 402 | if (err < 0) |
Cong Wang | 52b5ae5 | 2020-02-04 11:10:12 -0800 | [diff] [blame] | 403 | goto errout_alloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | if (old_r) |
Cong Wang | 1db817e7 | 2019-02-11 13:06:16 -0800 | [diff] [blame] | 405 | cr = r->res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | err = -EBUSY; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | /* Hash already allocated, make sure that we still meet the |
| 410 | * requirements for the allocated hash. |
| 411 | */ |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 412 | if (cp->perfect) { |
| 413 | if (!valid_perfect_hash(cp) || |
| 414 | cp->hash > cp->alloc_hash) |
WANG Cong | 44b75e4 | 2014-09-15 16:43:42 -0700 | [diff] [blame] | 415 | goto errout_alloc; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 416 | } else if (cp->h && cp->hash != cp->alloc_hash) { |
WANG Cong | 44b75e4 | 2014-09-15 16:43:42 -0700 | [diff] [blame] | 417 | goto errout_alloc; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | err = -EINVAL; |
Patrick McHardy | 6fa8c01 | 2008-01-23 20:36:12 -0800 | [diff] [blame] | 421 | if (tb[TCA_TCINDEX_FALL_THROUGH]) |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 422 | cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
WANG Cong | 68f6a7c | 2014-09-25 12:06:05 -0700 | [diff] [blame] | 424 | if (!cp->perfect && !cp->h) |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 425 | cp->alloc_hash = cp->hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | /* Note: this could be as restrictive as if (handle & ~(mask >> shift)) |
| 428 | * but then, we'd fail handles that may become valid after some future |
| 429 | * mask change. While this is extremely unlikely to ever matter, |
| 430 | * the check below is safer (and also more backwards-compatible). |
| 431 | */ |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 432 | if (cp->perfect || valid_perfect_hash(cp)) |
| 433 | if (handle >= cp->alloc_hash) |
WANG Cong | 44b75e4 | 2014-09-15 16:43:42 -0700 | [diff] [blame] | 434 | goto errout_alloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | |
| 437 | err = -ENOMEM; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 438 | if (!cp->perfect && !cp->h) { |
| 439 | if (valid_perfect_hash(cp)) { |
Cong Wang | 033b228 | 2019-02-11 13:06:15 -0800 | [diff] [blame] | 440 | if (tcindex_alloc_perfect_hash(net, cp) < 0) |
WANG Cong | 44b75e4 | 2014-09-15 16:43:42 -0700 | [diff] [blame] | 441 | goto errout_alloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | balloc = 1; |
| 443 | } else { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 444 | struct tcindex_filter __rcu **hash; |
| 445 | |
| 446 | hash = kcalloc(cp->hash, |
| 447 | sizeof(struct tcindex_filter *), |
| 448 | GFP_KERNEL); |
| 449 | |
| 450 | if (!hash) |
WANG Cong | 44b75e4 | 2014-09-15 16:43:42 -0700 | [diff] [blame] | 451 | goto errout_alloc; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 452 | |
| 453 | cp->h = hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | balloc = 2; |
| 455 | } |
| 456 | } |
| 457 | |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 458 | if (cp->perfect) |
| 459 | r = cp->perfect + handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | else |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 461 | r = tcindex_lookup(cp, handle) ? : &new_filter_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
| 463 | if (r == &new_filter_result) { |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 464 | f = kzalloc(sizeof(*f), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (!f) |
| 466 | goto errout_alloc; |
WANG Cong | 6e05656 | 2014-09-30 16:07:23 -0700 | [diff] [blame] | 467 | f->key = handle; |
WANG Cong | 6e05656 | 2014-09-30 16:07:23 -0700 | [diff] [blame] | 468 | f->next = NULL; |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 469 | err = tcindex_filter_result_init(&f->result, cp, net); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 470 | if (err < 0) { |
| 471 | kfree(f); |
| 472 | goto errout_alloc; |
| 473 | } |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 476 | if (tb[TCA_TCINDEX_CLASSID]) { |
Cong Wang | 1db817e7 | 2019-02-11 13:06:16 -0800 | [diff] [blame] | 477 | cr.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]); |
| 478 | tcf_bind_filter(tp, &cr, base); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 479 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 481 | if (old_r && old_r != r) { |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 482 | err = tcindex_filter_result_init(old_r, cp, net); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 483 | if (err < 0) { |
| 484 | kfree(f); |
| 485 | goto errout_alloc; |
| 486 | } |
| 487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 489 | oldp = p; |
Cong Wang | 1db817e7 | 2019-02-11 13:06:16 -0800 | [diff] [blame] | 490 | r->res = cr; |
Hangbin Liu | 2df8bee | 2018-08-13 18:44:03 +0800 | [diff] [blame] | 491 | tcf_exts_change(&r->exts, &e); |
| 492 | |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 493 | rcu_assign_pointer(tp->root, cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
| 495 | if (r == &new_filter_result) { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 496 | struct tcindex_filter *nfp; |
| 497 | struct tcindex_filter __rcu **fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | |
Hangbin Liu | 008369d | 2018-08-13 18:44:04 +0800 | [diff] [blame] | 499 | f->result.res = r->res; |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 500 | tcf_exts_change(&f->result.exts, &r->exts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
WANG Cong | 69301ea | 2014-09-15 16:43:43 -0700 | [diff] [blame] | 502 | fp = cp->h + (handle % cp->hash); |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 503 | for (nfp = rtnl_dereference(*fp); |
| 504 | nfp; |
| 505 | fp = &nfp->next, nfp = rtnl_dereference(*fp)) |
| 506 | ; /* nothing */ |
| 507 | |
| 508 | rcu_assign_pointer(*fp, f); |
Cong Wang | 1db817e7 | 2019-02-11 13:06:16 -0800 | [diff] [blame] | 509 | } else { |
| 510 | tcf_exts_destroy(&new_filter_result.exts); |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (oldp) |
Cong Wang | 3d21053 | 2019-02-16 10:58:26 -0800 | [diff] [blame] | 514 | tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | return 0; |
| 516 | |
| 517 | errout_alloc: |
| 518 | if (balloc == 1) |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 519 | tcindex_free_perfect_hash(cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | else if (balloc == 2) |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 521 | kfree(cp->h); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 522 | tcf_exts_destroy(&new_filter_result.exts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | errout: |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 524 | kfree(cp); |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 525 | tcf_exts_destroy(&e); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | return err; |
| 527 | } |
| 528 | |
| 529 | static int |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 530 | tcindex_change(struct net *net, struct sk_buff *in_skb, |
Eric W. Biederman | af4c664 | 2012-05-25 13:42:45 -0600 | [diff] [blame] | 531 | struct tcf_proto *tp, unsigned long base, u32 handle, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 532 | struct nlattr **tca, void **arg, u32 flags, |
| 533 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 535 | struct nlattr *opt = tca[TCA_OPTIONS]; |
| 536 | struct nlattr *tb[TCA_TCINDEX_MAX + 1]; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 537 | struct tcindex_data *p = rtnl_dereference(tp->root); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 538 | struct tcindex_filter_result *r = *arg; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 539 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 541 | pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p," |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 542 | "p %p,r %p,*arg %p\n", |
Gaurav Singh | c5efcf1 | 2020-06-21 22:24:30 -0400 | [diff] [blame] | 543 | tp, handle, tca, arg, opt, p, r, *arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
| 545 | if (!opt) |
| 546 | return 0; |
| 547 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 548 | err = nla_parse_nested_deprecated(tb, TCA_TCINDEX_MAX, opt, |
| 549 | tcindex_policy, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 550 | if (err < 0) |
| 551 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 553 | return tcindex_set_parms(net, tp, base, handle, p, r, tb, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 554 | tca[TCA_RATE], flags, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 557 | static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker, |
| 558 | bool rtnl_held) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 560 | struct tcindex_data *p = rtnl_dereference(tp->root); |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 561 | struct tcindex_filter *f, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | int i; |
| 563 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 564 | pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | if (p->perfect) { |
| 566 | for (i = 0; i < p->hash; i++) { |
| 567 | if (!p->perfect[i].res.class) |
| 568 | continue; |
| 569 | if (walker->count >= walker->skip) { |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 570 | if (walker->fn(tp, p->perfect + i, walker) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | walker->stop = 1; |
| 572 | return; |
| 573 | } |
| 574 | } |
| 575 | walker->count++; |
| 576 | } |
| 577 | } |
| 578 | if (!p->h) |
| 579 | return; |
| 580 | for (i = 0; i < p->hash; i++) { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 581 | for (f = rtnl_dereference(p->h[i]); f; f = next) { |
| 582 | next = rtnl_dereference(f->next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | if (walker->count >= walker->skip) { |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 584 | if (walker->fn(tp, &f->result, walker) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | walker->stop = 1; |
| 586 | return; |
| 587 | } |
| 588 | } |
| 589 | walker->count++; |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 594 | static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held, |
Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 595 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 597 | struct tcindex_data *p = rtnl_dereference(tp->root); |
Cong Wang | 51dcb69 | 2019-02-16 10:58:27 -0800 | [diff] [blame] | 598 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 600 | pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p); |
Cong Wang | 51dcb69 | 2019-02-16 10:58:27 -0800 | [diff] [blame] | 601 | |
| 602 | if (p->perfect) { |
| 603 | for (i = 0; i < p->hash; i++) { |
| 604 | struct tcindex_filter_result *r = p->perfect + i; |
| 605 | |
Cong Wang | 304e024 | 2020-03-28 12:12:59 -0700 | [diff] [blame] | 606 | /* tcf_queue_work() does not guarantee the ordering we |
| 607 | * want, so we have to take this refcnt temporarily to |
| 608 | * ensure 'p' is freed after all tcindex_filter_result |
| 609 | * here. Imperfect hash does not need this, because it |
| 610 | * uses linked lists rather than an array. |
| 611 | */ |
| 612 | tcindex_data_get(p); |
| 613 | |
Cong Wang | 51dcb69 | 2019-02-16 10:58:27 -0800 | [diff] [blame] | 614 | tcf_unbind_filter(tp, &r->res); |
| 615 | if (tcf_exts_get_net(&r->exts)) |
| 616 | tcf_queue_work(&r->rwork, |
| 617 | tcindex_destroy_rexts_work); |
| 618 | else |
| 619 | __tcindex_destroy_rexts(r); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | for (i = 0; p->h && i < p->hash; i++) { |
| 624 | struct tcindex_filter *f, *next; |
| 625 | bool last; |
| 626 | |
| 627 | for (f = rtnl_dereference(p->h[i]); f; f = next) { |
| 628 | next = rtnl_dereference(f->next); |
| 629 | tcindex_delete(tp, &f->result, &last, rtnl_held, NULL); |
| 630 | } |
| 631 | } |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 632 | |
Cong Wang | 3d21053 | 2019-02-16 10:58:26 -0800 | [diff] [blame] | 633 | tcf_queue_work(&p->rwork, tcindex_destroy_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 637 | static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 638 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 640 | struct tcindex_data *p = rtnl_dereference(tp->root); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 641 | struct tcindex_filter_result *r = fh; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 642 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 644 | pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n", |
Jiri Pirko | 6ea3b44 | 2014-12-09 22:23:29 +0100 | [diff] [blame] | 645 | tp, fh, skb, t, p, r); |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 646 | pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 647 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 648 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 649 | if (nest == NULL) |
| 650 | goto nla_put_failure; |
| 651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | if (!fh) { |
| 653 | t->tcm_handle = ~0; /* whatever ... */ |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 654 | if (nla_put_u32(skb, TCA_TCINDEX_HASH, p->hash) || |
| 655 | nla_put_u16(skb, TCA_TCINDEX_MASK, p->mask) || |
| 656 | nla_put_u32(skb, TCA_TCINDEX_SHIFT, p->shift) || |
| 657 | nla_put_u32(skb, TCA_TCINDEX_FALL_THROUGH, p->fall_through)) |
| 658 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 659 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } else { |
| 661 | if (p->perfect) { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 662 | t->tcm_handle = r - p->perfect; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | } else { |
| 664 | struct tcindex_filter *f; |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 665 | struct tcindex_filter __rcu **fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | int i; |
| 667 | |
| 668 | t->tcm_handle = 0; |
| 669 | for (i = 0; !t->tcm_handle && i < p->hash; i++) { |
John Fastabend | 331b729 | 2014-09-12 20:08:20 -0700 | [diff] [blame] | 670 | fp = &p->h[i]; |
| 671 | for (f = rtnl_dereference(*fp); |
| 672 | !t->tcm_handle && f; |
| 673 | fp = &f->next, f = rtnl_dereference(*fp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | if (&f->result == r) |
| 675 | t->tcm_handle = f->key; |
| 676 | } |
| 677 | } |
| 678 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 679 | pr_debug("handle = %d\n", t->tcm_handle); |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 680 | if (r->res.class && |
| 681 | nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid)) |
| 682 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 684 | if (tcf_exts_dump(skb, &r->exts) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 685 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 686 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 688 | if (tcf_exts_dump_stats(skb, &r->exts) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 689 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | return skb->len; |
| 693 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 694 | nla_put_failure: |
Jiri Pirko | 6ea3b44 | 2014-12-09 22:23:29 +0100 | [diff] [blame] | 695 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | return -1; |
| 697 | } |
| 698 | |
Cong Wang | 2e24cd7 | 2020-01-23 16:26:18 -0800 | [diff] [blame] | 699 | static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl, |
| 700 | void *q, unsigned long base) |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 701 | { |
| 702 | struct tcindex_filter_result *r = fh; |
| 703 | |
Cong Wang | 2e24cd7 | 2020-01-23 16:26:18 -0800 | [diff] [blame] | 704 | if (r && r->res.classid == classid) { |
| 705 | if (cl) |
| 706 | __tcf_bind_filter(q, &r->res, base); |
| 707 | else |
| 708 | __tcf_unbind_filter(q, &r->res); |
| 709 | } |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Patrick McHardy | 2eb9d75 | 2008-01-22 22:10:42 -0800 | [diff] [blame] | 712 | static struct tcf_proto_ops cls_tcindex_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | .kind = "tcindex", |
| 714 | .classify = tcindex_classify, |
| 715 | .init = tcindex_init, |
| 716 | .destroy = tcindex_destroy, |
| 717 | .get = tcindex_get, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | .change = tcindex_change, |
| 719 | .delete = tcindex_delete, |
| 720 | .walk = tcindex_walk, |
| 721 | .dump = tcindex_dump, |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 722 | .bind_class = tcindex_bind_class, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | .owner = THIS_MODULE, |
| 724 | }; |
| 725 | |
| 726 | static int __init init_tcindex(void) |
| 727 | { |
| 728 | return register_tcf_proto_ops(&cls_tcindex_ops); |
| 729 | } |
| 730 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 731 | static void __exit exit_tcindex(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | { |
| 733 | unregister_tcf_proto_ops(&cls_tcindex_ops); |
| 734 | } |
| 735 | |
| 736 | module_init(init_tcindex) |
| 737 | module_exit(exit_tcindex) |
| 738 | MODULE_LICENSE("GPL"); |