blob: 61593bee08db2a0f575cf76cae37905c3acf58bc [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 Fastabend1ce877202014-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 Fastabend1ce877202014-09-12 20:09:16 -070051 struct tc_u_knode __rcu *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 u32 handle;
John Fastabend1ce877202014-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 Fastabend1ce877202014-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 Fastabend1ce877202014-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 Fastabend1ce877202014-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 Fastabend1ce877202014-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;
Al Virob245d322018-10-08 06:22:43 -0400100 long knodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000103static inline unsigned int u32_hash_fold(__be32 key,
104 const struct tc_u32_sel *sel,
105 u8 fshift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000107 unsigned int h = ntohl(key & sel->hmask) >> fshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 return h;
110}
111
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400112static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp,
113 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct {
116 struct tc_u_knode *knode;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700117 unsigned int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 } stack[TC_U32_MAXDEPTH];
119
John Fastabend1ce877202014-09-12 20:09:16 -0700120 struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700121 unsigned int off = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct tc_u_knode *n;
123 int sdepth = 0;
124 int off2 = 0;
125 int sel = 0;
126#ifdef CONFIG_CLS_U32_PERF
127 int j;
128#endif
129 int i, r;
130
131next_ht:
John Fastabend1ce877202014-09-12 20:09:16 -0700132 n = rcu_dereference_bh(ht->ht[sel]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134next_knode:
135 if (n) {
136 struct tc_u32_key *key = n->sel.keys;
137
138#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -0700139 __this_cpu_inc(n->pf->rcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 j = 0;
141#endif
142
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700143 if (tc_skip_sw(n->flags)) {
144 n = rcu_dereference_bh(n->next);
145 goto next_knode;
146 }
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -0700149 if ((skb->mark & n->mask) != n->val) {
John Fastabend1ce877202014-09-12 20:09:16 -0700150 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto next_knode;
152 } else {
John Fastabend459d5f62014-09-12 20:08:47 -0700153 __this_cpu_inc(*n->pcpu_success);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155#endif
156
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000157 for (i = n->sel.nkeys; i > 0; i--, key++) {
stephen hemminger66d50d22010-08-02 13:44:13 +0000158 int toff = off + key->off + (off2 & key->offmask);
stephen hemminger86fce3b2011-02-20 16:14:23 +0000159 __be32 *data, hdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Dan Carpenter4e18b3e2010-10-04 02:28:36 +0000161 if (skb_headroom(skb) + toff > INT_MAX)
stephen hemminger66d50d22010-08-02 13:44:13 +0000162 goto out;
163
stephen hemminger86fce3b2011-02-20 16:14:23 +0000164 data = skb_header_pointer(skb, toff, 4, &hdata);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700165 if (!data)
166 goto out;
167 if ((*data ^ key->val) & key->mask) {
John Fastabend1ce877202014-09-12 20:09:16 -0700168 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto next_knode;
170 }
171#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -0700172 __this_cpu_inc(n->pf->kcnts[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 j++;
174#endif
175 }
John Fastabend1ce877202014-09-12 20:09:16 -0700176
177 ht = rcu_dereference_bh(n->ht_down);
178 if (!ht) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179check_terminal:
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000180 if (n->sel.flags & TC_U32_TERMINAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 *res = n->res;
183#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -0800184 if (!tcf_match_indev(skb, n->ifindex)) {
John Fastabend1ce877202014-09-12 20:09:16 -0700185 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto next_knode;
187 }
188#endif
189#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -0700190 __this_cpu_inc(n->pf->rhit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif
192 r = tcf_exts_exec(skb, &n->exts, res);
193 if (r < 0) {
John Fastabend1ce877202014-09-12 20:09:16 -0700194 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto next_knode;
196 }
197
198 return r;
199 }
John Fastabend1ce877202014-09-12 20:09:16 -0700200 n = rcu_dereference_bh(n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto next_knode;
202 }
203
204 /* PUSH */
205 if (sdepth >= TC_U32_MAXDEPTH)
206 goto deadloop;
207 stack[sdepth].knode = n;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700208 stack[sdepth].off = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 sdepth++;
210
John Fastabend1ce877202014-09-12 20:09:16 -0700211 ht = rcu_dereference_bh(n->ht_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 sel = 0;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700213 if (ht->divisor) {
stephen hemminger86fce3b2011-02-20 16:14:23 +0000214 __be32 *data, hdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700216 data = skb_header_pointer(skb, off + n->sel.hoff, 4,
stephen hemminger86fce3b2011-02-20 16:14:23 +0000217 &hdata);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700218 if (!data)
219 goto out;
220 sel = ht->divisor & u32_hash_fold(*data, &n->sel,
221 n->fshift);
222 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000223 if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto next_ht;
225
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000226 if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 off2 = n->sel.off + 3;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700228 if (n->sel.flags & TC_U32_VAROFFSET) {
stephen hemminger86fce3b2011-02-20 16:14:23 +0000229 __be16 *data, hdata;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700230
231 data = skb_header_pointer(skb,
232 off + n->sel.offoff,
stephen hemminger86fce3b2011-02-20 16:14:23 +0000233 2, &hdata);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700234 if (!data)
235 goto out;
236 off2 += ntohs(n->sel.offmask & *data) >>
237 n->sel.offshift;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 off2 &= ~3;
240 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000241 if (n->sel.flags & TC_U32_EAT) {
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700242 off += off2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 off2 = 0;
244 }
245
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700246 if (off < skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto next_ht;
248 }
249
250 /* POP */
251 if (sdepth--) {
252 n = stack[sdepth].knode;
John Fastabend1ce877202014-09-12 20:09:16 -0700253 ht = rcu_dereference_bh(n->ht_up);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700254 off = stack[sdepth].off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 goto check_terminal;
256 }
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700257out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return -1;
259
260deadloop:
Joe Perchese87cc472012-05-13 21:56:26 +0000261 net_warn_ratelimited("cls_u32: dead loop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -1;
263}
264
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400265static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct tc_u_hnode *ht;
268
John Fastabend1ce877202014-09-12 20:09:16 -0700269 for (ht = rtnl_dereference(tp_c->hlist);
270 ht;
271 ht = rtnl_dereference(ht->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (ht->handle == handle)
273 break;
274
275 return ht;
276}
277
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400278static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000280 unsigned int sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 struct tc_u_knode *n = NULL;
282
283 sel = TC_U32_HASH(handle);
284 if (sel > ht->divisor)
285 goto out;
286
John Fastabend1ce877202014-09-12 20:09:16 -0700287 for (n = rtnl_dereference(ht->ht[sel]);
288 n;
289 n = rtnl_dereference(n->next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (n->handle == handle)
291 break;
292out:
293 return n;
294}
295
296
WANG Cong8113c092017-08-04 21:31:43 -0700297static void *u32_get(struct tcf_proto *tp, u32 handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct tc_u_hnode *ht;
300 struct tc_u_common *tp_c = tp->data;
301
302 if (TC_U32_HTID(handle) == TC_U32_ROOT)
John Fastabend1ce877202014-09-12 20:09:16 -0700303 ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 else
305 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
306
307 if (!ht)
WANG Cong8113c092017-08-04 21:31:43 -0700308 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 if (TC_U32_KEY(handle) == 0)
WANG Cong8113c092017-08-04 21:31:43 -0700311 return ht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
WANG Cong8113c092017-08-04 21:31:43 -0700313 return u32_lookup_key(ht, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Matthew Wilcoxffdc2d92017-11-28 12:05:54 -0500316/* Protected by rtnl lock */
Cong Wange7614372017-09-25 10:13:51 -0700317static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Matthew Wilcoxffdc2d92017-11-28 12:05:54 -0500319 int id = idr_alloc_cyclic(&tp_c->handle_idr, ptr, 1, 0x7FF, GFP_KERNEL);
320 if (id < 0)
Cong Wange7614372017-09-25 10:13:51 -0700321 return 0;
Matthew Wilcoxffdc2d92017-11-28 12:05:54 -0500322 return (id | 0x800U) << 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
WANG Cong3cd904e2017-08-24 16:51:30 -0700325static struct hlist_head *tc_u_common_hash;
326
327#define U32_HASH_SHIFT 10
328#define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
329
Jiri Pirko339c21d2018-02-13 12:00:17 +0100330static void *tc_u_common_ptr(const struct tcf_proto *tp)
331{
332 struct tcf_block *block = tp->chain->block;
333
334 /* The block sharing is currently supported only
335 * for classless qdiscs. In that case we use block
336 * for tc_u_common identification. In case the
337 * block is not shared, block->q is a valid pointer
338 * and we can use that. That works for classful qdiscs.
339 */
340 if (tcf_block_shared(block))
341 return block;
342 else
343 return block->q;
344}
345
Al Viro4895c422018-10-08 06:22:39 -0400346static struct hlist_head *tc_u_hash(void *key)
WANG Cong3cd904e2017-08-24 16:51:30 -0700347{
Al Viro4895c422018-10-08 06:22:39 -0400348 return tc_u_common_hash + hash_ptr(key, U32_HASH_SHIFT);
WANG Cong3cd904e2017-08-24 16:51:30 -0700349}
350
Al Viro4895c422018-10-08 06:22:39 -0400351static struct tc_u_common *tc_u_common_find(void *key)
WANG Cong3cd904e2017-08-24 16:51:30 -0700352{
353 struct tc_u_common *tc;
Al Viro4895c422018-10-08 06:22:39 -0400354 hlist_for_each_entry(tc, tc_u_hash(key), hnode) {
355 if (tc->ptr == key)
WANG Cong3cd904e2017-08-24 16:51:30 -0700356 return tc;
357 }
358 return NULL;
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static int u32_init(struct tcf_proto *tp)
362{
363 struct tc_u_hnode *root_ht;
Al Viro4895c422018-10-08 06:22:39 -0400364 void *key = tc_u_common_ptr(tp);
365 struct tc_u_common *tp_c = tc_u_common_find(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700367 root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (root_ht == NULL)
369 return -ENOBUFS;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 root_ht->refcnt++;
Cong Wange7614372017-09-25 10:13:51 -0700372 root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 root_ht->prio = tp->prio;
Al Virob44ef842018-10-08 06:22:33 -0400374 root_ht->is_root = true;
Cong Wange7614372017-09-25 10:13:51 -0700375 idr_init(&root_ht->handle_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (tp_c == NULL) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700378 tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (tp_c == NULL) {
380 kfree(root_ht);
381 return -ENOBUFS;
382 }
Al Viro4895c422018-10-08 06:22:39 -0400383 tp_c->ptr = key;
WANG Cong3cd904e2017-08-24 16:51:30 -0700384 INIT_HLIST_NODE(&tp_c->hnode);
Cong Wange7614372017-09-25 10:13:51 -0700385 idr_init(&tp_c->handle_idr);
WANG Cong3cd904e2017-08-24 16:51:30 -0700386
Al Viro4895c422018-10-08 06:22:39 -0400387 hlist_add_head(&tp_c->hnode, tc_u_hash(key));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
390 tp_c->refcnt++;
John Fastabend1ce877202014-09-12 20:09:16 -0700391 RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
392 rcu_assign_pointer(tp_c->hlist, root_ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
John Fastabend1ce877202014-09-12 20:09:16 -0700394 rcu_assign_pointer(tp->root, root_ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 tp->data = tp_c;
396 return 0;
397}
398
Al Virodc07c572018-10-08 06:22:36 -0400399static int u32_destroy_key(struct tc_u_knode *n, bool free_pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Paolo Abenid7cdee52018-02-05 22:23:01 +0100401 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
402
WANG Cong18d02642014-09-25 10:26:37 -0700403 tcf_exts_destroy(&n->exts);
Cong Wang35c55fc2017-11-06 13:47:30 -0800404 tcf_exts_put_net(&n->exts);
Paolo Abenid7cdee52018-02-05 22:23:01 +0100405 if (ht && --ht->refcnt == 0)
406 kfree(ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#ifdef CONFIG_CLS_U32_PERF
John Fastabendde5df632014-09-19 21:50:34 -0700408 if (free_pf)
409 free_percpu(n->pf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410#endif
John Fastabenda1ddcfe2014-09-19 21:50:04 -0700411#ifdef CONFIG_CLS_U32_MARK
John Fastabendde5df632014-09-19 21:50:34 -0700412 if (free_pf)
413 free_percpu(n->pcpu_success);
John Fastabenda1ddcfe2014-09-19 21:50:04 -0700414#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 kfree(n);
416 return 0;
417}
418
John Fastabendde5df632014-09-19 21:50:34 -0700419/* u32_delete_key_rcu should be called when free'ing a copied
420 * version of a tc_u_knode obtained from u32_init_knode(). When
421 * copies are obtained from u32_init_knode() the statistics are
422 * shared between the old and new copies to allow readers to
423 * continue to update the statistics during the copy. To support
424 * this the u32_delete_key_rcu variant does not free the percpu
425 * statistics.
426 */
Cong Wangc0d378e2017-10-26 18:24:36 -0700427static void u32_delete_key_work(struct work_struct *work)
428{
Cong Wangaaa908f2018-05-23 15:26:53 -0700429 struct tc_u_knode *key = container_of(to_rcu_work(work),
430 struct tc_u_knode,
431 rwork);
Cong Wangc0d378e2017-10-26 18:24:36 -0700432 rtnl_lock();
Al Virodc07c572018-10-08 06:22:36 -0400433 u32_destroy_key(key, false);
Cong Wangc0d378e2017-10-26 18:24:36 -0700434 rtnl_unlock();
435}
436
John Fastabendde5df632014-09-19 21:50:34 -0700437/* u32_delete_key_freepf_rcu is the rcu callback variant
438 * that free's the entire structure including the statistics
439 * percpu variables. Only use this if the key is not a copy
440 * returned by u32_init_knode(). See u32_delete_key_rcu()
441 * for the variant that should be used with keys return from
442 * u32_init_knode()
443 */
Cong Wangc0d378e2017-10-26 18:24:36 -0700444static void u32_delete_key_freepf_work(struct work_struct *work)
445{
Cong Wangaaa908f2018-05-23 15:26:53 -0700446 struct tc_u_knode *key = container_of(to_rcu_work(work),
447 struct tc_u_knode,
448 rwork);
Cong Wangc0d378e2017-10-26 18:24:36 -0700449 rtnl_lock();
Al Virodc07c572018-10-08 06:22:36 -0400450 u32_destroy_key(key, true);
Cong Wangc0d378e2017-10-26 18:24:36 -0700451 rtnl_unlock();
452}
453
Yang Yingliang82d567c2013-12-10 20:55:31 +0800454static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Al Virob245d322018-10-08 06:22:43 -0400456 struct tc_u_common *tp_c = tp->data;
John Fastabend1ce877202014-09-12 20:09:16 -0700457 struct tc_u_knode __rcu **kp;
458 struct tc_u_knode *pkp;
John Fastabenda96366bf2014-09-15 23:30:49 -0700459 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (ht) {
John Fastabend1ce877202014-09-12 20:09:16 -0700462 kp = &ht->ht[TC_U32_HASH(key->handle)];
463 for (pkp = rtnl_dereference(*kp); pkp;
464 kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
465 if (pkp == key) {
466 RCU_INIT_POINTER(*kp, key->next);
Al Virob245d322018-10-08 06:22:43 -0400467 tp_c->knodes--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
WANG Conga0efb802014-09-30 16:07:24 -0700469 tcf_unbind_filter(tp, &key->res);
Cong Wangf12c6432018-04-06 17:19:41 -0700470 idr_remove(&ht->handle_idr, key->handle);
Cong Wang35c55fc2017-11-06 13:47:30 -0800471 tcf_exts_get_net(&key->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700472 tcf_queue_work(&key->rwork, u32_delete_key_freepf_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return 0;
474 }
475 }
476 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700477 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 0;
479}
480
Jakub Kicinski458e7042018-01-24 12:54:23 -0800481static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
482 struct netlink_ext_ack *extack)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800483{
Jiri Pirko245dc512017-10-19 15:50:35 +0200484 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200485 struct tc_cls_u32_offload cls_u32 = {};
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800486
Jakub Kicinski458e7042018-01-24 12:54:23 -0800487 tc_cls_common_offload_init(&cls_u32.common, tp, h->flags, extack);
Jiri Pirko77460412017-10-19 15:50:34 +0200488 cls_u32.command = TC_CLSU32_DELETE_HNODE;
489 cls_u32.hnode.divisor = h->divisor;
490 cls_u32.hnode.handle = h->handle;
491 cls_u32.hnode.prio = h->prio;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200492
Jiri Pirko245dc512017-10-19 15:50:35 +0200493 tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800494}
495
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400496static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
Quentin Monnet10a47e02018-01-19 17:44:45 -0800497 u32 flags, struct netlink_ext_ack *extack)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800498{
Jiri Pirko245dc512017-10-19 15:50:35 +0200499 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200500 struct tc_cls_u32_offload cls_u32 = {};
Jiri Pirko245dc512017-10-19 15:50:35 +0200501 bool skip_sw = tc_skip_sw(flags);
502 bool offloaded = false;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700503 int err;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800504
Jakub Kicinskif40fe582018-01-24 12:54:22 -0800505 tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200506 cls_u32.command = TC_CLSU32_NEW_HNODE;
507 cls_u32.hnode.divisor = h->divisor;
508 cls_u32.hnode.handle = h->handle;
509 cls_u32.hnode.prio = h->prio;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800510
Jiri Pirko245dc512017-10-19 15:50:35 +0200511 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
512 if (err < 0) {
Jakub Kicinski458e7042018-01-24 12:54:23 -0800513 u32_clear_hw_hnode(tp, h, NULL);
Jakub Kicinskid47a0f32016-06-06 16:16:48 +0100514 return err;
Jiri Pirko245dc512017-10-19 15:50:35 +0200515 } else if (err > 0) {
516 offloaded = true;
517 }
518
519 if (skip_sw && !offloaded)
520 return -EINVAL;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700521
522 return 0;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800523}
524
Jakub Kicinski458e7042018-01-24 12:54:23 -0800525static void u32_remove_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
526 struct netlink_ext_ack *extack)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800527{
Jiri Pirko245dc512017-10-19 15:50:35 +0200528 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200529 struct tc_cls_u32_offload cls_u32 = {};
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800530
Jakub Kicinski458e7042018-01-24 12:54:23 -0800531 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
Jiri Pirko77460412017-10-19 15:50:34 +0200532 cls_u32.command = TC_CLSU32_DELETE_KNODE;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100533 cls_u32.knode.handle = n->handle;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800534
Jiri Pirko245dc512017-10-19 15:50:35 +0200535 tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100536 tcf_block_offload_dec(block, &n->flags);
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800537}
538
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400539static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
Quentin Monnet10a47e02018-01-19 17:44:45 -0800540 u32 flags, struct netlink_ext_ack *extack)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800541{
Paolo Abeni058a6c02018-02-02 16:02:22 +0100542 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
Jiri Pirko245dc512017-10-19 15:50:35 +0200543 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200544 struct tc_cls_u32_offload cls_u32 = {};
Jiri Pirko245dc512017-10-19 15:50:35 +0200545 bool skip_sw = tc_skip_sw(flags);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700546 int err;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800547
Jakub Kicinskif40fe582018-01-24 12:54:22 -0800548 tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200549 cls_u32.command = TC_CLSU32_REPLACE_KNODE;
550 cls_u32.knode.handle = n->handle;
551 cls_u32.knode.fshift = n->fshift;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100552#ifdef CONFIG_CLS_U32_MARK
Jiri Pirkode4784c2017-08-07 10:15:32 +0200553 cls_u32.knode.val = n->val;
554 cls_u32.knode.mask = n->mask;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100555#else
Jiri Pirkode4784c2017-08-07 10:15:32 +0200556 cls_u32.knode.val = 0;
557 cls_u32.knode.mask = 0;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100558#endif
Jiri Pirkode4784c2017-08-07 10:15:32 +0200559 cls_u32.knode.sel = &n->sel;
560 cls_u32.knode.exts = &n->exts;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100561 if (n->ht_down)
Paolo Abeni058a6c02018-02-02 16:02:22 +0100562 cls_u32.knode.link_handle = ht->handle;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100563
Jiri Pirko245dc512017-10-19 15:50:35 +0200564 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
565 if (err < 0) {
Jakub Kicinski458e7042018-01-24 12:54:23 -0800566 u32_remove_hw_knode(tp, n, NULL);
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100567 return err;
Jiri Pirko245dc512017-10-19 15:50:35 +0200568 } else if (err > 0) {
John Hurley530d9952018-06-25 14:30:08 -0700569 n->in_hw_count = err;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100570 tcf_block_offload_inc(block, &n->flags);
Jiri Pirko245dc512017-10-19 15:50:35 +0200571 }
572
Colin Ian King0f04d052017-11-03 08:09:45 +0000573 if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
Jiri Pirko245dc512017-10-19 15:50:35 +0200574 return -EINVAL;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700575
576 return 0;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800577}
578
Jakub Kicinski458e7042018-01-24 12:54:23 -0800579static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
580 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Al Virob245d322018-10-08 06:22:43 -0400582 struct tc_u_common *tp_c = tp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct tc_u_knode *n;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000584 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000586 for (h = 0; h <= ht->divisor; h++) {
John Fastabend1ce877202014-09-12 20:09:16 -0700587 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
588 RCU_INIT_POINTER(ht->ht[h],
589 rtnl_dereference(n->next));
Al Virob245d322018-10-08 06:22:43 -0400590 tp_c->knodes--;
WANG Conga0efb802014-09-30 16:07:24 -0700591 tcf_unbind_filter(tp, &n->res);
Jakub Kicinski458e7042018-01-24 12:54:23 -0800592 u32_remove_hw_knode(tp, n, extack);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500593 idr_remove(&ht->handle_idr, n->handle);
Cong Wang35c55fc2017-11-06 13:47:30 -0800594 if (tcf_exts_get_net(&n->exts))
Cong Wangaaa908f2018-05-23 15:26:53 -0700595 tcf_queue_work(&n->rwork, u32_delete_key_freepf_work);
Cong Wang35c55fc2017-11-06 13:47:30 -0800596 else
Al Virodc07c572018-10-08 06:22:36 -0400597 u32_destroy_key(n, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599 }
600}
601
Jakub Kicinski458e7042018-01-24 12:54:23 -0800602static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
603 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 struct tc_u_common *tp_c = tp->data;
John Fastabend1ce877202014-09-12 20:09:16 -0700606 struct tc_u_hnode __rcu **hn;
607 struct tc_u_hnode *phn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700609 WARN_ON(ht->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Jakub Kicinski458e7042018-01-24 12:54:23 -0800611 u32_clear_hnode(tp, ht, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
John Fastabend1ce877202014-09-12 20:09:16 -0700613 hn = &tp_c->hlist;
614 for (phn = rtnl_dereference(*hn);
615 phn;
616 hn = &phn->next, phn = rtnl_dereference(*hn)) {
617 if (phn == ht) {
Jakub Kicinski458e7042018-01-24 12:54:23 -0800618 u32_clear_hw_hnode(tp, ht, extack);
Cong Wange7614372017-09-25 10:13:51 -0700619 idr_destroy(&ht->handle_idr);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500620 idr_remove(&tp_c->handle_idr, ht->handle);
John Fastabend1ce877202014-09-12 20:09:16 -0700621 RCU_INIT_POINTER(*hn, ht->next);
622 kfree_rcu(ht, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624 }
625 }
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return -ENOENT;
628}
629
Cong Wang1e052be2015-03-06 11:47:59 -0800630static bool ht_empty(struct tc_u_hnode *ht)
631{
632 unsigned int h;
633
634 for (h = 0; h <= ht->divisor; h++)
635 if (rcu_access_pointer(ht->ht[h]))
636 return false;
637
638 return true;
639}
640
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800641static void u32_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 struct tc_u_common *tp_c = tp->data;
John Fastabend1ce877202014-09-12 20:09:16 -0700644 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700646 WARN_ON(root_ht == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (root_ht && --root_ht->refcnt == 0)
Jakub Kicinski458e7042018-01-24 12:54:23 -0800649 u32_destroy_hnode(tp, root_ht, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (--tp_c->refcnt == 0) {
652 struct tc_u_hnode *ht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
WANG Cong3cd904e2017-08-24 16:51:30 -0700654 hlist_del(&tp_c->hnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
John Fastabend1ce877202014-09-12 20:09:16 -0700656 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
Paolo Abenid7cdee52018-02-05 22:23:01 +0100657 u32_clear_hnode(tp, ht, extack);
John Fastabend1ce877202014-09-12 20:09:16 -0700658 RCU_INIT_POINTER(tp_c->hlist, ht->next);
Paolo Abenid7cdee52018-02-05 22:23:01 +0100659
660 /* u32_destroy_key() will later free ht for us, if it's
661 * still referenced by some knode
662 */
663 if (--ht->refcnt == 0)
664 kfree_rcu(ht, rcu);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Cong Wange7614372017-09-25 10:13:51 -0700667 idr_destroy(&tp_c->handle_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 kfree(tp_c);
669 }
670
671 tp->data = NULL;
672}
673
Alexander Aring571acf22018-01-18 11:20:53 -0500674static int u32_delete(struct tcf_proto *tp, void *arg, bool *last,
675 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
WANG Cong8113c092017-08-04 21:31:43 -0700677 struct tc_u_hnode *ht = arg;
John Fastabend1ce877202014-09-12 20:09:16 -0700678 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
WANG Cong763dbf62017-04-19 14:21:21 -0700679 struct tc_u_common *tp_c = tp->data;
680 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 if (ht == NULL)
WANG Cong763dbf62017-04-19 14:21:21 -0700683 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800685 if (TC_U32_KEY(ht->handle)) {
Jakub Kicinski458e7042018-01-24 12:54:23 -0800686 u32_remove_hw_knode(tp, (struct tc_u_knode *)ht, extack);
WANG Cong763dbf62017-04-19 14:21:21 -0700687 ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
688 goto out;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Al Virob44ef842018-10-08 06:22:33 -0400691 if (ht->is_root) {
Alexander Aring4b981db2018-01-18 11:20:55 -0500692 NL_SET_ERR_MSG_MOD(extack, "Not allowed to delete root node");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700696 if (ht->refcnt == 1) {
697 ht->refcnt--;
Jakub Kicinski458e7042018-01-24 12:54:23 -0800698 u32_destroy_hnode(tp, ht, extack);
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700699 } else {
Alexander Aring4b981db2018-01-18 11:20:55 -0500700 NL_SET_ERR_MSG_MOD(extack, "Can not delete in-use filter");
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700701 return -EBUSY;
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
WANG Cong763dbf62017-04-19 14:21:21 -0700704out:
705 *last = true;
706 if (root_ht) {
707 if (root_ht->refcnt > 1) {
708 *last = false;
709 goto ret;
710 }
711 if (root_ht->refcnt == 1) {
712 if (!ht_empty(root_ht)) {
713 *last = false;
714 goto ret;
715 }
716 }
717 }
718
719 if (tp_c->refcnt > 1) {
720 *last = false;
721 goto ret;
722 }
723
724 if (tp_c->refcnt == 1) {
725 struct tc_u_hnode *ht;
726
727 for (ht = rtnl_dereference(tp_c->hlist);
728 ht;
729 ht = rtnl_dereference(ht->next))
730 if (!ht_empty(ht)) {
731 *last = false;
732 break;
733 }
734 }
735
736ret:
737 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Cong Wange7614372017-09-25 10:13:51 -0700740static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Matthew Wilcoxf730cb92017-11-28 13:45:02 -0500742 u32 index = htid | 0x800;
Cong Wange7614372017-09-25 10:13:51 -0700743 u32 max = htid | 0xFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Matthew Wilcoxf730cb92017-11-28 13:45:02 -0500745 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max, GFP_KERNEL)) {
746 index = htid + 1;
747 if (idr_alloc_u32(&ht->handle_idr, NULL, &index, max,
748 GFP_KERNEL))
749 index = max;
Cong Wange7614372017-09-25 10:13:51 -0700750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Matthew Wilcoxf730cb92017-11-28 13:45:02 -0500752 return index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800755static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
756 [TCA_U32_CLASSID] = { .type = NLA_U32 },
757 [TCA_U32_HASH] = { .type = NLA_U32 },
758 [TCA_U32_LINK] = { .type = NLA_U32 },
759 [TCA_U32_DIVISOR] = { .type = NLA_U32 },
760 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
761 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
762 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
John Fastabend9e8ce792016-02-26 07:54:39 -0800763 [TCA_U32_FLAGS] = { .type = NLA_U32 },
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800764};
765
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000766static int u32_set_parms(struct net *net, struct tcf_proto *tp,
Al Viro8a8065f2018-10-08 06:22:42 -0400767 unsigned long base,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800768 struct tc_u_knode *n, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500769 struct nlattr *est, bool ovr,
770 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
WANG Congb9a24bb2016-08-19 12:36:54 -0700772 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Alexander Aring50a56192018-01-18 11:20:52 -0500774 err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (err < 0)
776 return err;
777
Patrick McHardyadd93b62008-01-22 22:11:33 -0800778 if (tb[TCA_U32_LINK]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800779 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000780 struct tc_u_hnode *ht_down = NULL, *ht_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Alexander Aring4b981db2018-01-18 11:20:55 -0500782 if (TC_U32_KEY(handle)) {
783 NL_SET_ERR_MSG_MOD(extack, "u32 Link handle must be a hash table");
Jiri Pirko705c7092017-08-04 14:29:14 +0200784 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 if (handle) {
Al Viro8a8065f2018-10-08 06:22:42 -0400788 ht_down = u32_lookup_ht(tp->data, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Alexander Aring4b981db2018-01-18 11:20:55 -0500790 if (!ht_down) {
791 NL_SET_ERR_MSG_MOD(extack, "Link hash table not found");
Jiri Pirko705c7092017-08-04 14:29:14 +0200792 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500793 }
Al Viro27594ec2018-10-08 06:22:34 -0400794 if (ht_down->is_root) {
795 NL_SET_ERR_MSG_MOD(extack, "Not linking to root node");
796 return -EINVAL;
797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 ht_down->refcnt++;
799 }
800
John Fastabend1ce877202014-09-12 20:09:16 -0700801 ht_old = rtnl_dereference(n->ht_down);
802 rcu_assign_pointer(n->ht_down, ht_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000804 if (ht_old)
805 ht_old->refcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Patrick McHardyadd93b62008-01-22 22:11:33 -0800807 if (tb[TCA_U32_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800808 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 tcf_bind_filter(tp, &n->res, base);
810 }
811
812#ifdef CONFIG_NET_CLS_IND
Patrick McHardyadd93b62008-01-22 22:11:33 -0800813 if (tb[TCA_U32_INDEV]) {
WANG Cong2519a602014-01-09 16:14:02 -0800814 int ret;
Alexander Aring1057c552018-01-18 11:20:54 -0500815 ret = tcf_change_indev(net, tb[TCA_U32_INDEV], extack);
WANG Cong2519a602014-01-09 16:14:02 -0800816 if (ret < 0)
Jiri Pirko705c7092017-08-04 14:29:14 +0200817 return -EINVAL;
WANG Cong2519a602014-01-09 16:14:02 -0800818 n->ifindex = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400824static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
John Fastabendde5df632014-09-19 21:50:34 -0700825 struct tc_u_knode *n)
826{
827 struct tc_u_knode __rcu **ins;
828 struct tc_u_knode *pins;
829 struct tc_u_hnode *ht;
830
831 if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
832 ht = rtnl_dereference(tp->root);
833 else
834 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
835
836 ins = &ht->ht[TC_U32_HASH(n->handle)];
837
838 /* The node must always exist for it to be replaced if this is not the
839 * case then something went very wrong elsewhere.
840 */
841 for (pins = rtnl_dereference(*ins); ;
842 ins = &pins->next, pins = rtnl_dereference(*ins))
843 if (pins->handle == n->handle)
844 break;
845
Matthew Wilcox234a4622017-11-28 09:56:36 -0500846 idr_replace(&ht->handle_idr, n, n->handle);
John Fastabendde5df632014-09-19 21:50:34 -0700847 RCU_INIT_POINTER(n->next, pins->next);
848 rcu_assign_pointer(*ins, n);
849}
850
851static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
852 struct tc_u_knode *n)
853{
Paolo Abeni058a6c02018-02-02 16:02:22 +0100854 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
John Fastabendde5df632014-09-19 21:50:34 -0700855 struct tc_u32_sel *s = &n->sel;
Paolo Abeni058a6c02018-02-02 16:02:22 +0100856 struct tc_u_knode *new;
John Fastabendde5df632014-09-19 21:50:34 -0700857
858 new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
859 GFP_KERNEL);
860
861 if (!new)
862 return NULL;
863
864 RCU_INIT_POINTER(new->next, n->next);
865 new->handle = n->handle;
866 RCU_INIT_POINTER(new->ht_up, n->ht_up);
867
868#ifdef CONFIG_NET_CLS_IND
869 new->ifindex = n->ifindex;
870#endif
871 new->fshift = n->fshift;
872 new->res = n->res;
John Fastabend9e8ce792016-02-26 07:54:39 -0800873 new->flags = n->flags;
Paolo Abeni058a6c02018-02-02 16:02:22 +0100874 RCU_INIT_POINTER(new->ht_down, ht);
John Fastabendde5df632014-09-19 21:50:34 -0700875
876 /* bump reference count as long as we hold pointer to structure */
Paolo Abeni058a6c02018-02-02 16:02:22 +0100877 if (ht)
878 ht->refcnt++;
John Fastabendde5df632014-09-19 21:50:34 -0700879
880#ifdef CONFIG_CLS_U32_PERF
881 /* Statistics may be incremented by readers during update
882 * so we must keep them in tact. When the node is later destroyed
883 * a special destroy call must be made to not free the pf memory.
884 */
885 new->pf = n->pf;
886#endif
887
888#ifdef CONFIG_CLS_U32_MARK
889 new->val = n->val;
890 new->mask = n->mask;
891 /* Similarly success statistics must be moved as pointers */
892 new->pcpu_success = n->pcpu_success;
893#endif
John Fastabendde5df632014-09-19 21:50:34 -0700894 memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
895
WANG Congb9a24bb2016-08-19 12:36:54 -0700896 if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
897 kfree(new);
898 return NULL;
899 }
John Fastabendde5df632014-09-19 21:50:34 -0700900
901 return new;
902}
903
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000904static int u32_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600905 struct tcf_proto *tp, unsigned long base, u32 handle,
Alexander Aring7306db32018-01-18 11:20:51 -0500906 struct nlattr **tca, void **arg, bool ovr,
907 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 struct tc_u_common *tp_c = tp->data;
910 struct tc_u_hnode *ht;
911 struct tc_u_knode *n;
912 struct tc_u32_sel *s;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800913 struct nlattr *opt = tca[TCA_OPTIONS];
914 struct nlattr *tb[TCA_U32_MAX + 1];
John Fastabend9e8ce792016-02-26 07:54:39 -0800915 u32 htid, flags = 0;
Kees Cook98c8f122018-08-25 22:58:01 -0700916 size_t sel_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int err;
John Fastabend459d5f62014-09-12 20:08:47 -0700918#ifdef CONFIG_CLS_U32_PERF
919 size_t size;
920#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Alexander Aring4b981db2018-01-18 11:20:55 -0500922 if (!opt) {
923 if (handle) {
924 NL_SET_ERR_MSG_MOD(extack, "Filter handle requires options");
925 return -EINVAL;
926 } else {
927 return 0;
928 }
929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Alexander Aring4b981db2018-01-18 11:20:55 -0500931 err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, extack);
Patrick McHardycee63722008-01-23 20:33:32 -0800932 if (err < 0)
933 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700935 if (tb[TCA_U32_FLAGS]) {
John Fastabend9e8ce792016-02-26 07:54:39 -0800936 flags = nla_get_u32(tb[TCA_U32_FLAGS]);
Alexander Aring4b981db2018-01-18 11:20:55 -0500937 if (!tc_flags_valid(flags)) {
938 NL_SET_ERR_MSG_MOD(extack, "Invalid filter flags");
Jakub Kicinski1a0f7d22016-06-06 16:16:47 +0100939 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500940 }
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700941 }
John Fastabend9e8ce792016-02-26 07:54:39 -0800942
WANG Cong8113c092017-08-04 21:31:43 -0700943 n = *arg;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000944 if (n) {
John Fastabendde5df632014-09-19 21:50:34 -0700945 struct tc_u_knode *new;
946
Alexander Aring4b981db2018-01-18 11:20:55 -0500947 if (TC_U32_KEY(n->handle) == 0) {
948 NL_SET_ERR_MSG_MOD(extack, "Key node id cannot be zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Ivan Veceraeb53f7a2018-02-08 16:10:39 +0100952 if ((n->flags ^ flags) &
953 ~(TCA_CLS_FLAGS_IN_HW | TCA_CLS_FLAGS_NOT_IN_HW)) {
Alexander Aring4b981db2018-01-18 11:20:55 -0500954 NL_SET_ERR_MSG_MOD(extack, "Key node flags do not match passed flags");
John Fastabend9e8ce792016-02-26 07:54:39 -0800955 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500956 }
John Fastabend9e8ce792016-02-26 07:54:39 -0800957
John Fastabendde5df632014-09-19 21:50:34 -0700958 new = u32_init_knode(tp, n);
959 if (!new)
960 return -ENOMEM;
961
Al Viro8a8065f2018-10-08 06:22:42 -0400962 err = u32_set_parms(net, tp, base, new, tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500963 tca[TCA_RATE], ovr, extack);
John Fastabendde5df632014-09-19 21:50:34 -0700964
965 if (err) {
Al Virodc07c572018-10-08 06:22:36 -0400966 u32_destroy_key(new, false);
John Fastabendde5df632014-09-19 21:50:34 -0700967 return err;
968 }
969
Quentin Monnet10a47e02018-01-19 17:44:45 -0800970 err = u32_replace_hw_knode(tp, new, flags, extack);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700971 if (err) {
Al Virodc07c572018-10-08 06:22:36 -0400972 u32_destroy_key(new, false);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700973 return err;
974 }
975
Or Gerlitz24d3dc62017-02-16 10:31:15 +0200976 if (!tc_in_hw(new->flags))
977 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
978
John Fastabendde5df632014-09-19 21:50:34 -0700979 u32_replace_knode(tp, tp_c, new);
WANG Conga0efb802014-09-30 16:07:24 -0700980 tcf_unbind_filter(tp, &n->res);
Cong Wang35c55fc2017-11-06 13:47:30 -0800981 tcf_exts_get_net(&n->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -0700982 tcf_queue_work(&n->rwork, u32_delete_key_work);
John Fastabendde5df632014-09-19 21:50:34 -0700983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985
Patrick McHardyadd93b62008-01-22 22:11:33 -0800986 if (tb[TCA_U32_DIVISOR]) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000987 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Al Viro2f0c9822018-10-08 06:22:35 -0400989 if (!is_power_of_2(divisor)) {
990 NL_SET_ERR_MSG_MOD(extack, "Divisor is not a power of 2");
991 return -EINVAL;
992 }
993 if (divisor-- > 0x100) {
Alexander Aring4b981db2018-01-18 11:20:55 -0500994 NL_SET_ERR_MSG_MOD(extack, "Exceeded maximum 256 hash buckets");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -0500996 }
997 if (TC_U32_KEY(handle)) {
998 NL_SET_ERR_MSG_MOD(extack, "Divisor can only be used on a hash table");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -05001000 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001001 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (ht == NULL)
1003 return -ENOBUFS;
Cong Wange7614372017-09-25 10:13:51 -07001004 if (handle == 0) {
1005 handle = gen_new_htid(tp->data, ht);
1006 if (handle == 0) {
1007 kfree(ht);
1008 return -ENOMEM;
1009 }
1010 } else {
Matthew Wilcoxf730cb92017-11-28 13:45:02 -05001011 err = idr_alloc_u32(&tp_c->handle_idr, ht, &handle,
1012 handle, GFP_KERNEL);
Cong Wange7614372017-09-25 10:13:51 -07001013 if (err) {
1014 kfree(ht);
1015 return err;
1016 }
1017 }
Jarek Poplawskie56cfad2008-04-12 18:37:13 -07001018 ht->refcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ht->divisor = divisor;
1020 ht->handle = handle;
1021 ht->prio = tp->prio;
Cong Wange7614372017-09-25 10:13:51 -07001022 idr_init(&ht->handle_idr);
Jakub Kicinskif40fe582018-01-24 12:54:22 -08001023 ht->flags = flags;
Jakub Kicinski6eef3802016-06-08 20:11:03 +01001024
Quentin Monnet10a47e02018-01-19 17:44:45 -08001025 err = u32_replace_hw_hnode(tp, ht, flags, extack);
Jakub Kicinski6eef3802016-06-08 20:11:03 +01001026 if (err) {
Matthew Wilcox9c160942017-11-28 09:48:43 -05001027 idr_remove(&tp_c->handle_idr, handle);
Jakub Kicinski6eef3802016-06-08 20:11:03 +01001028 kfree(ht);
1029 return err;
1030 }
1031
John Fastabend1ce877202014-09-12 20:09:16 -07001032 RCU_INIT_POINTER(ht->next, tp_c->hlist);
1033 rcu_assign_pointer(tp_c->hlist, ht);
WANG Cong8113c092017-08-04 21:31:43 -07001034 *arg = ht;
John Fastabenda1b7c5f2016-02-16 21:17:09 -08001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return 0;
1037 }
1038
Patrick McHardyadd93b62008-01-22 22:11:33 -08001039 if (tb[TCA_U32_HASH]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -08001040 htid = nla_get_u32(tb[TCA_U32_HASH]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
John Fastabend1ce877202014-09-12 20:09:16 -07001042 ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 htid = ht->handle;
1044 } else {
1045 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
Alexander Aring4b981db2018-01-18 11:20:55 -05001046 if (!ht) {
1047 NL_SET_ERR_MSG_MOD(extack, "Specified hash table not found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -05001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051 } else {
John Fastabend1ce877202014-09-12 20:09:16 -07001052 ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 htid = ht->handle;
1054 }
1055
Alexander Aring4b981db2018-01-18 11:20:55 -05001056 if (ht->divisor < TC_U32_HASH(htid)) {
1057 NL_SET_ERR_MSG_MOD(extack, "Specified hash table buckets exceed configured value");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -05001059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (handle) {
Alexander Aring4b981db2018-01-18 11:20:55 -05001062 if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) {
1063 NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return -EINVAL;
Alexander Aring4b981db2018-01-18 11:20:55 -05001065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 handle = htid | TC_U32_NODE(handle);
Matthew Wilcoxf730cb92017-11-28 13:45:02 -05001067 err = idr_alloc_u32(&ht->handle_idr, NULL, &handle, handle,
Cong Wange7614372017-09-25 10:13:51 -07001068 GFP_KERNEL);
1069 if (err)
1070 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 } else
1072 handle = gen_new_kid(ht, htid);
1073
Cong Wange7614372017-09-25 10:13:51 -07001074 if (tb[TCA_U32_SEL] == NULL) {
Alexander Aring4b981db2018-01-18 11:20:55 -05001075 NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
Cong Wange7614372017-09-25 10:13:51 -07001076 err = -EINVAL;
1077 goto erridr;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Patrick McHardyadd93b62008-01-22 22:11:33 -08001080 s = nla_data(tb[TCA_U32_SEL]);
Kees Cook98c8f122018-08-25 22:58:01 -07001081 sel_size = struct_size(s, keys, s->nkeys);
1082 if (nla_len(tb[TCA_U32_SEL]) < sel_size) {
1083 err = -EINVAL;
1084 goto erridr;
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Kees Cook98c8f122018-08-25 22:58:01 -07001087 n = kzalloc(offsetof(typeof(*n), sel) + sel_size, GFP_KERNEL);
Cong Wange7614372017-09-25 10:13:51 -07001088 if (n == NULL) {
1089 err = -ENOBUFS;
1090 goto erridr;
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -07001094 size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1095 n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1096 if (!n->pf) {
Cong Wange7614372017-09-25 10:13:51 -07001097 err = -ENOBUFS;
1098 goto errfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100#endif
1101
Kees Cook98c8f122018-08-25 22:58:01 -07001102 memcpy(&n->sel, s, sel_size);
John Fastabenda96366bf2014-09-15 23:30:49 -07001103 RCU_INIT_POINTER(n->ht_up, ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 n->handle = handle;
Radu Rendecb2268012007-11-10 21:54:50 -08001105 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
John Fastabend9e8ce792016-02-26 07:54:39 -08001106 n->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
WANG Congb9a24bb2016-08-19 12:36:54 -07001108 err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
1109 if (err < 0)
1110 goto errout;
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -07001113 n->pcpu_success = alloc_percpu(u32);
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001114 if (!n->pcpu_success) {
1115 err = -ENOMEM;
1116 goto errout;
1117 }
John Fastabend459d5f62014-09-12 20:08:47 -07001118
Patrick McHardyadd93b62008-01-22 22:11:33 -08001119 if (tb[TCA_U32_MARK]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct tc_u32_mark *mark;
1121
Patrick McHardyadd93b62008-01-22 22:11:33 -08001122 mark = nla_data(tb[TCA_U32_MARK]);
John Fastabend459d5f62014-09-12 20:08:47 -07001123 n->val = mark->val;
1124 n->mask = mark->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126#endif
1127
Al Viro8a8065f2018-10-08 06:22:42 -04001128 err = u32_set_parms(net, tp, base, n, tb, tca[TCA_RATE], ovr,
Alexander Aring50a56192018-01-18 11:20:52 -05001129 extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (err == 0) {
John Fastabend1ce877202014-09-12 20:09:16 -07001131 struct tc_u_knode __rcu **ins;
1132 struct tc_u_knode *pins;
1133
Quentin Monnet10a47e02018-01-19 17:44:45 -08001134 err = u32_replace_hw_knode(tp, n, flags, extack);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -07001135 if (err)
1136 goto errhw;
1137
Or Gerlitz24d3dc62017-02-16 10:31:15 +02001138 if (!tc_in_hw(n->flags))
1139 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1140
John Fastabend1ce877202014-09-12 20:09:16 -07001141 ins = &ht->ht[TC_U32_HASH(handle)];
1142 for (pins = rtnl_dereference(*ins); pins;
1143 ins = &pins->next, pins = rtnl_dereference(*ins))
1144 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 break;
1146
John Fastabend1ce877202014-09-12 20:09:16 -07001147 RCU_INIT_POINTER(n->next, pins);
1148 rcu_assign_pointer(*ins, n);
Al Virob245d322018-10-08 06:22:43 -04001149 tp_c->knodes++;
WANG Cong8113c092017-08-04 21:31:43 -07001150 *arg = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return 0;
1152 }
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001153
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -07001154errhw:
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001155#ifdef CONFIG_CLS_U32_MARK
1156 free_percpu(n->pcpu_success);
1157#endif
1158
WANG Congb9a24bb2016-08-19 12:36:54 -07001159errout:
1160 tcf_exts_destroy(&n->exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161#ifdef CONFIG_CLS_U32_PERF
Cong Wange7614372017-09-25 10:13:51 -07001162errfree:
John Fastabend1ce877202014-09-12 20:09:16 -07001163 free_percpu(n->pf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164#endif
1165 kfree(n);
Cong Wange7614372017-09-25 10:13:51 -07001166erridr:
Matthew Wilcox9c160942017-11-28 09:48:43 -05001167 idr_remove(&ht->handle_idr, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return err;
1169}
1170
1171static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1172{
1173 struct tc_u_common *tp_c = tp->data;
1174 struct tc_u_hnode *ht;
1175 struct tc_u_knode *n;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001176 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 if (arg->stop)
1179 return;
1180
John Fastabend1ce877202014-09-12 20:09:16 -07001181 for (ht = rtnl_dereference(tp_c->hlist);
1182 ht;
1183 ht = rtnl_dereference(ht->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (ht->prio != tp->prio)
1185 continue;
1186 if (arg->count >= arg->skip) {
WANG Cong8113c092017-08-04 21:31:43 -07001187 if (arg->fn(tp, ht, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 arg->stop = 1;
1189 return;
1190 }
1191 }
1192 arg->count++;
1193 for (h = 0; h <= ht->divisor; h++) {
John Fastabend1ce877202014-09-12 20:09:16 -07001194 for (n = rtnl_dereference(ht->ht[h]);
1195 n;
1196 n = rtnl_dereference(n->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (arg->count < arg->skip) {
1198 arg->count++;
1199 continue;
1200 }
WANG Cong8113c092017-08-04 21:31:43 -07001201 if (arg->fn(tp, n, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 arg->stop = 1;
1203 return;
1204 }
1205 arg->count++;
1206 }
1207 }
1208 }
1209}
1210
John Hurley530d9952018-06-25 14:30:08 -07001211static int u32_reoffload_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
1212 bool add, tc_setup_cb_t *cb, void *cb_priv,
1213 struct netlink_ext_ack *extack)
1214{
1215 struct tc_cls_u32_offload cls_u32 = {};
1216 int err;
1217
1218 tc_cls_common_offload_init(&cls_u32.common, tp, ht->flags, extack);
1219 cls_u32.command = add ? TC_CLSU32_NEW_HNODE : TC_CLSU32_DELETE_HNODE;
1220 cls_u32.hnode.divisor = ht->divisor;
1221 cls_u32.hnode.handle = ht->handle;
1222 cls_u32.hnode.prio = ht->prio;
1223
1224 err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
1225 if (err && add && tc_skip_sw(ht->flags))
1226 return err;
1227
1228 return 0;
1229}
1230
1231static int u32_reoffload_knode(struct tcf_proto *tp, struct tc_u_knode *n,
1232 bool add, tc_setup_cb_t *cb, void *cb_priv,
1233 struct netlink_ext_ack *extack)
1234{
1235 struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
1236 struct tcf_block *block = tp->chain->block;
1237 struct tc_cls_u32_offload cls_u32 = {};
1238 int err;
1239
1240 tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
1241 cls_u32.command = add ?
1242 TC_CLSU32_REPLACE_KNODE : TC_CLSU32_DELETE_KNODE;
1243 cls_u32.knode.handle = n->handle;
1244
1245 if (add) {
1246 cls_u32.knode.fshift = n->fshift;
1247#ifdef CONFIG_CLS_U32_MARK
1248 cls_u32.knode.val = n->val;
1249 cls_u32.knode.mask = n->mask;
1250#else
1251 cls_u32.knode.val = 0;
1252 cls_u32.knode.mask = 0;
1253#endif
1254 cls_u32.knode.sel = &n->sel;
1255 cls_u32.knode.exts = &n->exts;
1256 if (n->ht_down)
1257 cls_u32.knode.link_handle = ht->handle;
1258 }
1259
1260 err = cb(TC_SETUP_CLSU32, &cls_u32, cb_priv);
1261 if (err) {
1262 if (add && tc_skip_sw(n->flags))
1263 return err;
1264 return 0;
1265 }
1266
1267 tc_cls_offload_cnt_update(block, &n->in_hw_count, &n->flags, add);
1268
1269 return 0;
1270}
1271
1272static int u32_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1273 void *cb_priv, struct netlink_ext_ack *extack)
1274{
1275 struct tc_u_common *tp_c = tp->data;
1276 struct tc_u_hnode *ht;
1277 struct tc_u_knode *n;
1278 unsigned int h;
1279 int err;
1280
1281 for (ht = rtnl_dereference(tp_c->hlist);
1282 ht;
1283 ht = rtnl_dereference(ht->next)) {
1284 if (ht->prio != tp->prio)
1285 continue;
1286
1287 /* When adding filters to a new dev, try to offload the
1288 * hashtable first. When removing, do the filters before the
1289 * hashtable.
1290 */
1291 if (add && !tc_skip_hw(ht->flags)) {
1292 err = u32_reoffload_hnode(tp, ht, add, cb, cb_priv,
1293 extack);
1294 if (err)
1295 return err;
1296 }
1297
1298 for (h = 0; h <= ht->divisor; h++) {
1299 for (n = rtnl_dereference(ht->ht[h]);
1300 n;
1301 n = rtnl_dereference(n->next)) {
1302 if (tc_skip_hw(n->flags))
1303 continue;
1304
1305 err = u32_reoffload_knode(tp, n, add, cb,
1306 cb_priv, extack);
1307 if (err)
1308 return err;
1309 }
1310 }
1311
1312 if (!add && !tc_skip_hw(ht->flags))
1313 u32_reoffload_hnode(tp, ht, add, cb, cb_priv, extack);
1314 }
1315
1316 return 0;
1317}
1318
Cong Wang07d79fc2017-08-30 14:30:36 -07001319static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
1320{
1321 struct tc_u_knode *n = fh;
1322
1323 if (n && n->res.classid == classid)
1324 n->res.class = cl;
1325}
1326
WANG Cong8113c092017-08-04 21:31:43 -07001327static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001328 struct sk_buff *skb, struct tcmsg *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
WANG Cong8113c092017-08-04 21:31:43 -07001330 struct tc_u_knode *n = fh;
John Fastabend1ce877202014-09-12 20:09:16 -07001331 struct tc_u_hnode *ht_up, *ht_down;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001332 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 if (n == NULL)
1335 return skb->len;
1336
1337 t->tcm_handle = n->handle;
1338
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001339 nest = nla_nest_start(skb, TCA_OPTIONS);
1340 if (nest == NULL)
1341 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 if (TC_U32_KEY(n->handle) == 0) {
WANG Cong8113c092017-08-04 21:31:43 -07001344 struct tc_u_hnode *ht = fh;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001345 u32 divisor = ht->divisor + 1;
1346
David S. Miller1b34ec42012-03-29 05:11:39 -04001347 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1348 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 } else {
John Fastabend459d5f62014-09-12 20:08:47 -07001350#ifdef CONFIG_CLS_U32_PERF
1351 struct tc_u32_pcnt *gpf;
John Fastabend459d5f62014-09-12 20:08:47 -07001352 int cpu;
John Fastabend80aab732014-09-15 23:30:26 -07001353#endif
John Fastabend459d5f62014-09-12 20:08:47 -07001354
David S. Miller1b34ec42012-03-29 05:11:39 -04001355 if (nla_put(skb, TCA_U32_SEL,
1356 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1357 &n->sel))
1358 goto nla_put_failure;
John Fastabend1ce877202014-09-12 20:09:16 -07001359
1360 ht_up = rtnl_dereference(n->ht_up);
1361 if (ht_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 u32 htid = n->handle & 0xFFFFF000;
David S. Miller1b34ec42012-03-29 05:11:39 -04001363 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1364 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
David S. Miller1b34ec42012-03-29 05:11:39 -04001366 if (n->res.classid &&
1367 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1368 goto nla_put_failure;
John Fastabend1ce877202014-09-12 20:09:16 -07001369
1370 ht_down = rtnl_dereference(n->ht_down);
1371 if (ht_down &&
1372 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
David S. Miller1b34ec42012-03-29 05:11:39 -04001373 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
John Fastabend9e8ce792016-02-26 07:54:39 -08001375 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1376 goto nla_put_failure;
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -07001379 if ((n->val || n->mask)) {
1380 struct tc_u32_mark mark = {.val = n->val,
1381 .mask = n->mask,
1382 .success = 0};
John Fastabend80aab732014-09-15 23:30:26 -07001383 int cpum;
John Fastabend459d5f62014-09-12 20:08:47 -07001384
John Fastabend80aab732014-09-15 23:30:26 -07001385 for_each_possible_cpu(cpum) {
1386 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
John Fastabend459d5f62014-09-12 20:08:47 -07001387
1388 mark.success += cnt;
1389 }
1390
1391 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1392 goto nla_put_failure;
1393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394#endif
1395
WANG Cong5da57f42013-12-15 20:15:07 -08001396 if (tcf_exts_dump(skb, &n->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001397 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -08001400 if (n->ifindex) {
1401 struct net_device *dev;
1402 dev = __dev_get_by_index(net, n->ifindex);
1403 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1404 goto nla_put_failure;
1405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406#endif
1407#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -07001408 gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1409 n->sel.nkeys * sizeof(u64),
1410 GFP_KERNEL);
1411 if (!gpf)
1412 goto nla_put_failure;
1413
1414 for_each_possible_cpu(cpu) {
1415 int i;
1416 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1417
1418 gpf->rcnt += pf->rcnt;
1419 gpf->rhit += pf->rhit;
1420 for (i = 0; i < n->sel.nkeys; i++)
1421 gpf->kcnts[i] += pf->kcnts[i];
1422 }
1423
Nicolas Dichtel9854518e2016-04-26 10:06:18 +02001424 if (nla_put_64bit(skb, TCA_U32_PCNT,
1425 sizeof(struct tc_u32_pcnt) +
1426 n->sel.nkeys * sizeof(u64),
1427 gpf, TCA_U32_PAD)) {
John Fastabend459d5f62014-09-12 20:08:47 -07001428 kfree(gpf);
David S. Miller1b34ec42012-03-29 05:11:39 -04001429 goto nla_put_failure;
John Fastabend459d5f62014-09-12 20:08:47 -07001430 }
1431 kfree(gpf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432#endif
1433 }
1434
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001435 nla_nest_end(skb, nest);
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (TC_U32_KEY(n->handle))
WANG Cong5da57f42013-12-15 20:15:07 -08001438 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001439 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return skb->len;
1441
Patrick McHardyadd93b62008-01-22 22:11:33 -08001442nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001443 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -1;
1445}
1446
Patrick McHardy2eb9d752008-01-22 22:10:42 -08001447static struct tcf_proto_ops cls_u32_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 .kind = "u32",
1449 .classify = u32_classify,
1450 .init = u32_init,
1451 .destroy = u32_destroy,
1452 .get = u32_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 .change = u32_change,
1454 .delete = u32_delete,
1455 .walk = u32_walk,
John Hurley530d9952018-06-25 14:30:08 -07001456 .reoffload = u32_reoffload,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 .dump = u32_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001458 .bind_class = u32_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 .owner = THIS_MODULE,
1460};
1461
1462static int __init init_u32(void)
1463{
WANG Cong3cd904e2017-08-24 16:51:30 -07001464 int i, ret;
1465
stephen hemminger6ff9c362010-05-12 06:37:05 +00001466 pr_info("u32 classifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467#ifdef CONFIG_CLS_U32_PERF
stephen hemminger6ff9c362010-05-12 06:37:05 +00001468 pr_info(" Performance counters on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470#ifdef CONFIG_NET_CLS_IND
stephen hemminger6ff9c362010-05-12 06:37:05 +00001471 pr_info(" input device check on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472#endif
1473#ifdef CONFIG_NET_CLS_ACT
stephen hemminger6ff9c362010-05-12 06:37:05 +00001474 pr_info(" Actions configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475#endif
WANG Cong3cd904e2017-08-24 16:51:30 -07001476 tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
1477 sizeof(struct hlist_head),
1478 GFP_KERNEL);
1479 if (!tc_u_common_hash)
1480 return -ENOMEM;
1481
1482 for (i = 0; i < U32_HASH_SIZE; i++)
1483 INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1484
1485 ret = register_tcf_proto_ops(&cls_u32_ops);
1486 if (ret)
1487 kvfree(tc_u_common_hash);
1488 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001491static void __exit exit_u32(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 unregister_tcf_proto_ops(&cls_u32_ops);
WANG Cong3cd904e2017-08-24 16:51:30 -07001494 kvfree(tc_u_common_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
1497module_init(init_u32)
1498module_exit(exit_u32)
1499MODULE_LICENSE("GPL");