blob: 742c7d49a9581c185b649352034d548bd7fdf4c5 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Torvalds1da177e2005-04-16 15:20:36 -07008#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 Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Cong Wang304e0242020-03-28 12:12:59 -070014#include <linux/refcount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070016#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <net/pkt_cls.h>
Jiri Pirko1abf2722017-10-13 14:01:03 +020018#include <net/sch_generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/*
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 Wang304e0242020-03-28 12:12:59 -070030struct tcindex_data;
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct tcindex_filter_result {
33 struct tcf_exts exts;
34 struct tcf_result res;
Cong Wang304e0242020-03-28 12:12:59 -070035 struct tcindex_data *p;
Cong Wangaaa908f2018-05-23 15:26:53 -070036 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
39struct tcindex_filter {
40 u16 key;
41 struct tcindex_filter_result result;
John Fastabend331b7292014-09-12 20:08:20 -070042 struct tcindex_filter __rcu *next;
Cong Wangaaa908f2018-05-23 15:26:53 -070043 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
46
47struct tcindex_data {
48 struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
John Fastabend331b7292014-09-12 20:08:20 -070049 struct tcindex_filter __rcu **h; /* imperfect hash; */
50 struct tcf_proto *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 u16 mask; /* AND key with mask */
John Fastabend331b7292014-09-12 20:08:20 -070052 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 Wang304e0242020-03-28 12:12:59 -070056 refcount_t refcnt; /* a temporary refcnt for perfect hash */
Cong Wang3d210532019-02-16 10:58:26 -080057 struct rcu_work rwork;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -040060static inline int tcindex_filter_is_set(struct tcindex_filter_result *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Jiri Pirko6fc6d062017-08-04 14:29:00 +020062 return tcf_exts_has_actions(&r->exts) || r->res.classid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Cong Wang304e0242020-03-28 12:12:59 -070065static void tcindex_data_get(struct tcindex_data *p)
66{
67 refcount_inc(&p->refcnt);
68}
69
70static 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 Salim5a7a5552016-09-18 08:45:33 -040079static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p,
80 u16 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
John Fastabend331b7292014-09-12 20:08:20 -070082 if (p->perfect) {
83 struct tcindex_filter_result *f = p->perfect + key;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
John Fastabend331b7292014-09-12 20:08:20 -070085 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 Torvalds1da177e2005-04-16 15:20:36 -070094 if (f->key == key)
95 return &f->result;
96 }
97
98 return NULL;
99}
100
101
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000102static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct tcf_result *res)
104{
WANG Cong2f9a2202014-09-15 14:06:48 -0700105 struct tcindex_data *p = rcu_dereference_bh(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct tcindex_filter_result *f;
107 int key = (skb->tc_index & p->mask) >> p->shift;
108
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800109 pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
110 skb, tp, res, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 f = tcindex_lookup(p, key);
113 if (!f) {
Jiri Pirko1abf2722017-10-13 14:01:03 +0200114 struct Qdisc *q = tcf_block_q(tp->chain->block);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!p->fall_through)
117 return -1;
Jiri Pirko1abf2722017-10-13 14:01:03 +0200118 res->classid = TC_H_MAKE(TC_H_MAJ(q->handle), key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 res->class = 0;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800120 pr_debug("alg 0x%x\n", res->classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 0;
122 }
123 *res = f->res;
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800124 pr_debug("map 0x%x\n", res->classid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 return tcf_exts_exec(skb, &f->exts, res);
127}
128
129
WANG Cong8113c092017-08-04 21:31:43 -0700130static void *tcindex_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
John Fastabend331b7292014-09-12 20:08:20 -0700132 struct tcindex_data *p = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct tcindex_filter_result *r;
134
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800135 pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (p->perfect && handle >= p->alloc_hash)
WANG Cong8113c092017-08-04 21:31:43 -0700137 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 r = tcindex_lookup(p, handle);
WANG Cong8113c092017-08-04 21:31:43 -0700139 return r && tcindex_filter_is_set(r) ? r : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int tcindex_init(struct tcf_proto *tp)
143{
144 struct tcindex_data *p;
145
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800146 pr_debug("tcindex_init(tp %p)\n", tp);
147 p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (!p)
149 return -ENOMEM;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 p->mask = 0xffff;
152 p->hash = DEFAULT_HASH_SIZE;
153 p->fall_through = 1;
Cong Wanga8eab6d2020-04-02 20:58:51 -0700154 refcount_set(&p->refcnt, 1); /* Paired with tcindex_destroy_work() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
John Fastabend331b7292014-09-12 20:08:20 -0700156 rcu_assign_pointer(tp->root, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return 0;
158}
159
Cong Wangf2b75102017-11-06 13:47:29 -0800160static void __tcindex_destroy_rexts(struct tcindex_filter_result *r)
161{
162 tcf_exts_destroy(&r->exts);
163 tcf_exts_put_net(&r->exts);
Cong Wang304e0242020-03-28 12:12:59 -0700164 tcindex_data_put(r->p);
Cong Wangf2b75102017-11-06 13:47:29 -0800165}
166
Cong Wang27ce4f02017-10-26 18:24:39 -0700167static void tcindex_destroy_rexts_work(struct work_struct *work)
168{
169 struct tcindex_filter_result *r;
170
Cong Wangaaa908f2018-05-23 15:26:53 -0700171 r = container_of(to_rcu_work(work),
172 struct tcindex_filter_result,
173 rwork);
Cong Wang27ce4f02017-10-26 18:24:39 -0700174 rtnl_lock();
Cong Wangf2b75102017-11-06 13:47:29 -0800175 __tcindex_destroy_rexts(r);
Cong Wang27ce4f02017-10-26 18:24:39 -0700176 rtnl_unlock();
177}
178
Cong Wangf2b75102017-11-06 13:47:29 -0800179static 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 Wang27ce4f02017-10-26 18:24:39 -0700186static void tcindex_destroy_fexts_work(struct work_struct *work)
187{
Cong Wangaaa908f2018-05-23 15:26:53 -0700188 struct tcindex_filter *f = container_of(to_rcu_work(work),
189 struct tcindex_filter,
190 rwork);
Cong Wang27ce4f02017-10-26 18:24:39 -0700191
192 rtnl_lock();
Cong Wangf2b75102017-11-06 13:47:29 -0800193 __tcindex_destroy_fexts(f);
Cong Wang27ce4f02017-10-26 18:24:39 -0700194 rtnl_unlock();
Alexei Starovoitoved7aa872015-08-25 20:06:33 -0700195}
196
Alexander Aring571acf22018-01-18 11:20:53 -0500197static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200198 bool rtnl_held, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
John Fastabend331b7292014-09-12 20:08:20 -0700200 struct tcindex_data *p = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700201 struct tcindex_filter_result *r = arg;
John Fastabend331b7292014-09-12 20:08:20 -0700202 struct tcindex_filter __rcu **walk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct tcindex_filter *f = NULL;
204
WANG Cong8113c092017-08-04 21:31:43 -0700205 pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (p->perfect) {
207 if (!r->res.class)
208 return -ENOENT;
209 } else {
210 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
John Fastabend331b7292014-09-12 20:08:20 -0700212 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 Torvalds1da177e2005-04-16 15:20:36 -0700217 goto found;
John Fastabend331b7292014-09-12 20:08:20 -0700218 }
219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return -ENOENT;
221
222found:
John Fastabend331b7292014-09-12 20:08:20 -0700223 rcu_assign_pointer(*walk, rtnl_dereference(f->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225 tcf_unbind_filter(tp, &r->res);
Alexei Starovoitoved7aa872015-08-25 20:06:33 -0700226 /* 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 Wangf2b75102017-11-06 13:47:29 -0800230 if (f) {
231 if (tcf_exts_get_net(&f->result.exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700232 tcf_queue_work(&f->rwork, tcindex_destroy_fexts_work);
Cong Wangf2b75102017-11-06 13:47:29 -0800233 else
234 __tcindex_destroy_fexts(f);
235 } else {
Cong Wang304e0242020-03-28 12:12:59 -0700236 tcindex_data_get(p);
237
Cong Wangf2b75102017-11-06 13:47:29 -0800238 if (tcf_exts_get_net(&r->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700239 tcf_queue_work(&r->rwork, tcindex_destroy_rexts_work);
Cong Wangf2b75102017-11-06 13:47:29 -0800240 else
241 __tcindex_destroy_rexts(r);
242 }
WANG Cong763dbf62017-04-19 14:21:21 -0700243
244 *last = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246}
247
Cong Wang3d210532019-02-16 10:58:26 -0800248static void tcindex_destroy_work(struct work_struct *work)
John Fastabend331b7292014-09-12 20:08:20 -0700249{
Cong Wang3d210532019-02-16 10:58:26 -0800250 struct tcindex_data *p = container_of(to_rcu_work(work),
251 struct tcindex_data,
252 rwork);
John Fastabend331b7292014-09-12 20:08:20 -0700253
Cong Wang304e0242020-03-28 12:12:59 -0700254 tcindex_data_put(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257static inline int
258valid_perfect_hash(struct tcindex_data *p)
259{
260 return p->hash > (p->mask >> p->shift);
261}
262
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800263static 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 Wang14215102019-02-20 21:37:42 -0800271static int tcindex_filter_result_init(struct tcindex_filter_result *r,
Cong Wang304e0242020-03-28 12:12:59 -0700272 struct tcindex_data *p,
Cong Wang14215102019-02-20 21:37:42 -0800273 struct net *net)
Cong Wangbf63ac72014-05-19 12:15:49 -0700274{
275 memset(r, 0, sizeof(*r));
Cong Wang304e0242020-03-28 12:12:59 -0700276 r->p = p;
Cong Wang14215102019-02-20 21:37:42 -0800277 return tcf_exts_init(&r->exts, net, TCA_TCINDEX_ACT,
278 TCA_TCINDEX_POLICE);
Cong Wangbf63ac72014-05-19 12:15:49 -0700279}
280
Pavel Skripkinf5051bc2021-07-17 14:29:33 +0300281static void tcindex_free_perfect_hash(struct tcindex_data *cp);
282
Cong Wang3d210532019-02-16 10:58:26 -0800283static void tcindex_partial_destroy_work(struct work_struct *work)
John Fastabend331b7292014-09-12 20:08:20 -0700284{
Cong Wang3d210532019-02-16 10:58:26 -0800285 struct tcindex_data *p = container_of(to_rcu_work(work),
286 struct tcindex_data,
287 rwork);
John Fastabend331b7292014-09-12 20:08:20 -0700288
Cong Wangb1be2e82020-03-11 22:42:27 -0700289 rtnl_lock();
Pavel Skripkinf5051bc2021-07-17 14:29:33 +0300290 if (p->perfect)
291 tcindex_free_perfect_hash(p);
John Fastabend331b7292014-09-12 20:08:20 -0700292 kfree(p);
Cong Wangb1be2e82020-03-11 22:42:27 -0700293 rtnl_unlock();
John Fastabend331b7292014-09-12 20:08:20 -0700294}
295
WANG Congb9a24bb2016-08-19 12:36:54 -0700296static 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 Wang033b2282019-02-11 13:06:15 -0800305static int tcindex_alloc_perfect_hash(struct net *net, struct tcindex_data *cp)
WANG Congb9a24bb2016-08-19 12:36:54 -0700306{
307 int i, err = 0;
308
309 cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result),
Pavel Skripkin3f2db252021-06-25 23:23:48 +0300310 GFP_KERNEL | __GFP_NOWARN);
WANG Congb9a24bb2016-08-19 12:36:54 -0700311 if (!cp->perfect)
312 return -ENOMEM;
313
314 for (i = 0; i < cp->hash; i++) {
Cong Wang14215102019-02-20 21:37:42 -0800315 err = tcf_exts_init(&cp->perfect[i].exts, net,
WANG Congb9a24bb2016-08-19 12:36:54 -0700316 TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
317 if (err < 0)
318 goto errout;
Cong Wang304e0242020-03-28 12:12:59 -0700319 cp->perfect[i].p = cp;
WANG Congb9a24bb2016-08-19 12:36:54 -0700320 }
321
322 return 0;
323
324errout:
325 tcindex_free_perfect_hash(cp);
326 return err;
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static int
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000330tcindex_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 Wang695176b2021-07-29 16:12:14 -0700333 struct nlattr *est, u32 flags, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct tcindex_filter_result new_filter_result, *old_r = r;
WANG Congb9a24bb2016-08-19 12:36:54 -0700336 struct tcindex_data *cp = NULL, *oldp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct tcindex_filter *f = NULL; /* make gcc behave */
Cong Wang1db817e72019-02-11 13:06:16 -0800338 struct tcf_result cr = {};
WANG Congb9a24bb2016-08-19 12:36:54 -0700339 int err, balloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct tcf_exts e;
341
Cong Wang14215102019-02-20 21:37:42 -0800342 err = tcf_exts_init(&e, net, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (err < 0)
344 return err;
Cong Wang695176b2021-07-29 16:12:14 -0700345 err = tcf_exts_validate(net, tp, tb, est, &e, flags, extack);
WANG Congb9a24bb2016-08-19 12:36:54 -0700346 if (err < 0)
347 goto errout;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900348
WANG Cong02c5e842014-09-25 12:06:04 -0700349 err = -ENOMEM;
John Fastabend331b7292014-09-12 20:08:20 -0700350 /* 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 Conga57a65b2014-09-15 14:06:46 -0700354 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
WANG Cong02c5e842014-09-25 12:06:04 -0700355 if (!cp)
WANG Cong44b75e42014-09-15 16:43:42 -0700356 goto errout;
John Fastabend331b7292014-09-12 20:08:20 -0700357
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 Wang304e0242020-03-28 12:12:59 -0700364 refcount_set(&cp->refcnt, 1); /* Paired with tcindex_destroy_work() */
John Fastabend331b7292014-09-12 20:08:20 -0700365
Cong Wang599be012020-02-02 21:14:35 -0800366 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 Dumazetbcd0cf12021-01-14 10:52:29 -0800372 if (tb[TCA_TCINDEX_SHIFT]) {
Cong Wang599be012020-02-02 21:14:35 -0800373 cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
Eric Dumazetbcd0cf12021-01-14 10:52:29 -0800374 if (cp->shift > 16) {
375 err = -EINVAL;
376 goto errout;
377 }
378 }
Cong Wang599be012020-02-02 21:14:35 -0800379 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 Fastabend331b7292014-09-12 20:08:20 -0700389 if (p->perfect) {
WANG Cong6e056562014-09-30 16:07:23 -0700390 int i;
391
Cong Wang033b2282019-02-11 13:06:15 -0800392 if (tcindex_alloc_perfect_hash(net, cp) < 0)
John Fastabend331b7292014-09-12 20:08:20 -0700393 goto errout;
Cong Wang0d1c3532020-03-11 22:42:28 -0700394 cp->alloc_hash = cp->hash;
Cong Wang599be012020-02-02 21:14:35 -0800395 for (i = 0; i < min(cp->hash, p->hash); i++)
WANG Congb9a24bb2016-08-19 12:36:54 -0700396 cp->perfect[i].res = p->perfect[i].res;
WANG Cong44b75e42014-09-15 16:43:42 -0700397 balloc = 1;
John Fastabend331b7292014-09-12 20:08:20 -0700398 }
399 cp->h = p->h;
400
Cong Wang304e0242020-03-28 12:12:59 -0700401 err = tcindex_filter_result_init(&new_filter_result, cp, net);
WANG Congb9a24bb2016-08-19 12:36:54 -0700402 if (err < 0)
Cong Wang52b5ae52020-02-04 11:10:12 -0800403 goto errout_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (old_r)
Cong Wang1db817e72019-02-11 13:06:16 -0800405 cr = r->res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 err = -EBUSY;
John Fastabend331b7292014-09-12 20:08:20 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* Hash already allocated, make sure that we still meet the
410 * requirements for the allocated hash.
411 */
John Fastabend331b7292014-09-12 20:08:20 -0700412 if (cp->perfect) {
413 if (!valid_perfect_hash(cp) ||
414 cp->hash > cp->alloc_hash)
WANG Cong44b75e42014-09-15 16:43:42 -0700415 goto errout_alloc;
John Fastabend331b7292014-09-12 20:08:20 -0700416 } else if (cp->h && cp->hash != cp->alloc_hash) {
WANG Cong44b75e42014-09-15 16:43:42 -0700417 goto errout_alloc;
John Fastabend331b7292014-09-12 20:08:20 -0700418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 err = -EINVAL;
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800421 if (tb[TCA_TCINDEX_FALL_THROUGH])
John Fastabend331b7292014-09-12 20:08:20 -0700422 cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
WANG Cong68f6a7c2014-09-25 12:06:05 -0700424 if (!cp->perfect && !cp->h)
John Fastabend331b7292014-09-12 20:08:20 -0700425 cp->alloc_hash = cp->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
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 Fastabend331b7292014-09-12 20:08:20 -0700432 if (cp->perfect || valid_perfect_hash(cp))
433 if (handle >= cp->alloc_hash)
WANG Cong44b75e42014-09-15 16:43:42 -0700434 goto errout_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436
437 err = -ENOMEM;
John Fastabend331b7292014-09-12 20:08:20 -0700438 if (!cp->perfect && !cp->h) {
439 if (valid_perfect_hash(cp)) {
Cong Wang033b2282019-02-11 13:06:15 -0800440 if (tcindex_alloc_perfect_hash(net, cp) < 0)
WANG Cong44b75e42014-09-15 16:43:42 -0700441 goto errout_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 balloc = 1;
443 } else {
John Fastabend331b7292014-09-12 20:08:20 -0700444 struct tcindex_filter __rcu **hash;
445
446 hash = kcalloc(cp->hash,
447 sizeof(struct tcindex_filter *),
448 GFP_KERNEL);
449
450 if (!hash)
WANG Cong44b75e42014-09-15 16:43:42 -0700451 goto errout_alloc;
John Fastabend331b7292014-09-12 20:08:20 -0700452
453 cp->h = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 balloc = 2;
455 }
456 }
457
John Fastabend331b7292014-09-12 20:08:20 -0700458 if (cp->perfect)
459 r = cp->perfect + handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 else
John Fastabend331b7292014-09-12 20:08:20 -0700461 r = tcindex_lookup(cp, handle) ? : &new_filter_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (r == &new_filter_result) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700464 f = kzalloc(sizeof(*f), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!f)
466 goto errout_alloc;
WANG Cong6e056562014-09-30 16:07:23 -0700467 f->key = handle;
WANG Cong6e056562014-09-30 16:07:23 -0700468 f->next = NULL;
Cong Wang304e0242020-03-28 12:12:59 -0700469 err = tcindex_filter_result_init(&f->result, cp, net);
WANG Congb9a24bb2016-08-19 12:36:54 -0700470 if (err < 0) {
471 kfree(f);
472 goto errout_alloc;
473 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Patrick McHardyadd93b62008-01-22 22:11:33 -0800476 if (tb[TCA_TCINDEX_CLASSID]) {
Cong Wang1db817e72019-02-11 13:06:16 -0800477 cr.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]);
478 tcf_bind_filter(tp, &cr, base);
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
WANG Congb9a24bb2016-08-19 12:36:54 -0700481 if (old_r && old_r != r) {
Cong Wang304e0242020-03-28 12:12:59 -0700482 err = tcindex_filter_result_init(old_r, cp, net);
WANG Congb9a24bb2016-08-19 12:36:54 -0700483 if (err < 0) {
484 kfree(f);
485 goto errout_alloc;
486 }
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
John Fastabend331b7292014-09-12 20:08:20 -0700489 oldp = p;
Cong Wang1db817e72019-02-11 13:06:16 -0800490 r->res = cr;
Hangbin Liu2df8bee2018-08-13 18:44:03 +0800491 tcf_exts_change(&r->exts, &e);
492
John Fastabend331b7292014-09-12 20:08:20 -0700493 rcu_assign_pointer(tp->root, cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if (r == &new_filter_result) {
John Fastabend331b7292014-09-12 20:08:20 -0700496 struct tcindex_filter *nfp;
497 struct tcindex_filter __rcu **fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Hangbin Liu008369d2018-08-13 18:44:04 +0800499 f->result.res = r->res;
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200500 tcf_exts_change(&f->result.exts, &r->exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
WANG Cong69301ea2014-09-15 16:43:43 -0700502 fp = cp->h + (handle % cp->hash);
John Fastabend331b7292014-09-12 20:08:20 -0700503 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 Wang1db817e72019-02-11 13:06:16 -0800509 } else {
510 tcf_exts_destroy(&new_filter_result.exts);
John Fastabend331b7292014-09-12 20:08:20 -0700511 }
512
513 if (oldp)
Cong Wang3d210532019-02-16 10:58:26 -0800514 tcf_queue_work(&oldp->rwork, tcindex_partial_destroy_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 0;
516
517errout_alloc:
518 if (balloc == 1)
WANG Congb9a24bb2016-08-19 12:36:54 -0700519 tcindex_free_perfect_hash(cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 else if (balloc == 2)
John Fastabend331b7292014-09-12 20:08:20 -0700521 kfree(cp->h);
WANG Congb9a24bb2016-08-19 12:36:54 -0700522 tcf_exts_destroy(&new_filter_result.exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523errout:
John Fastabend331b7292014-09-12 20:08:20 -0700524 kfree(cp);
WANG Cong18d02642014-09-25 10:26:37 -0700525 tcf_exts_destroy(&e);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return err;
527}
528
529static int
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000530tcindex_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600531 struct tcf_proto *tp, unsigned long base, u32 handle,
Cong Wang695176b2021-07-29 16:12:14 -0700532 struct nlattr **tca, void **arg, u32 flags,
533 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Patrick McHardyadd93b62008-01-22 22:11:33 -0800535 struct nlattr *opt = tca[TCA_OPTIONS];
536 struct nlattr *tb[TCA_TCINDEX_MAX + 1];
John Fastabend331b7292014-09-12 20:08:20 -0700537 struct tcindex_data *p = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700538 struct tcindex_filter_result *r = *arg;
Patrick McHardycee63722008-01-23 20:33:32 -0800539 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800541 pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
WANG Cong8113c092017-08-04 21:31:43 -0700542 "p %p,r %p,*arg %p\n",
Gaurav Singhc5efcf12020-06-21 22:24:30 -0400543 tp, handle, tca, arg, opt, p, r, *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 if (!opt)
546 return 0;
547
Johannes Berg8cb08172019-04-26 14:07:28 +0200548 err = nla_parse_nested_deprecated(tb, TCA_TCINDEX_MAX, opt,
549 tcindex_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800550 if (err < 0)
551 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000553 return tcindex_set_parms(net, tp, base, handle, p, r, tb,
Cong Wang695176b2021-07-29 16:12:14 -0700554 tca[TCA_RATE], flags, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Vlad Buslov12db03b2019-02-11 10:55:45 +0200557static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker,
558 bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
John Fastabend331b7292014-09-12 20:08:20 -0700560 struct tcindex_data *p = rtnl_dereference(tp->root);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800561 struct tcindex_filter *f, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int i;
563
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800564 pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 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 Cong8113c092017-08-04 21:31:43 -0700570 if (walker->fn(tp, p->perfect + i, walker) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 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 Fastabend331b7292014-09-12 20:08:20 -0700581 for (f = rtnl_dereference(p->h[i]); f; f = next) {
582 next = rtnl_dereference(f->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (walker->count >= walker->skip) {
WANG Cong8113c092017-08-04 21:31:43 -0700584 if (walker->fn(tp, &f->result, walker) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 walker->stop = 1;
586 return;
587 }
588 }
589 walker->count++;
590 }
591 }
592}
593
Vlad Buslov12db03b2019-02-11 10:55:45 +0200594static void tcindex_destroy(struct tcf_proto *tp, bool rtnl_held,
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800595 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
John Fastabend331b7292014-09-12 20:08:20 -0700597 struct tcindex_data *p = rtnl_dereference(tp->root);
Cong Wang51dcb692019-02-16 10:58:27 -0800598 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800600 pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
Cong Wang51dcb692019-02-16 10:58:27 -0800601
602 if (p->perfect) {
603 for (i = 0; i < p->hash; i++) {
604 struct tcindex_filter_result *r = p->perfect + i;
605
Cong Wang304e0242020-03-28 12:12:59 -0700606 /* 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 Wang51dcb692019-02-16 10:58:27 -0800614 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 Fastabend331b7292014-09-12 20:08:20 -0700632
Cong Wang3d210532019-02-16 10:58:26 -0800633 tcf_queue_work(&p->rwork, tcindex_destroy_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
636
WANG Cong8113c092017-08-04 21:31:43 -0700637static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +0200638 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
John Fastabend331b7292014-09-12 20:08:20 -0700640 struct tcindex_data *p = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700641 struct tcindex_filter_result *r = fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800642 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
WANG Cong8113c092017-08-04 21:31:43 -0700644 pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100645 tp, fh, skb, t, p, r);
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800646 pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800647
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200648 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800649 if (nest == NULL)
650 goto nla_put_failure;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!fh) {
653 t->tcm_handle = ~0; /* whatever ... */
David S. Miller1b34ec42012-03-29 05:11:39 -0400654 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 McHardy4b3550ef2008-01-23 20:34:11 -0800659 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 } else {
661 if (p->perfect) {
John Fastabend331b7292014-09-12 20:08:20 -0700662 t->tcm_handle = r - p->perfect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 } else {
664 struct tcindex_filter *f;
John Fastabend331b7292014-09-12 20:08:20 -0700665 struct tcindex_filter __rcu **fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 int i;
667
668 t->tcm_handle = 0;
669 for (i = 0; !t->tcm_handle && i < p->hash; i++) {
John Fastabend331b7292014-09-12 20:08:20 -0700670 fp = &p->h[i];
671 for (f = rtnl_dereference(*fp);
672 !t->tcm_handle && f;
673 fp = &f->next, f = rtnl_dereference(*fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (&f->result == r)
675 t->tcm_handle = f->key;
676 }
677 }
678 }
Stephen Hemmingeraa767bf2008-01-21 02:26:41 -0800679 pr_debug("handle = %d\n", t->tcm_handle);
David S. Miller1b34ec42012-03-29 05:11:39 -0400680 if (r->res.class &&
681 nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid))
682 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
WANG Cong5da57f42013-12-15 20:15:07 -0800684 if (tcf_exts_dump(skb, &r->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800685 goto nla_put_failure;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800686 nla_nest_end(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
WANG Cong5da57f42013-12-15 20:15:07 -0800688 if (tcf_exts_dump_stats(skb, &r->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800689 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return skb->len;
693
Patrick McHardyadd93b62008-01-22 22:11:33 -0800694nla_put_failure:
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100695 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -1;
697}
698
Cong Wang2e24cd72020-01-23 16:26:18 -0800699static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl,
700 void *q, unsigned long base)
Cong Wang07d79fc2017-08-30 14:30:36 -0700701{
702 struct tcindex_filter_result *r = fh;
703
Cong Wang2e24cd72020-01-23 16:26:18 -0800704 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 Wang07d79fc2017-08-30 14:30:36 -0700710}
711
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800712static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 .kind = "tcindex",
714 .classify = tcindex_classify,
715 .init = tcindex_init,
716 .destroy = tcindex_destroy,
717 .get = tcindex_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .change = tcindex_change,
719 .delete = tcindex_delete,
720 .walk = tcindex_walk,
721 .dump = tcindex_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -0700722 .bind_class = tcindex_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 .owner = THIS_MODULE,
724};
725
726static int __init init_tcindex(void)
727{
728 return register_tcf_proto_ops(&cls_tcindex_ops);
729}
730
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900731static void __exit exit_tcindex(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 unregister_tcf_proto_ops(&cls_tcindex_ops);
734}
735
736module_init(init_tcindex)
737module_exit(exit_tcindex)
738MODULE_LICENSE("GPL");