Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v2.6.18 529 lines 12 kB view raw
1/* 2 * net/sched/cls_tcindex.c Packet classifier for skb->tc_index 3 * 4 * Written 1998,1999 by Werner Almesberger, EPFL ICA 5 */ 6 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/netdevice.h> 13#include <net/ip.h> 14#include <net/act_api.h> 15#include <net/pkt_cls.h> 16#include <net/route.h> 17 18 19/* 20 * Not quite sure if we need all the xchgs Alexey uses when accessing things. 21 * Can always add them later ... :) 22 */ 23 24/* 25 * Passing parameters to the root seems to be done more awkwardly than really 26 * necessary. At least, u32 doesn't seem to use such dirty hacks. To be 27 * verified. FIXME. 28 */ 29 30#define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */ 31#define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */ 32 33 34#if 1 /* control */ 35#define DPRINTK(format,args...) printk(KERN_DEBUG format,##args) 36#else 37#define DPRINTK(format,args...) 38#endif 39 40#if 0 /* data */ 41#define D2PRINTK(format,args...) printk(KERN_DEBUG format,##args) 42#else 43#define D2PRINTK(format,args...) 44#endif 45 46 47#define PRIV(tp) ((struct tcindex_data *) (tp)->root) 48 49 50struct tcindex_filter_result { 51 struct tcf_exts exts; 52 struct tcf_result res; 53}; 54 55struct tcindex_filter { 56 u16 key; 57 struct tcindex_filter_result result; 58 struct tcindex_filter *next; 59}; 60 61 62struct tcindex_data { 63 struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */ 64 struct tcindex_filter **h; /* imperfect hash; only used if !perfect; 65 NULL if unused */ 66 u16 mask; /* AND key with mask */ 67 int shift; /* shift ANDed key to the right */ 68 int hash; /* hash table size; 0 if undefined */ 69 int alloc_hash; /* allocated size */ 70 int fall_through; /* 0: only classify if explicit match */ 71}; 72 73static struct tcf_ext_map tcindex_ext_map = { 74 .police = TCA_TCINDEX_POLICE, 75 .action = TCA_TCINDEX_ACT 76}; 77 78static inline int 79tcindex_filter_is_set(struct tcindex_filter_result *r) 80{ 81 return tcf_exts_is_predicative(&r->exts) || r->res.classid; 82} 83 84static struct tcindex_filter_result * 85tcindex_lookup(struct tcindex_data *p, u16 key) 86{ 87 struct tcindex_filter *f; 88 89 if (p->perfect) 90 return tcindex_filter_is_set(p->perfect + key) ? 91 p->perfect + key : NULL; 92 else if (p->h) { 93 for (f = p->h[key % p->hash]; f; f = f->next) 94 if (f->key == key) 95 return &f->result; 96 } 97 98 return NULL; 99} 100 101 102static int tcindex_classify(struct sk_buff *skb, struct tcf_proto *tp, 103 struct tcf_result *res) 104{ 105 struct tcindex_data *p = PRIV(tp); 106 struct tcindex_filter_result *f; 107 int key = (skb->tc_index & p->mask) >> p->shift; 108 109 D2PRINTK("tcindex_classify(skb %p,tp %p,res %p),p %p\n",skb,tp,res,p); 110 111 f = tcindex_lookup(p, key); 112 if (!f) { 113 if (!p->fall_through) 114 return -1; 115 res->classid = TC_H_MAKE(TC_H_MAJ(tp->q->handle), key); 116 res->class = 0; 117 D2PRINTK("alg 0x%x\n",res->classid); 118 return 0; 119 } 120 *res = f->res; 121 D2PRINTK("map 0x%x\n",res->classid); 122 123 return tcf_exts_exec(skb, &f->exts, res); 124} 125 126 127static unsigned long tcindex_get(struct tcf_proto *tp, u32 handle) 128{ 129 struct tcindex_data *p = PRIV(tp); 130 struct tcindex_filter_result *r; 131 132 DPRINTK("tcindex_get(tp %p,handle 0x%08x)\n",tp,handle); 133 if (p->perfect && handle >= p->alloc_hash) 134 return 0; 135 r = tcindex_lookup(p, handle); 136 return r && tcindex_filter_is_set(r) ? (unsigned long) r : 0UL; 137} 138 139 140static void tcindex_put(struct tcf_proto *tp, unsigned long f) 141{ 142 DPRINTK("tcindex_put(tp %p,f 0x%lx)\n",tp,f); 143} 144 145 146static int tcindex_init(struct tcf_proto *tp) 147{ 148 struct tcindex_data *p; 149 150 DPRINTK("tcindex_init(tp %p)\n",tp); 151 p = kzalloc(sizeof(struct tcindex_data),GFP_KERNEL); 152 if (!p) 153 return -ENOMEM; 154 155 p->mask = 0xffff; 156 p->hash = DEFAULT_HASH_SIZE; 157 p->fall_through = 1; 158 159 tp->root = p; 160 return 0; 161} 162 163 164static int 165__tcindex_delete(struct tcf_proto *tp, unsigned long arg, int lock) 166{ 167 struct tcindex_data *p = PRIV(tp); 168 struct tcindex_filter_result *r = (struct tcindex_filter_result *) arg; 169 struct tcindex_filter *f = NULL; 170 171 DPRINTK("tcindex_delete(tp %p,arg 0x%lx),p %p,f %p\n",tp,arg,p,f); 172 if (p->perfect) { 173 if (!r->res.class) 174 return -ENOENT; 175 } else { 176 int i; 177 struct tcindex_filter **walk = NULL; 178 179 for (i = 0; i < p->hash; i++) 180 for (walk = p->h+i; *walk; walk = &(*walk)->next) 181 if (&(*walk)->result == r) 182 goto found; 183 return -ENOENT; 184 185found: 186 f = *walk; 187 if (lock) 188 tcf_tree_lock(tp); 189 *walk = f->next; 190 if (lock) 191 tcf_tree_unlock(tp); 192 } 193 tcf_unbind_filter(tp, &r->res); 194 tcf_exts_destroy(tp, &r->exts); 195 kfree(f); 196 return 0; 197} 198 199static int tcindex_delete(struct tcf_proto *tp, unsigned long arg) 200{ 201 return __tcindex_delete(tp, arg, 1); 202} 203 204static inline int 205valid_perfect_hash(struct tcindex_data *p) 206{ 207 return p->hash > (p->mask >> p->shift); 208} 209 210static int 211tcindex_set_parms(struct tcf_proto *tp, unsigned long base, u32 handle, 212 struct tcindex_data *p, struct tcindex_filter_result *r, 213 struct rtattr **tb, struct rtattr *est) 214{ 215 int err, balloc = 0; 216 struct tcindex_filter_result new_filter_result, *old_r = r; 217 struct tcindex_filter_result cr; 218 struct tcindex_data cp; 219 struct tcindex_filter *f = NULL; /* make gcc behave */ 220 struct tcf_exts e; 221 222 err = tcf_exts_validate(tp, tb, est, &e, &tcindex_ext_map); 223 if (err < 0) 224 return err; 225 226 memcpy(&cp, p, sizeof(cp)); 227 memset(&new_filter_result, 0, sizeof(new_filter_result)); 228 229 if (old_r) 230 memcpy(&cr, r, sizeof(cr)); 231 else 232 memset(&cr, 0, sizeof(cr)); 233 234 err = -EINVAL; 235 if (tb[TCA_TCINDEX_HASH-1]) { 236 if (RTA_PAYLOAD(tb[TCA_TCINDEX_HASH-1]) < sizeof(u32)) 237 goto errout; 238 cp.hash = *(u32 *) RTA_DATA(tb[TCA_TCINDEX_HASH-1]); 239 } 240 241 if (tb[TCA_TCINDEX_MASK-1]) { 242 if (RTA_PAYLOAD(tb[TCA_TCINDEX_MASK-1]) < sizeof(u16)) 243 goto errout; 244 cp.mask = *(u16 *) RTA_DATA(tb[TCA_TCINDEX_MASK-1]); 245 } 246 247 if (tb[TCA_TCINDEX_SHIFT-1]) { 248 if (RTA_PAYLOAD(tb[TCA_TCINDEX_SHIFT-1]) < sizeof(u16)) 249 goto errout; 250 cp.shift = *(u16 *) RTA_DATA(tb[TCA_TCINDEX_SHIFT-1]); 251 } 252 253 err = -EBUSY; 254 /* Hash already allocated, make sure that we still meet the 255 * requirements for the allocated hash. 256 */ 257 if (cp.perfect) { 258 if (!valid_perfect_hash(&cp) || 259 cp.hash > cp.alloc_hash) 260 goto errout; 261 } else if (cp.h && cp.hash != cp.alloc_hash) 262 goto errout; 263 264 err = -EINVAL; 265 if (tb[TCA_TCINDEX_FALL_THROUGH-1]) { 266 if (RTA_PAYLOAD(tb[TCA_TCINDEX_FALL_THROUGH-1]) < sizeof(u32)) 267 goto errout; 268 cp.fall_through = 269 *(u32 *) RTA_DATA(tb[TCA_TCINDEX_FALL_THROUGH-1]); 270 } 271 272 if (!cp.hash) { 273 /* Hash not specified, use perfect hash if the upper limit 274 * of the hashing index is below the threshold. 275 */ 276 if ((cp.mask >> cp.shift) < PERFECT_HASH_THRESHOLD) 277 cp.hash = (cp.mask >> cp.shift)+1; 278 else 279 cp.hash = DEFAULT_HASH_SIZE; 280 } 281 282 if (!cp.perfect && !cp.h) 283 cp.alloc_hash = cp.hash; 284 285 /* Note: this could be as restrictive as if (handle & ~(mask >> shift)) 286 * but then, we'd fail handles that may become valid after some future 287 * mask change. While this is extremely unlikely to ever matter, 288 * the check below is safer (and also more backwards-compatible). 289 */ 290 if (cp.perfect || valid_perfect_hash(&cp)) 291 if (handle >= cp.alloc_hash) 292 goto errout; 293 294 295 err = -ENOMEM; 296 if (!cp.perfect && !cp.h) { 297 if (valid_perfect_hash(&cp)) { 298 cp.perfect = kcalloc(cp.hash, sizeof(*r), GFP_KERNEL); 299 if (!cp.perfect) 300 goto errout; 301 balloc = 1; 302 } else { 303 cp.h = kcalloc(cp.hash, sizeof(f), GFP_KERNEL); 304 if (!cp.h) 305 goto errout; 306 balloc = 2; 307 } 308 } 309 310 if (cp.perfect) 311 r = cp.perfect + handle; 312 else 313 r = tcindex_lookup(&cp, handle) ? : &new_filter_result; 314 315 if (r == &new_filter_result) { 316 f = kzalloc(sizeof(*f), GFP_KERNEL); 317 if (!f) 318 goto errout_alloc; 319 } 320 321 if (tb[TCA_TCINDEX_CLASSID-1]) { 322 cr.res.classid = *(u32 *) RTA_DATA(tb[TCA_TCINDEX_CLASSID-1]); 323 tcf_bind_filter(tp, &cr.res, base); 324 } 325 326 tcf_exts_change(tp, &cr.exts, &e); 327 328 tcf_tree_lock(tp); 329 if (old_r && old_r != r) 330 memset(old_r, 0, sizeof(*old_r)); 331 332 memcpy(p, &cp, sizeof(cp)); 333 memcpy(r, &cr, sizeof(cr)); 334 335 if (r == &new_filter_result) { 336 struct tcindex_filter **fp; 337 338 f->key = handle; 339 f->result = new_filter_result; 340 f->next = NULL; 341 for (fp = p->h+(handle % p->hash); *fp; fp = &(*fp)->next) 342 /* nothing */; 343 *fp = f; 344 } 345 tcf_tree_unlock(tp); 346 347 return 0; 348 349errout_alloc: 350 if (balloc == 1) 351 kfree(cp.perfect); 352 else if (balloc == 2) 353 kfree(cp.h); 354errout: 355 tcf_exts_destroy(tp, &e); 356 return err; 357} 358 359static int 360tcindex_change(struct tcf_proto *tp, unsigned long base, u32 handle, 361 struct rtattr **tca, unsigned long *arg) 362{ 363 struct rtattr *opt = tca[TCA_OPTIONS-1]; 364 struct rtattr *tb[TCA_TCINDEX_MAX]; 365 struct tcindex_data *p = PRIV(tp); 366 struct tcindex_filter_result *r = (struct tcindex_filter_result *) *arg; 367 368 DPRINTK("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p," 369 "p %p,r %p,*arg 0x%lx\n", 370 tp, handle, tca, arg, opt, p, r, arg ? *arg : 0L); 371 372 if (!opt) 373 return 0; 374 375 if (rtattr_parse_nested(tb, TCA_TCINDEX_MAX, opt) < 0) 376 return -EINVAL; 377 378 return tcindex_set_parms(tp, base, handle, p, r, tb, tca[TCA_RATE-1]); 379} 380 381 382static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker) 383{ 384 struct tcindex_data *p = PRIV(tp); 385 struct tcindex_filter *f,*next; 386 int i; 387 388 DPRINTK("tcindex_walk(tp %p,walker %p),p %p\n",tp,walker,p); 389 if (p->perfect) { 390 for (i = 0; i < p->hash; i++) { 391 if (!p->perfect[i].res.class) 392 continue; 393 if (walker->count >= walker->skip) { 394 if (walker->fn(tp, 395 (unsigned long) (p->perfect+i), walker) 396 < 0) { 397 walker->stop = 1; 398 return; 399 } 400 } 401 walker->count++; 402 } 403 } 404 if (!p->h) 405 return; 406 for (i = 0; i < p->hash; i++) { 407 for (f = p->h[i]; f; f = next) { 408 next = f->next; 409 if (walker->count >= walker->skip) { 410 if (walker->fn(tp,(unsigned long) &f->result, 411 walker) < 0) { 412 walker->stop = 1; 413 return; 414 } 415 } 416 walker->count++; 417 } 418 } 419} 420 421 422static int tcindex_destroy_element(struct tcf_proto *tp, 423 unsigned long arg, struct tcf_walker *walker) 424{ 425 return __tcindex_delete(tp, arg, 0); 426} 427 428 429static void tcindex_destroy(struct tcf_proto *tp) 430{ 431 struct tcindex_data *p = PRIV(tp); 432 struct tcf_walker walker; 433 434 DPRINTK("tcindex_destroy(tp %p),p %p\n",tp,p); 435 walker.count = 0; 436 walker.skip = 0; 437 walker.fn = &tcindex_destroy_element; 438 tcindex_walk(tp,&walker); 439 kfree(p->perfect); 440 kfree(p->h); 441 kfree(p); 442 tp->root = NULL; 443} 444 445 446static int tcindex_dump(struct tcf_proto *tp, unsigned long fh, 447 struct sk_buff *skb, struct tcmsg *t) 448{ 449 struct tcindex_data *p = PRIV(tp); 450 struct tcindex_filter_result *r = (struct tcindex_filter_result *) fh; 451 unsigned char *b = skb->tail; 452 struct rtattr *rta; 453 454 DPRINTK("tcindex_dump(tp %p,fh 0x%lx,skb %p,t %p),p %p,r %p,b %p\n", 455 tp,fh,skb,t,p,r,b); 456 DPRINTK("p->perfect %p p->h %p\n",p->perfect,p->h); 457 rta = (struct rtattr *) b; 458 RTA_PUT(skb,TCA_OPTIONS,0,NULL); 459 if (!fh) { 460 t->tcm_handle = ~0; /* whatever ... */ 461 RTA_PUT(skb,TCA_TCINDEX_HASH,sizeof(p->hash),&p->hash); 462 RTA_PUT(skb,TCA_TCINDEX_MASK,sizeof(p->mask),&p->mask); 463 RTA_PUT(skb,TCA_TCINDEX_SHIFT,sizeof(p->shift),&p->shift); 464 RTA_PUT(skb,TCA_TCINDEX_FALL_THROUGH,sizeof(p->fall_through), 465 &p->fall_through); 466 rta->rta_len = skb->tail-b; 467 } else { 468 if (p->perfect) { 469 t->tcm_handle = r-p->perfect; 470 } else { 471 struct tcindex_filter *f; 472 int i; 473 474 t->tcm_handle = 0; 475 for (i = 0; !t->tcm_handle && i < p->hash; i++) { 476 for (f = p->h[i]; !t->tcm_handle && f; 477 f = f->next) { 478 if (&f->result == r) 479 t->tcm_handle = f->key; 480 } 481 } 482 } 483 DPRINTK("handle = %d\n",t->tcm_handle); 484 if (r->res.class) 485 RTA_PUT(skb, TCA_TCINDEX_CLASSID, 4, &r->res.classid); 486 487 if (tcf_exts_dump(skb, &r->exts, &tcindex_ext_map) < 0) 488 goto rtattr_failure; 489 rta->rta_len = skb->tail-b; 490 491 if (tcf_exts_dump_stats(skb, &r->exts, &tcindex_ext_map) < 0) 492 goto rtattr_failure; 493 } 494 495 return skb->len; 496 497rtattr_failure: 498 skb_trim(skb, b - skb->data); 499 return -1; 500} 501 502static struct tcf_proto_ops cls_tcindex_ops = { 503 .next = NULL, 504 .kind = "tcindex", 505 .classify = tcindex_classify, 506 .init = tcindex_init, 507 .destroy = tcindex_destroy, 508 .get = tcindex_get, 509 .put = tcindex_put, 510 .change = tcindex_change, 511 .delete = tcindex_delete, 512 .walk = tcindex_walk, 513 .dump = tcindex_dump, 514 .owner = THIS_MODULE, 515}; 516 517static int __init init_tcindex(void) 518{ 519 return register_tcf_proto_ops(&cls_tcindex_ops); 520} 521 522static void __exit exit_tcindex(void) 523{ 524 unregister_tcf_proto_ops(&cls_tcindex_ops); 525} 526 527module_init(init_tcindex) 528module_exit(exit_tcindex) 529MODULE_LICENSE("GPL");