2 * net/sched/cls_tcindex.c Packet classifier for skb->tc_index
4 * Written 1998,1999 by Werner Almesberger, EPFL ICA
7 #include <linux/module.h>
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/skbuff.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <net/act_api.h>
14 #include <net/netlink.h>
15 #include <net/pkt_cls.h>
18 * Passing parameters to the root seems to be done more awkwardly than really
19 * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
23 #define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */
24 #define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
27 struct tcindex_filter_result
{
29 struct tcf_result res
;
31 struct work_struct work
;
36 struct tcindex_filter
{
38 struct tcindex_filter_result result
;
39 struct tcindex_filter __rcu
*next
;
41 struct work_struct work
;
48 struct tcindex_filter_result
*perfect
; /* perfect hash; NULL if none */
49 struct tcindex_filter __rcu
**h
; /* imperfect hash; */
51 u16 mask
; /* AND key with mask */
52 u32 shift
; /* shift ANDed key to the right */
53 u32 hash
; /* hash table size; 0 if undefined */
54 u32 alloc_hash
; /* allocated size */
55 u32 fall_through
; /* 0: only classify if explicit match */
59 static inline int tcindex_filter_is_set(struct tcindex_filter_result
*r
)
61 return tcf_exts_has_actions(&r
->exts
) || r
->res
.classid
;
64 static struct tcindex_filter_result
*tcindex_lookup(struct tcindex_data
*p
,
68 struct tcindex_filter_result
*f
= p
->perfect
+ key
;
70 return tcindex_filter_is_set(f
) ? f
: NULL
;
72 struct tcindex_filter __rcu
**fp
;
73 struct tcindex_filter
*f
;
75 fp
= &p
->h
[key
% p
->hash
];
76 for (f
= rcu_dereference_bh_rtnl(*fp
);
78 fp
= &f
->next
, f
= rcu_dereference_bh_rtnl(*fp
))
87 static int tcindex_classify(struct sk_buff
*skb
, const struct tcf_proto
*tp
,
88 struct tcf_result
*res
)
90 struct tcindex_data
*p
= rcu_dereference_bh(tp
->root
);
91 struct tcindex_filter_result
*f
;
92 int key
= (skb
->tc_index
& p
->mask
) >> p
->shift
;
94 pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n",
97 f
= tcindex_lookup(p
, key
);
101 res
->classid
= TC_H_MAKE(TC_H_MAJ(tp
->q
->handle
), key
);
103 pr_debug("alg 0x%x\n", res
->classid
);
107 pr_debug("map 0x%x\n", res
->classid
);
109 return tcf_exts_exec(skb
, &f
->exts
, res
);
113 static void *tcindex_get(struct tcf_proto
*tp
, u32 handle
)
115 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
116 struct tcindex_filter_result
*r
;
118 pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp
, handle
);
119 if (p
->perfect
&& handle
>= p
->alloc_hash
)
121 r
= tcindex_lookup(p
, handle
);
122 return r
&& tcindex_filter_is_set(r
) ? r
: NULL
;
125 static int tcindex_init(struct tcf_proto
*tp
)
127 struct tcindex_data
*p
;
129 pr_debug("tcindex_init(tp %p)\n", tp
);
130 p
= kzalloc(sizeof(struct tcindex_data
), GFP_KERNEL
);
135 p
->hash
= DEFAULT_HASH_SIZE
;
138 rcu_assign_pointer(tp
->root
, p
);
142 static void __tcindex_destroy_rexts(struct tcindex_filter_result
*r
)
144 tcf_exts_destroy(&r
->exts
);
145 tcf_exts_put_net(&r
->exts
);
148 static void tcindex_destroy_rexts_work(struct work_struct
*work
)
150 struct tcindex_filter_result
*r
;
152 r
= container_of(work
, struct tcindex_filter_result
, work
);
154 __tcindex_destroy_rexts(r
);
158 static void tcindex_destroy_rexts(struct rcu_head
*head
)
160 struct tcindex_filter_result
*r
;
162 r
= container_of(head
, struct tcindex_filter_result
, rcu
);
163 INIT_WORK(&r
->work
, tcindex_destroy_rexts_work
);
164 tcf_queue_work(&r
->work
);
167 static void __tcindex_destroy_fexts(struct tcindex_filter
*f
)
169 tcf_exts_destroy(&f
->result
.exts
);
170 tcf_exts_put_net(&f
->result
.exts
);
174 static void tcindex_destroy_fexts_work(struct work_struct
*work
)
176 struct tcindex_filter
*f
= container_of(work
, struct tcindex_filter
,
180 __tcindex_destroy_fexts(f
);
184 static void tcindex_destroy_fexts(struct rcu_head
*head
)
186 struct tcindex_filter
*f
= container_of(head
, struct tcindex_filter
,
189 INIT_WORK(&f
->work
, tcindex_destroy_fexts_work
);
190 tcf_queue_work(&f
->work
);
193 static int tcindex_delete(struct tcf_proto
*tp
, void *arg
, bool *last
)
195 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
196 struct tcindex_filter_result
*r
= arg
;
197 struct tcindex_filter __rcu
**walk
;
198 struct tcindex_filter
*f
= NULL
;
200 pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp
, arg
, p
);
207 for (i
= 0; i
< p
->hash
; i
++) {
209 for (f
= rtnl_dereference(*walk
); f
;
210 walk
= &f
->next
, f
= rtnl_dereference(*walk
)) {
218 rcu_assign_pointer(*walk
, rtnl_dereference(f
->next
));
220 tcf_unbind_filter(tp
, &r
->res
);
221 /* all classifiers are required to call tcf_exts_destroy() after rcu
222 * grace period, since converted-to-rcu actions are relying on that
223 * in cleanup() callback
226 if (tcf_exts_get_net(&f
->result
.exts
))
227 call_rcu(&f
->rcu
, tcindex_destroy_fexts
);
229 __tcindex_destroy_fexts(f
);
231 if (tcf_exts_get_net(&r
->exts
))
232 call_rcu(&r
->rcu
, tcindex_destroy_rexts
);
234 __tcindex_destroy_rexts(r
);
241 static int tcindex_destroy_element(struct tcf_proto
*tp
,
242 void *arg
, struct tcf_walker
*walker
)
246 return tcindex_delete(tp
, arg
, &last
);
249 static void __tcindex_destroy(struct rcu_head
*head
)
251 struct tcindex_data
*p
= container_of(head
, struct tcindex_data
, rcu
);
259 valid_perfect_hash(struct tcindex_data
*p
)
261 return p
->hash
> (p
->mask
>> p
->shift
);
264 static const struct nla_policy tcindex_policy
[TCA_TCINDEX_MAX
+ 1] = {
265 [TCA_TCINDEX_HASH
] = { .type
= NLA_U32
},
266 [TCA_TCINDEX_MASK
] = { .type
= NLA_U16
},
267 [TCA_TCINDEX_SHIFT
] = { .type
= NLA_U32
},
268 [TCA_TCINDEX_FALL_THROUGH
] = { .type
= NLA_U32
},
269 [TCA_TCINDEX_CLASSID
] = { .type
= NLA_U32
},
272 static int tcindex_filter_result_init(struct tcindex_filter_result
*r
)
274 memset(r
, 0, sizeof(*r
));
275 return tcf_exts_init(&r
->exts
, TCA_TCINDEX_ACT
, TCA_TCINDEX_POLICE
);
278 static void __tcindex_partial_destroy(struct rcu_head
*head
)
280 struct tcindex_data
*p
= container_of(head
, struct tcindex_data
, rcu
);
286 static void tcindex_free_perfect_hash(struct tcindex_data
*cp
)
290 for (i
= 0; i
< cp
->hash
; i
++)
291 tcf_exts_destroy(&cp
->perfect
[i
].exts
);
295 static int tcindex_alloc_perfect_hash(struct tcindex_data
*cp
)
299 cp
->perfect
= kcalloc(cp
->hash
, sizeof(struct tcindex_filter_result
),
304 for (i
= 0; i
< cp
->hash
; i
++) {
305 err
= tcf_exts_init(&cp
->perfect
[i
].exts
,
306 TCA_TCINDEX_ACT
, TCA_TCINDEX_POLICE
);
314 tcindex_free_perfect_hash(cp
);
319 tcindex_set_parms(struct net
*net
, struct tcf_proto
*tp
, unsigned long base
,
320 u32 handle
, struct tcindex_data
*p
,
321 struct tcindex_filter_result
*r
, struct nlattr
**tb
,
322 struct nlattr
*est
, bool ovr
)
324 struct tcindex_filter_result new_filter_result
, *old_r
= r
;
325 struct tcindex_filter_result cr
;
326 struct tcindex_data
*cp
= NULL
, *oldp
;
327 struct tcindex_filter
*f
= NULL
; /* make gcc behave */
331 err
= tcf_exts_init(&e
, TCA_TCINDEX_ACT
, TCA_TCINDEX_POLICE
);
334 err
= tcf_exts_validate(net
, tp
, tb
, est
, &e
, ovr
);
339 /* tcindex_data attributes must look atomic to classifier/lookup so
340 * allocate new tcindex data and RCU assign it onto root. Keeping
341 * perfect hash and hash pointers from old data.
343 cp
= kzalloc(sizeof(*cp
), GFP_KERNEL
);
348 cp
->shift
= p
->shift
;
350 cp
->alloc_hash
= p
->alloc_hash
;
351 cp
->fall_through
= p
->fall_through
;
357 if (tcindex_alloc_perfect_hash(cp
) < 0)
359 for (i
= 0; i
< cp
->hash
; i
++)
360 cp
->perfect
[i
].res
= p
->perfect
[i
].res
;
365 err
= tcindex_filter_result_init(&new_filter_result
);
368 err
= tcindex_filter_result_init(&cr
);
374 if (tb
[TCA_TCINDEX_HASH
])
375 cp
->hash
= nla_get_u32(tb
[TCA_TCINDEX_HASH
]);
377 if (tb
[TCA_TCINDEX_MASK
])
378 cp
->mask
= nla_get_u16(tb
[TCA_TCINDEX_MASK
]);
380 if (tb
[TCA_TCINDEX_SHIFT
])
381 cp
->shift
= nla_get_u32(tb
[TCA_TCINDEX_SHIFT
]);
385 /* Hash already allocated, make sure that we still meet the
386 * requirements for the allocated hash.
389 if (!valid_perfect_hash(cp
) ||
390 cp
->hash
> cp
->alloc_hash
)
392 } else if (cp
->h
&& cp
->hash
!= cp
->alloc_hash
) {
397 if (tb
[TCA_TCINDEX_FALL_THROUGH
])
398 cp
->fall_through
= nla_get_u32(tb
[TCA_TCINDEX_FALL_THROUGH
]);
401 /* Hash not specified, use perfect hash if the upper limit
402 * of the hashing index is below the threshold.
404 if ((cp
->mask
>> cp
->shift
) < PERFECT_HASH_THRESHOLD
)
405 cp
->hash
= (cp
->mask
>> cp
->shift
) + 1;
407 cp
->hash
= DEFAULT_HASH_SIZE
;
410 if (!cp
->perfect
&& !cp
->h
)
411 cp
->alloc_hash
= cp
->hash
;
413 /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
414 * but then, we'd fail handles that may become valid after some future
415 * mask change. While this is extremely unlikely to ever matter,
416 * the check below is safer (and also more backwards-compatible).
418 if (cp
->perfect
|| valid_perfect_hash(cp
))
419 if (handle
>= cp
->alloc_hash
)
424 if (!cp
->perfect
&& !cp
->h
) {
425 if (valid_perfect_hash(cp
)) {
426 if (tcindex_alloc_perfect_hash(cp
) < 0)
430 struct tcindex_filter __rcu
**hash
;
432 hash
= kcalloc(cp
->hash
,
433 sizeof(struct tcindex_filter
*),
445 r
= cp
->perfect
+ handle
;
447 r
= tcindex_lookup(cp
, handle
) ? : &new_filter_result
;
449 if (r
== &new_filter_result
) {
450 f
= kzalloc(sizeof(*f
), GFP_KERNEL
);
455 err
= tcindex_filter_result_init(&f
->result
);
462 if (tb
[TCA_TCINDEX_CLASSID
]) {
463 cr
.res
.classid
= nla_get_u32(tb
[TCA_TCINDEX_CLASSID
]);
464 tcf_bind_filter(tp
, &cr
.res
, base
);
467 if (old_r
&& old_r
!= r
) {
468 err
= tcindex_filter_result_init(old_r
);
477 tcf_exts_change(&r
->exts
, &e
);
479 rcu_assign_pointer(tp
->root
, cp
);
481 if (r
== &new_filter_result
) {
482 struct tcindex_filter
*nfp
;
483 struct tcindex_filter __rcu
**fp
;
485 f
->result
.res
= r
->res
;
486 tcf_exts_change(&f
->result
.exts
, &r
->exts
);
488 fp
= cp
->h
+ (handle
% cp
->hash
);
489 for (nfp
= rtnl_dereference(*fp
);
491 fp
= &nfp
->next
, nfp
= rtnl_dereference(*fp
))
494 rcu_assign_pointer(*fp
, f
);
498 call_rcu(&oldp
->rcu
, __tcindex_partial_destroy
);
503 tcindex_free_perfect_hash(cp
);
504 else if (balloc
== 2)
507 tcf_exts_destroy(&cr
.exts
);
508 tcf_exts_destroy(&new_filter_result
.exts
);
511 tcf_exts_destroy(&e
);
516 tcindex_change(struct net
*net
, struct sk_buff
*in_skb
,
517 struct tcf_proto
*tp
, unsigned long base
, u32 handle
,
518 struct nlattr
**tca
, void **arg
, bool ovr
)
520 struct nlattr
*opt
= tca
[TCA_OPTIONS
];
521 struct nlattr
*tb
[TCA_TCINDEX_MAX
+ 1];
522 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
523 struct tcindex_filter_result
*r
= *arg
;
526 pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
527 "p %p,r %p,*arg %p\n",
528 tp
, handle
, tca
, arg
, opt
, p
, r
, arg
? *arg
: NULL
);
533 err
= nla_parse_nested(tb
, TCA_TCINDEX_MAX
, opt
, tcindex_policy
, NULL
);
537 return tcindex_set_parms(net
, tp
, base
, handle
, p
, r
, tb
,
541 static void tcindex_walk(struct tcf_proto
*tp
, struct tcf_walker
*walker
)
543 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
544 struct tcindex_filter
*f
, *next
;
547 pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp
, walker
, p
);
549 for (i
= 0; i
< p
->hash
; i
++) {
550 if (!p
->perfect
[i
].res
.class)
552 if (walker
->count
>= walker
->skip
) {
553 if (walker
->fn(tp
, p
->perfect
+ i
, walker
) < 0) {
563 for (i
= 0; i
< p
->hash
; i
++) {
564 for (f
= rtnl_dereference(p
->h
[i
]); f
; f
= next
) {
565 next
= rtnl_dereference(f
->next
);
566 if (walker
->count
>= walker
->skip
) {
567 if (walker
->fn(tp
, &f
->result
, walker
) < 0) {
577 static void tcindex_destroy(struct tcf_proto
*tp
)
579 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
580 struct tcf_walker walker
;
582 pr_debug("tcindex_destroy(tp %p),p %p\n", tp
, p
);
585 walker
.fn
= tcindex_destroy_element
;
586 tcindex_walk(tp
, &walker
);
588 call_rcu(&p
->rcu
, __tcindex_destroy
);
592 static int tcindex_dump(struct net
*net
, struct tcf_proto
*tp
, void *fh
,
593 struct sk_buff
*skb
, struct tcmsg
*t
)
595 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
596 struct tcindex_filter_result
*r
= fh
;
599 pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
600 tp
, fh
, skb
, t
, p
, r
);
601 pr_debug("p->perfect %p p->h %p\n", p
->perfect
, p
->h
);
603 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
605 goto nla_put_failure
;
608 t
->tcm_handle
= ~0; /* whatever ... */
609 if (nla_put_u32(skb
, TCA_TCINDEX_HASH
, p
->hash
) ||
610 nla_put_u16(skb
, TCA_TCINDEX_MASK
, p
->mask
) ||
611 nla_put_u32(skb
, TCA_TCINDEX_SHIFT
, p
->shift
) ||
612 nla_put_u32(skb
, TCA_TCINDEX_FALL_THROUGH
, p
->fall_through
))
613 goto nla_put_failure
;
614 nla_nest_end(skb
, nest
);
617 t
->tcm_handle
= r
- p
->perfect
;
619 struct tcindex_filter
*f
;
620 struct tcindex_filter __rcu
**fp
;
624 for (i
= 0; !t
->tcm_handle
&& i
< p
->hash
; i
++) {
626 for (f
= rtnl_dereference(*fp
);
628 fp
= &f
->next
, f
= rtnl_dereference(*fp
)) {
630 t
->tcm_handle
= f
->key
;
634 pr_debug("handle = %d\n", t
->tcm_handle
);
636 nla_put_u32(skb
, TCA_TCINDEX_CLASSID
, r
->res
.classid
))
637 goto nla_put_failure
;
639 if (tcf_exts_dump(skb
, &r
->exts
) < 0)
640 goto nla_put_failure
;
641 nla_nest_end(skb
, nest
);
643 if (tcf_exts_dump_stats(skb
, &r
->exts
) < 0)
644 goto nla_put_failure
;
650 nla_nest_cancel(skb
, nest
);
654 static void tcindex_bind_class(void *fh
, u32 classid
, unsigned long cl
)
656 struct tcindex_filter_result
*r
= fh
;
658 if (r
&& r
->res
.classid
== classid
)
662 static struct tcf_proto_ops cls_tcindex_ops __read_mostly
= {
664 .classify
= tcindex_classify
,
665 .init
= tcindex_init
,
666 .destroy
= tcindex_destroy
,
668 .change
= tcindex_change
,
669 .delete = tcindex_delete
,
670 .walk
= tcindex_walk
,
671 .dump
= tcindex_dump
,
672 .bind_class
= tcindex_bind_class
,
673 .owner
= THIS_MODULE
,
676 static int __init
init_tcindex(void)
678 return register_tcf_proto_ops(&cls_tcindex_ops
);
681 static void __exit
exit_tcindex(void)
683 unregister_tcf_proto_ops(&cls_tcindex_ops
);
686 module_init(init_tcindex
)
687 module_exit(exit_tcindex
)
688 MODULE_LICENSE("GPL");