blob: 70c0be8d0121db461c1e21793a1b68d844f37e8b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/skbuff.h>
Cong Wang7801db82014-07-17 17:34:53 -070041#include <linux/bitmap.h>
Patrick McHardy0ba48052007-07-02 22:49:07 -070042#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <net/act_api.h>
44#include <net/pkt_cls.h>
45
Eric Dumazetcc7ec452011-01-19 19:26:56 +000046struct tc_u_knode {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct tc_u_knode *next;
48 u32 handle;
49 struct tc_u_hnode *ht_up;
50 struct tcf_exts exts;
51#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -080052 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#endif
54 u8 fshift;
55 struct tcf_result res;
56 struct tc_u_hnode *ht_down;
57#ifdef CONFIG_CLS_U32_PERF
58 struct tc_u32_pcnt *pf;
59#endif
60#ifdef CONFIG_CLS_U32_MARK
61 struct tc_u32_mark mark;
62#endif
63 struct tc_u32_sel sel;
64};
65
Eric Dumazetcc7ec452011-01-19 19:26:56 +000066struct tc_u_hnode {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct tc_u_hnode *next;
68 u32 handle;
69 u32 prio;
70 struct tc_u_common *tp_c;
71 int refcnt;
Eric Dumazetcc7ec452011-01-19 19:26:56 +000072 unsigned int divisor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct tc_u_knode *ht[1];
74};
75
Eric Dumazetcc7ec452011-01-19 19:26:56 +000076struct tc_u_common {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct tc_u_hnode *hlist;
78 struct Qdisc *q;
79 int refcnt;
80 u32 hgenerator;
81};
82
Eric Dumazetcc7ec452011-01-19 19:26:56 +000083static inline unsigned int u32_hash_fold(__be32 key,
84 const struct tc_u32_sel *sel,
85 u8 fshift)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Eric Dumazetcc7ec452011-01-19 19:26:56 +000087 unsigned int h = ntohl(key & sel->hmask) >> fshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 return h;
90}
91
Eric Dumazetdc7f9f62011-07-05 23:25:42 +000092static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp, struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct {
95 struct tc_u_knode *knode;
Changli Gaofbc2e7d2010-06-02 07:32:42 -070096 unsigned int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 } stack[TC_U32_MAXDEPTH];
98
WANG Conga8701a62014-01-09 16:14:03 -080099 struct tc_u_hnode *ht = tp->root;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700100 unsigned int off = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct tc_u_knode *n;
102 int sdepth = 0;
103 int off2 = 0;
104 int sel = 0;
105#ifdef CONFIG_CLS_U32_PERF
106 int j;
107#endif
108 int i, r;
109
110next_ht:
111 n = ht->ht[sel];
112
113next_knode:
114 if (n) {
115 struct tc_u32_key *key = n->sel.keys;
116
117#ifdef CONFIG_CLS_U32_PERF
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000118 n->pf->rcnt += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 j = 0;
120#endif
121
122#ifdef CONFIG_CLS_U32_MARK
Thomas Graf82e91ff2006-11-09 15:19:14 -0800123 if ((skb->mark & n->mark.mask) != n->mark.val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 n = n->next;
125 goto next_knode;
126 } else {
127 n->mark.success++;
128 }
129#endif
130
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000131 for (i = n->sel.nkeys; i > 0; i--, key++) {
stephen hemminger66d50d22010-08-02 13:44:13 +0000132 int toff = off + key->off + (off2 & key->offmask);
stephen hemminger86fce3b2011-02-20 16:14:23 +0000133 __be32 *data, hdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Dan Carpenter4e18b3e2010-10-04 02:28:36 +0000135 if (skb_headroom(skb) + toff > INT_MAX)
stephen hemminger66d50d22010-08-02 13:44:13 +0000136 goto out;
137
stephen hemminger86fce3b2011-02-20 16:14:23 +0000138 data = skb_header_pointer(skb, toff, 4, &hdata);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700139 if (!data)
140 goto out;
141 if ((*data ^ key->val) & key->mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 n = n->next;
143 goto next_knode;
144 }
145#ifdef CONFIG_CLS_U32_PERF
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000146 n->pf->kcnts[j] += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 j++;
148#endif
149 }
150 if (n->ht_down == NULL) {
151check_terminal:
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000152 if (n->sel.flags & TC_U32_TERMINAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 *res = n->res;
155#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -0800156 if (!tcf_match_indev(skb, n->ifindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 n = n->next;
158 goto next_knode;
159 }
160#endif
161#ifdef CONFIG_CLS_U32_PERF
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000162 n->pf->rhit += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#endif
164 r = tcf_exts_exec(skb, &n->exts, res);
165 if (r < 0) {
166 n = n->next;
167 goto next_knode;
168 }
169
170 return r;
171 }
172 n = n->next;
173 goto next_knode;
174 }
175
176 /* PUSH */
177 if (sdepth >= TC_U32_MAXDEPTH)
178 goto deadloop;
179 stack[sdepth].knode = n;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700180 stack[sdepth].off = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 sdepth++;
182
183 ht = n->ht_down;
184 sel = 0;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700185 if (ht->divisor) {
stephen hemminger86fce3b2011-02-20 16:14:23 +0000186 __be32 *data, hdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700188 data = skb_header_pointer(skb, off + n->sel.hoff, 4,
stephen hemminger86fce3b2011-02-20 16:14:23 +0000189 &hdata);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700190 if (!data)
191 goto out;
192 sel = ht->divisor & u32_hash_fold(*data, &n->sel,
193 n->fshift);
194 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000195 if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 goto next_ht;
197
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000198 if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 off2 = n->sel.off + 3;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700200 if (n->sel.flags & TC_U32_VAROFFSET) {
stephen hemminger86fce3b2011-02-20 16:14:23 +0000201 __be16 *data, hdata;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700202
203 data = skb_header_pointer(skb,
204 off + n->sel.offoff,
stephen hemminger86fce3b2011-02-20 16:14:23 +0000205 2, &hdata);
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700206 if (!data)
207 goto out;
208 off2 += ntohs(n->sel.offmask & *data) >>
209 n->sel.offshift;
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 off2 &= ~3;
212 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000213 if (n->sel.flags & TC_U32_EAT) {
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700214 off += off2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 off2 = 0;
216 }
217
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700218 if (off < skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 goto next_ht;
220 }
221
222 /* POP */
223 if (sdepth--) {
224 n = stack[sdepth].knode;
225 ht = n->ht_up;
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700226 off = stack[sdepth].off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto check_terminal;
228 }
Changli Gaofbc2e7d2010-06-02 07:32:42 -0700229out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -1;
231
232deadloop:
Joe Perchese87cc472012-05-13 21:56:26 +0000233 net_warn_ratelimited("cls_u32: dead loop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -1;
235}
236
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000237static struct tc_u_hnode *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
239{
240 struct tc_u_hnode *ht;
241
242 for (ht = tp_c->hlist; ht; ht = ht->next)
243 if (ht->handle == handle)
244 break;
245
246 return ht;
247}
248
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000249static struct tc_u_knode *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
251{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000252 unsigned int sel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct tc_u_knode *n = NULL;
254
255 sel = TC_U32_HASH(handle);
256 if (sel > ht->divisor)
257 goto out;
258
259 for (n = ht->ht[sel]; n; n = n->next)
260 if (n->handle == handle)
261 break;
262out:
263 return n;
264}
265
266
267static unsigned long u32_get(struct tcf_proto *tp, u32 handle)
268{
269 struct tc_u_hnode *ht;
270 struct tc_u_common *tp_c = tp->data;
271
272 if (TC_U32_HTID(handle) == TC_U32_ROOT)
273 ht = tp->root;
274 else
275 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
276
277 if (!ht)
278 return 0;
279
280 if (TC_U32_KEY(handle) == 0)
281 return (unsigned long)ht;
282
283 return (unsigned long)u32_lookup_key(ht, handle);
284}
285
286static void u32_put(struct tcf_proto *tp, unsigned long f)
287{
288}
289
290static u32 gen_new_htid(struct tc_u_common *tp_c)
291{
292 int i = 0x800;
293
294 do {
295 if (++tp_c->hgenerator == 0x7FF)
296 tp_c->hgenerator = 1;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000297 } while (--i > 0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0;
300}
301
302static int u32_init(struct tcf_proto *tp)
303{
304 struct tc_u_hnode *root_ht;
305 struct tc_u_common *tp_c;
306
David S. Miller72b25a92008-07-18 20:54:17 -0700307 tp_c = tp->q->u32_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700309 root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (root_ht == NULL)
311 return -ENOBUFS;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 root_ht->divisor = 0;
314 root_ht->refcnt++;
315 root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
316 root_ht->prio = tp->prio;
317
318 if (tp_c == NULL) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700319 tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (tp_c == NULL) {
321 kfree(root_ht);
322 return -ENOBUFS;
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 tp_c->q = tp->q;
David S. Miller72b25a92008-07-18 20:54:17 -0700325 tp->q->u32_node = tp_c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
328 tp_c->refcnt++;
329 root_ht->next = tp_c->hlist;
330 tp_c->hlist = root_ht;
331 root_ht->tp_c = tp_c;
332
333 tp->root = root_ht;
334 tp->data = tp_c;
335 return 0;
336}
337
338static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n)
339{
340 tcf_unbind_filter(tp, &n->res);
341 tcf_exts_destroy(tp, &n->exts);
342 if (n->ht_down)
343 n->ht_down->refcnt--;
344#ifdef CONFIG_CLS_U32_PERF
Patrick McHardy1ae39a42006-03-23 01:16:48 -0800345 kfree(n->pf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#endif
347 kfree(n);
348 return 0;
349}
350
Yang Yingliang82d567c2013-12-10 20:55:31 +0800351static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct tc_u_knode **kp;
354 struct tc_u_hnode *ht = key->ht_up;
355
356 if (ht) {
357 for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) {
358 if (*kp == key) {
359 tcf_tree_lock(tp);
360 *kp = key->next;
361 tcf_tree_unlock(tp);
362
363 u32_destroy_key(tp, key);
364 return 0;
365 }
366 }
367 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700368 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
371
372static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
373{
374 struct tc_u_knode *n;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000375 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000377 for (h = 0; h <= ht->divisor; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 while ((n = ht->ht[h]) != NULL) {
379 ht->ht[h] = n->next;
380
381 u32_destroy_key(tp, n);
382 }
383 }
384}
385
386static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
387{
388 struct tc_u_common *tp_c = tp->data;
389 struct tc_u_hnode **hn;
390
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700391 WARN_ON(ht->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 u32_clear_hnode(tp, ht);
394
395 for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) {
396 if (*hn == ht) {
397 *hn = ht->next;
398 kfree(ht);
399 return 0;
400 }
401 }
402
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700403 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return -ENOENT;
405}
406
407static void u32_destroy(struct tcf_proto *tp)
408{
409 struct tc_u_common *tp_c = tp->data;
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000410 struct tc_u_hnode *root_ht = tp->root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700412 WARN_ON(root_ht == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (root_ht && --root_ht->refcnt == 0)
415 u32_destroy_hnode(tp, root_ht);
416
417 if (--tp_c->refcnt == 0) {
418 struct tc_u_hnode *ht;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
David S. Miller72b25a92008-07-18 20:54:17 -0700420 tp->q->u32_node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700422 for (ht = tp_c->hlist; ht; ht = ht->next) {
423 ht->refcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 u32_clear_hnode(tp, ht);
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 while ((ht = tp_c->hlist) != NULL) {
428 tp_c->hlist = ht->next;
429
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700430 WARN_ON(ht->refcnt != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 kfree(ht);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 kfree(tp_c);
436 }
437
438 tp->data = NULL;
439}
440
441static int u32_delete(struct tcf_proto *tp, unsigned long arg)
442{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000443 struct tc_u_hnode *ht = (struct tc_u_hnode *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (ht == NULL)
446 return 0;
447
448 if (TC_U32_KEY(ht->handle))
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000449 return u32_delete_key(tp, (struct tc_u_knode *)ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (tp->root == ht)
452 return -EINVAL;
453
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700454 if (ht->refcnt == 1) {
455 ht->refcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 u32_destroy_hnode(tp, ht);
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700457 } else {
458 return -EBUSY;
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 return 0;
462}
463
Cong Wang7801db82014-07-17 17:34:53 -0700464#define NR_U32_NODE (1<<12)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle)
466{
467 struct tc_u_knode *n;
Cong Wang7801db82014-07-17 17:34:53 -0700468 unsigned long i;
469 unsigned long *bitmap = kzalloc(BITS_TO_LONGS(NR_U32_NODE) * sizeof(unsigned long),
470 GFP_KERNEL);
471 if (!bitmap)
472 return handle | 0xFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000474 for (n = ht->ht[TC_U32_HASH(handle)]; n; n = n->next)
Cong Wang7801db82014-07-17 17:34:53 -0700475 set_bit(TC_U32_NODE(n->handle), bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Cong Wang7801db82014-07-17 17:34:53 -0700477 i = find_next_zero_bit(bitmap, NR_U32_NODE, 0x800);
478 if (i >= NR_U32_NODE)
479 i = find_next_zero_bit(bitmap, NR_U32_NODE, 1);
480
481 kfree(bitmap);
482 return handle | (i >= NR_U32_NODE ? 0xFFF : i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800485static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
486 [TCA_U32_CLASSID] = { .type = NLA_U32 },
487 [TCA_U32_HASH] = { .type = NLA_U32 },
488 [TCA_U32_LINK] = { .type = NLA_U32 },
489 [TCA_U32_DIVISOR] = { .type = NLA_U32 },
490 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) },
491 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ },
492 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) },
493};
494
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000495static int u32_set_parms(struct net *net, struct tcf_proto *tp,
496 unsigned long base, struct tc_u_hnode *ht,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800497 struct tc_u_knode *n, struct nlattr **tb,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700498 struct nlattr *est, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 int err;
501 struct tcf_exts e;
502
WANG Cong5da57f42013-12-15 20:15:07 -0800503 tcf_exts_init(&e, TCA_U32_ACT, TCA_U32_POLICE);
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700504 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (err < 0)
506 return err;
507
508 err = -EINVAL;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800509 if (tb[TCA_U32_LINK]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800510 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000511 struct tc_u_hnode *ht_down = NULL, *ht_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if (TC_U32_KEY(handle))
514 goto errout;
515
516 if (handle) {
517 ht_down = u32_lookup_ht(ht->tp_c, handle);
518
519 if (ht_down == NULL)
520 goto errout;
521 ht_down->refcnt++;
522 }
523
524 tcf_tree_lock(tp);
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000525 ht_old = n->ht_down;
526 n->ht_down = ht_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 tcf_tree_unlock(tp);
528
Patrick McHardy47a1a1d2008-11-19 08:03:09 +0000529 if (ht_old)
530 ht_old->refcnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Patrick McHardyadd93b62008-01-22 22:11:33 -0800532 if (tb[TCA_U32_CLASSID]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800533 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 tcf_bind_filter(tp, &n->res, base);
535 }
536
537#ifdef CONFIG_NET_CLS_IND
Patrick McHardyadd93b62008-01-22 22:11:33 -0800538 if (tb[TCA_U32_INDEV]) {
WANG Cong2519a602014-01-09 16:14:02 -0800539 int ret;
540 ret = tcf_change_indev(net, tb[TCA_U32_INDEV]);
541 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto errout;
WANG Cong2519a602014-01-09 16:14:02 -0800543 n->ifindex = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545#endif
546 tcf_exts_change(tp, &n->exts, &e);
547
548 return 0;
549errout:
550 tcf_exts_destroy(tp, &e);
551 return err;
552}
553
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000554static int u32_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600555 struct tcf_proto *tp, unsigned long base, u32 handle,
Patrick McHardyadd93b62008-01-22 22:11:33 -0800556 struct nlattr **tca,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700557 unsigned long *arg, bool ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct tc_u_common *tp_c = tp->data;
560 struct tc_u_hnode *ht;
561 struct tc_u_knode *n;
562 struct tc_u32_sel *s;
Patrick McHardyadd93b62008-01-22 22:11:33 -0800563 struct nlattr *opt = tca[TCA_OPTIONS];
564 struct nlattr *tb[TCA_U32_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 u32 htid;
566 int err;
567
568 if (opt == NULL)
569 return handle ? -EINVAL : 0;
570
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800571 err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy);
Patrick McHardycee63722008-01-23 20:33:32 -0800572 if (err < 0)
573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000575 n = (struct tc_u_knode *)*arg;
576 if (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (TC_U32_KEY(n->handle) == 0)
578 return -EINVAL;
579
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000580 return u32_set_parms(net, tp, base, n->ht_up, n, tb,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700581 tca[TCA_RATE], ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
Patrick McHardyadd93b62008-01-22 22:11:33 -0800584 if (tb[TCA_U32_DIVISOR]) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000585 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (--divisor > 0x100)
588 return -EINVAL;
589 if (TC_U32_KEY(handle))
590 return -EINVAL;
591 if (handle == 0) {
592 handle = gen_new_htid(tp->data);
593 if (handle == 0)
594 return -ENOMEM;
595 }
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000596 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (ht == NULL)
598 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 ht->tp_c = tp_c;
Jarek Poplawskie56cfad2008-04-12 18:37:13 -0700600 ht->refcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 ht->divisor = divisor;
602 ht->handle = handle;
603 ht->prio = tp->prio;
604 ht->next = tp_c->hlist;
605 tp_c->hlist = ht;
606 *arg = (unsigned long)ht;
607 return 0;
608 }
609
Patrick McHardyadd93b62008-01-22 22:11:33 -0800610 if (tb[TCA_U32_HASH]) {
Patrick McHardy1587bac2008-01-23 20:35:03 -0800611 htid = nla_get_u32(tb[TCA_U32_HASH]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (TC_U32_HTID(htid) == TC_U32_ROOT) {
613 ht = tp->root;
614 htid = ht->handle;
615 } else {
616 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
617 if (ht == NULL)
618 return -EINVAL;
619 }
620 } else {
621 ht = tp->root;
622 htid = ht->handle;
623 }
624
625 if (ht->divisor < TC_U32_HASH(htid))
626 return -EINVAL;
627
628 if (handle) {
629 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid))
630 return -EINVAL;
631 handle = htid | TC_U32_NODE(handle);
632 } else
633 handle = gen_new_kid(ht, htid);
634
Patrick McHardy6fa8c012008-01-23 20:36:12 -0800635 if (tb[TCA_U32_SEL] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -EINVAL;
637
Patrick McHardyadd93b62008-01-22 22:11:33 -0800638 s = nla_data(tb[TCA_U32_SEL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700640 n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (n == NULL)
642 return -ENOBUFS;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644#ifdef CONFIG_CLS_U32_PERF
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700645 n->pf = kzalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (n->pf == NULL) {
647 kfree(n);
648 return -ENOBUFS;
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650#endif
651
652 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
653 n->ht_up = ht;
654 n->handle = handle;
Radu Rendecb2268012007-11-10 21:54:50 -0800655 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
WANG Cong5da57f42013-12-15 20:15:07 -0800656 tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658#ifdef CONFIG_CLS_U32_MARK
Patrick McHardyadd93b62008-01-22 22:11:33 -0800659 if (tb[TCA_U32_MARK]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct tc_u32_mark *mark;
661
Patrick McHardyadd93b62008-01-22 22:11:33 -0800662 mark = nla_data(tb[TCA_U32_MARK]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 memcpy(&n->mark, mark, sizeof(struct tc_u32_mark));
664 n->mark.success = 0;
665 }
666#endif
667
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700668 err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (err == 0) {
670 struct tc_u_knode **ins;
671 for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next)
672 if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle))
673 break;
674
675 n->next = *ins;
Jarek Poplawski6f573212009-01-05 18:14:19 -0800676 tcf_tree_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 *ins = n;
Jarek Poplawski6f573212009-01-05 18:14:19 -0800678 tcf_tree_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 *arg = (unsigned long)n;
681 return 0;
682 }
683#ifdef CONFIG_CLS_U32_PERF
Patrick McHardy1ae39a42006-03-23 01:16:48 -0800684 kfree(n->pf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685#endif
686 kfree(n);
687 return err;
688}
689
690static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
691{
692 struct tc_u_common *tp_c = tp->data;
693 struct tc_u_hnode *ht;
694 struct tc_u_knode *n;
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000695 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (arg->stop)
698 return;
699
700 for (ht = tp_c->hlist; ht; ht = ht->next) {
701 if (ht->prio != tp->prio)
702 continue;
703 if (arg->count >= arg->skip) {
704 if (arg->fn(tp, (unsigned long)ht, arg) < 0) {
705 arg->stop = 1;
706 return;
707 }
708 }
709 arg->count++;
710 for (h = 0; h <= ht->divisor; h++) {
711 for (n = ht->ht[h]; n; n = n->next) {
712 if (arg->count < arg->skip) {
713 arg->count++;
714 continue;
715 }
716 if (arg->fn(tp, (unsigned long)n, arg) < 0) {
717 arg->stop = 1;
718 return;
719 }
720 arg->count++;
721 }
722 }
723 }
724}
725
WANG Cong832d1d52014-01-09 16:14:01 -0800726static int u32_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct sk_buff *skb, struct tcmsg *t)
728{
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000729 struct tc_u_knode *n = (struct tc_u_knode *)fh;
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800730 struct nlattr *nest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (n == NULL)
733 return skb->len;
734
735 t->tcm_handle = n->handle;
736
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800737 nest = nla_nest_start(skb, TCA_OPTIONS);
738 if (nest == NULL)
739 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (TC_U32_KEY(n->handle) == 0) {
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000742 struct tc_u_hnode *ht = (struct tc_u_hnode *)fh;
743 u32 divisor = ht->divisor + 1;
744
David S. Miller1b34ec42012-03-29 05:11:39 -0400745 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
746 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 } else {
David S. Miller1b34ec42012-03-29 05:11:39 -0400748 if (nla_put(skb, TCA_U32_SEL,
749 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
750 &n->sel))
751 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (n->ht_up) {
753 u32 htid = n->handle & 0xFFFFF000;
David S. Miller1b34ec42012-03-29 05:11:39 -0400754 if (nla_put_u32(skb, TCA_U32_HASH, htid))
755 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400757 if (n->res.classid &&
758 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
759 goto nla_put_failure;
760 if (n->ht_down &&
761 nla_put_u32(skb, TCA_U32_LINK, n->ht_down->handle))
762 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764#ifdef CONFIG_CLS_U32_MARK
David S. Miller1b34ec42012-03-29 05:11:39 -0400765 if ((n->mark.val || n->mark.mask) &&
766 nla_put(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark))
767 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768#endif
769
WANG Cong5da57f42013-12-15 20:15:07 -0800770 if (tcf_exts_dump(skb, &n->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800771 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773#ifdef CONFIG_NET_CLS_IND
WANG Cong2519a602014-01-09 16:14:02 -0800774 if (n->ifindex) {
775 struct net_device *dev;
776 dev = __dev_get_by_index(net, n->ifindex);
777 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
778 goto nla_put_failure;
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780#endif
781#ifdef CONFIG_CLS_U32_PERF
David S. Miller1b34ec42012-03-29 05:11:39 -0400782 if (nla_put(skb, TCA_U32_PCNT,
783 sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64),
784 n->pf))
785 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#endif
787 }
788
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800789 nla_nest_end(skb, nest);
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (TC_U32_KEY(n->handle))
WANG Cong5da57f42013-12-15 20:15:07 -0800792 if (tcf_exts_dump_stats(skb, &n->exts) < 0)
Patrick McHardyadd93b62008-01-22 22:11:33 -0800793 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return skb->len;
795
Patrick McHardyadd93b62008-01-22 22:11:33 -0800796nla_put_failure:
Patrick McHardy4b3550ef2008-01-23 20:34:11 -0800797 nla_nest_cancel(skb, nest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return -1;
799}
800
Patrick McHardy2eb9d752008-01-22 22:10:42 -0800801static struct tcf_proto_ops cls_u32_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 .kind = "u32",
803 .classify = u32_classify,
804 .init = u32_init,
805 .destroy = u32_destroy,
806 .get = u32_get,
807 .put = u32_put,
808 .change = u32_change,
809 .delete = u32_delete,
810 .walk = u32_walk,
811 .dump = u32_dump,
812 .owner = THIS_MODULE,
813};
814
815static int __init init_u32(void)
816{
stephen hemminger6ff9c362010-05-12 06:37:05 +0000817 pr_info("u32 classifier\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818#ifdef CONFIG_CLS_U32_PERF
stephen hemminger6ff9c362010-05-12 06:37:05 +0000819 pr_info(" Performance counters on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821#ifdef CONFIG_NET_CLS_IND
stephen hemminger6ff9c362010-05-12 06:37:05 +0000822 pr_info(" input device check on\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823#endif
824#ifdef CONFIG_NET_CLS_ACT
stephen hemminger6ff9c362010-05-12 06:37:05 +0000825 pr_info(" Actions configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826#endif
827 return register_tcf_proto_ops(&cls_u32_ops);
828}
829
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900830static void __exit exit_u32(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 unregister_tcf_proto_ops(&cls_u32_ops);
833}
834
835module_init(init_u32)
836module_exit(exit_u32)
837MODULE_LICENSE("GPL");