blob: d53da7968edaa4edeb7b58538c33124836aa71dc [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>
John Fastabenda1b7c5f2016-02-16 21:17:09 -080048#include <linux/netdevice.h>
Cong Wange7614372017-09-25 10:13:51 -070049#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Eric Dumazetcc7ec452011-01-19 19:26:56 +000051struct tc_u_knode {
John Fastabend1ce87722014-09-12 20:09:16 -070052 struct tc_u_knode __rcu *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 u32 handle;
John Fastabend1ce87722014-09-12 20:09:16 -070054 struct tc_u_hnode __rcu *ht_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct tcf_exts exts;
56#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -080057 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59 u8 fshift;
60 struct tcf_result res;
John Fastabend1ce87722014-09-12 20:09:16 -070061 struct tc_u_hnode __rcu *ht_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -070063 struct tc_u32_pcnt __percpu *pf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
John Fastabend9e8ce792016-02-26 07:54:39 -080065 u32 flags;
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
John Fastabend1ce87722014-09-12 20:09:16 -070071 struct tcf_proto *tp;
John Fastabend1ce87722014-09-12 20:09:16 -070072 struct rcu_head rcu;
John Fastabend4e2840e2014-09-17 11:11:46 -070073 /* The 'sel' field MUST be the last field in structure to allow for
74 * tc_u32_keys allocated at end of structure.
75 */
76 struct tc_u32_sel sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Eric Dumazetcc7ec452011-01-19 19:26:56 +000079struct tc_u_hnode {
John Fastabend1ce87722014-09-12 20:09:16 -070080 struct tc_u_hnode __rcu *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 u32 handle;
82 u32 prio;
83 struct tc_u_common *tp_c;
84 int refcnt;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000085 unsigned int divisor;
Cong Wange7614372017-09-25 10:13:51 -070086 struct idr handle_idr;
John Fastabend1ce87722014-09-12 20:09:16 -070087 struct rcu_head rcu;
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 Pirko7fa9d972017-10-13 14:01:02 +020096 struct tcf_block *block;
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;
John Fastabend1ce87722014-09-12 20:09:16 -0700100 struct rcu_head rcu;
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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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 Fastabend1ce87722014-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
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{
Cong Wange7614372017-09-25 10:13:51 -0700318 unsigned long idr_index;
319 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Cong Wange7614372017-09-25 10:13:51 -0700321 /* This is only used inside rtnl lock it is safe to increment
John Fastabend1ce87722014-09-12 20:09:16 -0700322 * without read _copy_ update semantics
323 */
Cong Wange7614372017-09-25 10:13:51 -0700324 err = idr_alloc_ext(&tp_c->handle_idr, ptr, &idr_index,
325 1, 0x7FF, GFP_KERNEL);
326 if (err)
327 return 0;
328 return (u32)(idr_index | 0x800) << 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
WANG Cong3cd904e2017-08-24 16:51:30 -0700331static struct hlist_head *tc_u_common_hash;
332
333#define U32_HASH_SHIFT 10
334#define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
335
336static unsigned int tc_u_hash(const struct tcf_proto *tp)
337{
Arnd Bergmannd18b4b32017-10-18 10:33:37 +0200338 return hash_ptr(tp->chain->block, U32_HASH_SHIFT);
WANG Cong3cd904e2017-08-24 16:51:30 -0700339}
340
341static struct tc_u_common *tc_u_common_find(const struct tcf_proto *tp)
342{
343 struct tc_u_common *tc;
344 unsigned int h;
345
346 h = tc_u_hash(tp);
347 hlist_for_each_entry(tc, &tc_u_common_hash[h], hnode) {
Jiri Pirko7fa9d972017-10-13 14:01:02 +0200348 if (tc->block == tp->chain->block)
WANG Cong3cd904e2017-08-24 16:51:30 -0700349 return tc;
350 }
351 return NULL;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static int u32_init(struct tcf_proto *tp)
355{
356 struct tc_u_hnode *root_ht;
357 struct tc_u_common *tp_c;
WANG Cong3cd904e2017-08-24 16:51:30 -0700358 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
WANG Cong3cd904e2017-08-24 16:51:30 -0700360 tp_c = tc_u_common_find(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700362 root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (root_ht == NULL)
364 return -ENOBUFS;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 root_ht->refcnt++;
Cong Wange7614372017-09-25 10:13:51 -0700367 root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 root_ht->prio = tp->prio;
Cong Wange7614372017-09-25 10:13:51 -0700369 idr_init(&root_ht->handle_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if (tp_c == NULL) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700372 tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (tp_c == NULL) {
374 kfree(root_ht);
375 return -ENOBUFS;
376 }
Jiri Pirko7fa9d972017-10-13 14:01:02 +0200377 tp_c->block = tp->chain->block;
WANG Cong3cd904e2017-08-24 16:51:30 -0700378 INIT_HLIST_NODE(&tp_c->hnode);
Cong Wange7614372017-09-25 10:13:51 -0700379 idr_init(&tp_c->handle_idr);
WANG Cong3cd904e2017-08-24 16:51:30 -0700380
381 h = tc_u_hash(tp);
382 hlist_add_head(&tp_c->hnode, &tc_u_common_hash[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
385 tp_c->refcnt++;
John Fastabend1ce87722014-09-12 20:09:16 -0700386 RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
387 rcu_assign_pointer(tp_c->hlist, root_ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 root_ht->tp_c = tp_c;
389
John Fastabend1ce87722014-09-12 20:09:16 -0700390 rcu_assign_pointer(tp->root, root_ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 tp->data = tp_c;
392 return 0;
393}
394
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400395static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n,
John Fastabendde5df632014-09-19 21:50:34 -0700396 bool free_pf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
WANG Cong18d02642014-09-25 10:26:37 -0700398 tcf_exts_destroy(&n->exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (n->ht_down)
400 n->ht_down->refcnt--;
401#ifdef CONFIG_CLS_U32_PERF
John Fastabendde5df632014-09-19 21:50:34 -0700402 if (free_pf)
403 free_percpu(n->pf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404#endif
John Fastabenda1ddcfe2014-09-19 21:50:04 -0700405#ifdef CONFIG_CLS_U32_MARK
John Fastabendde5df632014-09-19 21:50:34 -0700406 if (free_pf)
407 free_percpu(n->pcpu_success);
John Fastabenda1ddcfe2014-09-19 21:50:04 -0700408#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 kfree(n);
410 return 0;
411}
412
John Fastabendde5df632014-09-19 21:50:34 -0700413/* u32_delete_key_rcu should be called when free'ing a copied
414 * version of a tc_u_knode obtained from u32_init_knode(). When
415 * copies are obtained from u32_init_knode() the statistics are
416 * shared between the old and new copies to allow readers to
417 * continue to update the statistics during the copy. To support
418 * this the u32_delete_key_rcu variant does not free the percpu
419 * statistics.
420 */
John Fastabend1ce87722014-09-12 20:09:16 -0700421static void u32_delete_key_rcu(struct rcu_head *rcu)
422{
423 struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
424
John Fastabendde5df632014-09-19 21:50:34 -0700425 u32_destroy_key(key->tp, key, false);
426}
427
428/* u32_delete_key_freepf_rcu is the rcu callback variant
429 * that free's the entire structure including the statistics
430 * percpu variables. Only use this if the key is not a copy
431 * returned by u32_init_knode(). See u32_delete_key_rcu()
432 * for the variant that should be used with keys return from
433 * u32_init_knode()
434 */
435static void u32_delete_key_freepf_rcu(struct rcu_head *rcu)
436{
437 struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
438
439 u32_destroy_key(key->tp, key, true);
John Fastabend1ce87722014-09-12 20:09:16 -0700440}
441
Yang Yingliang82d567c2013-12-10 20:55:31 +0800442static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
John Fastabend1ce87722014-09-12 20:09:16 -0700444 struct tc_u_knode __rcu **kp;
445 struct tc_u_knode *pkp;
John Fastabenda96366bf2014-09-15 23:30:49 -0700446 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (ht) {
John Fastabend1ce87722014-09-12 20:09:16 -0700449 kp = &ht->ht[TC_U32_HASH(key->handle)];
450 for (pkp = rtnl_dereference(*kp); pkp;
451 kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
452 if (pkp == key) {
453 RCU_INIT_POINTER(*kp, key->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
WANG Conga0efb802014-09-30 16:07:24 -0700455 tcf_unbind_filter(tp, &key->res);
John Fastabendde5df632014-09-19 21:50:34 -0700456 call_rcu(&key->rcu, u32_delete_key_freepf_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458 }
459 }
460 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700461 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
463}
464
Jiri Pirko77460412017-10-19 15:50:34 +0200465static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800466{
467 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirko245dc512017-10-19 15:50:35 +0200468 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200469 struct tc_cls_u32_offload cls_u32 = {};
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800470
Jiri Pirkode4784c2017-08-07 10:15:32 +0200471 tc_cls_common_offload_init(&cls_u32.common, tp);
Jiri Pirko77460412017-10-19 15:50:34 +0200472 cls_u32.command = TC_CLSU32_DELETE_HNODE;
473 cls_u32.hnode.divisor = h->divisor;
474 cls_u32.hnode.handle = h->handle;
475 cls_u32.hnode.prio = h->prio;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200476
Jiri Pirko245dc512017-10-19 15:50:35 +0200477 if (tc_can_offload(dev))
478 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32);
479 tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800480}
481
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400482static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
483 u32 flags)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800484{
485 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirko245dc512017-10-19 15:50:35 +0200486 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200487 struct tc_cls_u32_offload cls_u32 = {};
Jiri Pirko245dc512017-10-19 15:50:35 +0200488 bool skip_sw = tc_skip_sw(flags);
489 bool offloaded = false;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700490 int err;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800491
Jiri Pirkode4784c2017-08-07 10:15:32 +0200492 tc_cls_common_offload_init(&cls_u32.common, tp);
493 cls_u32.command = TC_CLSU32_NEW_HNODE;
494 cls_u32.hnode.divisor = h->divisor;
495 cls_u32.hnode.handle = h->handle;
496 cls_u32.hnode.prio = h->prio;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800497
Jiri Pirko245dc512017-10-19 15:50:35 +0200498 if (tc_can_offload(dev)) {
499 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32,
500 &cls_u32);
501 if (err) {
502 if (skip_sw)
503 return err;
504 } else {
505 offloaded = true;
506 }
507 }
508
509 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
510 if (err < 0) {
511 u32_clear_hw_hnode(tp, h);
Jakub Kicinskid47a0f32016-06-06 16:16:48 +0100512 return err;
Jiri Pirko245dc512017-10-19 15:50:35 +0200513 } else if (err > 0) {
514 offloaded = true;
515 }
516
517 if (skip_sw && !offloaded)
518 return -EINVAL;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700519
520 return 0;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800521}
522
Jiri Pirko77460412017-10-19 15:50:34 +0200523static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800524{
525 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirko245dc512017-10-19 15:50:35 +0200526 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200527 struct tc_cls_u32_offload cls_u32 = {};
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800528
Jiri Pirkode4784c2017-08-07 10:15:32 +0200529 tc_cls_common_offload_init(&cls_u32.common, tp);
Jiri Pirko77460412017-10-19 15:50:34 +0200530 cls_u32.command = TC_CLSU32_DELETE_KNODE;
531 cls_u32.knode.handle = handle;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800532
Jiri Pirko245dc512017-10-19 15:50:35 +0200533 if (tc_can_offload(dev))
534 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32);
535 tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800536}
537
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400538static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
539 u32 flags)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800540{
541 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirko245dc512017-10-19 15:50:35 +0200542 struct tcf_block *block = tp->chain->block;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200543 struct tc_cls_u32_offload cls_u32 = {};
Jiri Pirko245dc512017-10-19 15:50:35 +0200544 bool skip_sw = tc_skip_sw(flags);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700545 int err;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800546
Jiri Pirkode4784c2017-08-07 10:15:32 +0200547 tc_cls_common_offload_init(&cls_u32.common, tp);
548 cls_u32.command = TC_CLSU32_REPLACE_KNODE;
549 cls_u32.knode.handle = n->handle;
550 cls_u32.knode.fshift = n->fshift;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100551#ifdef CONFIG_CLS_U32_MARK
Jiri Pirkode4784c2017-08-07 10:15:32 +0200552 cls_u32.knode.val = n->val;
553 cls_u32.knode.mask = n->mask;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100554#else
Jiri Pirkode4784c2017-08-07 10:15:32 +0200555 cls_u32.knode.val = 0;
556 cls_u32.knode.mask = 0;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100557#endif
Jiri Pirkode4784c2017-08-07 10:15:32 +0200558 cls_u32.knode.sel = &n->sel;
559 cls_u32.knode.exts = &n->exts;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100560 if (n->ht_down)
Jiri Pirkode4784c2017-08-07 10:15:32 +0200561 cls_u32.knode.link_handle = n->ht_down->handle;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100562
Or Gerlitz24d3dc62017-02-16 10:31:15 +0200563
Jiri Pirko245dc512017-10-19 15:50:35 +0200564 if (tc_can_offload(dev)) {
565 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32,
566 &cls_u32);
567 if (err) {
568 if (skip_sw)
569 return err;
570 } else {
571 n->flags |= TCA_CLS_FLAGS_IN_HW;
572 }
573 }
Or Gerlitz24d3dc62017-02-16 10:31:15 +0200574
Jiri Pirko245dc512017-10-19 15:50:35 +0200575 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
576 if (err < 0) {
577 u32_remove_hw_knode(tp, n->handle);
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100578 return err;
Jiri Pirko245dc512017-10-19 15:50:35 +0200579 } else if (err > 0) {
580 n->flags |= TCA_CLS_FLAGS_IN_HW;
581 }
582
583 if (skip_sw && !(n->flags && TCA_CLS_FLAGS_IN_HW))
584 return -EINVAL;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700585
586 return 0;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800587}
588
WANG Conga0efb802014-09-30 16:07:24 -0700589static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct tc_u_knode *n;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000592 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000594 for (h = 0; h <= ht->divisor; h++) {
John Fastabend1ce87722014-09-12 20:09:16 -0700595 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
596 RCU_INIT_POINTER(ht->ht[h],
597 rtnl_dereference(n->next));
WANG Conga0efb802014-09-30 16:07:24 -0700598 tcf_unbind_filter(tp, &n->res);
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800599 u32_remove_hw_knode(tp, n->handle);
Cong Wange7614372017-09-25 10:13:51 -0700600 idr_remove_ext(&ht->handle_idr, n->handle);
John Fastabendde5df632014-09-19 21:50:34 -0700601 call_rcu(&n->rcu, u32_delete_key_freepf_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 }
604}
605
606static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
607{
608 struct tc_u_common *tp_c = tp->data;
John Fastabend1ce87722014-09-12 20:09:16 -0700609 struct tc_u_hnode __rcu **hn;
610 struct tc_u_hnode *phn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700612 WARN_ON(ht->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
WANG Conga0efb802014-09-30 16:07:24 -0700614 u32_clear_hnode(tp, ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
John Fastabend1ce87722014-09-12 20:09:16 -0700616 hn = &tp_c->hlist;
617 for (phn = rtnl_dereference(*hn);
618 phn;
619 hn = &phn->next, phn = rtnl_dereference(*hn)) {
620 if (phn == ht) {
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800621 u32_clear_hw_hnode(tp, ht);
Cong Wange7614372017-09-25 10:13:51 -0700622 idr_destroy(&ht->handle_idr);
623 idr_remove_ext(&tp_c->handle_idr, ht->handle);
John Fastabend1ce87722014-09-12 20:09:16 -0700624 RCU_INIT_POINTER(*hn, ht->next);
625 kfree_rcu(ht, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return 0;
627 }
628 }
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return -ENOENT;
631}
632
Cong Wang1e052be2015-03-06 11:47:59 -0800633static bool ht_empty(struct tc_u_hnode *ht)
634{
635 unsigned int h;
636
637 for (h = 0; h <= ht->divisor; h++)
638 if (rcu_access_pointer(ht->ht[h]))
639 return false;
640
641 return true;
642}
643
WANG Cong763dbf62017-04-19 14:21:21 -0700644static void u32_destroy(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 struct tc_u_common *tp_c = tp->data;
John Fastabend1ce87722014-09-12 20:09:16 -0700647 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700649 WARN_ON(root_ht == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (root_ht && --root_ht->refcnt == 0)
652 u32_destroy_hnode(tp, root_ht);
653
654 if (--tp_c->refcnt == 0) {
655 struct tc_u_hnode *ht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
WANG Cong3cd904e2017-08-24 16:51:30 -0700657 hlist_del(&tp_c->hnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
John Fastabend1ce87722014-09-12 20:09:16 -0700659 for (ht = rtnl_dereference(tp_c->hlist);
660 ht;
661 ht = rtnl_dereference(ht->next)) {
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700662 ht->refcnt--;
WANG Conga0efb802014-09-30 16:07:24 -0700663 u32_clear_hnode(tp, ht);
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
John Fastabend1ce87722014-09-12 20:09:16 -0700666 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
667 RCU_INIT_POINTER(tp_c->hlist, ht->next);
668 kfree_rcu(ht, rcu);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Cong Wange7614372017-09-25 10:13:51 -0700671 idr_destroy(&tp_c->handle_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 kfree(tp_c);
673 }
674
675 tp->data = NULL;
676}
677
WANG Cong8113c092017-08-04 21:31:43 -0700678static int u32_delete(struct tcf_proto *tp, void *arg, bool *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
WANG Cong8113c092017-08-04 21:31:43 -0700680 struct tc_u_hnode *ht = arg;
John Fastabend1ce87722014-09-12 20:09:16 -0700681 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
WANG Cong763dbf62017-04-19 14:21:21 -0700682 struct tc_u_common *tp_c = tp->data;
683 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (ht == NULL)
WANG Cong763dbf62017-04-19 14:21:21 -0700686 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800688 if (TC_U32_KEY(ht->handle)) {
689 u32_remove_hw_knode(tp, ht->handle);
WANG Cong763dbf62017-04-19 14:21:21 -0700690 ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
691 goto out;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
John Fastabend1ce87722014-09-12 20:09:16 -0700694 if (root_ht == ht)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -EINVAL;
696
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700697 if (ht->refcnt == 1) {
698 ht->refcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 u32_destroy_hnode(tp, ht);
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700700 } else {
701 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{
Cong Wange7614372017-09-25 10:13:51 -0700742 unsigned long idr_index;
743 u32 start = htid | 0x800;
744 u32 max = htid | 0xFFF;
745 u32 min = htid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Cong Wange7614372017-09-25 10:13:51 -0700747 if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
748 start, max + 1, GFP_KERNEL)) {
749 if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
750 min + 1, max + 1, GFP_KERNEL))
751 return max;
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Cong Wange7614372017-09-25 10:13:51 -0700754 return (u32)idr_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800757static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
758 [TCA_U32_CLASSID] = { .type = NLA_U32 },
759 [TCA_U32_HASH] = { .type = NLA_U32 },
760 [TCA_U32_LINK] = { .type = NLA_U32 },
761 [TCA_U32_DIVISOR] = { .type = NLA_U32 },
762 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
763 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
764 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
John Fastabend9e8ce792016-02-26 07:54:39 -0800765 [TCA_U32_FLAGS] = { .type = NLA_U32 },
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800766};
767
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000768static int u32_set_parms(struct net *net, struct tcf_proto *tp,
769 unsigned long base, struct tc_u_hnode *ht,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800770 struct tc_u_knode *n, struct nlattr **tb,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700771 struct nlattr *est, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
WANG Congb9a24bb2016-08-19 12:36:54 -0700773 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Jiri Pirko705c7092017-08-04 14:29:14 +0200775 err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (err < 0)
777 return err;
778
Patrick McHardyadd93b62008-01-22 22:11:33 -0800779 if (tb[TCA_U32_LINK]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800780 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000781 struct tc_u_hnode *ht_down = NULL, *ht_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 if (TC_U32_KEY(handle))
Jiri Pirko705c7092017-08-04 14:29:14 +0200784 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 if (handle) {
787 ht_down = u32_lookup_ht(ht->tp_c, handle);
788
789 if (ht_down == NULL)
Jiri Pirko705c7092017-08-04 14:29:14 +0200790 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 ht_down->refcnt++;
792 }
793
John Fastabend1ce87722014-09-12 20:09:16 -0700794 ht_old = rtnl_dereference(n->ht_down);
795 rcu_assign_pointer(n->ht_down, ht_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000797 if (ht_old)
798 ht_old->refcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Patrick McHardyadd93b62008-01-22 22:11:33 -0800800 if (tb[TCA_U32_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800801 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 tcf_bind_filter(tp, &n->res, base);
803 }
804
805#ifdef CONFIG_NET_CLS_IND
Patrick McHardyadd93b62008-01-22 22:11:33 -0800806 if (tb[TCA_U32_INDEV]) {
WANG Cong2519a602014-01-09 16:14:02 -0800807 int ret;
808 ret = tcf_change_indev(net, tb[TCA_U32_INDEV]);
809 if (ret < 0)
Jiri Pirko705c7092017-08-04 14:29:14 +0200810 return -EINVAL;
WANG Cong2519a602014-01-09 16:14:02 -0800811 n->ifindex = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400817static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
John Fastabendde5df632014-09-19 21:50:34 -0700818 struct tc_u_knode *n)
819{
820 struct tc_u_knode __rcu **ins;
821 struct tc_u_knode *pins;
822 struct tc_u_hnode *ht;
823
824 if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
825 ht = rtnl_dereference(tp->root);
826 else
827 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
828
829 ins = &ht->ht[TC_U32_HASH(n->handle)];
830
831 /* The node must always exist for it to be replaced if this is not the
832 * case then something went very wrong elsewhere.
833 */
834 for (pins = rtnl_dereference(*ins); ;
835 ins = &pins->next, pins = rtnl_dereference(*ins))
836 if (pins->handle == n->handle)
837 break;
838
Cong Wange7614372017-09-25 10:13:51 -0700839 idr_replace_ext(&ht->handle_idr, n, n->handle);
John Fastabendde5df632014-09-19 21:50:34 -0700840 RCU_INIT_POINTER(n->next, pins->next);
841 rcu_assign_pointer(*ins, n);
842}
843
844static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
845 struct tc_u_knode *n)
846{
847 struct tc_u_knode *new;
848 struct tc_u32_sel *s = &n->sel;
849
850 new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
851 GFP_KERNEL);
852
853 if (!new)
854 return NULL;
855
856 RCU_INIT_POINTER(new->next, n->next);
857 new->handle = n->handle;
858 RCU_INIT_POINTER(new->ht_up, n->ht_up);
859
860#ifdef CONFIG_NET_CLS_IND
861 new->ifindex = n->ifindex;
862#endif
863 new->fshift = n->fshift;
864 new->res = n->res;
John Fastabend9e8ce792016-02-26 07:54:39 -0800865 new->flags = n->flags;
John Fastabendde5df632014-09-19 21:50:34 -0700866 RCU_INIT_POINTER(new->ht_down, n->ht_down);
867
868 /* bump reference count as long as we hold pointer to structure */
869 if (new->ht_down)
870 new->ht_down->refcnt++;
871
872#ifdef CONFIG_CLS_U32_PERF
873 /* Statistics may be incremented by readers during update
874 * so we must keep them in tact. When the node is later destroyed
875 * a special destroy call must be made to not free the pf memory.
876 */
877 new->pf = n->pf;
878#endif
879
880#ifdef CONFIG_CLS_U32_MARK
881 new->val = n->val;
882 new->mask = n->mask;
883 /* Similarly success statistics must be moved as pointers */
884 new->pcpu_success = n->pcpu_success;
885#endif
886 new->tp = tp;
887 memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
888
WANG Congb9a24bb2016-08-19 12:36:54 -0700889 if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
890 kfree(new);
891 return NULL;
892 }
John Fastabendde5df632014-09-19 21:50:34 -0700893
894 return new;
895}
896
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000897static int u32_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600898 struct tcf_proto *tp, unsigned long base, u32 handle,
WANG Cong8113c092017-08-04 21:31:43 -0700899 struct nlattr **tca, void **arg, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 struct tc_u_common *tp_c = tp->data;
902 struct tc_u_hnode *ht;
903 struct tc_u_knode *n;
904 struct tc_u32_sel *s;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800905 struct nlattr *opt = tca[TCA_OPTIONS];
906 struct nlattr *tb[TCA_U32_MAX + 1];
John Fastabend9e8ce792016-02-26 07:54:39 -0800907 u32 htid, flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 int err;
John Fastabend459d5f62014-09-12 20:08:47 -0700909#ifdef CONFIG_CLS_U32_PERF
910 size_t size;
911#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 if (opt == NULL)
914 return handle ? -EINVAL : 0;
915
Johannes Bergfceb6432017-04-12 14:34:07 +0200916 err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800917 if (err < 0)
918 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700920 if (tb[TCA_U32_FLAGS]) {
John Fastabend9e8ce792016-02-26 07:54:39 -0800921 flags = nla_get_u32(tb[TCA_U32_FLAGS]);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700922 if (!tc_flags_valid(flags))
Jakub Kicinski1a0f7d22016-06-06 16:16:47 +0100923 return -EINVAL;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700924 }
John Fastabend9e8ce792016-02-26 07:54:39 -0800925
WANG Cong8113c092017-08-04 21:31:43 -0700926 n = *arg;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000927 if (n) {
John Fastabendde5df632014-09-19 21:50:34 -0700928 struct tc_u_knode *new;
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (TC_U32_KEY(n->handle) == 0)
931 return -EINVAL;
932
John Fastabend9e8ce792016-02-26 07:54:39 -0800933 if (n->flags != flags)
934 return -EINVAL;
935
John Fastabendde5df632014-09-19 21:50:34 -0700936 new = u32_init_knode(tp, n);
937 if (!new)
938 return -ENOMEM;
939
940 err = u32_set_parms(net, tp, base,
941 rtnl_dereference(n->ht_up), new, tb,
942 tca[TCA_RATE], ovr);
943
944 if (err) {
945 u32_destroy_key(tp, new, false);
946 return err;
947 }
948
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700949 err = u32_replace_hw_knode(tp, new, flags);
950 if (err) {
951 u32_destroy_key(tp, new, false);
952 return err;
953 }
954
Or Gerlitz24d3dc62017-02-16 10:31:15 +0200955 if (!tc_in_hw(new->flags))
956 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
957
John Fastabendde5df632014-09-19 21:50:34 -0700958 u32_replace_knode(tp, tp_c, new);
WANG Conga0efb802014-09-30 16:07:24 -0700959 tcf_unbind_filter(tp, &n->res);
John Fastabendde5df632014-09-19 21:50:34 -0700960 call_rcu(&n->rcu, u32_delete_key_rcu);
961 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963
Patrick McHardyadd93b62008-01-22 22:11:33 -0800964 if (tb[TCA_U32_DIVISOR]) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000965 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (--divisor > 0x100)
968 return -EINVAL;
969 if (TC_U32_KEY(handle))
970 return -EINVAL;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000971 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (ht == NULL)
973 return -ENOBUFS;
Cong Wange7614372017-09-25 10:13:51 -0700974 if (handle == 0) {
975 handle = gen_new_htid(tp->data, ht);
976 if (handle == 0) {
977 kfree(ht);
978 return -ENOMEM;
979 }
980 } else {
981 err = idr_alloc_ext(&tp_c->handle_idr, ht, NULL,
982 handle, handle + 1, GFP_KERNEL);
983 if (err) {
984 kfree(ht);
985 return err;
986 }
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ht->tp_c = tp_c;
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700989 ht->refcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ht->divisor = divisor;
991 ht->handle = handle;
992 ht->prio = tp->prio;
Cong Wange7614372017-09-25 10:13:51 -0700993 idr_init(&ht->handle_idr);
Jakub Kicinski6eef3802016-06-08 20:11:03 +0100994
995 err = u32_replace_hw_hnode(tp, ht, flags);
996 if (err) {
Cong Wange7614372017-09-25 10:13:51 -0700997 idr_remove_ext(&tp_c->handle_idr, handle);
Jakub Kicinski6eef3802016-06-08 20:11:03 +0100998 kfree(ht);
999 return err;
1000 }
1001
John Fastabend1ce87722014-09-12 20:09:16 -07001002 RCU_INIT_POINTER(ht->next, tp_c->hlist);
1003 rcu_assign_pointer(tp_c->hlist, ht);
WANG Cong8113c092017-08-04 21:31:43 -07001004 *arg = ht;
John Fastabenda1b7c5f2016-02-16 21:17:09 -08001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return 0;
1007 }
1008
Patrick McHardyadd93b62008-01-22 22:11:33 -08001009 if (tb[TCA_U32_HASH]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -08001010 htid = nla_get_u32(tb[TCA_U32_HASH]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
John Fastabend1ce87722014-09-12 20:09:16 -07001012 ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 htid = ht->handle;
1014 } else {
1015 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
1016 if (ht == NULL)
1017 return -EINVAL;
1018 }
1019 } else {
John Fastabend1ce87722014-09-12 20:09:16 -07001020 ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 htid = ht->handle;
1022 }
1023
1024 if (ht->divisor < TC_U32_HASH(htid))
1025 return -EINVAL;
1026
1027 if (handle) {
1028 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
1029 return -EINVAL;
1030 handle = htid | TC_U32_NODE(handle);
Cong Wange7614372017-09-25 10:13:51 -07001031 err = idr_alloc_ext(&ht->handle_idr, NULL, NULL,
1032 handle, handle + 1,
1033 GFP_KERNEL);
1034 if (err)
1035 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 } else
1037 handle = gen_new_kid(ht, htid);
1038
Cong Wange7614372017-09-25 10:13:51 -07001039 if (tb[TCA_U32_SEL] == NULL) {
1040 err = -EINVAL;
1041 goto erridr;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Patrick McHardyadd93b62008-01-22 22:11:33 -08001044 s = nla_data(tb[TCA_U32_SEL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001046 n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
Cong Wange7614372017-09-25 10:13:51 -07001047 if (n == NULL) {
1048 err = -ENOBUFS;
1049 goto erridr;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -07001053 size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1054 n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1055 if (!n->pf) {
Cong Wange7614372017-09-25 10:13:51 -07001056 err = -ENOBUFS;
1057 goto errfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059#endif
1060
1061 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
John Fastabenda96366bf2014-09-15 23:30:49 -07001062 RCU_INIT_POINTER(n->ht_up, ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 n->handle = handle;
Radu Rendecb2268012007-11-10 21:54:50 -08001064 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
John Fastabend9e8ce792016-02-26 07:54:39 -08001065 n->flags = flags;
John Fastabend1ce87722014-09-12 20:09:16 -07001066 n->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
WANG Congb9a24bb2016-08-19 12:36:54 -07001068 err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
1069 if (err < 0)
1070 goto errout;
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -07001073 n->pcpu_success = alloc_percpu(u32);
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001074 if (!n->pcpu_success) {
1075 err = -ENOMEM;
1076 goto errout;
1077 }
John Fastabend459d5f62014-09-12 20:08:47 -07001078
Patrick McHardyadd93b62008-01-22 22:11:33 -08001079 if (tb[TCA_U32_MARK]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct tc_u32_mark *mark;
1081
Patrick McHardyadd93b62008-01-22 22:11:33 -08001082 mark = nla_data(tb[TCA_U32_MARK]);
John Fastabend459d5f62014-09-12 20:08:47 -07001083 n->val = mark->val;
1084 n->mask = mark->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086#endif
1087
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001088 err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (err == 0) {
John Fastabend1ce87722014-09-12 20:09:16 -07001090 struct tc_u_knode __rcu **ins;
1091 struct tc_u_knode *pins;
1092
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -07001093 err = u32_replace_hw_knode(tp, n, flags);
1094 if (err)
1095 goto errhw;
1096
Or Gerlitz24d3dc62017-02-16 10:31:15 +02001097 if (!tc_in_hw(n->flags))
1098 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1099
John Fastabend1ce87722014-09-12 20:09:16 -07001100 ins = &ht->ht[TC_U32_HASH(handle)];
1101 for (pins = rtnl_dereference(*ins); pins;
1102 ins = &pins->next, pins = rtnl_dereference(*ins))
1103 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 break;
1105
John Fastabend1ce87722014-09-12 20:09:16 -07001106 RCU_INIT_POINTER(n->next, pins);
1107 rcu_assign_pointer(*ins, n);
WANG Cong8113c092017-08-04 21:31:43 -07001108 *arg = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return 0;
1110 }
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001111
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -07001112errhw:
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001113#ifdef CONFIG_CLS_U32_MARK
1114 free_percpu(n->pcpu_success);
1115#endif
1116
WANG Congb9a24bb2016-08-19 12:36:54 -07001117errout:
1118 tcf_exts_destroy(&n->exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119#ifdef CONFIG_CLS_U32_PERF
Cong Wange7614372017-09-25 10:13:51 -07001120errfree:
John Fastabend1ce87722014-09-12 20:09:16 -07001121 free_percpu(n->pf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122#endif
1123 kfree(n);
Cong Wange7614372017-09-25 10:13:51 -07001124erridr:
1125 idr_remove_ext(&ht->handle_idr, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return err;
1127}
1128
1129static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1130{
1131 struct tc_u_common *tp_c = tp->data;
1132 struct tc_u_hnode *ht;
1133 struct tc_u_knode *n;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001134 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 if (arg->stop)
1137 return;
1138
John Fastabend1ce87722014-09-12 20:09:16 -07001139 for (ht = rtnl_dereference(tp_c->hlist);
1140 ht;
1141 ht = rtnl_dereference(ht->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (ht->prio != tp->prio)
1143 continue;
1144 if (arg->count >= arg->skip) {
WANG Cong8113c092017-08-04 21:31:43 -07001145 if (arg->fn(tp, ht, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 arg->stop = 1;
1147 return;
1148 }
1149 }
1150 arg->count++;
1151 for (h = 0; h <= ht->divisor; h++) {
John Fastabend1ce87722014-09-12 20:09:16 -07001152 for (n = rtnl_dereference(ht->ht[h]);
1153 n;
1154 n = rtnl_dereference(n->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (arg->count < arg->skip) {
1156 arg->count++;
1157 continue;
1158 }
WANG Cong8113c092017-08-04 21:31:43 -07001159 if (arg->fn(tp, n, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 arg->stop = 1;
1161 return;
1162 }
1163 arg->count++;
1164 }
1165 }
1166 }
1167}
1168
Cong Wang07d79fc2017-08-30 14:30:36 -07001169static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
1170{
1171 struct tc_u_knode *n = fh;
1172
1173 if (n && n->res.classid == classid)
1174 n->res.class = cl;
1175}
1176
WANG Cong8113c092017-08-04 21:31:43 -07001177static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001178 struct sk_buff *skb, struct tcmsg *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
WANG Cong8113c092017-08-04 21:31:43 -07001180 struct tc_u_knode *n = fh;
John Fastabend1ce87722014-09-12 20:09:16 -07001181 struct tc_u_hnode *ht_up, *ht_down;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001182 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if (n == NULL)
1185 return skb->len;
1186
1187 t->tcm_handle = n->handle;
1188
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001189 nest = nla_nest_start(skb, TCA_OPTIONS);
1190 if (nest == NULL)
1191 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 if (TC_U32_KEY(n->handle) == 0) {
WANG Cong8113c092017-08-04 21:31:43 -07001194 struct tc_u_hnode *ht = fh;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001195 u32 divisor = ht->divisor + 1;
1196
David S. Miller1b34ec42012-03-29 05:11:39 -04001197 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1198 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 } else {
John Fastabend459d5f62014-09-12 20:08:47 -07001200#ifdef CONFIG_CLS_U32_PERF
1201 struct tc_u32_pcnt *gpf;
John Fastabend459d5f62014-09-12 20:08:47 -07001202 int cpu;
John Fastabend80aab732014-09-15 23:30:26 -07001203#endif
John Fastabend459d5f62014-09-12 20:08:47 -07001204
David S. Miller1b34ec42012-03-29 05:11:39 -04001205 if (nla_put(skb, TCA_U32_SEL,
1206 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1207 &n->sel))
1208 goto nla_put_failure;
John Fastabend1ce87722014-09-12 20:09:16 -07001209
1210 ht_up = rtnl_dereference(n->ht_up);
1211 if (ht_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 u32 htid = n->handle & 0xFFFFF000;
David S. Miller1b34ec42012-03-29 05:11:39 -04001213 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1214 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
David S. Miller1b34ec42012-03-29 05:11:39 -04001216 if (n->res.classid &&
1217 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1218 goto nla_put_failure;
John Fastabend1ce87722014-09-12 20:09:16 -07001219
1220 ht_down = rtnl_dereference(n->ht_down);
1221 if (ht_down &&
1222 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
David S. Miller1b34ec42012-03-29 05:11:39 -04001223 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
John Fastabend9e8ce792016-02-26 07:54:39 -08001225 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1226 goto nla_put_failure;
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -07001229 if ((n->val || n->mask)) {
1230 struct tc_u32_mark mark = {.val = n->val,
1231 .mask = n->mask,
1232 .success = 0};
John Fastabend80aab732014-09-15 23:30:26 -07001233 int cpum;
John Fastabend459d5f62014-09-12 20:08:47 -07001234
John Fastabend80aab732014-09-15 23:30:26 -07001235 for_each_possible_cpu(cpum) {
1236 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
John Fastabend459d5f62014-09-12 20:08:47 -07001237
1238 mark.success += cnt;
1239 }
1240
1241 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1242 goto nla_put_failure;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244#endif
1245
WANG Cong5da57f42013-12-15 20:15:07 -08001246 if (tcf_exts_dump(skb, &n->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001247 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -08001250 if (n->ifindex) {
1251 struct net_device *dev;
1252 dev = __dev_get_by_index(net, n->ifindex);
1253 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1254 goto nla_put_failure;
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256#endif
1257#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -07001258 gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1259 n->sel.nkeys * sizeof(u64),
1260 GFP_KERNEL);
1261 if (!gpf)
1262 goto nla_put_failure;
1263
1264 for_each_possible_cpu(cpu) {
1265 int i;
1266 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1267
1268 gpf->rcnt += pf->rcnt;
1269 gpf->rhit += pf->rhit;
1270 for (i = 0; i < n->sel.nkeys; i++)
1271 gpf->kcnts[i] += pf->kcnts[i];
1272 }
1273
Nicolas Dichtel98545182016-04-26 10:06:18 +02001274 if (nla_put_64bit(skb, TCA_U32_PCNT,
1275 sizeof(struct tc_u32_pcnt) +
1276 n->sel.nkeys * sizeof(u64),
1277 gpf, TCA_U32_PAD)) {
John Fastabend459d5f62014-09-12 20:08:47 -07001278 kfree(gpf);
David S. Miller1b34ec42012-03-29 05:11:39 -04001279 goto nla_put_failure;
John Fastabend459d5f62014-09-12 20:08:47 -07001280 }
1281 kfree(gpf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282#endif
1283 }
1284
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001285 nla_nest_end(skb, nest);
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (TC_U32_KEY(n->handle))
WANG Cong5da57f42013-12-15 20:15:07 -08001288 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001289 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return skb->len;
1291
Patrick McHardyadd93b62008-01-22 22:11:33 -08001292nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001293 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return -1;
1295}
1296
Patrick McHardy2eb9d752008-01-22 22:10:42 -08001297static struct tcf_proto_ops cls_u32_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 .kind = "u32",
1299 .classify = u32_classify,
1300 .init = u32_init,
1301 .destroy = u32_destroy,
1302 .get = u32_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 .change = u32_change,
1304 .delete = u32_delete,
1305 .walk = u32_walk,
1306 .dump = u32_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001307 .bind_class = u32_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 .owner = THIS_MODULE,
1309};
1310
1311static int __init init_u32(void)
1312{
WANG Cong3cd904e2017-08-24 16:51:30 -07001313 int i, ret;
1314
stephen hemminger6ff9c362010-05-12 06:37:05 +00001315 pr_info("u32 classifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316#ifdef CONFIG_CLS_U32_PERF
stephen hemminger6ff9c362010-05-12 06:37:05 +00001317 pr_info(" Performance counters on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319#ifdef CONFIG_NET_CLS_IND
stephen hemminger6ff9c362010-05-12 06:37:05 +00001320 pr_info(" input device check on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321#endif
1322#ifdef CONFIG_NET_CLS_ACT
stephen hemminger6ff9c362010-05-12 06:37:05 +00001323 pr_info(" Actions configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324#endif
WANG Cong3cd904e2017-08-24 16:51:30 -07001325 tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
1326 sizeof(struct hlist_head),
1327 GFP_KERNEL);
1328 if (!tc_u_common_hash)
1329 return -ENOMEM;
1330
1331 for (i = 0; i < U32_HASH_SIZE; i++)
1332 INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1333
1334 ret = register_tcf_proto_ops(&cls_u32_ops);
1335 if (ret)
1336 kvfree(tc_u_common_hash);
1337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001340static void __exit exit_u32(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 unregister_tcf_proto_ops(&cls_u32_ops);
WANG Cong3cd904e2017-08-24 16:51:30 -07001343 kvfree(tc_u_common_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346module_init(init_u32)
1347module_exit(exit_u32)
1348MODULE_LICENSE("GPL");