blob: b6d46065f6618d45271d34f42e2f1f054f80ddba [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{
Jiri Pirko7fa9d972017-10-13 14:01:02 +0200338 return hash_64((u64) 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
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800465static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle)
466{
467 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200468 struct tc_cls_u32_offload cls_u32 = {};
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800469
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200470 if (!tc_should_offload(dev, 0))
Jiri Pirkode4784c2017-08-07 10:15:32 +0200471 return;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800472
Jiri Pirkode4784c2017-08-07 10:15:32 +0200473 tc_cls_common_offload_init(&cls_u32.common, tp);
474 cls_u32.command = TC_CLSU32_DELETE_KNODE;
475 cls_u32.knode.handle = handle;
476
477 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32);
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800478}
479
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400480static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
481 u32 flags)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800482{
483 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200484 struct tc_cls_u32_offload cls_u32 = {};
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700485 int err;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800486
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200487 if (!tc_should_offload(dev, flags))
Jakub Kicinskid47a0f32016-06-06 16:16:48 +0100488 return tc_skip_sw(flags) ? -EINVAL : 0;
489
Jiri Pirkode4784c2017-08-07 10:15:32 +0200490 tc_cls_common_offload_init(&cls_u32.common, tp);
491 cls_u32.command = TC_CLSU32_NEW_HNODE;
492 cls_u32.hnode.divisor = h->divisor;
493 cls_u32.hnode.handle = h->handle;
494 cls_u32.hnode.prio = h->prio;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800495
Jiri Pirkode4784c2017-08-07 10:15:32 +0200496 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32);
Jakub Kicinskid47a0f32016-06-06 16:16:48 +0100497 if (tc_skip_sw(flags))
498 return err;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700499
500 return 0;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800501}
502
503static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h)
504{
505 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200506 struct tc_cls_u32_offload cls_u32 = {};
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800507
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200508 if (!tc_should_offload(dev, 0))
Jiri Pirkode4784c2017-08-07 10:15:32 +0200509 return;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800510
Jiri Pirkode4784c2017-08-07 10:15:32 +0200511 tc_cls_common_offload_init(&cls_u32.common, tp);
512 cls_u32.command = TC_CLSU32_DELETE_HNODE;
513 cls_u32.hnode.divisor = h->divisor;
514 cls_u32.hnode.handle = h->handle;
515 cls_u32.hnode.prio = h->prio;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800516
Jiri Pirkode4784c2017-08-07 10:15:32 +0200517 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32);
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800518}
519
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400520static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
521 u32 flags)
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800522{
523 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200524 struct tc_cls_u32_offload cls_u32 = {};
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700525 int err;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800526
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200527 if (!tc_should_offload(dev, flags))
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100528 return tc_skip_sw(flags) ? -EINVAL : 0;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800529
Jiri Pirkode4784c2017-08-07 10:15:32 +0200530 tc_cls_common_offload_init(&cls_u32.common, tp);
531 cls_u32.command = TC_CLSU32_REPLACE_KNODE;
532 cls_u32.knode.handle = n->handle;
533 cls_u32.knode.fshift = n->fshift;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100534#ifdef CONFIG_CLS_U32_MARK
Jiri Pirkode4784c2017-08-07 10:15:32 +0200535 cls_u32.knode.val = n->val;
536 cls_u32.knode.mask = n->mask;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100537#else
Jiri Pirkode4784c2017-08-07 10:15:32 +0200538 cls_u32.knode.val = 0;
539 cls_u32.knode.mask = 0;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100540#endif
Jiri Pirkode4784c2017-08-07 10:15:32 +0200541 cls_u32.knode.sel = &n->sel;
542 cls_u32.knode.exts = &n->exts;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100543 if (n->ht_down)
Jiri Pirkode4784c2017-08-07 10:15:32 +0200544 cls_u32.knode.link_handle = n->ht_down->handle;
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100545
Jiri Pirkode4784c2017-08-07 10:15:32 +0200546 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32);
Or Gerlitz24d3dc62017-02-16 10:31:15 +0200547
548 if (!err)
549 n->flags |= TCA_CLS_FLAGS_IN_HW;
550
Jakub Kicinski201c44b2016-06-08 20:11:04 +0100551 if (tc_skip_sw(flags))
552 return err;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700553
554 return 0;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800555}
556
WANG Conga0efb802014-09-30 16:07:24 -0700557static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct tc_u_knode *n;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000560 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000562 for (h = 0; h <= ht->divisor; h++) {
John Fastabend1ce87722014-09-12 20:09:16 -0700563 while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
564 RCU_INIT_POINTER(ht->ht[h],
565 rtnl_dereference(n->next));
WANG Conga0efb802014-09-30 16:07:24 -0700566 tcf_unbind_filter(tp, &n->res);
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800567 u32_remove_hw_knode(tp, n->handle);
Cong Wange7614372017-09-25 10:13:51 -0700568 idr_remove_ext(&ht->handle_idr, n->handle);
John Fastabendde5df632014-09-19 21:50:34 -0700569 call_rcu(&n->rcu, u32_delete_key_freepf_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 }
572}
573
574static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
575{
576 struct tc_u_common *tp_c = tp->data;
John Fastabend1ce87722014-09-12 20:09:16 -0700577 struct tc_u_hnode __rcu **hn;
578 struct tc_u_hnode *phn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700580 WARN_ON(ht->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
WANG Conga0efb802014-09-30 16:07:24 -0700582 u32_clear_hnode(tp, ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
John Fastabend1ce87722014-09-12 20:09:16 -0700584 hn = &tp_c->hlist;
585 for (phn = rtnl_dereference(*hn);
586 phn;
587 hn = &phn->next, phn = rtnl_dereference(*hn)) {
588 if (phn == ht) {
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800589 u32_clear_hw_hnode(tp, ht);
Cong Wange7614372017-09-25 10:13:51 -0700590 idr_destroy(&ht->handle_idr);
591 idr_remove_ext(&tp_c->handle_idr, ht->handle);
John Fastabend1ce87722014-09-12 20:09:16 -0700592 RCU_INIT_POINTER(*hn, ht->next);
593 kfree_rcu(ht, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return 0;
595 }
596 }
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return -ENOENT;
599}
600
Cong Wang1e052be2015-03-06 11:47:59 -0800601static bool ht_empty(struct tc_u_hnode *ht)
602{
603 unsigned int h;
604
605 for (h = 0; h <= ht->divisor; h++)
606 if (rcu_access_pointer(ht->ht[h]))
607 return false;
608
609 return true;
610}
611
WANG Cong763dbf62017-04-19 14:21:21 -0700612static void u32_destroy(struct tcf_proto *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 struct tc_u_common *tp_c = tp->data;
John Fastabend1ce87722014-09-12 20:09:16 -0700615 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700617 WARN_ON(root_ht == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (root_ht && --root_ht->refcnt == 0)
620 u32_destroy_hnode(tp, root_ht);
621
622 if (--tp_c->refcnt == 0) {
623 struct tc_u_hnode *ht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
WANG Cong3cd904e2017-08-24 16:51:30 -0700625 hlist_del(&tp_c->hnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
John Fastabend1ce87722014-09-12 20:09:16 -0700627 for (ht = rtnl_dereference(tp_c->hlist);
628 ht;
629 ht = rtnl_dereference(ht->next)) {
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700630 ht->refcnt--;
WANG Conga0efb802014-09-30 16:07:24 -0700631 u32_clear_hnode(tp, ht);
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
John Fastabend1ce87722014-09-12 20:09:16 -0700634 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
635 RCU_INIT_POINTER(tp_c->hlist, ht->next);
636 kfree_rcu(ht, rcu);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Cong Wange7614372017-09-25 10:13:51 -0700639 idr_destroy(&tp_c->handle_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 kfree(tp_c);
641 }
642
643 tp->data = NULL;
644}
645
WANG Cong8113c092017-08-04 21:31:43 -0700646static int u32_delete(struct tcf_proto *tp, void *arg, bool *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
WANG Cong8113c092017-08-04 21:31:43 -0700648 struct tc_u_hnode *ht = arg;
John Fastabend1ce87722014-09-12 20:09:16 -0700649 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
WANG Cong763dbf62017-04-19 14:21:21 -0700650 struct tc_u_common *tp_c = tp->data;
651 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (ht == NULL)
WANG Cong763dbf62017-04-19 14:21:21 -0700654 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800656 if (TC_U32_KEY(ht->handle)) {
657 u32_remove_hw_knode(tp, ht->handle);
WANG Cong763dbf62017-04-19 14:21:21 -0700658 ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
659 goto out;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
John Fastabend1ce87722014-09-12 20:09:16 -0700662 if (root_ht == ht)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -EINVAL;
664
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700665 if (ht->refcnt == 1) {
666 ht->refcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 u32_destroy_hnode(tp, ht);
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700668 } else {
669 return -EBUSY;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
WANG Cong763dbf62017-04-19 14:21:21 -0700672out:
673 *last = true;
674 if (root_ht) {
675 if (root_ht->refcnt > 1) {
676 *last = false;
677 goto ret;
678 }
679 if (root_ht->refcnt == 1) {
680 if (!ht_empty(root_ht)) {
681 *last = false;
682 goto ret;
683 }
684 }
685 }
686
687 if (tp_c->refcnt > 1) {
688 *last = false;
689 goto ret;
690 }
691
692 if (tp_c->refcnt == 1) {
693 struct tc_u_hnode *ht;
694
695 for (ht = rtnl_dereference(tp_c->hlist);
696 ht;
697 ht = rtnl_dereference(ht->next))
698 if (!ht_empty(ht)) {
699 *last = false;
700 break;
701 }
702 }
703
704ret:
705 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Cong Wange7614372017-09-25 10:13:51 -0700708static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Cong Wange7614372017-09-25 10:13:51 -0700710 unsigned long idr_index;
711 u32 start = htid | 0x800;
712 u32 max = htid | 0xFFF;
713 u32 min = htid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Cong Wange7614372017-09-25 10:13:51 -0700715 if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
716 start, max + 1, GFP_KERNEL)) {
717 if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
718 min + 1, max + 1, GFP_KERNEL))
719 return max;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Cong Wange7614372017-09-25 10:13:51 -0700722 return (u32)idr_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800725static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
726 [TCA_U32_CLASSID] = { .type = NLA_U32 },
727 [TCA_U32_HASH] = { .type = NLA_U32 },
728 [TCA_U32_LINK] = { .type = NLA_U32 },
729 [TCA_U32_DIVISOR] = { .type = NLA_U32 },
730 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
731 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
732 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
John Fastabend9e8ce792016-02-26 07:54:39 -0800733 [TCA_U32_FLAGS] = { .type = NLA_U32 },
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800734};
735
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000736static int u32_set_parms(struct net *net, struct tcf_proto *tp,
737 unsigned long base, struct tc_u_hnode *ht,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800738 struct tc_u_knode *n, struct nlattr **tb,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700739 struct nlattr *est, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
WANG Congb9a24bb2016-08-19 12:36:54 -0700741 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Jiri Pirko705c7092017-08-04 14:29:14 +0200743 err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (err < 0)
745 return err;
746
Patrick McHardyadd93b62008-01-22 22:11:33 -0800747 if (tb[TCA_U32_LINK]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800748 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000749 struct tc_u_hnode *ht_down = NULL, *ht_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (TC_U32_KEY(handle))
Jiri Pirko705c7092017-08-04 14:29:14 +0200752 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (handle) {
755 ht_down = u32_lookup_ht(ht->tp_c, handle);
756
757 if (ht_down == NULL)
Jiri Pirko705c7092017-08-04 14:29:14 +0200758 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ht_down->refcnt++;
760 }
761
John Fastabend1ce87722014-09-12 20:09:16 -0700762 ht_old = rtnl_dereference(n->ht_down);
763 rcu_assign_pointer(n->ht_down, ht_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000765 if (ht_old)
766 ht_old->refcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Patrick McHardyadd93b62008-01-22 22:11:33 -0800768 if (tb[TCA_U32_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800769 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 tcf_bind_filter(tp, &n->res, base);
771 }
772
773#ifdef CONFIG_NET_CLS_IND
Patrick McHardyadd93b62008-01-22 22:11:33 -0800774 if (tb[TCA_U32_INDEV]) {
WANG Cong2519a602014-01-09 16:14:02 -0800775 int ret;
776 ret = tcf_change_indev(net, tb[TCA_U32_INDEV]);
777 if (ret < 0)
Jiri Pirko705c7092017-08-04 14:29:14 +0200778 return -EINVAL;
WANG Cong2519a602014-01-09 16:14:02 -0800779 n->ifindex = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400785static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
John Fastabendde5df632014-09-19 21:50:34 -0700786 struct tc_u_knode *n)
787{
788 struct tc_u_knode __rcu **ins;
789 struct tc_u_knode *pins;
790 struct tc_u_hnode *ht;
791
792 if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
793 ht = rtnl_dereference(tp->root);
794 else
795 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
796
797 ins = &ht->ht[TC_U32_HASH(n->handle)];
798
799 /* The node must always exist for it to be replaced if this is not the
800 * case then something went very wrong elsewhere.
801 */
802 for (pins = rtnl_dereference(*ins); ;
803 ins = &pins->next, pins = rtnl_dereference(*ins))
804 if (pins->handle == n->handle)
805 break;
806
Cong Wange7614372017-09-25 10:13:51 -0700807 idr_replace_ext(&ht->handle_idr, n, n->handle);
John Fastabendde5df632014-09-19 21:50:34 -0700808 RCU_INIT_POINTER(n->next, pins->next);
809 rcu_assign_pointer(*ins, n);
810}
811
812static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
813 struct tc_u_knode *n)
814{
815 struct tc_u_knode *new;
816 struct tc_u32_sel *s = &n->sel;
817
818 new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
819 GFP_KERNEL);
820
821 if (!new)
822 return NULL;
823
824 RCU_INIT_POINTER(new->next, n->next);
825 new->handle = n->handle;
826 RCU_INIT_POINTER(new->ht_up, n->ht_up);
827
828#ifdef CONFIG_NET_CLS_IND
829 new->ifindex = n->ifindex;
830#endif
831 new->fshift = n->fshift;
832 new->res = n->res;
John Fastabend9e8ce792016-02-26 07:54:39 -0800833 new->flags = n->flags;
John Fastabendde5df632014-09-19 21:50:34 -0700834 RCU_INIT_POINTER(new->ht_down, n->ht_down);
835
836 /* bump reference count as long as we hold pointer to structure */
837 if (new->ht_down)
838 new->ht_down->refcnt++;
839
840#ifdef CONFIG_CLS_U32_PERF
841 /* Statistics may be incremented by readers during update
842 * so we must keep them in tact. When the node is later destroyed
843 * a special destroy call must be made to not free the pf memory.
844 */
845 new->pf = n->pf;
846#endif
847
848#ifdef CONFIG_CLS_U32_MARK
849 new->val = n->val;
850 new->mask = n->mask;
851 /* Similarly success statistics must be moved as pointers */
852 new->pcpu_success = n->pcpu_success;
853#endif
854 new->tp = tp;
855 memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
856
WANG Congb9a24bb2016-08-19 12:36:54 -0700857 if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
858 kfree(new);
859 return NULL;
860 }
John Fastabendde5df632014-09-19 21:50:34 -0700861
862 return new;
863}
864
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000865static int u32_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600866 struct tcf_proto *tp, unsigned long base, u32 handle,
WANG Cong8113c092017-08-04 21:31:43 -0700867 struct nlattr **tca, void **arg, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 struct tc_u_common *tp_c = tp->data;
870 struct tc_u_hnode *ht;
871 struct tc_u_knode *n;
872 struct tc_u32_sel *s;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800873 struct nlattr *opt = tca[TCA_OPTIONS];
874 struct nlattr *tb[TCA_U32_MAX + 1];
John Fastabend9e8ce792016-02-26 07:54:39 -0800875 u32 htid, flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int err;
John Fastabend459d5f62014-09-12 20:08:47 -0700877#ifdef CONFIG_CLS_U32_PERF
878 size_t size;
879#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 if (opt == NULL)
882 return handle ? -EINVAL : 0;
883
Johannes Bergfceb6432017-04-12 14:34:07 +0200884 err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, NULL);
Patrick McHardycee63722008-01-23 20:33:32 -0800885 if (err < 0)
886 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700888 if (tb[TCA_U32_FLAGS]) {
John Fastabend9e8ce792016-02-26 07:54:39 -0800889 flags = nla_get_u32(tb[TCA_U32_FLAGS]);
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700890 if (!tc_flags_valid(flags))
Jakub Kicinski1a0f7d22016-06-06 16:16:47 +0100891 return -EINVAL;
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700892 }
John Fastabend9e8ce792016-02-26 07:54:39 -0800893
WANG Cong8113c092017-08-04 21:31:43 -0700894 n = *arg;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000895 if (n) {
John Fastabendde5df632014-09-19 21:50:34 -0700896 struct tc_u_knode *new;
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (TC_U32_KEY(n->handle) == 0)
899 return -EINVAL;
900
John Fastabend9e8ce792016-02-26 07:54:39 -0800901 if (n->flags != flags)
902 return -EINVAL;
903
John Fastabendde5df632014-09-19 21:50:34 -0700904 new = u32_init_knode(tp, n);
905 if (!new)
906 return -ENOMEM;
907
908 err = u32_set_parms(net, tp, base,
909 rtnl_dereference(n->ht_up), new, tb,
910 tca[TCA_RATE], ovr);
911
912 if (err) {
913 u32_destroy_key(tp, new, false);
914 return err;
915 }
916
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700917 err = u32_replace_hw_knode(tp, new, flags);
918 if (err) {
919 u32_destroy_key(tp, new, false);
920 return err;
921 }
922
Or Gerlitz24d3dc62017-02-16 10:31:15 +0200923 if (!tc_in_hw(new->flags))
924 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
925
John Fastabendde5df632014-09-19 21:50:34 -0700926 u32_replace_knode(tp, tp_c, new);
WANG Conga0efb802014-09-30 16:07:24 -0700927 tcf_unbind_filter(tp, &n->res);
John Fastabendde5df632014-09-19 21:50:34 -0700928 call_rcu(&n->rcu, u32_delete_key_rcu);
929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
Patrick McHardyadd93b62008-01-22 22:11:33 -0800932 if (tb[TCA_U32_DIVISOR]) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000933 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 if (--divisor > 0x100)
936 return -EINVAL;
937 if (TC_U32_KEY(handle))
938 return -EINVAL;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000939 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (ht == NULL)
941 return -ENOBUFS;
Cong Wange7614372017-09-25 10:13:51 -0700942 if (handle == 0) {
943 handle = gen_new_htid(tp->data, ht);
944 if (handle == 0) {
945 kfree(ht);
946 return -ENOMEM;
947 }
948 } else {
949 err = idr_alloc_ext(&tp_c->handle_idr, ht, NULL,
950 handle, handle + 1, GFP_KERNEL);
951 if (err) {
952 kfree(ht);
953 return err;
954 }
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 ht->tp_c = tp_c;
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700957 ht->refcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 ht->divisor = divisor;
959 ht->handle = handle;
960 ht->prio = tp->prio;
Cong Wange7614372017-09-25 10:13:51 -0700961 idr_init(&ht->handle_idr);
Jakub Kicinski6eef3802016-06-08 20:11:03 +0100962
963 err = u32_replace_hw_hnode(tp, ht, flags);
964 if (err) {
Cong Wange7614372017-09-25 10:13:51 -0700965 idr_remove_ext(&tp_c->handle_idr, handle);
Jakub Kicinski6eef3802016-06-08 20:11:03 +0100966 kfree(ht);
967 return err;
968 }
969
John Fastabend1ce87722014-09-12 20:09:16 -0700970 RCU_INIT_POINTER(ht->next, tp_c->hlist);
971 rcu_assign_pointer(tp_c->hlist, ht);
WANG Cong8113c092017-08-04 21:31:43 -0700972 *arg = ht;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 0;
975 }
976
Patrick McHardyadd93b62008-01-22 22:11:33 -0800977 if (tb[TCA_U32_HASH]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800978 htid = nla_get_u32(tb[TCA_U32_HASH]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
John Fastabend1ce87722014-09-12 20:09:16 -0700980 ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 htid = ht->handle;
982 } else {
983 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
984 if (ht == NULL)
985 return -EINVAL;
986 }
987 } else {
John Fastabend1ce87722014-09-12 20:09:16 -0700988 ht = rtnl_dereference(tp->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 htid = ht->handle;
990 }
991
992 if (ht->divisor < TC_U32_HASH(htid))
993 return -EINVAL;
994
995 if (handle) {
996 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
997 return -EINVAL;
998 handle = htid | TC_U32_NODE(handle);
Cong Wange7614372017-09-25 10:13:51 -0700999 err = idr_alloc_ext(&ht->handle_idr, NULL, NULL,
1000 handle, handle + 1,
1001 GFP_KERNEL);
1002 if (err)
1003 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 } else
1005 handle = gen_new_kid(ht, htid);
1006
Cong Wange7614372017-09-25 10:13:51 -07001007 if (tb[TCA_U32_SEL] == NULL) {
1008 err = -EINVAL;
1009 goto erridr;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Patrick McHardyadd93b62008-01-22 22:11:33 -08001012 s = nla_data(tb[TCA_U32_SEL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001014 n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
Cong Wange7614372017-09-25 10:13:51 -07001015 if (n == NULL) {
1016 err = -ENOBUFS;
1017 goto erridr;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -07001021 size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1022 n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1023 if (!n->pf) {
Cong Wange7614372017-09-25 10:13:51 -07001024 err = -ENOBUFS;
1025 goto errfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027#endif
1028
1029 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
John Fastabenda96366bf2014-09-15 23:30:49 -07001030 RCU_INIT_POINTER(n->ht_up, ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 n->handle = handle;
Radu Rendecb2268012007-11-10 21:54:50 -08001032 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
John Fastabend9e8ce792016-02-26 07:54:39 -08001033 n->flags = flags;
John Fastabend1ce87722014-09-12 20:09:16 -07001034 n->tp = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
WANG Congb9a24bb2016-08-19 12:36:54 -07001036 err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
1037 if (err < 0)
1038 goto errout;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -07001041 n->pcpu_success = alloc_percpu(u32);
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001042 if (!n->pcpu_success) {
1043 err = -ENOMEM;
1044 goto errout;
1045 }
John Fastabend459d5f62014-09-12 20:08:47 -07001046
Patrick McHardyadd93b62008-01-22 22:11:33 -08001047 if (tb[TCA_U32_MARK]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct tc_u32_mark *mark;
1049
Patrick McHardyadd93b62008-01-22 22:11:33 -08001050 mark = nla_data(tb[TCA_U32_MARK]);
John Fastabend459d5f62014-09-12 20:08:47 -07001051 n->val = mark->val;
1052 n->mask = mark->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054#endif
1055
Cong Wang2f7ef2f2014-04-25 13:54:06 -07001056 err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (err == 0) {
John Fastabend1ce87722014-09-12 20:09:16 -07001058 struct tc_u_knode __rcu **ins;
1059 struct tc_u_knode *pins;
1060
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -07001061 err = u32_replace_hw_knode(tp, n, flags);
1062 if (err)
1063 goto errhw;
1064
Or Gerlitz24d3dc62017-02-16 10:31:15 +02001065 if (!tc_in_hw(n->flags))
1066 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1067
John Fastabend1ce87722014-09-12 20:09:16 -07001068 ins = &ht->ht[TC_U32_HASH(handle)];
1069 for (pins = rtnl_dereference(*ins); pins;
1070 ins = &pins->next, pins = rtnl_dereference(*ins))
1071 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 break;
1073
John Fastabend1ce87722014-09-12 20:09:16 -07001074 RCU_INIT_POINTER(n->next, pins);
1075 rcu_assign_pointer(*ins, n);
WANG Cong8113c092017-08-04 21:31:43 -07001076 *arg = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return 0;
1078 }
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001079
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -07001080errhw:
John Fastabenda1ddcfe2014-09-19 21:50:04 -07001081#ifdef CONFIG_CLS_U32_MARK
1082 free_percpu(n->pcpu_success);
1083#endif
1084
WANG Congb9a24bb2016-08-19 12:36:54 -07001085errout:
1086 tcf_exts_destroy(&n->exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087#ifdef CONFIG_CLS_U32_PERF
Cong Wange7614372017-09-25 10:13:51 -07001088errfree:
John Fastabend1ce87722014-09-12 20:09:16 -07001089 free_percpu(n->pf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090#endif
1091 kfree(n);
Cong Wange7614372017-09-25 10:13:51 -07001092erridr:
1093 idr_remove_ext(&ht->handle_idr, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return err;
1095}
1096
1097static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1098{
1099 struct tc_u_common *tp_c = tp->data;
1100 struct tc_u_hnode *ht;
1101 struct tc_u_knode *n;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001102 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 if (arg->stop)
1105 return;
1106
John Fastabend1ce87722014-09-12 20:09:16 -07001107 for (ht = rtnl_dereference(tp_c->hlist);
1108 ht;
1109 ht = rtnl_dereference(ht->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (ht->prio != tp->prio)
1111 continue;
1112 if (arg->count >= arg->skip) {
WANG Cong8113c092017-08-04 21:31:43 -07001113 if (arg->fn(tp, ht, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 arg->stop = 1;
1115 return;
1116 }
1117 }
1118 arg->count++;
1119 for (h = 0; h <= ht->divisor; h++) {
John Fastabend1ce87722014-09-12 20:09:16 -07001120 for (n = rtnl_dereference(ht->ht[h]);
1121 n;
1122 n = rtnl_dereference(n->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (arg->count < arg->skip) {
1124 arg->count++;
1125 continue;
1126 }
WANG Cong8113c092017-08-04 21:31:43 -07001127 if (arg->fn(tp, n, arg) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 arg->stop = 1;
1129 return;
1130 }
1131 arg->count++;
1132 }
1133 }
1134 }
1135}
1136
Cong Wang07d79fc2017-08-30 14:30:36 -07001137static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
1138{
1139 struct tc_u_knode *n = fh;
1140
1141 if (n && n->res.classid == classid)
1142 n->res.class = cl;
1143}
1144
WANG Cong8113c092017-08-04 21:31:43 -07001145static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -04001146 struct sk_buff *skb, struct tcmsg *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
WANG Cong8113c092017-08-04 21:31:43 -07001148 struct tc_u_knode *n = fh;
John Fastabend1ce87722014-09-12 20:09:16 -07001149 struct tc_u_hnode *ht_up, *ht_down;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001150 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (n == NULL)
1153 return skb->len;
1154
1155 t->tcm_handle = n->handle;
1156
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001157 nest = nla_nest_start(skb, TCA_OPTIONS);
1158 if (nest == NULL)
1159 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 if (TC_U32_KEY(n->handle) == 0) {
WANG Cong8113c092017-08-04 21:31:43 -07001162 struct tc_u_hnode *ht = fh;
Eric Dumazetcc7ec452011-01-19 19:26:56 +00001163 u32 divisor = ht->divisor + 1;
1164
David S. Miller1b34ec42012-03-29 05:11:39 -04001165 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1166 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 } else {
John Fastabend459d5f62014-09-12 20:08:47 -07001168#ifdef CONFIG_CLS_U32_PERF
1169 struct tc_u32_pcnt *gpf;
John Fastabend459d5f62014-09-12 20:08:47 -07001170 int cpu;
John Fastabend80aab732014-09-15 23:30:26 -07001171#endif
John Fastabend459d5f62014-09-12 20:08:47 -07001172
David S. Miller1b34ec42012-03-29 05:11:39 -04001173 if (nla_put(skb, TCA_U32_SEL,
1174 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1175 &n->sel))
1176 goto nla_put_failure;
John Fastabend1ce87722014-09-12 20:09:16 -07001177
1178 ht_up = rtnl_dereference(n->ht_up);
1179 if (ht_up) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 u32 htid = n->handle & 0xFFFFF000;
David S. Miller1b34ec42012-03-29 05:11:39 -04001181 if (nla_put_u32(skb, TCA_U32_HASH, htid))
1182 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
David S. Miller1b34ec42012-03-29 05:11:39 -04001184 if (n->res.classid &&
1185 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1186 goto nla_put_failure;
John Fastabend1ce87722014-09-12 20:09:16 -07001187
1188 ht_down = rtnl_dereference(n->ht_down);
1189 if (ht_down &&
1190 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
David S. Miller1b34ec42012-03-29 05:11:39 -04001191 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
John Fastabend9e8ce792016-02-26 07:54:39 -08001193 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1194 goto nla_put_failure;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196#ifdef CONFIG_CLS_U32_MARK
John Fastabend459d5f62014-09-12 20:08:47 -07001197 if ((n->val || n->mask)) {
1198 struct tc_u32_mark mark = {.val = n->val,
1199 .mask = n->mask,
1200 .success = 0};
John Fastabend80aab732014-09-15 23:30:26 -07001201 int cpum;
John Fastabend459d5f62014-09-12 20:08:47 -07001202
John Fastabend80aab732014-09-15 23:30:26 -07001203 for_each_possible_cpu(cpum) {
1204 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
John Fastabend459d5f62014-09-12 20:08:47 -07001205
1206 mark.success += cnt;
1207 }
1208
1209 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1210 goto nla_put_failure;
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212#endif
1213
WANG Cong5da57f42013-12-15 20:15:07 -08001214 if (tcf_exts_dump(skb, &n->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001215 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -08001218 if (n->ifindex) {
1219 struct net_device *dev;
1220 dev = __dev_get_by_index(net, n->ifindex);
1221 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1222 goto nla_put_failure;
1223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224#endif
1225#ifdef CONFIG_CLS_U32_PERF
John Fastabend459d5f62014-09-12 20:08:47 -07001226 gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1227 n->sel.nkeys * sizeof(u64),
1228 GFP_KERNEL);
1229 if (!gpf)
1230 goto nla_put_failure;
1231
1232 for_each_possible_cpu(cpu) {
1233 int i;
1234 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1235
1236 gpf->rcnt += pf->rcnt;
1237 gpf->rhit += pf->rhit;
1238 for (i = 0; i < n->sel.nkeys; i++)
1239 gpf->kcnts[i] += pf->kcnts[i];
1240 }
1241
Nicolas Dichtel98545182016-04-26 10:06:18 +02001242 if (nla_put_64bit(skb, TCA_U32_PCNT,
1243 sizeof(struct tc_u32_pcnt) +
1244 n->sel.nkeys * sizeof(u64),
1245 gpf, TCA_U32_PAD)) {
John Fastabend459d5f62014-09-12 20:08:47 -07001246 kfree(gpf);
David S. Miller1b34ec42012-03-29 05:11:39 -04001247 goto nla_put_failure;
John Fastabend459d5f62014-09-12 20:08:47 -07001248 }
1249 kfree(gpf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250#endif
1251 }
1252
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001253 nla_nest_end(skb, nest);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (TC_U32_KEY(n->handle))
WANG Cong5da57f42013-12-15 20:15:07 -08001256 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -08001257 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return skb->len;
1259
Patrick McHardyadd93b62008-01-22 22:11:33 -08001260nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -08001261 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return -1;
1263}
1264
Patrick McHardy2eb9d752008-01-22 22:10:42 -08001265static struct tcf_proto_ops cls_u32_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 .kind = "u32",
1267 .classify = u32_classify,
1268 .init = u32_init,
1269 .destroy = u32_destroy,
1270 .get = u32_get,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 .change = u32_change,
1272 .delete = u32_delete,
1273 .walk = u32_walk,
1274 .dump = u32_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001275 .bind_class = u32_bind_class,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 .owner = THIS_MODULE,
1277};
1278
1279static int __init init_u32(void)
1280{
WANG Cong3cd904e2017-08-24 16:51:30 -07001281 int i, ret;
1282
stephen hemminger6ff9c362010-05-12 06:37:05 +00001283 pr_info("u32 classifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284#ifdef CONFIG_CLS_U32_PERF
stephen hemminger6ff9c362010-05-12 06:37:05 +00001285 pr_info(" Performance counters on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287#ifdef CONFIG_NET_CLS_IND
stephen hemminger6ff9c362010-05-12 06:37:05 +00001288 pr_info(" input device check on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289#endif
1290#ifdef CONFIG_NET_CLS_ACT
stephen hemminger6ff9c362010-05-12 06:37:05 +00001291 pr_info(" Actions configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292#endif
WANG Cong3cd904e2017-08-24 16:51:30 -07001293 tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
1294 sizeof(struct hlist_head),
1295 GFP_KERNEL);
1296 if (!tc_u_common_hash)
1297 return -ENOMEM;
1298
1299 for (i = 0; i < U32_HASH_SIZE; i++)
1300 INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1301
1302 ret = register_tcf_proto_ops(&cls_u32_ops);
1303 if (ret)
1304 kvfree(tc_u_common_hash);
1305 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +09001308static void __exit exit_u32(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 unregister_tcf_proto_ops(&cls_u32_ops);
WANG Cong3cd904e2017-08-24 16:51:30 -07001311 kvfree(tc_u_common_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314module_init(init_u32)
1315module_exit(exit_u32)
1316MODULE_LICENSE("GPL");