blob: 3d4c360f9b0cecb70d1c699cbbcbb8af3b484772 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sched/cls_u32.c Ugly (or Universal) 32bit key Packet Classifier.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 * The filters are packed to hash tables of key nodes
12 * with a set of 32bit key/mask pairs at every node.
13 * Nodes reference next level hash tables etc.
14 *
15 * This scheme is the best universal classifier I managed to
16 * invent; it is not super-fast, but it is not slow (provided you
17 * program it correctly), and general enough. And its relative
18 * speed grows as the number of rules becomes larger.
19 *
20 * It seems that it represents the best middle point between
21 * speed and manageability both by human and by machine.
22 *
23 * It is especially useful for link sharing combined with QoS;
24 * pure RSVP doesn't need such a general approach and can use
25 * much simpler (and faster) schemes, sort of cls_rsvp.c.
26 *
27 * JHS: We should remove the CONFIG_NET_CLS_IND from here
28 * eventually when the meta match extension is made available
29 *
30 * nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro>
31 */
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/types.h>
36#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/errno.h>
John Fastabend1ce87722014-09-12 20:09:16 -070039#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/skbuff.h>
Cong Wang7801db82014-07-17 17:34:53 -070042#include <linux/bitmap.h>
WANG Cong3cd904e2017-08-24 16:51:30 -070043#include <linux/netdevice.h>
44#include <linux/hash.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070045#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/act_api.h>
47#include <net/pkt_cls.h>
Cong Wange7614372017-09-25 10:13:51 -070048#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Eric Dumazetcc7ec452011-01-19 19:26:56 +000050struct tc_u_knode {
John Fastabend1ce87722014-09-12 20:09:16 -070051 struct tc_u_knode __rcu *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 u32 handle;
John Fastabend1ce87722014-09-12 20:09:16 -070053 struct tc_u_hnode __rcu *ht_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 struct tcf_exts exts;
55#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -080056 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
58 u8 fshift;
59 struct tcf_result res;
John Fastabend1ce87722014-09-12 20:09:16 -070060 struct tc_u_hnode __rcu *ht_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -070062 struct tc_u32_pcnt __percpu *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
John Fastabend9e8ce792016-02-26 07:54:39 -080064 u32 flags;
John Hurley530d9952018-06-25 14:30:08 -070065 unsigned int in_hw_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -070067 u32 val;
68 u32 mask;
69 u32 __percpu *pcpu_success;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#endif
Cong Wangaaa908f2018-05-23 15:26:53 -070071 struct rcu_work rwork;
John Fastabend4e2840e2014-09-17 11:11:46 -070072 /* The 'sel' field MUST be the last field in structure to allow for
73 * tc_u32_keys allocated at end of structure.
74 */
75 struct tc_u32_sel sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Eric Dumazetcc7ec452011-01-19 19:26:56 +000078struct tc_u_hnode {
John Fastabend1ce87722014-09-12 20:09:16 -070079 struct tc_u_hnode __rcu *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 u32 handle;
81 u32 prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 int refcnt;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000083 unsigned int divisor;
Cong Wange7614372017-09-25 10:13:51 -070084 struct idr handle_idr;
Al Virob44ef842018-10-08 06:22:33 -040085 bool is_root;
John Fastabend1ce87722014-09-12 20:09:16 -070086 struct rcu_head rcu;
Jakub Kicinskif40fe582018-01-24 12:54:22 -080087 u32 flags;
WANG Cong5778d392015-03-09 17:03:40 -070088 /* The 'ht' field MUST be the last field in structure to allow for
89 * more entries allocated at end of structure.
90 */
91 struct tc_u_knode __rcu *ht[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
Eric Dumazetcc7ec452011-01-19 19:26:56 +000094struct tc_u_common {
John Fastabend1ce87722014-09-12 20:09:16 -070095 struct tc_u_hnode __rcu *hlist;
Jiri Pirko339c21d2018-02-13 12:00:17 +010096 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int refcnt;
Cong Wange7614372017-09-25 10:13:51 -070098 struct idr handle_idr;
WANG Cong3cd904e2017-08-24 16:51:30 -070099 struct hlist_node hnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000102static inline unsigned int u32_hash_fold(__be32 key,
103 const struct tc_u32_sel *sel,
104 u8 fshift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000106 unsigned int h = ntohl(key & sel->hmask) >> fshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 return h;
109}
110
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400111static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp,
112 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct {
115 struct tc_u_knode *knode;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700116 unsigned int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 } stack[TC_U32_MAXDEPTH];
118
John Fastabend1ce87722014-09-12 20:09:16 -0700119 struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700120 unsigned int off = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct tc_u_knode *n;
122 int sdepth = 0;
123 int off2 = 0;
124 int sel = 0;
125#ifdef CONFIG_CLS_U32_PERF
126 int j;
127#endif
128 int i, r;
129
130next_ht:
John Fastabend1ce87722014-09-12 20:09:16 -0700131 n = rcu_dereference_bh(ht->ht[sel]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133next_knode:
134 if (n) {
135 struct tc_u32_key *key = n->sel.keys;
136
137#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -0700138 __this_cpu_inc(n->pf->rcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 j = 0;
140#endif
141
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700142 if (tc_skip_sw(n->flags)) {
143 n = rcu_dereference_bh(n->next);
144 goto next_knode;
145 }
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -0700148 if ((skb->mark & n->mask) != n->val) {
John Fastabend1ce87722014-09-12 20:09:16 -0700149 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 goto next_knode;
151 } else {
John Fastabend459d5f62014-09-12 20:08:47 -0700152 __this_cpu_inc(*n->pcpu_success);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154#endif
155
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000156 for (i = n->sel.nkeys; i > 0; i--, key++) {
stephen hemminger66d50d22010-08-02 13:44:13 +0000157 int toff = off + key->off + (off2 & key->offmask);
stephen hemminger86fce3b2011-02-20 16:14:23 +0000158 __be32 *data, hdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Dan Carpenter4e18b3e2010-10-04 02:28:36 +0000160 if (skb_headroom(skb) + toff > INT_MAX)
stephen hemminger66d50d22010-08-02 13:44:13 +0000161 goto out;
162
stephen hemminger86fce3b2011-02-20 16:14:23 +0000163 data = skb_header_pointer(skb, toff, 4, &hdata);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700164 if (!data)
165 goto out;
166 if ((*data ^ key->val) & key->mask) {
John Fastabend1ce87722014-09-12 20:09:16 -0700167 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto next_knode;
169 }
170#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -0700171 __this_cpu_inc(n->pf->kcnts[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 j++;
173#endif
174 }
John Fastabend1ce87722014-09-12 20:09:16 -0700175
176 ht = rcu_dereference_bh(n->ht_down);
177 if (!ht) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178check_terminal:
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000179 if (n->sel.flags & TC_U32_TERMINAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 *res = n->res;
182#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -0800183 if (!tcf_match_indev(skb, n->ifindex)) {
John Fastabend1ce87722014-09-12 20:09:16 -0700184 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto next_knode;
186 }
187#endif
188#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -0700189 __this_cpu_inc(n->pf->rhit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#endif
191 r = tcf_exts_exec(skb, &n->exts, res);
192 if (r < 0) {
John Fastabend1ce87722014-09-12 20:09:16 -0700193 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 goto next_knode;
195 }
196
197 return r;
198 }
John Fastabend1ce87722014-09-12 20:09:16 -0700199 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto next_knode;
201 }
202
203 /* PUSH */
204 if (sdepth >= TC_U32_MAXDEPTH)
205 goto deadloop;
206 stack[sdepth].knode = n;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700207 stack[sdepth].off = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 sdepth++;
209
John Fastabend1ce87722014-09-12 20:09:16 -0700210 ht = rcu_dereference_bh(n->ht_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 sel = 0;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700212 if (ht->divisor) {
stephen hemminger86fce3b2011-02-20 16:14:23 +0000213 __be32 *data, hdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700215 data = skb_header_pointer(skb, off + n->sel.hoff, 4,
stephen hemminger86fce3b2011-02-20 16:14:23 +0000216 &hdata);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700217 if (!data)
218 goto out;
219 sel = ht->divisor & u32_hash_fold(*data, &n->sel,
220 n->fshift);
221 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000222 if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 goto next_ht;
224
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000225 if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 off2 = n->sel.off + 3;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700227 if (n->sel.flags & TC_U32_VAROFFSET) {
stephen hemminger86fce3b2011-02-20 16:14:23 +0000228 __be16 *data, hdata;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700229
230 data = skb_header_pointer(skb,
231 off + n->sel.offoff,
stephen hemminger86fce3b2011-02-20 16:14:23 +0000232 2, &hdata);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700233 if (!data)
234 goto out;
235 off2 += ntohs(n->sel.offmask & *data) >>
236 n->sel.offshift;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 off2 &= ~3;
239 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000240 if (n->sel.flags & TC_U32_EAT) {
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700241 off += off2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 off2 = 0;
243 }
244
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700245 if (off < skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 goto next_ht;
247 }
248
249 /* POP */
250 if (sdepth--) {
251 n = stack[sdepth].knode;
John Fastabend1ce87722014-09-12 20:09:16 -0700252 ht = rcu_dereference_bh(n->ht_up);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700253 off = stack[sdepth].off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto check_terminal;
255 }
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700256out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return -1;
258
259deadloop:
Joe Perchese87cc472012-05-13 21:56:26 +0000260 net_warn_ratelimited("cls_u32: dead loop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return -1;
262}
263
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400264static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct tc_u_hnode *ht;
267
John Fastabend1ce87722014-09-12 20:09:16 -0700268 for (ht = rtnl_dereference(tp_c->hlist);
269 ht;
270 ht = rtnl_dereference(ht->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (ht->handle == handle)
272 break;
273
274 return ht;
275}
276
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400277static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000279 unsigned int sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct tc_u_knode *n = NULL;
281
282 sel = TC_U32_HASH(handle);
283 if (sel > ht->divisor)
284 goto out;
285
John Fastabend1ce87722014-09-12 20:09:16 -0700286 for (n = rtnl_dereference(ht->ht[sel]);
287 n;
288 n = rtnl_dereference(n->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (n->handle == handle)
290 break;
291out:
292 return n;
293}
294
295
WANG Cong8113c092017-08-04 21:31:43 -0700296static void *u32_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct tc_u_hnode *ht;
299 struct tc_u_common *tp_c = tp->data;
300
301 if (TC_U32_HTID(handle) == TC_U32_ROOT)
John Fastabend1ce87722014-09-12 20:09:16 -0700302 ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 else
304 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
305
306 if (!ht)
WANG Cong8113c092017-08-04 21:31:43 -0700307 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (TC_U32_KEY(handle) == 0)
WANG Cong8113c092017-08-04 21:31:43 -0700310 return ht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
WANG Cong8113c092017-08-04 21:31:43 -0700312 return u32_lookup_key(ht, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Matthew Wilcoxffdc2d92017-11-28 12:05:54 -0500315/* Protected by rtnl lock */
Cong Wange7614372017-09-25 10:13:51 -0700316static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Matthew Wilcoxffdc2d92017-11-28 12:05:54 -0500318 int id = idr_alloc_cyclic(&tp_c->handle_idr, ptr, 1, 0x7FF, GFP_KERNEL);
319 if (id < 0)
Cong Wange7614372017-09-25 10:13:51 -0700320 return 0;
Matthew Wilcoxffdc2d92017-11-28 12:05:54 -0500321 return (id | 0x800U) << 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
WANG Cong3cd904e2017-08-24 16:51:30 -0700324static struct hlist_head *tc_u_common_hash;
325
326#define U32_HASH_SHIFT 10
327#define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
328
Jiri Pirko339c21d2018-02-13 12:00:17 +0100329static void *tc_u_common_ptr(const struct tcf_proto *tp)
330{
331 struct tcf_block *block = tp->chain->block;
332
333 /* The block sharing is currently supported only
334 * for classless qdiscs. In that case we use block
335 * for tc_u_common identification. In case the
336 * block is not shared, block->q is a valid pointer
337 * and we can use that. That works for classful qdiscs.
338 */
339 if (tcf_block_shared(block))
340 return block;
341 else
342 return block->q;
343}
344
Al Viro4895c422018-10-08 06:22:39 -0400345static struct hlist_head *tc_u_hash(void *key)
WANG Cong3cd904e2017-08-24 16:51:30 -0700346{
Al Viro4895c422018-10-08 06:22:39 -0400347 return tc_u_common_hash + hash_ptr(key, U32_HASH_SHIFT);
WANG Cong3cd904e2017-08-24 16:51:30 -0700348}
349
Al Viro4895c422018-10-08 06:22:39 -0400350static struct tc_u_common *tc_u_common_find(void *key)
WANG Cong3cd904e2017-08-24 16:51:30 -0700351{
352 struct tc_u_common *tc;
Al Viro4895c422018-10-08 06:22:39 -0400353 hlist_for_each_entry(tc, tc_u_hash(key), hnode) {
354 if (tc->ptr == key)
WANG Cong3cd904e2017-08-24 16:51:30 -0700355 return tc;
356 }
357 return NULL;
358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360static int u32_init(struct tcf_proto *tp)
361{
362 struct tc_u_hnode *root_ht;
Al Viro4895c422018-10-08 06:22:39 -0400363 void *key = tc_u_common_ptr(tp);
364 struct tc_u_common *tp_c = tc_u_common_find(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700366 root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (root_ht == NULL)
368 return -ENOBUFS;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 root_ht->refcnt++;
Cong Wange7614372017-09-25 10:13:51 -0700371 root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 root_ht->prio = tp->prio;
Al Virob44ef842018-10-08 06:22:33 -0400373 root_ht->is_root = true;
Cong Wange7614372017-09-25 10:13:51 -0700374 idr_init(&root_ht->handle_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (tp_c == NULL) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700377 tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (tp_c == NULL) {
379 kfree(root_ht);
380 return -ENOBUFS;
381 }
Al Viro4895c422018-10-08 06:22:39 -0400382 tp_c->ptr = key;
WANG Cong3cd904e2017-08-24 16:51:30 -0700383 INIT_HLIST_NODE(&tp_c->hnode);
Cong Wange7614372017-09-25 10:13:51 -0700384 idr_init(&tp_c->handle_idr);
WANG Cong3cd904e2017-08-24 16:51:30 -0700385
Al Viro4895c422018-10-08 06:22:39 -0400386 hlist_add_head(&tp_c->hnode, tc_u_hash(key));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 tp_c->refcnt++;
John Fastabend1ce87722014-09-12 20:09:16 -0700390 RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
391 rcu_assign_pointer(tp_c->hlist, root_ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
John Fastabend1ce87722014-09-12 20:09:16 -0700393 rcu_assign_pointer(tp->root, root_ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 tp->data = tp_c;
395 return 0;
396}
397
Al Virodc07c572018-10-08 06:22:36 -0400398static int u32_destroy_key(struct tc_u_knode *n, bool free_pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Paolo Abenid7cdee52018-02-05 22:23:01 +0100400 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
401
WANG Cong18d02642014-09-25 10:26:37 -0700402 tcf_exts_destroy(&n->exts);
Cong Wang35c55fc2017-11-06 13:47:30 -0800403 tcf_exts_put_net(&n->exts);
Paolo Abenid7cdee52018-02-05 22:23:01 +0100404 if (ht && --ht->refcnt == 0)
405 kfree(ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#ifdef CONFIG_CLS_U32_PERF
John Fastabendde5df632014-09-19 21:50:34 -0700407 if (free_pf)
408 free_percpu(n->pf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#endif
John Fastabenda1ddcfe2014-09-19 21:50:04 -0700410#ifdef CONFIG_CLS_U32_MARK
John Fastabendde5df632014-09-19 21:50:34 -0700411 if (free_pf)
412 free_percpu(n->pcpu_success);
John Fastabenda1ddcfe2014-09-19 21:50:04 -0700413#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 kfree(n);
415 return 0;
416}
417
John Fastabendde5df632014-09-19 21:50:34 -0700418/* u32_delete_key_rcu should be called when free'ing a copied
419 * version of a tc_u_knode obtained from u32_init_knode(). When
420 * copies are obtained from u32_init_knode() the statistics are
421 * shared between the old and new copies to allow readers to
422 * continue to update the statistics during the copy. To support
423 * this the u32_delete_key_rcu variant does not free the percpu
424 * statistics.
425 */
Cong Wangc0d378e2017-10-26 18:24:36 -0700426static void u32_delete_key_work(struct work_struct *work)
427{
Cong Wangaaa908f2018-05-23 15:26:53 -0700428 struct tc_u_knode *key = container_of(to_rcu_work(work),
429 struct tc_u_knode,
430 rwork);
Cong Wangc0d378e2017-10-26 18:24:36 -0700431 rtnl_lock();
Al Virodc07c572018-10-08 06:22:36 -0400432 u32_destroy_key(key, false);
Cong Wangc0d378e2017-10-26 18:24:36 -0700433 rtnl_unlock();
434}
435
John Fastabendde5df632014-09-19 21:50:34 -0700436/* u32_delete_key_freepf_rcu is the rcu callback variant
437 * that free's the entire structure including the statistics
438 * percpu variables. Only use this if the key is not a copy
439 * returned by u32_init_knode(). See u32_delete_key_rcu()
440 * for the variant that should be used with keys return from
441 * u32_init_knode()
442 */
Cong Wangc0d378e2017-10-26 18:24:36 -0700443static void u32_delete_key_freepf_work(struct work_struct *work)
444{
Cong Wangaaa908f2018-05-23 15:26:53 -0700445 struct tc_u_knode *key = container_of(to_rcu_work(work),
446 struct tc_u_knode,
447 rwork);
Cong Wangc0d378e2017-10-26 18:24:36 -0700448 rtnl_lock();
Al Virodc07c572018-10-08 06:22:36 -0400449 u32_destroy_key(key, true);
Cong Wangc0d378e2017-10-26 18:24:36 -0700450 rtnl_unlock();
451}
452
Yang Yingliang82d567c2013-12-10 20:55:31 +0800453static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
John Fastabend1ce87722014-09-12 20:09:16 -0700455 struct tc_u_knode __rcu **kp;
456 struct tc_u_knode *pkp;
John Fastabenda96366bf2014-09-15 23:30:49 -0700457 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 if (ht) {
John Fastabend1ce87722014-09-12 20:09:16 -0700460 kp = &ht->ht[TC_U32_HASH(key->handle)];
461 for (pkp = rtnl_dereference(*kp); pkp;
462 kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
463 if (pkp == key) {
464 RCU_INIT_POINTER(*kp, key->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
WANG Conga0efb802014-09-30 16:07:24 -0700466 tcf_unbind_filter(tp, &key->res);
Cong Wangf12c6432018-04-06 17:19:41 -0700467 idr_remove(&ht->handle_idr, key->handle);
Cong Wang35c55fc2017-11-06 13:47:30 -0800468 tcf_exts_get_net(&key->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700469 tcf_queue_work(&key->rwork, u32_delete_key_freepf_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 0;
471 }
472 }
473 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700474 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0;
476}
477
Jakub Kicinski458e7042018-01-24 12:54:23 -0800478static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
479 struct netlink_ext_ack *extack)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800480{
Jiri Pirko245dc512017-10-19 15:50:35 +0200481 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200482 struct tc_cls_u32_offload cls_u32 = {};
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800483
Jakub Kicinski458e7042018-01-24 12:54:23 -0800484 tc_cls_common_offload_init(&cls_u32.common, tp, h->flags, extack);
Jiri Pirko77460412017-10-19 15:50:34 +0200485 cls_u32.command = TC_CLSU32_DELETE_HNODE;
486 cls_u32.hnode.divisor = h->divisor;
487 cls_u32.hnode.handle = h->handle;
488 cls_u32.hnode.prio = h->prio;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200489
Jiri Pirko245dc512017-10-19 15:50:35 +0200490 tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800491}
492
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400493static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
Quentin Monnet10a47e02018-01-19 17:44:45 -0800494 u32 flags, struct netlink_ext_ack *extack)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800495{
Jiri Pirko245dc512017-10-19 15:50:35 +0200496 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200497 struct tc_cls_u32_offload cls_u32 = {};
Jiri Pirko245dc512017-10-19 15:50:35 +0200498 bool skip_sw = tc_skip_sw(flags);
499 bool offloaded = false;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700500 int err;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800501
Jakub Kicinskif40fe582018-01-24 12:54:22 -0800502 tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200503 cls_u32.command = TC_CLSU32_NEW_HNODE;
504 cls_u32.hnode.divisor = h->divisor;
505 cls_u32.hnode.handle = h->handle;
506 cls_u32.hnode.prio = h->prio;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800507
Jiri Pirko245dc512017-10-19 15:50:35 +0200508 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
509 if (err < 0) {
Jakub Kicinski458e7042018-01-24 12:54:23 -0800510 u32_clear_hw_hnode(tp, h, NULL);
Jakub Kicinskid47a0f32016-06-06 16:16:48 +0100511 return err;
Jiri Pirko245dc512017-10-19 15:50:35 +0200512 } else if (err > 0) {
513 offloaded = true;
514 }
515
516 if (skip_sw && !offloaded)
517 return -EINVAL;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700518
519 return 0;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800520}
521
Jakub Kicinski458e7042018-01-24 12:54:23 -0800522static void u32_remove_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
523 struct netlink_ext_ack *extack)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800524{
Jiri Pirko245dc512017-10-19 15:50:35 +0200525 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200526 struct tc_cls_u32_offload cls_u32 = {};
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800527
Jakub Kicinski458e7042018-01-24 12:54:23 -0800528 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
Jiri Pirko77460412017-10-19 15:50:34 +0200529 cls_u32.command = TC_CLSU32_DELETE_KNODE;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100530 cls_u32.knode.handle = n->handle;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800531
Jiri Pirko245dc512017-10-19 15:50:35 +0200532 tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100533 tcf_block_offload_dec(block, &n->flags);
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800534}
535
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400536static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
Quentin Monnet10a47e02018-01-19 17:44:45 -0800537 u32 flags, struct netlink_ext_ack *extack)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800538{
Paolo Abeni058a6c02018-02-02 16:02:22 +0100539 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
Jiri Pirko245dc512017-10-19 15:50:35 +0200540 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200541 struct tc_cls_u32_offload cls_u32 = {};
Jiri Pirko245dc512017-10-19 15:50:35 +0200542 bool skip_sw = tc_skip_sw(flags);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700543 int err;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800544
Jakub Kicinskif40fe582018-01-24 12:54:22 -0800545 tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200546 cls_u32.command = TC_CLSU32_REPLACE_KNODE;
547 cls_u32.knode.handle = n->handle;
548 cls_u32.knode.fshift = n->fshift;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100549#ifdef CONFIG_CLS_U32_MARK
Jiri Pirkode4784c2017-08-07 10:15:32 +0200550 cls_u32.knode.val = n->val;
551 cls_u32.knode.mask = n->mask;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100552#else
Jiri Pirkode4784c2017-08-07 10:15:32 +0200553 cls_u32.knode.val = 0;
554 cls_u32.knode.mask = 0;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100555#endif
Jiri Pirkode4784c2017-08-07 10:15:32 +0200556 cls_u32.knode.sel = &n->sel;
557 cls_u32.knode.exts = &n->exts;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100558 if (n->ht_down)
Paolo Abeni058a6c02018-02-02 16:02:22 +0100559 cls_u32.knode.link_handle = ht->handle;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100560
Jiri Pirko245dc512017-10-19 15:50:35 +0200561 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
562 if (err < 0) {
Jakub Kicinski458e7042018-01-24 12:54:23 -0800563 u32_remove_hw_knode(tp, n, NULL);
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100564 return err;
Jiri Pirko245dc512017-10-19 15:50:35 +0200565 } else if (err > 0) {
John Hurley530d9952018-06-25 14:30:08 -0700566 n->in_hw_count = err;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100567 tcf_block_offload_inc(block, &n->flags);
Jiri Pirko245dc512017-10-19 15:50:35 +0200568 }
569
Colin Ian King0f04d052017-11-03 08:09:45 +0000570 if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
Jiri Pirko245dc512017-10-19 15:50:35 +0200571 return -EINVAL;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700572
573 return 0;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800574}
575
Jakub Kicinski458e7042018-01-24 12:54:23 -0800576static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
577 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct tc_u_knode *n;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000580 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000582 for (h = 0; h <= ht->divisor; h++) {
John Fastabend1ce87722014-09-12 20:09:16 -0700583 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
584 RCU_INIT_POINTER(ht->ht[h],
585 rtnl_dereference(n->next));
WANG Conga0efb802014-09-30 16:07:24 -0700586 tcf_unbind_filter(tp, &n->res);
Jakub Kicinski458e7042018-01-24 12:54:23 -0800587 u32_remove_hw_knode(tp, n, extack);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500588 idr_remove(&ht->handle_idr, n->handle);
Cong Wang35c55fc2017-11-06 13:47:30 -0800589 if (tcf_exts_get_net(&n->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700590 tcf_queue_work(&n->rwork, u32_delete_key_freepf_work);
Cong Wang35c55fc2017-11-06 13:47:30 -0800591 else
Al Virodc07c572018-10-08 06:22:36 -0400592 u32_destroy_key(n, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 }
595}
596
Jakub Kicinski458e7042018-01-24 12:54:23 -0800597static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
598 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct tc_u_common *tp_c = tp->data;
John Fastabend1ce87722014-09-12 20:09:16 -0700601 struct tc_u_hnode __rcu **hn;
602 struct tc_u_hnode *phn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700604 WARN_ON(ht->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Jakub Kicinski458e7042018-01-24 12:54:23 -0800606 u32_clear_hnode(tp, ht, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
John Fastabend1ce87722014-09-12 20:09:16 -0700608 hn = &tp_c->hlist;
609 for (phn = rtnl_dereference(*hn);
610 phn;
611 hn = &phn->next, phn = rtnl_dereference(*hn)) {
612 if (phn == ht) {
Jakub Kicinski458e7042018-01-24 12:54:23 -0800613 u32_clear_hw_hnode(tp, ht, extack);
Cong Wange7614372017-09-25 10:13:51 -0700614 idr_destroy(&ht->handle_idr);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500615 idr_remove(&tp_c->handle_idr, ht->handle);
John Fastabend1ce87722014-09-12 20:09:16 -0700616 RCU_INIT_POINTER(*hn, ht->next);
617 kfree_rcu(ht, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
619 }
620 }
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -ENOENT;
623}
624
Cong Wang1e052be2015-03-06 11:47:59 -0800625static bool ht_empty(struct tc_u_hnode *ht)
626{
627 unsigned int h;
628
629 for (h = 0; h <= ht->divisor; h++)
630 if (rcu_access_pointer(ht->ht[h]))
631 return false;
632
633 return true;
634}
635
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800636static void u32_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct tc_u_common *tp_c = tp->data;
John Fastabend1ce87722014-09-12 20:09:16 -0700639 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700641 WARN_ON(root_ht == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (root_ht && --root_ht->refcnt == 0)
Jakub Kicinski458e7042018-01-24 12:54:23 -0800644 u32_destroy_hnode(tp, root_ht, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 if (--tp_c->refcnt == 0) {
647 struct tc_u_hnode *ht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
WANG Cong3cd904e2017-08-24 16:51:30 -0700649 hlist_del(&tp_c->hnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
John Fastabend1ce87722014-09-12 20:09:16 -0700651 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
Paolo Abenid7cdee52018-02-05 22:23:01 +0100652 u32_clear_hnode(tp, ht, extack);
John Fastabend1ce87722014-09-12 20:09:16 -0700653 RCU_INIT_POINTER(tp_c->hlist, ht->next);
Paolo Abenid7cdee52018-02-05 22:23:01 +0100654
655 /* u32_destroy_key() will later free ht for us, if it's
656 * still referenced by some knode
657 */
658 if (--ht->refcnt == 0)
659 kfree_rcu(ht, rcu);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Cong Wange7614372017-09-25 10:13:51 -0700662 idr_destroy(&tp_c->handle_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 kfree(tp_c);
664 }
665
666 tp->data = NULL;
667}
668
Alexander Aring571acf22018-01-18 11:20:53 -0500669static int u32_delete(struct tcf_proto *tp, void *arg, bool *last,
670 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
WANG Cong8113c092017-08-04 21:31:43 -0700672 struct tc_u_hnode *ht = arg;
John Fastabend1ce87722014-09-12 20:09:16 -0700673 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
WANG Cong763dbf62017-04-19 14:21:21 -0700674 struct tc_u_common *tp_c = tp->data;
675 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (ht == NULL)
WANG Cong763dbf62017-04-19 14:21:21 -0700678 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800680 if (TC_U32_KEY(ht->handle)) {
Jakub Kicinski458e7042018-01-24 12:54:23 -0800681 u32_remove_hw_knode(tp, (struct tc_u_knode *)ht, extack);
WANG Cong763dbf62017-04-19 14:21:21 -0700682 ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
683 goto out;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Al Virob44ef842018-10-08 06:22:33 -0400686 if (ht->is_root) {
Alexander Aring4b981db2018-01-18 11:20:55 -0500687 NL_SET_ERR_MSG_MOD(extack, "Not allowed to delete root node");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700691 if (ht->refcnt == 1) {
692 ht->refcnt--;
Jakub Kicinski458e7042018-01-24 12:54:23 -0800693 u32_destroy_hnode(tp, ht, extack);
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700694 } else {
Alexander Aring4b981db2018-01-18 11:20:55 -0500695 NL_SET_ERR_MSG_MOD(extack, "Can not delete in-use filter");
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700696 return -EBUSY;
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
WANG Cong763dbf62017-04-19 14:21:21 -0700699out:
700 *last = true;
701 if (root_ht) {
702 if (root_ht->refcnt > 1) {
703 *last = false;
704 goto ret;
705 }
706 if (root_ht->refcnt == 1) {
707 if (!ht_empty(root_ht)) {
708 *last = false;
709 goto ret;
710 }
711 }
712 }
713
714 if (tp_c->refcnt > 1) {
715 *last = false;
716 goto ret;
717 }
718
719 if (tp_c->refcnt == 1) {
720 struct tc_u_hnode *ht;
721
722 for (ht = rtnl_dereference(tp_c->hlist);
723 ht;
724 ht = rtnl_dereference(ht->next))
725 if (!ht_empty(ht)) {
726 *last = false;
727 break;
728 }
729 }
730
731ret:
732 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Cong Wange7614372017-09-25 10:13:51 -0700735static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Matthew Wilcoxf730cb92017-11-28 13:45:02 -0500737 u32 index = htid | 0x800;
Cong Wange7614372017-09-25 10:13:51 -0700738 u32 max = htid | 0xFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Matthew Wilcoxf730cb92017-11-28 13:45:02 -0500740 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max, GFP_KERNEL)) {
741 index = htid + 1;
742 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max,
743 GFP_KERNEL))
744 index = max;
Cong Wange7614372017-09-25 10:13:51 -0700745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Matthew Wilcoxf730cb92017-11-28 13:45:02 -0500747 return index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800750static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
751 [TCA_U32_CLASSID] = { .type = NLA_U32 },
752 [TCA_U32_HASH] = { .type = NLA_U32 },
753 [TCA_U32_LINK] = { .type = NLA_U32 },
754 [TCA_U32_DIVISOR] = { .type = NLA_U32 },
755 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
756 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
757 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
John Fastabend9e8ce792016-02-26 07:54:39 -0800758 [TCA_U32_FLAGS] = { .type = NLA_U32 },
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800759};
760
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000761static int u32_set_parms(struct net *net, struct tcf_proto *tp,
Al Viro8a8065f2018-10-08 06:22:42 -0400762 unsigned long base,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800763 struct tc_u_knode *n, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500764 struct nlattr *est, bool ovr,
765 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
WANG Congb9a24bb2016-08-19 12:36:54 -0700767 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Alexander Aring50a56192018-01-18 11:20:52 -0500769 err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (err < 0)
771 return err;
772
Patrick McHardyadd93b62008-01-22 22:11:33 -0800773 if (tb[TCA_U32_LINK]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800774 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000775 struct tc_u_hnode *ht_down = NULL, *ht_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Alexander Aring4b981db2018-01-18 11:20:55 -0500777 if (TC_U32_KEY(handle)) {
778 NL_SET_ERR_MSG_MOD(extack, "u32 Link handle must be a hash table");
Jiri Pirko705c7092017-08-04 14:29:14 +0200779 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 if (handle) {
Al Viro8a8065f2018-10-08 06:22:42 -0400783 ht_down = u32_lookup_ht(tp->data, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Alexander Aring4b981db2018-01-18 11:20:55 -0500785 if (!ht_down) {
786 NL_SET_ERR_MSG_MOD(extack, "Link hash table not found");
Jiri Pirko705c7092017-08-04 14:29:14 +0200787 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500788 }
Al Viro27594ec2018-10-08 06:22:34 -0400789 if (ht_down->is_root) {
790 NL_SET_ERR_MSG_MOD(extack, "Not linking to root node");
791 return -EINVAL;
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 ht_down->refcnt++;
794 }
795
John Fastabend1ce87722014-09-12 20:09:16 -0700796 ht_old = rtnl_dereference(n->ht_down);
797 rcu_assign_pointer(n->ht_down, ht_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000799 if (ht_old)
800 ht_old->refcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Patrick McHardyadd93b62008-01-22 22:11:33 -0800802 if (tb[TCA_U32_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800803 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 tcf_bind_filter(tp, &n->res, base);
805 }
806
807#ifdef CONFIG_NET_CLS_IND
Patrick McHardyadd93b62008-01-22 22:11:33 -0800808 if (tb[TCA_U32_INDEV]) {
WANG Cong2519a602014-01-09 16:14:02 -0800809 int ret;
Alexander Aring1057c552018-01-18 11:20:54 -0500810 ret = tcf_change_indev(net, tb[TCA_U32_INDEV], extack);
WANG Cong2519a602014-01-09 16:14:02 -0800811 if (ret < 0)
Jiri Pirko705c7092017-08-04 14:29:14 +0200812 return -EINVAL;
WANG Cong2519a602014-01-09 16:14:02 -0800813 n->ifindex = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400819static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
John Fastabendde5df632014-09-19 21:50:34 -0700820 struct tc_u_knode *n)
821{
822 struct tc_u_knode __rcu **ins;
823 struct tc_u_knode *pins;
824 struct tc_u_hnode *ht;
825
826 if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
827 ht = rtnl_dereference(tp->root);
828 else
829 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
830
831 ins = &ht->ht[TC_U32_HASH(n->handle)];
832
833 /* The node must always exist for it to be replaced if this is not the
834 * case then something went very wrong elsewhere.
835 */
836 for (pins = rtnl_dereference(*ins); ;
837 ins = &pins->next, pins = rtnl_dereference(*ins))
838 if (pins->handle == n->handle)
839 break;
840
Matthew Wilcox234a4622017-11-28 09:56:36 -0500841 idr_replace(&ht->handle_idr, n, n->handle);
John Fastabendde5df632014-09-19 21:50:34 -0700842 RCU_INIT_POINTER(n->next, pins->next);
843 rcu_assign_pointer(*ins, n);
844}
845
846static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
847 struct tc_u_knode *n)
848{
Paolo Abeni058a6c02018-02-02 16:02:22 +0100849 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
John Fastabendde5df632014-09-19 21:50:34 -0700850 struct tc_u32_sel *s = &n->sel;
Paolo Abeni058a6c02018-02-02 16:02:22 +0100851 struct tc_u_knode *new;
John Fastabendde5df632014-09-19 21:50:34 -0700852
853 new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
854 GFP_KERNEL);
855
856 if (!new)
857 return NULL;
858
859 RCU_INIT_POINTER(new->next, n->next);
860 new->handle = n->handle;
861 RCU_INIT_POINTER(new->ht_up, n->ht_up);
862
863#ifdef CONFIG_NET_CLS_IND
864 new->ifindex = n->ifindex;
865#endif
866 new->fshift = n->fshift;
867 new->res = n->res;
John Fastabend9e8ce792016-02-26 07:54:39 -0800868 new->flags = n->flags;
Paolo Abeni058a6c02018-02-02 16:02:22 +0100869 RCU_INIT_POINTER(new->ht_down, ht);
John Fastabendde5df632014-09-19 21:50:34 -0700870
871 /* bump reference count as long as we hold pointer to structure */
Paolo Abeni058a6c02018-02-02 16:02:22 +0100872 if (ht)
873 ht->refcnt++;
John Fastabendde5df632014-09-19 21:50:34 -0700874
875#ifdef CONFIG_CLS_U32_PERF
876 /* Statistics may be incremented by readers during update
877 * so we must keep them in tact. When the node is later destroyed
878 * a special destroy call must be made to not free the pf memory.
879 */
880 new->pf = n->pf;
881#endif
882
883#ifdef CONFIG_CLS_U32_MARK
884 new->val = n->val;
885 new->mask = n->mask;
886 /* Similarly success statistics must be moved as pointers */
887 new->pcpu_success = n->pcpu_success;
888#endif
John Fastabendde5df632014-09-19 21:50:34 -0700889 memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
890
WANG Congb9a24bb2016-08-19 12:36:54 -0700891 if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
892 kfree(new);
893 return NULL;
894 }
John Fastabendde5df632014-09-19 21:50:34 -0700895
896 return new;
897}
898
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000899static int u32_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600900 struct tcf_proto *tp, unsigned long base, u32 handle,
Alexander Aring7306db32018-01-18 11:20:51 -0500901 struct nlattr **tca, void **arg, bool ovr,
902 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct tc_u_common *tp_c = tp->data;
905 struct tc_u_hnode *ht;
906 struct tc_u_knode *n;
907 struct tc_u32_sel *s;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800908 struct nlattr *opt = tca[TCA_OPTIONS];
909 struct nlattr *tb[TCA_U32_MAX + 1];
John Fastabend9e8ce792016-02-26 07:54:39 -0800910 u32 htid, flags = 0;
Kees Cook98c8f122018-08-25 22:58:01 -0700911 size_t sel_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int err;
John Fastabend459d5f62014-09-12 20:08:47 -0700913#ifdef CONFIG_CLS_U32_PERF
914 size_t size;
915#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Alexander Aring4b981db2018-01-18 11:20:55 -0500917 if (!opt) {
918 if (handle) {
919 NL_SET_ERR_MSG_MOD(extack, "Filter handle requires options");
920 return -EINVAL;
921 } else {
922 return 0;
923 }
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Alexander Aring4b981db2018-01-18 11:20:55 -0500926 err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800927 if (err < 0)
928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700930 if (tb[TCA_U32_FLAGS]) {
John Fastabend9e8ce792016-02-26 07:54:39 -0800931 flags = nla_get_u32(tb[TCA_U32_FLAGS]);
Alexander Aring4b981db2018-01-18 11:20:55 -0500932 if (!tc_flags_valid(flags)) {
933 NL_SET_ERR_MSG_MOD(extack, "Invalid filter flags");
Jakub Kicinski1a0f7d22016-06-06 16:16:47 +0100934 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500935 }
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700936 }
John Fastabend9e8ce792016-02-26 07:54:39 -0800937
WANG Cong8113c092017-08-04 21:31:43 -0700938 n = *arg;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000939 if (n) {
John Fastabendde5df632014-09-19 21:50:34 -0700940 struct tc_u_knode *new;
941
Alexander Aring4b981db2018-01-18 11:20:55 -0500942 if (TC_U32_KEY(n->handle) == 0) {
943 NL_SET_ERR_MSG_MOD(extack, "Key node id cannot be zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Ivan Veceraeb53f7a2018-02-08 16:10:39 +0100947 if ((n->flags ^ flags) &
948 ~(TCA_CLS_FLAGS_IN_HW | TCA_CLS_FLAGS_NOT_IN_HW)) {
Alexander Aring4b981db2018-01-18 11:20:55 -0500949 NL_SET_ERR_MSG_MOD(extack, "Key node flags do not match passed flags");
John Fastabend9e8ce792016-02-26 07:54:39 -0800950 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500951 }
John Fastabend9e8ce792016-02-26 07:54:39 -0800952
John Fastabendde5df632014-09-19 21:50:34 -0700953 new = u32_init_knode(tp, n);
954 if (!new)
955 return -ENOMEM;
956
Al Viro8a8065f2018-10-08 06:22:42 -0400957 err = u32_set_parms(net, tp, base, new, tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500958 tca[TCA_RATE], ovr, extack);
John Fastabendde5df632014-09-19 21:50:34 -0700959
960 if (err) {
Al Virodc07c572018-10-08 06:22:36 -0400961 u32_destroy_key(new, false);
John Fastabendde5df632014-09-19 21:50:34 -0700962 return err;
963 }
964
Quentin Monnet10a47e02018-01-19 17:44:45 -0800965 err = u32_replace_hw_knode(tp, new, flags, extack);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700966 if (err) {
Al Virodc07c572018-10-08 06:22:36 -0400967 u32_destroy_key(new, false);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700968 return err;
969 }
970
Or Gerlitz24d3dc62017-02-16 10:31:15 +0200971 if (!tc_in_hw(new->flags))
972 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
973
John Fastabendde5df632014-09-19 21:50:34 -0700974 u32_replace_knode(tp, tp_c, new);
WANG Conga0efb802014-09-30 16:07:24 -0700975 tcf_unbind_filter(tp, &n->res);
Cong Wang35c55fc2017-11-06 13:47:30 -0800976 tcf_exts_get_net(&n->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700977 tcf_queue_work(&n->rwork, u32_delete_key_work);
John Fastabendde5df632014-09-19 21:50:34 -0700978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
Patrick McHardyadd93b62008-01-22 22:11:33 -0800981 if (tb[TCA_U32_DIVISOR]) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000982 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Al Viro2f0c9822018-10-08 06:22:35 -0400984 if (!is_power_of_2(divisor)) {
985 NL_SET_ERR_MSG_MOD(extack, "Divisor is not a power of 2");
986 return -EINVAL;
987 }
988 if (divisor-- > 0x100) {
Alexander Aring4b981db2018-01-18 11:20:55 -0500989 NL_SET_ERR_MSG_MOD(extack, "Exceeded maximum 256 hash buckets");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500991 }
992 if (TC_U32_KEY(handle)) {
993 NL_SET_ERR_MSG_MOD(extack, "Divisor can only be used on a hash table");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500995 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000996 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (ht == NULL)
998 return -ENOBUFS;
Cong Wange7614372017-09-25 10:13:51 -0700999 if (handle == 0) {
1000 handle = gen_new_htid(tp->data, ht);
1001 if (handle == 0) {
1002 kfree(ht);
1003 return -ENOMEM;
1004 }
1005 } else {
Matthew Wilcoxf730cb92017-11-28 13:45:02 -05001006 err = idr_alloc_u32(&tp_c->handle_idr, ht, &handle,
1007 handle, GFP_KERNEL);
Cong Wange7614372017-09-25 10:13:51 -07001008 if (err) {
1009 kfree(ht);
1010 return err;
1011 }
1012 }
Jarek Poplawskie56cfad2008-04-12 18:37:13 -07001013 ht->refcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 ht->divisor = divisor;
1015 ht->handle = handle;
1016 ht->prio = tp->prio;
Cong Wange7614372017-09-25 10:13:51 -07001017 idr_init(&ht->handle_idr);
Jakub Kicinskif40fe582018-01-24 12:54:22 -08001018 ht->flags = flags;
Jakub Kicinski6eef3802016-06-08 20:11:03 +01001019
Quentin Monnet10a47e02018-01-19 17:44:45 -08001020 err = u32_replace_hw_hnode(tp, ht, flags, extack);
Jakub Kicinski6eef3802016-06-08 20:11:03 +01001021 if (err) {
Matthew Wilcox9c160942017-11-28 09:48:43 -05001022 idr_remove(&tp_c->handle_idr, handle);
Jakub Kicinski6eef3802016-06-08 20:11:03 +01001023 kfree(ht);
1024 return err;
1025 }
1026
John Fastabend1ce87722014-09-12 20:09:16 -07001027 RCU_INIT_POINTER(ht->next, tp_c->hlist);
1028 rcu_assign_pointer(tp_c->hlist, ht);
WANG Cong8113c092017-08-04 21:31:43 -07001029 *arg = ht;
John Fastabenda1b7c5f2016-02-16 21:17:09 -08001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return 0;
1032 }
1033
Patrick McHardyadd93b62008-01-22 22:11:33 -08001034 if (tb[TCA_U32_HASH]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -08001035 htid = nla_get_u32(tb[TCA_U32_HASH]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
John Fastabend1ce87722014-09-12 20:09:16 -07001037 ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 htid = ht->handle;
1039 } else {
1040 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
Alexander Aring4b981db2018-01-18 11:20:55 -05001041 if (!ht) {
1042 NL_SET_ERR_MSG_MOD(extack, "Specified hash table not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -05001044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046 } else {
John Fastabend1ce87722014-09-12 20:09:16 -07001047 ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 htid = ht->handle;
1049 }
1050
Alexander Aring4b981db2018-01-18 11:20:55 -05001051 if (ht->divisor < TC_U32_HASH(htid)) {
1052 NL_SET_ERR_MSG_MOD(extack, "Specified hash table buckets exceed configured value");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -05001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (handle) {
Alexander Aring4b981db2018-01-18 11:20:55 -05001057 if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) {
1058 NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -05001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 handle = htid | TC_U32_NODE(handle);
Matthew Wilcoxf730cb92017-11-28 13:45:02 -05001062 err = idr_alloc_u32(&ht->handle_idr, NULL, &handle, handle,
Cong Wange7614372017-09-25 10:13:51 -07001063 GFP_KERNEL);
1064 if (err)
1065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 } else
1067 handle = gen_new_kid(ht, htid);
1068
Cong Wange7614372017-09-25 10:13:51 -07001069 if (tb[TCA_U32_SEL] == NULL) {
Alexander Aring4b981db2018-01-18 11:20:55 -05001070 NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
Cong Wange7614372017-09-25 10:13:51 -07001071 err = -EINVAL;
1072 goto erridr;
1073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Patrick McHardyadd93b62008-01-22 22:11:33 -08001075 s = nla_data(tb[TCA_U32_SEL]);
Kees Cook98c8f122018-08-25 22:58:01 -07001076 sel_size = struct_size(s, keys, s->nkeys);
1077 if (nla_len(tb[TCA_U32_SEL]) < sel_size) {
1078 err = -EINVAL;
1079 goto erridr;
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Kees Cook98c8f122018-08-25 22:58:01 -07001082 n = kzalloc(offsetof(typeof(*n), sel) + sel_size, GFP_KERNEL);
Cong Wange7614372017-09-25 10:13:51 -07001083 if (n == NULL) {
1084 err = -ENOBUFS;
1085 goto erridr;
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -07001089 size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1090 n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1091 if (!n->pf) {
Cong Wange7614372017-09-25 10:13:51 -07001092 err = -ENOBUFS;
1093 goto errfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095#endif
1096
Kees Cook98c8f122018-08-25 22:58:01 -07001097 memcpy(&n->sel, s, sel_size);
John Fastabenda96366bf2014-09-15 23:30:49 -07001098 RCU_INIT_POINTER(n->ht_up, ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 n->handle = handle;
Radu Rendecb2268012007-11-10 21:54:50 -08001100 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
John Fastabend9e8ce792016-02-26 07:54:39 -08001101 n->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
WANG Congb9a24bb2016-08-19 12:36:54 -07001103 err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
1104 if (err < 0)
1105 goto errout;
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -07001108 n->pcpu_success = alloc_percpu(u32);
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001109 if (!n->pcpu_success) {
1110 err = -ENOMEM;
1111 goto errout;
1112 }
John Fastabend459d5f62014-09-12 20:08:47 -07001113
Patrick McHardyadd93b62008-01-22 22:11:33 -08001114 if (tb[TCA_U32_MARK]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct tc_u32_mark *mark;
1116
Patrick McHardyadd93b62008-01-22 22:11:33 -08001117 mark = nla_data(tb[TCA_U32_MARK]);
John Fastabend459d5f62014-09-12 20:08:47 -07001118 n->val = mark->val;
1119 n->mask = mark->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121#endif
1122
Al Viro8a8065f2018-10-08 06:22:42 -04001123 err = u32_set_parms(net, tp, base, n, tb, tca[TCA_RATE], ovr,
Alexander Aring50a56192018-01-18 11:20:52 -05001124 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (err == 0) {
John Fastabend1ce87722014-09-12 20:09:16 -07001126 struct tc_u_knode __rcu **ins;
1127 struct tc_u_knode *pins;
1128
Quentin Monnet10a47e02018-01-19 17:44:45 -08001129 err = u32_replace_hw_knode(tp, n, flags, extack);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -07001130 if (err)
1131 goto errhw;
1132
Or Gerlitz24d3dc62017-02-16 10:31:15 +02001133 if (!tc_in_hw(n->flags))
1134 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1135
John Fastabend1ce87722014-09-12 20:09:16 -07001136 ins = &ht->ht[TC_U32_HASH(handle)];
1137 for (pins = rtnl_dereference(*ins); pins;
1138 ins = &pins->next, pins = rtnl_dereference(*ins))
1139 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 break;
1141
John Fastabend1ce87722014-09-12 20:09:16 -07001142 RCU_INIT_POINTER(n->next, pins);
1143 rcu_assign_pointer(*ins, n);
WANG Cong8113c092017-08-04 21:31:43 -07001144 *arg = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
1146 }
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001147
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -07001148errhw:
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001149#ifdef CONFIG_CLS_U32_MARK
1150 free_percpu(n->pcpu_success);
1151#endif
1152
WANG Congb9a24bb2016-08-19 12:36:54 -07001153errout:
1154 tcf_exts_destroy(&n->exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155#ifdef CONFIG_CLS_U32_PERF
Cong Wange7614372017-09-25 10:13:51 -07001156errfree:
John Fastabend1ce87722014-09-12 20:09:16 -07001157 free_percpu(n->pf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158#endif
1159 kfree(n);
Cong Wange7614372017-09-25 10:13:51 -07001160erridr:
Matthew Wilcox9c160942017-11-28 09:48:43 -05001161 idr_remove(&ht->handle_idr, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return err;
1163}
1164
1165static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1166{
1167 struct tc_u_common *tp_c = tp->data;
1168 struct tc_u_hnode *ht;
1169 struct tc_u_knode *n;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001170 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 if (arg->stop)
1173 return;
1174
John Fastabend1ce87722014-09-12 20:09:16 -07001175 for (ht = rtnl_dereference(tp_c->hlist);
1176 ht;
1177 ht = rtnl_dereference(ht->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (ht->prio != tp->prio)
1179 continue;
1180 if (arg->count >= arg->skip) {
WANG Cong8113c092017-08-04 21:31:43 -07001181 if (arg->fn(tp, ht, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 arg->stop = 1;
1183 return;
1184 }
1185 }
1186 arg->count++;
1187 for (h = 0; h <= ht->divisor; h++) {
John Fastabend1ce87722014-09-12 20:09:16 -07001188 for (n = rtnl_dereference(ht->ht[h]);
1189 n;
1190 n = rtnl_dereference(n->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (arg->count < arg->skip) {
1192 arg->count++;
1193 continue;
1194 }
WANG Cong8113c092017-08-04 21:31:43 -07001195 if (arg->fn(tp, n, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 arg->stop = 1;
1197 return;
1198 }
1199 arg->count++;
1200 }
1201 }
1202 }
1203}
1204
John Hurley530d9952018-06-25 14:30:08 -07001205static int u32_reoffload_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
1206 bool add, tc_setup_cb_t *cb, void *cb_priv,
1207 struct netlink_ext_ack *extack)
1208{
1209 struct tc_cls_u32_offload cls_u32 = {};
1210 int err;
1211
1212 tc_cls_common_offload_init(&cls_u32.common, tp, ht->flags, extack);
1213 cls_u32.command = add ? TC_CLSU32_NEW_HNODE : TC_CLSU32_DELETE_HNODE;
1214 cls_u32.hnode.divisor = ht->divisor;
1215 cls_u32.hnode.handle = ht->handle;
1216 cls_u32.hnode.prio = ht->prio;
1217
1218 err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
1219 if (err && add && tc_skip_sw(ht->flags))
1220 return err;
1221
1222 return 0;
1223}
1224
1225static int u32_reoffload_knode(struct tcf_proto *tp, struct tc_u_knode *n,
1226 bool add, tc_setup_cb_t *cb, void *cb_priv,
1227 struct netlink_ext_ack *extack)
1228{
1229 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
1230 struct tcf_block *block = tp->chain->block;
1231 struct tc_cls_u32_offload cls_u32 = {};
1232 int err;
1233
1234 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
1235 cls_u32.command = add ?
1236 TC_CLSU32_REPLACE_KNODE : TC_CLSU32_DELETE_KNODE;
1237 cls_u32.knode.handle = n->handle;
1238
1239 if (add) {
1240 cls_u32.knode.fshift = n->fshift;
1241#ifdef CONFIG_CLS_U32_MARK
1242 cls_u32.knode.val = n->val;
1243 cls_u32.knode.mask = n->mask;
1244#else
1245 cls_u32.knode.val = 0;
1246 cls_u32.knode.mask = 0;
1247#endif
1248 cls_u32.knode.sel = &n->sel;
1249 cls_u32.knode.exts = &n->exts;
1250 if (n->ht_down)
1251 cls_u32.knode.link_handle = ht->handle;
1252 }
1253
1254 err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
1255 if (err) {
1256 if (add && tc_skip_sw(n->flags))
1257 return err;
1258 return 0;
1259 }
1260
1261 tc_cls_offload_cnt_update(block, &n->in_hw_count, &n->flags, add);
1262
1263 return 0;
1264}
1265
1266static int u32_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1267 void *cb_priv, struct netlink_ext_ack *extack)
1268{
1269 struct tc_u_common *tp_c = tp->data;
1270 struct tc_u_hnode *ht;
1271 struct tc_u_knode *n;
1272 unsigned int h;
1273 int err;
1274
1275 for (ht = rtnl_dereference(tp_c->hlist);
1276 ht;
1277 ht = rtnl_dereference(ht->next)) {
1278 if (ht->prio != tp->prio)
1279 continue;
1280
1281 /* When adding filters to a new dev, try to offload the
1282 * hashtable first. When removing, do the filters before the
1283 * hashtable.
1284 */
1285 if (add && !tc_skip_hw(ht->flags)) {
1286 err = u32_reoffload_hnode(tp, ht, add, cb, cb_priv,
1287 extack);
1288 if (err)
1289 return err;
1290 }
1291
1292 for (h = 0; h <= ht->divisor; h++) {
1293 for (n = rtnl_dereference(ht->ht[h]);
1294 n;
1295 n = rtnl_dereference(n->next)) {
1296 if (tc_skip_hw(n->flags))
1297 continue;
1298
1299 err = u32_reoffload_knode(tp, n, add, cb,
1300 cb_priv, extack);
1301 if (err)
1302 return err;
1303 }
1304 }
1305
1306 if (!add && !tc_skip_hw(ht->flags))
1307 u32_reoffload_hnode(tp, ht, add, cb, cb_priv, extack);
1308 }
1309
1310 return 0;
1311}
1312
Cong Wang07d79fc2017-08-30 14:30:36 -07001313static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
1314{
1315 struct tc_u_knode *n = fh;
1316
1317 if (n && n->res.classid == classid)
1318 n->res.class = cl;
1319}
1320
WANG Cong8113c092017-08-04 21:31:43 -07001321static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001322 struct sk_buff *skb, struct tcmsg *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
WANG Cong8113c092017-08-04 21:31:43 -07001324 struct tc_u_knode *n = fh;
John Fastabend1ce87722014-09-12 20:09:16 -07001325 struct tc_u_hnode *ht_up, *ht_down;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001326 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (n == NULL)
1329 return skb->len;
1330
1331 t->tcm_handle = n->handle;
1332
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001333 nest = nla_nest_start(skb, TCA_OPTIONS);
1334 if (nest == NULL)
1335 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (TC_U32_KEY(n->handle) == 0) {
WANG Cong8113c092017-08-04 21:31:43 -07001338 struct tc_u_hnode *ht = fh;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001339 u32 divisor = ht->divisor + 1;
1340
David S. Miller1b34ec42012-03-29 05:11:39 -04001341 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1342 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 } else {
John Fastabend459d5f62014-09-12 20:08:47 -07001344#ifdef CONFIG_CLS_U32_PERF
1345 struct tc_u32_pcnt *gpf;
John Fastabend459d5f62014-09-12 20:08:47 -07001346 int cpu;
John Fastabend80aab732014-09-15 23:30:26 -07001347#endif
John Fastabend459d5f62014-09-12 20:08:47 -07001348
David S. Miller1b34ec42012-03-29 05:11:39 -04001349 if (nla_put(skb, TCA_U32_SEL,
1350 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1351 &n->sel))
1352 goto nla_put_failure;
John Fastabend1ce87722014-09-12 20:09:16 -07001353
1354 ht_up = rtnl_dereference(n->ht_up);
1355 if (ht_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 u32 htid = n->handle & 0xFFFFF000;
David S. Miller1b34ec42012-03-29 05:11:39 -04001357 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1358 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
David S. Miller1b34ec42012-03-29 05:11:39 -04001360 if (n->res.classid &&
1361 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1362 goto nla_put_failure;
John Fastabend1ce87722014-09-12 20:09:16 -07001363
1364 ht_down = rtnl_dereference(n->ht_down);
1365 if (ht_down &&
1366 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
David S. Miller1b34ec42012-03-29 05:11:39 -04001367 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
John Fastabend9e8ce792016-02-26 07:54:39 -08001369 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1370 goto nla_put_failure;
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -07001373 if ((n->val || n->mask)) {
1374 struct tc_u32_mark mark = {.val = n->val,
1375 .mask = n->mask,
1376 .success = 0};
John Fastabend80aab732014-09-15 23:30:26 -07001377 int cpum;
John Fastabend459d5f62014-09-12 20:08:47 -07001378
John Fastabend80aab732014-09-15 23:30:26 -07001379 for_each_possible_cpu(cpum) {
1380 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
John Fastabend459d5f62014-09-12 20:08:47 -07001381
1382 mark.success += cnt;
1383 }
1384
1385 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1386 goto nla_put_failure;
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388#endif
1389
WANG Cong5da57f42013-12-15 20:15:07 -08001390 if (tcf_exts_dump(skb, &n->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001391 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -08001394 if (n->ifindex) {
1395 struct net_device *dev;
1396 dev = __dev_get_by_index(net, n->ifindex);
1397 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1398 goto nla_put_failure;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400#endif
1401#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -07001402 gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1403 n->sel.nkeys * sizeof(u64),
1404 GFP_KERNEL);
1405 if (!gpf)
1406 goto nla_put_failure;
1407
1408 for_each_possible_cpu(cpu) {
1409 int i;
1410 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1411
1412 gpf->rcnt += pf->rcnt;
1413 gpf->rhit += pf->rhit;
1414 for (i = 0; i < n->sel.nkeys; i++)
1415 gpf->kcnts[i] += pf->kcnts[i];
1416 }
1417
Nicolas Dichtel98545182016-04-26 10:06:18 +02001418 if (nla_put_64bit(skb, TCA_U32_PCNT,
1419 sizeof(struct tc_u32_pcnt) +
1420 n->sel.nkeys * sizeof(u64),
1421 gpf, TCA_U32_PAD)) {
John Fastabend459d5f62014-09-12 20:08:47 -07001422 kfree(gpf);
David S. Miller1b34ec42012-03-29 05:11:39 -04001423 goto nla_put_failure;
John Fastabend459d5f62014-09-12 20:08:47 -07001424 }
1425 kfree(gpf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426#endif
1427 }
1428
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001429 nla_nest_end(skb, nest);
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (TC_U32_KEY(n->handle))
WANG Cong5da57f42013-12-15 20:15:07 -08001432 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001433 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return skb->len;
1435
Patrick McHardyadd93b62008-01-22 22:11:33 -08001436nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001437 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 return -1;
1439}
1440
Patrick McHardy2eb9d752008-01-22 22:10:42 -08001441static struct tcf_proto_ops cls_u32_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 .kind = "u32",
1443 .classify = u32_classify,
1444 .init = u32_init,
1445 .destroy = u32_destroy,
1446 .get = u32_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 .change = u32_change,
1448 .delete = u32_delete,
1449 .walk = u32_walk,
John Hurley530d9952018-06-25 14:30:08 -07001450 .reoffload = u32_reoffload,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 .dump = u32_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001452 .bind_class = u32_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 .owner = THIS_MODULE,
1454};
1455
1456static int __init init_u32(void)
1457{
WANG Cong3cd904e2017-08-24 16:51:30 -07001458 int i, ret;
1459
stephen hemminger6ff9c362010-05-12 06:37:05 +00001460 pr_info("u32 classifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461#ifdef CONFIG_CLS_U32_PERF
stephen hemminger6ff9c362010-05-12 06:37:05 +00001462 pr_info(" Performance counters on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464#ifdef CONFIG_NET_CLS_IND
stephen hemminger6ff9c362010-05-12 06:37:05 +00001465 pr_info(" input device check on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466#endif
1467#ifdef CONFIG_NET_CLS_ACT
stephen hemminger6ff9c362010-05-12 06:37:05 +00001468 pr_info(" Actions configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469#endif
WANG Cong3cd904e2017-08-24 16:51:30 -07001470 tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
1471 sizeof(struct hlist_head),
1472 GFP_KERNEL);
1473 if (!tc_u_common_hash)
1474 return -ENOMEM;
1475
1476 for (i = 0; i < U32_HASH_SIZE; i++)
1477 INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1478
1479 ret = register_tcf_proto_ops(&cls_u32_ops);
1480 if (ret)
1481 kvfree(tc_u_common_hash);
1482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001485static void __exit exit_u32(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 unregister_tcf_proto_ops(&cls_u32_ops);
WANG Cong3cd904e2017-08-24 16:51:30 -07001488 kvfree(tc_u_common_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491module_init(init_u32)
1492module_exit(exit_u32)
1493MODULE_LICENSE("GPL");