]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 LT |
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> | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
1da177e4 | 13 | #include <net/act_api.h> |
dc5fc579 | 14 | #include <net/netlink.h> |
1da177e4 | 15 | #include <net/pkt_cls.h> |
1da177e4 | 16 | |
1da177e4 LT |
17 | /* |
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 | |
20 | * verified. FIXME. | |
21 | */ | |
22 | ||
23 | #define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */ | |
24 | #define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */ | |
25 | ||
26 | ||
1da177e4 LT |
27 | struct tcindex_filter_result { |
28 | struct tcf_exts exts; | |
29 | struct tcf_result res; | |
27ce4f05 CW |
30 | union { |
31 | struct work_struct work; | |
32 | struct rcu_head rcu; | |
33 | }; | |
1da177e4 LT |
34 | }; |
35 | ||
36 | struct tcindex_filter { | |
37 | u16 key; | |
38 | struct tcindex_filter_result result; | |
331b7292 | 39 | struct tcindex_filter __rcu *next; |
27ce4f05 CW |
40 | union { |
41 | struct work_struct work; | |
42 | struct rcu_head rcu; | |
43 | }; | |
1da177e4 LT |
44 | }; |
45 | ||
46 | ||
47 | struct tcindex_data { | |
48 | struct tcindex_filter_result *perfect; /* perfect hash; NULL if none */ | |
331b7292 JF |
49 | struct tcindex_filter __rcu **h; /* imperfect hash; */ |
50 | struct tcf_proto *tp; | |
1da177e4 | 51 | u16 mask; /* AND key with mask */ |
331b7292 JF |
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 */ | |
56 | struct rcu_head rcu; | |
1da177e4 LT |
57 | }; |
58 | ||
5a7a5555 | 59 | static inline int tcindex_filter_is_set(struct tcindex_filter_result *r) |
1da177e4 | 60 | { |
6fc6d06e | 61 | return tcf_exts_has_actions(&r->exts) || r->res.classid; |
1da177e4 LT |
62 | } |
63 | ||
5a7a5555 JHS |
64 | static struct tcindex_filter_result *tcindex_lookup(struct tcindex_data *p, |
65 | u16 key) | |
1da177e4 | 66 | { |
331b7292 JF |
67 | if (p->perfect) { |
68 | struct tcindex_filter_result *f = p->perfect + key; | |
69 | ||
70 | return tcindex_filter_is_set(f) ? f : NULL; | |
71 | } else if (p->h) { | |
72 | struct tcindex_filter __rcu **fp; | |
73 | struct tcindex_filter *f; | |
1da177e4 | 74 | |
331b7292 JF |
75 | fp = &p->h[key % p->hash]; |
76 | for (f = rcu_dereference_bh_rtnl(*fp); | |
77 | f; | |
78 | fp = &f->next, f = rcu_dereference_bh_rtnl(*fp)) | |
1da177e4 LT |
79 | if (f->key == key) |
80 | return &f->result; | |
81 | } | |
82 | ||
83 | return NULL; | |
84 | } | |
85 | ||
86 | ||
dc7f9f6e | 87 | static int tcindex_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
1da177e4 LT |
88 | struct tcf_result *res) |
89 | { | |
2f9a220e | 90 | struct tcindex_data *p = rcu_dereference_bh(tp->root); |
1da177e4 LT |
91 | struct tcindex_filter_result *f; |
92 | int key = (skb->tc_index & p->mask) >> p->shift; | |
93 | ||
aa767bfe SH |
94 | pr_debug("tcindex_classify(skb %p,tp %p,res %p),p %p\n", |
95 | skb, tp, res, p); | |
1da177e4 LT |
96 | |
97 | f = tcindex_lookup(p, key); | |
98 | if (!f) { | |
99 | if (!p->fall_through) | |
100 | return -1; | |
101 | res->classid = TC_H_MAKE(TC_H_MAJ(tp->q->handle), key); | |
102 | res->class = 0; | |
aa767bfe | 103 | pr_debug("alg 0x%x\n", res->classid); |
1da177e4 LT |
104 | return 0; |
105 | } | |
106 | *res = f->res; | |
aa767bfe | 107 | pr_debug("map 0x%x\n", res->classid); |
1da177e4 LT |
108 | |
109 | return tcf_exts_exec(skb, &f->exts, res); | |
110 | } | |
111 | ||
112 | ||
8113c095 | 113 | static void *tcindex_get(struct tcf_proto *tp, u32 handle) |
1da177e4 | 114 | { |
331b7292 | 115 | struct tcindex_data *p = rtnl_dereference(tp->root); |
1da177e4 LT |
116 | struct tcindex_filter_result *r; |
117 | ||
aa767bfe | 118 | pr_debug("tcindex_get(tp %p,handle 0x%08x)\n", tp, handle); |
1da177e4 | 119 | if (p->perfect && handle >= p->alloc_hash) |
8113c095 | 120 | return NULL; |
1da177e4 | 121 | r = tcindex_lookup(p, handle); |
8113c095 | 122 | return r && tcindex_filter_is_set(r) ? r : NULL; |
1da177e4 LT |
123 | } |
124 | ||
1da177e4 LT |
125 | static int tcindex_init(struct tcf_proto *tp) |
126 | { | |
127 | struct tcindex_data *p; | |
128 | ||
aa767bfe SH |
129 | pr_debug("tcindex_init(tp %p)\n", tp); |
130 | p = kzalloc(sizeof(struct tcindex_data), GFP_KERNEL); | |
1da177e4 LT |
131 | if (!p) |
132 | return -ENOMEM; | |
133 | ||
1da177e4 LT |
134 | p->mask = 0xffff; |
135 | p->hash = DEFAULT_HASH_SIZE; | |
136 | p->fall_through = 1; | |
137 | ||
331b7292 | 138 | rcu_assign_pointer(tp->root, p); |
1da177e4 LT |
139 | return 0; |
140 | } | |
141 | ||
f2b75105 CW |
142 | static void __tcindex_destroy_rexts(struct tcindex_filter_result *r) |
143 | { | |
144 | tcf_exts_destroy(&r->exts); | |
145 | tcf_exts_put_net(&r->exts); | |
146 | } | |
147 | ||
27ce4f05 CW |
148 | static void tcindex_destroy_rexts_work(struct work_struct *work) |
149 | { | |
150 | struct tcindex_filter_result *r; | |
151 | ||
152 | r = container_of(work, struct tcindex_filter_result, work); | |
153 | rtnl_lock(); | |
f2b75105 | 154 | __tcindex_destroy_rexts(r); |
27ce4f05 CW |
155 | rtnl_unlock(); |
156 | } | |
157 | ||
ed7aa879 AS |
158 | static void tcindex_destroy_rexts(struct rcu_head *head) |
159 | { | |
160 | struct tcindex_filter_result *r; | |
161 | ||
162 | r = container_of(head, struct tcindex_filter_result, rcu); | |
27ce4f05 CW |
163 | INIT_WORK(&r->work, tcindex_destroy_rexts_work); |
164 | tcf_queue_work(&r->work); | |
165 | } | |
166 | ||
f2b75105 CW |
167 | static void __tcindex_destroy_fexts(struct tcindex_filter *f) |
168 | { | |
169 | tcf_exts_destroy(&f->result.exts); | |
170 | tcf_exts_put_net(&f->result.exts); | |
171 | kfree(f); | |
172 | } | |
173 | ||
27ce4f05 CW |
174 | static void tcindex_destroy_fexts_work(struct work_struct *work) |
175 | { | |
176 | struct tcindex_filter *f = container_of(work, struct tcindex_filter, | |
177 | work); | |
178 | ||
179 | rtnl_lock(); | |
f2b75105 | 180 | __tcindex_destroy_fexts(f); |
27ce4f05 | 181 | rtnl_unlock(); |
ed7aa879 AS |
182 | } |
183 | ||
184 | static void tcindex_destroy_fexts(struct rcu_head *head) | |
185 | { | |
5a7a5555 JHS |
186 | struct tcindex_filter *f = container_of(head, struct tcindex_filter, |
187 | rcu); | |
ed7aa879 | 188 | |
27ce4f05 CW |
189 | INIT_WORK(&f->work, tcindex_destroy_fexts_work); |
190 | tcf_queue_work(&f->work); | |
ed7aa879 AS |
191 | } |
192 | ||
8113c095 | 193 | static int tcindex_delete(struct tcf_proto *tp, void *arg, bool *last) |
1da177e4 | 194 | { |
331b7292 | 195 | struct tcindex_data *p = rtnl_dereference(tp->root); |
8113c095 | 196 | struct tcindex_filter_result *r = arg; |
331b7292 | 197 | struct tcindex_filter __rcu **walk; |
1da177e4 LT |
198 | struct tcindex_filter *f = NULL; |
199 | ||
8113c095 | 200 | pr_debug("tcindex_delete(tp %p,arg %p),p %p\n", tp, arg, p); |
1da177e4 LT |
201 | if (p->perfect) { |
202 | if (!r->res.class) | |
203 | return -ENOENT; | |
204 | } else { | |
205 | int i; | |
1da177e4 | 206 | |
331b7292 JF |
207 | for (i = 0; i < p->hash; i++) { |
208 | walk = p->h + i; | |
209 | for (f = rtnl_dereference(*walk); f; | |
210 | walk = &f->next, f = rtnl_dereference(*walk)) { | |
211 | if (&f->result == r) | |
1da177e4 | 212 | goto found; |
331b7292 JF |
213 | } |
214 | } | |
1da177e4 LT |
215 | return -ENOENT; |
216 | ||
217 | found: | |
331b7292 | 218 | rcu_assign_pointer(*walk, rtnl_dereference(f->next)); |
1da177e4 LT |
219 | } |
220 | tcf_unbind_filter(tp, &r->res); | |
ed7aa879 AS |
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 | |
224 | */ | |
f2b75105 CW |
225 | if (f) { |
226 | if (tcf_exts_get_net(&f->result.exts)) | |
227 | call_rcu(&f->rcu, tcindex_destroy_fexts); | |
228 | else | |
229 | __tcindex_destroy_fexts(f); | |
230 | } else { | |
231 | if (tcf_exts_get_net(&r->exts)) | |
232 | call_rcu(&r->rcu, tcindex_destroy_rexts); | |
233 | else | |
234 | __tcindex_destroy_rexts(r); | |
235 | } | |
763dbf63 WC |
236 | |
237 | *last = false; | |
1da177e4 LT |
238 | return 0; |
239 | } | |
240 | ||
331b7292 | 241 | static int tcindex_destroy_element(struct tcf_proto *tp, |
8113c095 | 242 | void *arg, struct tcf_walker *walker) |
1da177e4 | 243 | { |
763dbf63 WC |
244 | bool last; |
245 | ||
246 | return tcindex_delete(tp, arg, &last); | |
331b7292 JF |
247 | } |
248 | ||
249 | static void __tcindex_destroy(struct rcu_head *head) | |
250 | { | |
251 | struct tcindex_data *p = container_of(head, struct tcindex_data, rcu); | |
252 | ||
253 | kfree(p->perfect); | |
254 | kfree(p->h); | |
255 | kfree(p); | |
1da177e4 LT |
256 | } |
257 | ||
258 | static inline int | |
259 | valid_perfect_hash(struct tcindex_data *p) | |
260 | { | |
261 | return p->hash > (p->mask >> p->shift); | |
262 | } | |
263 | ||
6fa8c014 PM |
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 }, | |
270 | }; | |
271 | ||
b9a24bb7 | 272 | static int tcindex_filter_result_init(struct tcindex_filter_result *r) |
bf63ac73 CW |
273 | { |
274 | memset(r, 0, sizeof(*r)); | |
b9a24bb7 | 275 | return tcf_exts_init(&r->exts, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE); |
bf63ac73 CW |
276 | } |
277 | ||
331b7292 JF |
278 | static void __tcindex_partial_destroy(struct rcu_head *head) |
279 | { | |
280 | struct tcindex_data *p = container_of(head, struct tcindex_data, rcu); | |
281 | ||
282 | kfree(p->perfect); | |
283 | kfree(p); | |
284 | } | |
285 | ||
b9a24bb7 WC |
286 | static void tcindex_free_perfect_hash(struct tcindex_data *cp) |
287 | { | |
288 | int i; | |
289 | ||
290 | for (i = 0; i < cp->hash; i++) | |
291 | tcf_exts_destroy(&cp->perfect[i].exts); | |
292 | kfree(cp->perfect); | |
293 | } | |
294 | ||
295 | static int tcindex_alloc_perfect_hash(struct tcindex_data *cp) | |
296 | { | |
297 | int i, err = 0; | |
298 | ||
299 | cp->perfect = kcalloc(cp->hash, sizeof(struct tcindex_filter_result), | |
300 | GFP_KERNEL); | |
301 | if (!cp->perfect) | |
302 | return -ENOMEM; | |
303 | ||
304 | for (i = 0; i < cp->hash; i++) { | |
305 | err = tcf_exts_init(&cp->perfect[i].exts, | |
306 | TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE); | |
307 | if (err < 0) | |
308 | goto errout; | |
309 | } | |
310 | ||
311 | return 0; | |
312 | ||
313 | errout: | |
314 | tcindex_free_perfect_hash(cp); | |
315 | return err; | |
316 | } | |
317 | ||
1da177e4 | 318 | static int |
c1b52739 BL |
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, | |
2f7ef2f8 | 322 | struct nlattr *est, bool ovr) |
1da177e4 | 323 | { |
1da177e4 LT |
324 | struct tcindex_filter_result new_filter_result, *old_r = r; |
325 | struct tcindex_filter_result cr; | |
b9a24bb7 | 326 | struct tcindex_data *cp = NULL, *oldp; |
1da177e4 | 327 | struct tcindex_filter *f = NULL; /* make gcc behave */ |
b9a24bb7 | 328 | int err, balloc = 0; |
1da177e4 LT |
329 | struct tcf_exts e; |
330 | ||
b9a24bb7 | 331 | err = tcf_exts_init(&e, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE); |
1da177e4 LT |
332 | if (err < 0) |
333 | return err; | |
b9a24bb7 WC |
334 | err = tcf_exts_validate(net, tp, tb, est, &e, ovr); |
335 | if (err < 0) | |
336 | goto errout; | |
10297b99 | 337 | |
02c5e844 | 338 | err = -ENOMEM; |
331b7292 JF |
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. | |
342 | */ | |
a57a65ba | 343 | cp = kzalloc(sizeof(*cp), GFP_KERNEL); |
02c5e844 | 344 | if (!cp) |
44b75e43 | 345 | goto errout; |
331b7292 JF |
346 | |
347 | cp->mask = p->mask; | |
348 | cp->shift = p->shift; | |
349 | cp->hash = p->hash; | |
350 | cp->alloc_hash = p->alloc_hash; | |
351 | cp->fall_through = p->fall_through; | |
352 | cp->tp = tp; | |
353 | ||
354 | if (p->perfect) { | |
6e056569 WC |
355 | int i; |
356 | ||
b9a24bb7 | 357 | if (tcindex_alloc_perfect_hash(cp) < 0) |
331b7292 | 358 | goto errout; |
6e056569 | 359 | for (i = 0; i < cp->hash; i++) |
b9a24bb7 | 360 | cp->perfect[i].res = p->perfect[i].res; |
44b75e43 | 361 | balloc = 1; |
331b7292 JF |
362 | } |
363 | cp->h = p->h; | |
364 | ||
b9a24bb7 WC |
365 | err = tcindex_filter_result_init(&new_filter_result); |
366 | if (err < 0) | |
367 | goto errout1; | |
368 | err = tcindex_filter_result_init(&cr); | |
369 | if (err < 0) | |
370 | goto errout1; | |
1da177e4 | 371 | if (old_r) |
bf63ac73 | 372 | cr.res = r->res; |
1da177e4 | 373 | |
6fa8c014 | 374 | if (tb[TCA_TCINDEX_HASH]) |
331b7292 | 375 | cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]); |
1da177e4 | 376 | |
6fa8c014 | 377 | if (tb[TCA_TCINDEX_MASK]) |
331b7292 | 378 | cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]); |
1da177e4 | 379 | |
6fa8c014 | 380 | if (tb[TCA_TCINDEX_SHIFT]) |
331b7292 | 381 | cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]); |
1da177e4 LT |
382 | |
383 | err = -EBUSY; | |
331b7292 | 384 | |
1da177e4 LT |
385 | /* Hash already allocated, make sure that we still meet the |
386 | * requirements for the allocated hash. | |
387 | */ | |
331b7292 JF |
388 | if (cp->perfect) { |
389 | if (!valid_perfect_hash(cp) || | |
390 | cp->hash > cp->alloc_hash) | |
44b75e43 | 391 | goto errout_alloc; |
331b7292 | 392 | } else if (cp->h && cp->hash != cp->alloc_hash) { |
44b75e43 | 393 | goto errout_alloc; |
331b7292 | 394 | } |
1da177e4 LT |
395 | |
396 | err = -EINVAL; | |
6fa8c014 | 397 | if (tb[TCA_TCINDEX_FALL_THROUGH]) |
331b7292 | 398 | cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]); |
1da177e4 | 399 | |
331b7292 | 400 | if (!cp->hash) { |
1da177e4 LT |
401 | /* Hash not specified, use perfect hash if the upper limit |
402 | * of the hashing index is below the threshold. | |
403 | */ | |
331b7292 JF |
404 | if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD) |
405 | cp->hash = (cp->mask >> cp->shift) + 1; | |
1da177e4 | 406 | else |
331b7292 | 407 | cp->hash = DEFAULT_HASH_SIZE; |
1da177e4 LT |
408 | } |
409 | ||
68f6a7c6 | 410 | if (!cp->perfect && !cp->h) |
331b7292 | 411 | cp->alloc_hash = cp->hash; |
1da177e4 LT |
412 | |
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). | |
417 | */ | |
331b7292 JF |
418 | if (cp->perfect || valid_perfect_hash(cp)) |
419 | if (handle >= cp->alloc_hash) | |
44b75e43 | 420 | goto errout_alloc; |
1da177e4 LT |
421 | |
422 | ||
423 | err = -ENOMEM; | |
331b7292 JF |
424 | if (!cp->perfect && !cp->h) { |
425 | if (valid_perfect_hash(cp)) { | |
b9a24bb7 | 426 | if (tcindex_alloc_perfect_hash(cp) < 0) |
44b75e43 | 427 | goto errout_alloc; |
1da177e4 LT |
428 | balloc = 1; |
429 | } else { | |
331b7292 JF |
430 | struct tcindex_filter __rcu **hash; |
431 | ||
432 | hash = kcalloc(cp->hash, | |
433 | sizeof(struct tcindex_filter *), | |
434 | GFP_KERNEL); | |
435 | ||
436 | if (!hash) | |
44b75e43 | 437 | goto errout_alloc; |
331b7292 JF |
438 | |
439 | cp->h = hash; | |
1da177e4 LT |
440 | balloc = 2; |
441 | } | |
442 | } | |
443 | ||
331b7292 JF |
444 | if (cp->perfect) |
445 | r = cp->perfect + handle; | |
1da177e4 | 446 | else |
331b7292 | 447 | r = tcindex_lookup(cp, handle) ? : &new_filter_result; |
1da177e4 LT |
448 | |
449 | if (r == &new_filter_result) { | |
0da974f4 | 450 | f = kzalloc(sizeof(*f), GFP_KERNEL); |
1da177e4 LT |
451 | if (!f) |
452 | goto errout_alloc; | |
6e056569 | 453 | f->key = handle; |
6e056569 | 454 | f->next = NULL; |
b9a24bb7 WC |
455 | err = tcindex_filter_result_init(&f->result); |
456 | if (err < 0) { | |
457 | kfree(f); | |
458 | goto errout_alloc; | |
459 | } | |
10297b99 | 460 | } |
1da177e4 | 461 | |
add93b61 | 462 | if (tb[TCA_TCINDEX_CLASSID]) { |
1587bac4 | 463 | cr.res.classid = nla_get_u32(tb[TCA_TCINDEX_CLASSID]); |
1da177e4 | 464 | tcf_bind_filter(tp, &cr.res, base); |
10297b99 | 465 | } |
1da177e4 | 466 | |
bf63ac73 | 467 | if (old_r) |
9b0d4446 | 468 | tcf_exts_change(&r->exts, &e); |
bf63ac73 | 469 | else |
9b0d4446 | 470 | tcf_exts_change(&cr.exts, &e); |
1da177e4 | 471 | |
b9a24bb7 WC |
472 | if (old_r && old_r != r) { |
473 | err = tcindex_filter_result_init(old_r); | |
474 | if (err < 0) { | |
475 | kfree(f); | |
476 | goto errout_alloc; | |
477 | } | |
478 | } | |
1da177e4 | 479 | |
331b7292 | 480 | oldp = p; |
bf63ac73 | 481 | r->res = cr.res; |
331b7292 | 482 | rcu_assign_pointer(tp->root, cp); |
1da177e4 LT |
483 | |
484 | if (r == &new_filter_result) { | |
331b7292 JF |
485 | struct tcindex_filter *nfp; |
486 | struct tcindex_filter __rcu **fp; | |
1da177e4 | 487 | |
9b0d4446 | 488 | tcf_exts_change(&f->result.exts, &r->exts); |
331b7292 | 489 | |
69301eaa | 490 | fp = cp->h + (handle % cp->hash); |
331b7292 JF |
491 | for (nfp = rtnl_dereference(*fp); |
492 | nfp; | |
493 | fp = &nfp->next, nfp = rtnl_dereference(*fp)) | |
494 | ; /* nothing */ | |
495 | ||
496 | rcu_assign_pointer(*fp, f); | |
10297b99 | 497 | } |
1da177e4 | 498 | |
331b7292 JF |
499 | if (oldp) |
500 | call_rcu(&oldp->rcu, __tcindex_partial_destroy); | |
1da177e4 LT |
501 | return 0; |
502 | ||
503 | errout_alloc: | |
504 | if (balloc == 1) | |
b9a24bb7 | 505 | tcindex_free_perfect_hash(cp); |
1da177e4 | 506 | else if (balloc == 2) |
331b7292 | 507 | kfree(cp->h); |
b9a24bb7 WC |
508 | errout1: |
509 | tcf_exts_destroy(&cr.exts); | |
510 | tcf_exts_destroy(&new_filter_result.exts); | |
1da177e4 | 511 | errout: |
331b7292 | 512 | kfree(cp); |
18d0264f | 513 | tcf_exts_destroy(&e); |
1da177e4 LT |
514 | return err; |
515 | } | |
516 | ||
517 | static int | |
c1b52739 | 518 | tcindex_change(struct net *net, struct sk_buff *in_skb, |
af4c6641 | 519 | struct tcf_proto *tp, unsigned long base, u32 handle, |
8113c095 | 520 | struct nlattr **tca, void **arg, bool ovr) |
1da177e4 | 521 | { |
add93b61 PM |
522 | struct nlattr *opt = tca[TCA_OPTIONS]; |
523 | struct nlattr *tb[TCA_TCINDEX_MAX + 1]; | |
331b7292 | 524 | struct tcindex_data *p = rtnl_dereference(tp->root); |
8113c095 | 525 | struct tcindex_filter_result *r = *arg; |
cee63723 | 526 | int err; |
1da177e4 | 527 | |
aa767bfe | 528 | pr_debug("tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p," |
8113c095 WC |
529 | "p %p,r %p,*arg %p\n", |
530 | tp, handle, tca, arg, opt, p, r, arg ? *arg : NULL); | |
1da177e4 LT |
531 | |
532 | if (!opt) | |
533 | return 0; | |
534 | ||
fceb6435 | 535 | err = nla_parse_nested(tb, TCA_TCINDEX_MAX, opt, tcindex_policy, NULL); |
cee63723 PM |
536 | if (err < 0) |
537 | return err; | |
1da177e4 | 538 | |
c1b52739 | 539 | return tcindex_set_parms(net, tp, base, handle, p, r, tb, |
2f7ef2f8 | 540 | tca[TCA_RATE], ovr); |
1da177e4 LT |
541 | } |
542 | ||
1da177e4 LT |
543 | static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker) |
544 | { | |
331b7292 | 545 | struct tcindex_data *p = rtnl_dereference(tp->root); |
aa767bfe | 546 | struct tcindex_filter *f, *next; |
1da177e4 LT |
547 | int i; |
548 | ||
aa767bfe | 549 | pr_debug("tcindex_walk(tp %p,walker %p),p %p\n", tp, walker, p); |
1da177e4 LT |
550 | if (p->perfect) { |
551 | for (i = 0; i < p->hash; i++) { | |
552 | if (!p->perfect[i].res.class) | |
553 | continue; | |
554 | if (walker->count >= walker->skip) { | |
8113c095 | 555 | if (walker->fn(tp, p->perfect + i, walker) < 0) { |
1da177e4 LT |
556 | walker->stop = 1; |
557 | return; | |
558 | } | |
559 | } | |
560 | walker->count++; | |
561 | } | |
562 | } | |
563 | if (!p->h) | |
564 | return; | |
565 | for (i = 0; i < p->hash; i++) { | |
331b7292 JF |
566 | for (f = rtnl_dereference(p->h[i]); f; f = next) { |
567 | next = rtnl_dereference(f->next); | |
1da177e4 | 568 | if (walker->count >= walker->skip) { |
8113c095 | 569 | if (walker->fn(tp, &f->result, walker) < 0) { |
1da177e4 LT |
570 | walker->stop = 1; |
571 | return; | |
572 | } | |
573 | } | |
574 | walker->count++; | |
575 | } | |
576 | } | |
577 | } | |
578 | ||
763dbf63 | 579 | static void tcindex_destroy(struct tcf_proto *tp) |
1da177e4 | 580 | { |
331b7292 | 581 | struct tcindex_data *p = rtnl_dereference(tp->root); |
1da177e4 LT |
582 | struct tcf_walker walker; |
583 | ||
aa767bfe | 584 | pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p); |
1da177e4 LT |
585 | walker.count = 0; |
586 | walker.skip = 0; | |
e40f5c72 | 587 | walker.fn = tcindex_destroy_element; |
aa767bfe | 588 | tcindex_walk(tp, &walker); |
331b7292 | 589 | |
331b7292 | 590 | call_rcu(&p->rcu, __tcindex_destroy); |
1da177e4 LT |
591 | } |
592 | ||
593 | ||
8113c095 | 594 | static int tcindex_dump(struct net *net, struct tcf_proto *tp, void *fh, |
5a7a5555 | 595 | struct sk_buff *skb, struct tcmsg *t) |
1da177e4 | 596 | { |
331b7292 | 597 | struct tcindex_data *p = rtnl_dereference(tp->root); |
8113c095 | 598 | struct tcindex_filter_result *r = fh; |
4b3550ef | 599 | struct nlattr *nest; |
1da177e4 | 600 | |
8113c095 | 601 | pr_debug("tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p\n", |
6ea3b446 | 602 | tp, fh, skb, t, p, r); |
aa767bfe | 603 | pr_debug("p->perfect %p p->h %p\n", p->perfect, p->h); |
4b3550ef PM |
604 | |
605 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
606 | if (nest == NULL) | |
607 | goto nla_put_failure; | |
608 | ||
1da177e4 LT |
609 | if (!fh) { |
610 | t->tcm_handle = ~0; /* whatever ... */ | |
1b34ec43 DM |
611 | if (nla_put_u32(skb, TCA_TCINDEX_HASH, p->hash) || |
612 | nla_put_u16(skb, TCA_TCINDEX_MASK, p->mask) || | |
613 | nla_put_u32(skb, TCA_TCINDEX_SHIFT, p->shift) || | |
614 | nla_put_u32(skb, TCA_TCINDEX_FALL_THROUGH, p->fall_through)) | |
615 | goto nla_put_failure; | |
4b3550ef | 616 | nla_nest_end(skb, nest); |
1da177e4 LT |
617 | } else { |
618 | if (p->perfect) { | |
331b7292 | 619 | t->tcm_handle = r - p->perfect; |
1da177e4 LT |
620 | } else { |
621 | struct tcindex_filter *f; | |
331b7292 | 622 | struct tcindex_filter __rcu **fp; |
1da177e4 LT |
623 | int i; |
624 | ||
625 | t->tcm_handle = 0; | |
626 | for (i = 0; !t->tcm_handle && i < p->hash; i++) { | |
331b7292 JF |
627 | fp = &p->h[i]; |
628 | for (f = rtnl_dereference(*fp); | |
629 | !t->tcm_handle && f; | |
630 | fp = &f->next, f = rtnl_dereference(*fp)) { | |
1da177e4 LT |
631 | if (&f->result == r) |
632 | t->tcm_handle = f->key; | |
633 | } | |
634 | } | |
635 | } | |
aa767bfe | 636 | pr_debug("handle = %d\n", t->tcm_handle); |
1b34ec43 DM |
637 | if (r->res.class && |
638 | nla_put_u32(skb, TCA_TCINDEX_CLASSID, r->res.classid)) | |
639 | goto nla_put_failure; | |
1da177e4 | 640 | |
5da57f42 | 641 | if (tcf_exts_dump(skb, &r->exts) < 0) |
add93b61 | 642 | goto nla_put_failure; |
4b3550ef | 643 | nla_nest_end(skb, nest); |
1da177e4 | 644 | |
5da57f42 | 645 | if (tcf_exts_dump_stats(skb, &r->exts) < 0) |
add93b61 | 646 | goto nla_put_failure; |
1da177e4 | 647 | } |
10297b99 | 648 | |
1da177e4 LT |
649 | return skb->len; |
650 | ||
add93b61 | 651 | nla_put_failure: |
6ea3b446 | 652 | nla_nest_cancel(skb, nest); |
1da177e4 LT |
653 | return -1; |
654 | } | |
655 | ||
07d79fc7 CW |
656 | static void tcindex_bind_class(void *fh, u32 classid, unsigned long cl) |
657 | { | |
658 | struct tcindex_filter_result *r = fh; | |
659 | ||
660 | if (r && r->res.classid == classid) | |
661 | r->res.class = cl; | |
662 | } | |
663 | ||
2eb9d75c | 664 | static struct tcf_proto_ops cls_tcindex_ops __read_mostly = { |
1da177e4 LT |
665 | .kind = "tcindex", |
666 | .classify = tcindex_classify, | |
667 | .init = tcindex_init, | |
668 | .destroy = tcindex_destroy, | |
669 | .get = tcindex_get, | |
1da177e4 LT |
670 | .change = tcindex_change, |
671 | .delete = tcindex_delete, | |
672 | .walk = tcindex_walk, | |
673 | .dump = tcindex_dump, | |
07d79fc7 | 674 | .bind_class = tcindex_bind_class, |
1da177e4 LT |
675 | .owner = THIS_MODULE, |
676 | }; | |
677 | ||
678 | static int __init init_tcindex(void) | |
679 | { | |
680 | return register_tcf_proto_ops(&cls_tcindex_ops); | |
681 | } | |
682 | ||
10297b99 | 683 | static void __exit exit_tcindex(void) |
1da177e4 LT |
684 | { |
685 | unregister_tcf_proto_ops(&cls_tcindex_ops); | |
686 | } | |
687 | ||
688 | module_init(init_tcindex) | |
689 | module_exit(exit_tcindex) | |
690 | MODULE_LICENSE("GPL"); |