33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/types.h>
36 #include <linux/kernel.h>
37 #include <linux/string.h>
38 #include <linux/errno.h>
39 #include <linux/rtnetlink.h>
50 #ifdef CONFIG_NET_CLS_IND
56 #ifdef CONFIG_CLS_U32_PERF
59 #ifdef CONFIG_CLS_U32_MARK
87 static inline unsigned int u32_hash_fold(
__be32 key,
104 unsigned int off = skb_network_offset(skb);
109 #ifdef CONFIG_CLS_U32_PERF
121 #ifdef CONFIG_CLS_U32_PERF
126 #ifdef CONFIG_CLS_U32_MARK
127 if ((skb->
mark & n->mark.mask) != n->mark.val) {
135 for (i = n->
sel.nkeys; i > 0; i--, key++) {
139 if (skb_headroom(skb) + toff >
INT_MAX)
142 data = skb_header_pointer(skb, toff, 4, &hdata);
145 if ((*data ^ key->
val) & key->
mask) {
149 #ifdef CONFIG_CLS_U32_PERF
150 n->pf->kcnts[
j] += 1;
159 #ifdef CONFIG_NET_CLS_IND
160 if (!tcf_match_indev(skb, n->indev)) {
165 #ifdef CONFIG_CLS_U32_PERF
168 r = tcf_exts_exec(skb, &n->
exts, res);
192 data = skb_header_pointer(skb,
off + n->
sel.hoff, 4,
196 sel = ht->
divisor & u32_hash_fold(*data, &n->
sel,
203 off2 = n->
sel.off + 3;
207 data = skb_header_pointer(skb,
212 off2 +=
ntohs(n->
sel.offmask & *data) >>
228 n =
stack[sdepth].knode;
246 for (ht = tp_c->
hlist; ht; ht = ht->
next)
263 for (n = ht->
ht[sel]; n; n = n->
next)
271 static unsigned long u32_get(
struct tcf_proto *tp,
u32 handle)
285 return (
unsigned long)ht;
287 return (
unsigned long)u32_lookup_key(ht, handle);
290 static void u32_put(
struct tcf_proto *tp,
unsigned long f)
301 }
while (--i > 0 && u32_lookup_ht(tp_c, (tp_c->
hgenerator|0x800)<<20));
303 return i > 0 ? (tp_c->
hgenerator|0x800)<<20 : 0;
306 static int u32_init(
struct tcf_proto *tp)
311 tp_c = tp->
q->u32_node;
313 root_ht = kzalloc(
sizeof(*root_ht),
GFP_KERNEL);
319 root_ht->
handle = tp_c ? gen_new_htid(tp_c) : 0x80000000;
329 tp->
q->u32_node = tp_c;
334 tp_c->
hlist = root_ht;
335 root_ht->
tp_c = tp_c;
344 tcf_unbind_filter(tp, &n->
res);
348 #ifdef CONFIG_CLS_U32_PERF
367 u32_destroy_key(tp, key);
381 for (h = 0; h <= ht->
divisor; h++) {
382 while ((n = ht->
ht[h]) !=
NULL) {
385 u32_destroy_key(tp, n);
397 u32_clear_hnode(tp, ht);
399 for (hn = &tp_c->
hlist; *hn; hn = &(*hn)->
next) {
411 static void u32_destroy(
struct tcf_proto *tp)
418 if (root_ht && --root_ht->
refcnt == 0)
419 u32_destroy_hnode(tp, root_ht);
421 if (--tp_c->
refcnt == 0) {
424 tp->
q->u32_node =
NULL;
426 for (ht = tp_c->
hlist; ht; ht = ht->
next) {
428 u32_clear_hnode(tp, ht);
445 static int u32_delete(
struct tcf_proto *tp,
unsigned long arg)
460 u32_destroy_hnode(tp, ht);
471 unsigned int i = 0x7FF;
478 return handle | (i > 0xFFF ? 0xFFF :
i);
491 static int u32_set_parms(
struct tcf_proto *tp,
unsigned long base,
505 u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
512 ht_down = u32_lookup_ht(ht->
tp_c, handle);
528 n->
res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
529 tcf_bind_filter(tp, &n->
res, base);
532 #ifdef CONFIG_NET_CLS_IND
534 err = tcf_change_indev(tp, n->indev, tb[TCA_U32_INDEV]);
547 static int u32_change(
struct sk_buff *in_skb,
562 return handle ? -
EINVAL : 0;
564 err = nla_parse_nested(tb,
TCA_U32_MAX, opt, u32_policy);
573 return u32_set_parms(tp, base, n->
ht_up, n, tb, tca[
TCA_RATE]);
577 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
579 if (--divisor > 0x100)
584 handle = gen_new_htid(tp->
data);
588 ht = kzalloc(
sizeof(*ht) + divisor*
sizeof(
void *),
GFP_KERNEL);
598 *arg = (
unsigned long)ht;
603 htid = nla_get_u32(tb[TCA_U32_HASH]);
625 handle = gen_new_kid(ht, htid);
636 #ifdef CONFIG_CLS_U32_PERF
649 #ifdef CONFIG_CLS_U32_MARK
653 mark = nla_data(tb[TCA_U32_MARK]);
659 err = u32_set_parms(tp, base, ht, n, tb, tca[
TCA_RATE]);
671 *arg = (
unsigned long)n;
674 #ifdef CONFIG_CLS_U32_PERF
691 for (ht = tp_c->
hlist; ht; ht = ht->
next) {
695 if (arg->
fn(tp, (
unsigned long)ht, arg) < 0) {
701 for (h = 0; h <= ht->
divisor; h++) {
702 for (n = ht->
ht[h]; n; n = n->
next) {
707 if (arg->
fn(tp, (
unsigned long)n, arg) < 0) {
717 static int u32_dump(
struct tcf_proto *tp,
unsigned long fh,
730 goto nla_put_failure;
736 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
737 goto nla_put_failure;
742 goto nla_put_failure;
745 if (nla_put_u32(skb, TCA_U32_HASH, htid))
746 goto nla_put_failure;
748 if (n->
res.classid &&
749 nla_put_u32(skb, TCA_U32_CLASSID, n->
res.classid))
750 goto nla_put_failure;
752 nla_put_u32(skb, TCA_U32_LINK, n->
ht_down->handle))
753 goto nla_put_failure;
755 #ifdef CONFIG_CLS_U32_MARK
756 if ((n->mark.val || n->mark.mask) &&
757 nla_put(skb, TCA_U32_MARK,
sizeof(n->mark), &n->mark))
758 goto nla_put_failure;
762 goto nla_put_failure;
764 #ifdef CONFIG_NET_CLS_IND
766 nla_put_string(skb, TCA_U32_INDEV, n->indev))
767 goto nla_put_failure;
769 #ifdef CONFIG_CLS_U32_PERF
773 goto nla_put_failure;
777 nla_nest_end(skb, nest);
781 goto nla_put_failure;
785 nla_nest_cancel(skb, nest);
791 .classify = u32_classify,
793 .destroy = u32_destroy,
796 .change = u32_change,
797 .delete = u32_delete,
803 static int __init init_u32(
void)
806 #ifdef CONFIG_CLS_U32_PERF
807 pr_info(
" Performance counters on\n");
809 #ifdef CONFIG_NET_CLS_IND
810 pr_info(
" input device check on\n");
812 #ifdef CONFIG_NET_CLS_ACT
813 pr_info(
" Actions configured\n");
818 static void __exit exit_u32(
void)