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