2 * net/sched/cls_tcindex.c Packet classifier for skb->tc_index
4 * Written 1998,1999 by Werner Almesberger, EPFL ICA
7 #include <linux/config.h>
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/skbuff.h>
12 #include <linux/errno.h>
13 #include <linux/netdevice.h>
15 #include <net/act_api.h>
16 #include <net/pkt_cls.h>
17 #include <net/route.h>
21 * Not quite sure if we need all the xchgs Alexey uses when accessing things.
22 * Can always add them later ... :)
26 * Passing parameters to the root seems to be done more awkwardly than really
27 * necessary. At least, u32 doesn't seem to use such dirty hacks. To be
31 #define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */
32 #define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
36 #define DPRINTK(format,args...) printk(KERN_DEBUG format,##args)
38 #define DPRINTK(format,args...)
42 #define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args)
44 #define D2PRINTK(format,args...)
48 #define PRIV(tp) ((struct tcindex_data *) (tp)->root)
51 struct tcindex_filter_result {
52 struct tcf_police *police;
53 struct tcf_result res;
56 struct tcindex_filter {
58 struct tcindex_filter_result result;
59 struct tcindex_filter *next;
64 struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */
65 struct tcindex_filter **h; /* imperfect hash; only used if !perfect;
67 __u16 mask; /* AND key with mask */
68 int shift; /* shift ANDed key to the right */
69 int hash; /* hash table size; 0 if undefined */
70 int alloc_hash; /* allocated size */
71 int fall_through; /* 0: only classify if explicit match */
75 static struct tcindex_filter_result *lookup(struct tcindex_data *p,__u16 key)
77 struct tcindex_filter *f;
80 return p->perfect[key].res.class ? p->perfect+key : NULL;
83 for (f = p->h[key % p->hash]; f; f = f->next) {
91 static int tcindex_classify(struct sk_buff *skb, struct tcf_proto *tp,
92 struct tcf_result *res)
94 struct tcindex_data *p = PRIV(tp);
95 struct tcindex_filter_result *f;
97 D2PRINTK("tcindex_classify(skb %p,tp %p,res %p),p %p\n",skb,tp,res,p);
99 f = lookup(p,(skb->tc_index & p->mask) >> p->shift);
101 if (!p->fall_through)
103 res->classid = TC_H_MAKE(TC_H_MAJ(tp->q->handle),
104 (skb->tc_index& p->mask) >> p->shift);
106 D2PRINTK("alg 0x%x\n",res->classid);
110 D2PRINTK("map 0x%x\n",res->classid);
111 #ifdef CONFIG_NET_CLS_POLICE
115 result = tcf_police(skb,f->police);
116 D2PRINTK("police %d\n",res);
124 static unsigned long tcindex_get(struct tcf_proto *tp, u32 handle)
126 struct tcindex_data *p = PRIV(tp);
127 struct tcindex_filter_result *r;
129 DPRINTK("tcindex_get(tp %p,handle 0x%08x)\n",tp,handle);
130 if (p->perfect && handle >= p->alloc_hash)
132 r = lookup(PRIV(tp),handle);
133 return r && r->res.class ? (unsigned long) r : 0;
137 static void tcindex_put(struct tcf_proto *tp, unsigned long f)
139 DPRINTK("tcindex_put(tp %p,f 0x%lx)\n",tp,f);
143 static int tcindex_init(struct tcf_proto *tp)
145 struct tcindex_data *p;
147 DPRINTK("tcindex_init(tp %p)\n",tp);
148 p = kmalloc(sizeof(struct tcindex_data),GFP_KERNEL);
164 __tcindex_delete(struct tcf_proto *tp, unsigned long arg, int lock)
166 struct tcindex_data *p = PRIV(tp);
167 struct tcindex_filter_result *r = (struct tcindex_filter_result *) arg;
168 struct tcindex_filter *f = NULL;
170 DPRINTK("tcindex_delete(tp %p,arg 0x%lx),p %p,f %p\n",tp,arg,p,f);
176 struct tcindex_filter **walk = NULL;
178 for (i = 0; i < p->hash; i++)
179 for (walk = p->h+i; *walk; walk = &(*walk)->next)
180 if (&(*walk)->result == r)
192 tcf_unbind_filter(tp, &r->res);
193 #ifdef CONFIG_NET_CLS_POLICE
194 tcf_police_release(r->police, TCA_ACT_UNBIND);
201 static int tcindex_delete(struct tcf_proto *tp, unsigned long arg)
203 return __tcindex_delete(tp, arg, 1);
207 * There are no parameters for tcindex_init, so we overload tcindex_change
211 static int tcindex_change(struct tcf_proto *tp,unsigned long base,u32 handle,
212 struct rtattr **tca,unsigned long *arg)
214 struct tcindex_filter_result new_filter_result = {
215 NULL, /* no policing */
216 { 0,0 }, /* no classification */
218 struct rtattr *opt = tca[TCA_OPTIONS-1];
219 struct rtattr *tb[TCA_TCINDEX_MAX];
220 struct tcindex_data *p = PRIV(tp);
221 struct tcindex_filter *f;
222 struct tcindex_filter_result *r = (struct tcindex_filter_result *) *arg;
223 struct tcindex_filter **walk;
227 DPRINTK("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p,"
228 "p %p,r %p\n",tp,handle,tca,arg,opt,p,r);
230 DPRINTK("*arg = 0x%lx\n",*arg);
233 if (rtattr_parse(tb,TCA_TCINDEX_MAX,RTA_DATA(opt),RTA_PAYLOAD(opt)) < 0)
235 if (!tb[TCA_TCINDEX_HASH-1]) {
238 if (RTA_PAYLOAD(tb[TCA_TCINDEX_HASH-1]) < sizeof(int))
240 hash = *(int *) RTA_DATA(tb[TCA_TCINDEX_HASH-1]);
242 if (!tb[TCA_TCINDEX_MASK-1]) {
245 if (RTA_PAYLOAD(tb[TCA_TCINDEX_MASK-1]) < sizeof(__u16))
247 mask = *(__u16 *) RTA_DATA(tb[TCA_TCINDEX_MASK-1]);
249 if (!tb[TCA_TCINDEX_SHIFT-1])
252 if (RTA_PAYLOAD(tb[TCA_TCINDEX_SHIFT-1]) < sizeof(__u16))
254 shift = *(int *) RTA_DATA(tb[TCA_TCINDEX_SHIFT-1]);
256 if (p->perfect && hash <= (mask >> shift))
258 if (p->perfect && hash > p->alloc_hash)
260 if (p->h && hash != p->alloc_hash)
265 if (tb[TCA_TCINDEX_FALL_THROUGH-1]) {
266 if (RTA_PAYLOAD(tb[TCA_TCINDEX_FALL_THROUGH-1]) < sizeof(int))
269 *(int *) RTA_DATA(tb[TCA_TCINDEX_FALL_THROUGH-1]);
271 DPRINTK("classid/police %p/%p\n",tb[TCA_TCINDEX_CLASSID-1],
272 tb[TCA_TCINDEX_POLICE-1]);
273 if (!tb[TCA_TCINDEX_CLASSID-1] && !tb[TCA_TCINDEX_POLICE-1])
276 if ((mask >> shift) < PERFECT_HASH_THRESHOLD) {
277 p->hash = (mask >> shift)+1;
279 p->hash = DEFAULT_HASH_SIZE;
282 if (!p->perfect && !p->h) {
283 p->alloc_hash = p->hash;
284 DPRINTK("hash %d mask %d\n",p->hash,p->mask);
285 if (p->hash > (mask >> shift)) {
286 p->perfect = kmalloc(p->hash*
287 sizeof(struct tcindex_filter_result),GFP_KERNEL);
290 memset(p->perfect, 0,
291 p->hash * sizeof(struct tcindex_filter_result));
293 p->h = kmalloc(p->hash*sizeof(struct tcindex_filter *),
297 memset(p->h, 0, p->hash*sizeof(struct tcindex_filter *));
301 * Note: this could be as restrictive as
302 * if (handle & ~(mask >> shift))
303 * but then, we'd fail handles that may become valid after some
304 * future mask change. While this is extremely unlikely to ever
305 * matter, the check below is safer (and also more
306 * backwards-compatible).
308 if (p->perfect && handle >= p->alloc_hash)
311 r = p->perfect+handle;
313 r = lookup(p,handle);
316 r = &new_filter_result;
319 if (tb[TCA_TCINDEX_CLASSID-1]) {
320 r->res.classid = *(__u32 *) RTA_DATA(tb[TCA_TCINDEX_CLASSID-1]);
321 tcf_bind_filter(tp, &r->res, base);
328 #ifdef CONFIG_NET_CLS_POLICE
329 if (tb[TCA_TCINDEX_POLICE-1]) {
330 int err = tcf_change_police(tp, &r->police, tb[TCA_TCINDEX_POLICE-1], NULL);
335 if (r != &new_filter_result)
337 f = kmalloc(sizeof(struct tcindex_filter),GFP_KERNEL);
341 f->result = new_filter_result;
343 for (walk = p->h+(handle % p->hash); *walk; walk = &(*walk)->next)
351 static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker)
353 struct tcindex_data *p = PRIV(tp);
354 struct tcindex_filter *f,*next;
357 DPRINTK("tcindex_walk(tp %p,walker %p),p %p\n",tp,walker,p);
359 for (i = 0; i < p->hash; i++) {
360 if (!p->perfect[i].res.class)
362 if (walker->count >= walker->skip) {
364 (unsigned long) (p->perfect+i), walker)
375 for (i = 0; i < p->hash; i++) {
376 for (f = p->h[i]; f; f = next) {
378 if (walker->count >= walker->skip) {
379 if (walker->fn(tp,(unsigned long) &f->result,
391 static int tcindex_destroy_element(struct tcf_proto *tp,
392 unsigned long arg, struct tcf_walker *walker)
394 return __tcindex_delete(tp, arg, 0);
398 static void tcindex_destroy(struct tcf_proto *tp)
400 struct tcindex_data *p = PRIV(tp);
401 struct tcf_walker walker;
403 DPRINTK("tcindex_destroy(tp %p),p %p\n",tp,p);
406 walker.fn = &tcindex_destroy_element;
407 tcindex_walk(tp,&walker);
417 static int tcindex_dump(struct tcf_proto *tp, unsigned long fh,
418 struct sk_buff *skb, struct tcmsg *t)
420 struct tcindex_data *p = PRIV(tp);
421 struct tcindex_filter_result *r = (struct tcindex_filter_result *) fh;
422 unsigned char *b = skb->tail;
425 DPRINTK("tcindex_dump(tp %p,fh 0x%lx,skb %p,t %p),p %p,r %p,b %p\n",
427 DPRINTK("p->perfect %p p->h %p\n",p->perfect,p->h);
428 rta = (struct rtattr *) b;
429 RTA_PUT(skb,TCA_OPTIONS,0,NULL);
431 t->tcm_handle = ~0; /* whatever ... */
432 RTA_PUT(skb,TCA_TCINDEX_HASH,sizeof(p->hash),&p->hash);
433 RTA_PUT(skb,TCA_TCINDEX_MASK,sizeof(p->mask),&p->mask);
434 RTA_PUT(skb,TCA_TCINDEX_SHIFT,sizeof(p->shift),&p->shift);
435 RTA_PUT(skb,TCA_TCINDEX_FALL_THROUGH,sizeof(p->fall_through),
439 t->tcm_handle = r-p->perfect;
441 struct tcindex_filter *f;
445 for (i = 0; !t->tcm_handle && i < p->hash; i++) {
446 for (f = p->h[i]; !t->tcm_handle && f;
449 t->tcm_handle = f->key;
453 DPRINTK("handle = %d\n",t->tcm_handle);
455 RTA_PUT(skb, TCA_TCINDEX_CLASSID, 4, &r->res.classid);
456 #ifdef CONFIG_NET_CLS_POLICE
457 if (tcf_dump_police(skb, r->police, TCA_TCINDEX_POLICE) < 0)
461 rta->rta_len = skb->tail-b;
465 skb_trim(skb, b - skb->data);
469 static struct tcf_proto_ops cls_tcindex_ops = {
472 .classify = tcindex_classify,
473 .init = tcindex_init,
474 .destroy = tcindex_destroy,
477 .change = tcindex_change,
478 .delete = tcindex_delete,
479 .walk = tcindex_walk,
480 .dump = tcindex_dump,
481 .owner = THIS_MODULE,
484 static int __init init_tcindex(void)
486 return register_tcf_proto_ops(&cls_tcindex_ops);
489 static void __exit exit_tcindex(void)
491 unregister_tcf_proto_ops(&cls_tcindex_ops);
494 module_init(init_tcindex)
495 module_exit(exit_tcindex)
496 MODULE_LICENSE("GPL");