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_work(struct work_struct
*work
)
144 struct tcindex_filter_result
*r
;
146 r
= container_of(work
, struct tcindex_filter_result
, work
);
148 tcf_exts_destroy(&r
->exts
);
152 static void tcindex_destroy_rexts(struct rcu_head
*head
)
154 struct tcindex_filter_result
*r
;
156 r
= container_of(head
, struct tcindex_filter_result
, rcu
);
157 INIT_WORK(&r
->work
, tcindex_destroy_rexts_work
);
158 tcf_queue_work(&r
->work
);
161 static void tcindex_destroy_fexts_work(struct work_struct
*work
)
163 struct tcindex_filter
*f
= container_of(work
, struct tcindex_filter
,
167 tcf_exts_destroy(&f
->result
.exts
);
172 static void tcindex_destroy_fexts(struct rcu_head
*head
)
174 struct tcindex_filter
*f
= container_of(head
, struct tcindex_filter
,
177 INIT_WORK(&f
->work
, tcindex_destroy_fexts_work
);
178 tcf_queue_work(&f
->work
);
181 static int tcindex_delete(struct tcf_proto
*tp
, void *arg
, bool *last
)
183 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
184 struct tcindex_filter_result
*r
= arg
;
185 struct tcindex_filter __rcu
**walk
;
186 struct tcindex_filter
*f
= NULL
;
188 pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp
, arg
, p
);
195 for (i
= 0; i
< p
->hash
; i
++) {
197 for (f
= rtnl_dereference(*walk
); f
;
198 walk
= &f
->next
, f
= rtnl_dereference(*walk
)) {
206 rcu_assign_pointer(*walk
, rtnl_dereference(f
->next
));
208 tcf_unbind_filter(tp
, &r
->res
);
209 /* all classifiers are required to call tcf_exts_destroy() after rcu
210 * grace period, since converted-to-rcu actions are relying on that
211 * in cleanup() callback
214 call_rcu(&f
->rcu
, tcindex_destroy_fexts
);
216 call_rcu(&r
->rcu
, tcindex_destroy_rexts
);
222 static int tcindex_destroy_element(struct tcf_proto
*tp
,
223 void *arg
, struct tcf_walker
*walker
)
227 return tcindex_delete(tp
, arg
, &last
);
230 static void __tcindex_destroy(struct rcu_head
*head
)
232 struct tcindex_data
*p
= container_of(head
, struct tcindex_data
, rcu
);
240 valid_perfect_hash(struct tcindex_data
*p
)
242 return p
->hash
> (p
->mask
>> p
->shift
);
245 static const struct nla_policy tcindex_policy
[TCA_TCINDEX_MAX
+ 1] = {
246 [TCA_TCINDEX_HASH
] = { .type
= NLA_U32
},
247 [TCA_TCINDEX_MASK
] = { .type
= NLA_U16
},
248 [TCA_TCINDEX_SHIFT
] = { .type
= NLA_U32
},
249 [TCA_TCINDEX_FALL_THROUGH
] = { .type
= NLA_U32
},
250 [TCA_TCINDEX_CLASSID
] = { .type
= NLA_U32
},
253 static int tcindex_filter_result_init(struct tcindex_filter_result
*r
)
255 memset(r
, 0, sizeof(*r
));
256 return tcf_exts_init(&r
->exts
, TCA_TCINDEX_ACT
, TCA_TCINDEX_POLICE
);
259 static void __tcindex_partial_destroy(struct rcu_head
*head
)
261 struct tcindex_data
*p
= container_of(head
, struct tcindex_data
, rcu
);
267 static void tcindex_free_perfect_hash(struct tcindex_data
*cp
)
271 for (i
= 0; i
< cp
->hash
; i
++)
272 tcf_exts_destroy(&cp
->perfect
[i
].exts
);
276 static int tcindex_alloc_perfect_hash(struct tcindex_data
*cp
)
280 cp
->perfect
= kcalloc(cp
->hash
, sizeof(struct tcindex_filter_result
),
285 for (i
= 0; i
< cp
->hash
; i
++) {
286 err
= tcf_exts_init(&cp
->perfect
[i
].exts
,
287 TCA_TCINDEX_ACT
, TCA_TCINDEX_POLICE
);
295 tcindex_free_perfect_hash(cp
);
300 tcindex_set_parms(struct net
*net
, struct tcf_proto
*tp
, unsigned long base
,
301 u32 handle
, struct tcindex_data
*p
,
302 struct tcindex_filter_result
*r
, struct nlattr
**tb
,
303 struct nlattr
*est
, bool ovr
)
305 struct tcindex_filter_result new_filter_result
, *old_r
= r
;
306 struct tcindex_filter_result cr
;
307 struct tcindex_data
*cp
= NULL
, *oldp
;
308 struct tcindex_filter
*f
= NULL
; /* make gcc behave */
312 err
= tcf_exts_init(&e
, TCA_TCINDEX_ACT
, TCA_TCINDEX_POLICE
);
315 err
= tcf_exts_validate(net
, tp
, tb
, est
, &e
, ovr
);
320 /* tcindex_data attributes must look atomic to classifier/lookup so
321 * allocate new tcindex data and RCU assign it onto root. Keeping
322 * perfect hash and hash pointers from old data.
324 cp
= kzalloc(sizeof(*cp
), GFP_KERNEL
);
329 cp
->shift
= p
->shift
;
331 cp
->alloc_hash
= p
->alloc_hash
;
332 cp
->fall_through
= p
->fall_through
;
338 if (tcindex_alloc_perfect_hash(cp
) < 0)
340 for (i
= 0; i
< cp
->hash
; i
++)
341 cp
->perfect
[i
].res
= p
->perfect
[i
].res
;
346 err
= tcindex_filter_result_init(&new_filter_result
);
349 err
= tcindex_filter_result_init(&cr
);
355 if (tb
[TCA_TCINDEX_HASH
])
356 cp
->hash
= nla_get_u32(tb
[TCA_TCINDEX_HASH
]);
358 if (tb
[TCA_TCINDEX_MASK
])
359 cp
->mask
= nla_get_u16(tb
[TCA_TCINDEX_MASK
]);
361 if (tb
[TCA_TCINDEX_SHIFT
])
362 cp
->shift
= nla_get_u32(tb
[TCA_TCINDEX_SHIFT
]);
366 /* Hash already allocated, make sure that we still meet the
367 * requirements for the allocated hash.
370 if (!valid_perfect_hash(cp
) ||
371 cp
->hash
> cp
->alloc_hash
)
373 } else if (cp
->h
&& cp
->hash
!= cp
->alloc_hash
) {
378 if (tb
[TCA_TCINDEX_FALL_THROUGH
])
379 cp
->fall_through
= nla_get_u32(tb
[TCA_TCINDEX_FALL_THROUGH
]);
382 /* Hash not specified, use perfect hash if the upper limit
383 * of the hashing index is below the threshold.
385 if ((cp
->mask
>> cp
->shift
) < PERFECT_HASH_THRESHOLD
)
386 cp
->hash
= (cp
->mask
>> cp
->shift
) + 1;
388 cp
->hash
= DEFAULT_HASH_SIZE
;
391 if (!cp
->perfect
&& !cp
->h
)
392 cp
->alloc_hash
= cp
->hash
;
394 /* Note: this could be as restrictive as if (handle & ~(mask >> shift))
395 * but then, we'd fail handles that may become valid after some future
396 * mask change. While this is extremely unlikely to ever matter,
397 * the check below is safer (and also more backwards-compatible).
399 if (cp
->perfect
|| valid_perfect_hash(cp
))
400 if (handle
>= cp
->alloc_hash
)
405 if (!cp
->perfect
&& !cp
->h
) {
406 if (valid_perfect_hash(cp
)) {
407 if (tcindex_alloc_perfect_hash(cp
) < 0)
411 struct tcindex_filter __rcu
**hash
;
413 hash
= kcalloc(cp
->hash
,
414 sizeof(struct tcindex_filter
*),
426 r
= cp
->perfect
+ handle
;
428 r
= tcindex_lookup(cp
, handle
) ? : &new_filter_result
;
430 if (r
== &new_filter_result
) {
431 f
= kzalloc(sizeof(*f
), GFP_KERNEL
);
436 err
= tcindex_filter_result_init(&f
->result
);
443 if (tb
[TCA_TCINDEX_CLASSID
]) {
444 cr
.res
.classid
= nla_get_u32(tb
[TCA_TCINDEX_CLASSID
]);
445 tcf_bind_filter(tp
, &cr
.res
, base
);
449 tcf_exts_change(&r
->exts
, &e
);
451 tcf_exts_change(&cr
.exts
, &e
);
453 if (old_r
&& old_r
!= r
) {
454 err
= tcindex_filter_result_init(old_r
);
463 rcu_assign_pointer(tp
->root
, cp
);
465 if (r
== &new_filter_result
) {
466 struct tcindex_filter
*nfp
;
467 struct tcindex_filter __rcu
**fp
;
469 tcf_exts_change(&f
->result
.exts
, &r
->exts
);
471 fp
= cp
->h
+ (handle
% cp
->hash
);
472 for (nfp
= rtnl_dereference(*fp
);
474 fp
= &nfp
->next
, nfp
= rtnl_dereference(*fp
))
477 rcu_assign_pointer(*fp
, f
);
481 call_rcu(&oldp
->rcu
, __tcindex_partial_destroy
);
486 tcindex_free_perfect_hash(cp
);
487 else if (balloc
== 2)
490 tcf_exts_destroy(&cr
.exts
);
491 tcf_exts_destroy(&new_filter_result
.exts
);
494 tcf_exts_destroy(&e
);
499 tcindex_change(struct net
*net
, struct sk_buff
*in_skb
,
500 struct tcf_proto
*tp
, unsigned long base
, u32 handle
,
501 struct nlattr
**tca
, void **arg
, bool ovr
)
503 struct nlattr
*opt
= tca
[TCA_OPTIONS
];
504 struct nlattr
*tb
[TCA_TCINDEX_MAX
+ 1];
505 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
506 struct tcindex_filter_result
*r
= *arg
;
509 pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
510 "p %p,r %p,*arg %p\n",
511 tp
, handle
, tca
, arg
, opt
, p
, r
, arg
? *arg
: NULL
);
516 err
= nla_parse_nested(tb
, TCA_TCINDEX_MAX
, opt
, tcindex_policy
, NULL
);
520 return tcindex_set_parms(net
, tp
, base
, handle
, p
, r
, tb
,
524 static void tcindex_walk(struct tcf_proto
*tp
, struct tcf_walker
*walker
)
526 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
527 struct tcindex_filter
*f
, *next
;
530 pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp
, walker
, p
);
532 for (i
= 0; i
< p
->hash
; i
++) {
533 if (!p
->perfect
[i
].res
.class)
535 if (walker
->count
>= walker
->skip
) {
536 if (walker
->fn(tp
, p
->perfect
+ i
, walker
) < 0) {
546 for (i
= 0; i
< p
->hash
; i
++) {
547 for (f
= rtnl_dereference(p
->h
[i
]); f
; f
= next
) {
548 next
= rtnl_dereference(f
->next
);
549 if (walker
->count
>= walker
->skip
) {
550 if (walker
->fn(tp
, &f
->result
, walker
) < 0) {
560 static void tcindex_destroy(struct tcf_proto
*tp
)
562 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
563 struct tcf_walker walker
;
565 pr_debug("tcindex_destroy(tp %p),p %p\n", tp
, p
);
568 walker
.fn
= tcindex_destroy_element
;
569 tcindex_walk(tp
, &walker
);
571 call_rcu(&p
->rcu
, __tcindex_destroy
);
575 static int tcindex_dump(struct net
*net
, struct tcf_proto
*tp
, void *fh
,
576 struct sk_buff
*skb
, struct tcmsg
*t
)
578 struct tcindex_data
*p
= rtnl_dereference(tp
->root
);
579 struct tcindex_filter_result
*r
= fh
;
582 pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n",
583 tp
, fh
, skb
, t
, p
, r
);
584 pr_debug("p->perfect %p p->h %p\n", p
->perfect
, p
->h
);
586 nest
= nla_nest_start(skb
, TCA_OPTIONS
);
588 goto nla_put_failure
;
591 t
->tcm_handle
= ~0; /* whatever ... */
592 if (nla_put_u32(skb
, TCA_TCINDEX_HASH
, p
->hash
) ||
593 nla_put_u16(skb
, TCA_TCINDEX_MASK
, p
->mask
) ||
594 nla_put_u32(skb
, TCA_TCINDEX_SHIFT
, p
->shift
) ||
595 nla_put_u32(skb
, TCA_TCINDEX_FALL_THROUGH
, p
->fall_through
))
596 goto nla_put_failure
;
597 nla_nest_end(skb
, nest
);
600 t
->tcm_handle
= r
- p
->perfect
;
602 struct tcindex_filter
*f
;
603 struct tcindex_filter __rcu
**fp
;
607 for (i
= 0; !t
->tcm_handle
&& i
< p
->hash
; i
++) {
609 for (f
= rtnl_dereference(*fp
);
611 fp
= &f
->next
, f
= rtnl_dereference(*fp
)) {
613 t
->tcm_handle
= f
->key
;
617 pr_debug("handle = %d\n", t
->tcm_handle
);
619 nla_put_u32(skb
, TCA_TCINDEX_CLASSID
, r
->res
.classid
))
620 goto nla_put_failure
;
622 if (tcf_exts_dump(skb
, &r
->exts
) < 0)
623 goto nla_put_failure
;
624 nla_nest_end(skb
, nest
);
626 if (tcf_exts_dump_stats(skb
, &r
->exts
) < 0)
627 goto nla_put_failure
;
633 nla_nest_cancel(skb
, nest
);
637 static void tcindex_bind_class(void *fh
, u32 classid
, unsigned long cl
)
639 struct tcindex_filter_result
*r
= fh
;
641 if (r
&& r
->res
.classid
== classid
)
645 static struct tcf_proto_ops cls_tcindex_ops __read_mostly
= {
647 .classify
= tcindex_classify
,
648 .init
= tcindex_init
,
649 .destroy
= tcindex_destroy
,
651 .change
= tcindex_change
,
652 .delete = tcindex_delete
,
653 .walk
= tcindex_walk
,
654 .dump
= tcindex_dump
,
655 .bind_class
= tcindex_bind_class
,
656 .owner
= THIS_MODULE
,
659 static int __init
init_tcindex(void)
661 return register_tcf_proto_ops(&cls_tcindex_ops
);
664 static void __exit
exit_tcindex(void)
666 unregister_tcf_proto_ops(&cls_tcindex_ops
);
669 module_init(init_tcindex
)
670 module_exit(exit_tcindex
)
671 MODULE_LICENSE("GPL");