MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / net / sched / police.c
blob12bd44006537668f955fc3f30f50bece458a211c
1 /*
2 * net/sched/police.c Input police filter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * J Hadi Salim (action changes)
13 #include <asm/uaccess.h>
14 #include <asm/system.h>
15 #include <asm/bitops.h>
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/mm.h>
23 #include <linux/socket.h>
24 #include <linux/sockios.h>
25 #include <linux/in.h>
26 #include <linux/errno.h>
27 #include <linux/interrupt.h>
28 #include <linux/netdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/module.h>
31 #include <linux/rtnetlink.h>
32 #include <linux/init.h>
33 #include <net/sock.h>
34 #include <net/pkt_sched.h>
36 #define L2T(p,L) ((p)->R_tab->data[(L)>>(p)->R_tab->rate.cell_log])
37 #define L2T_P(p,L) ((p)->P_tab->data[(L)>>(p)->P_tab->rate.cell_log])
38 #define PRIV(a) ((struct tcf_police *) (a)->priv)
40 /* use generic hash table */
41 #define MY_TAB_SIZE 16
42 #define MY_TAB_MASK 15
43 static u32 idx_gen;
44 static struct tcf_police *tcf_police_ht[MY_TAB_SIZE];
45 /* Policer hash table lock */
46 static rwlock_t police_lock = RW_LOCK_UNLOCKED;
48 /* Each policer is serialized by its individual spinlock */
50 static __inline__ unsigned tcf_police_hash(u32 index)
52 return index&0xF;
55 static __inline__ struct tcf_police * tcf_police_lookup(u32 index)
57 struct tcf_police *p;
59 read_lock(&police_lock);
60 for (p = tcf_police_ht[tcf_police_hash(index)]; p; p = p->next) {
61 if (p->index == index)
62 break;
64 read_unlock(&police_lock);
65 return p;
68 #ifdef CONFIG_NET_CLS_ACT
69 static __inline__ int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb, int type, struct tc_action *a)
71 struct tcf_police *p;
72 int err =0, index = -1,i= 0, s_i = 0, n_i = 0;
73 struct rtattr *r ;
75 read_lock(&police_lock);
77 s_i = cb->args[0];
79 for (i = 0; i < MY_TAB_SIZE; i++) {
80 p = tcf_police_ht[tcf_police_hash(i)];
82 for (; p; p = p->next) {
83 index++;
84 if (index < s_i)
85 continue;
86 a->priv = p;
87 a->order = index;
88 r = (struct rtattr*) skb->tail;
89 RTA_PUT(skb, a->order, 0, NULL);
90 if (type == RTM_DELACTION)
91 err = tcf_action_dump_1(skb, a, 0, 1);
92 else
93 err = tcf_action_dump_1(skb, a, 0, 0);
94 if (0 > err) {
95 index--;
96 skb_trim(skb, (u8*)r - skb->data);
97 goto done;
99 r->rta_len = skb->tail - (u8*)r;
100 n_i++;
103 done:
104 read_unlock(&police_lock);
105 if (n_i)
106 cb->args[0] += n_i;
107 return n_i;
109 rtattr_failure:
110 skb_trim(skb, (u8*)r - skb->data);
111 goto done;
114 static inline int
115 tcf_hash_search(struct tc_action *a, u32 index)
117 struct tcf_police *p = tcf_police_lookup(index);
119 if (p != NULL) {
120 a->priv = p;
121 return 1;
122 } else {
123 return 0;
128 #endif
130 static __inline__ u32 tcf_police_new_index(void)
132 do {
133 if (++idx_gen == 0)
134 idx_gen = 1;
135 } while (tcf_police_lookup(idx_gen));
137 return idx_gen;
141 void tcf_police_destroy(struct tcf_police *p)
143 unsigned h = tcf_police_hash(p->index);
144 struct tcf_police **p1p;
146 for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->next) {
147 if (*p1p == p) {
148 write_lock_bh(&police_lock);
149 *p1p = p->next;
150 write_unlock_bh(&police_lock);
151 #ifdef CONFIG_NET_ESTIMATOR
152 qdisc_kill_estimator(&p->stats);
153 #endif
154 if (p->R_tab)
155 qdisc_put_rtab(p->R_tab);
156 if (p->P_tab)
157 qdisc_put_rtab(p->P_tab);
158 kfree(p);
159 return;
162 BUG_TRAP(0);
165 #ifdef CONFIG_NET_CLS_ACT
166 int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,struct tc_action *a, int ovr, int bind)
168 unsigned h;
169 int ret = 0;
170 struct rtattr *tb[TCA_POLICE_MAX];
171 struct tc_police *parm;
172 struct tcf_police *p;
174 if (NULL == a) {
175 if (net_ratelimit())
176 printk("BUG: tcf_police_locate called with NULL params\n");
177 return -1;
180 if (rtattr_parse(tb, TCA_POLICE_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) < 0)
181 return -1;
183 if (tb[TCA_POLICE_TBF-1] == NULL)
184 return -1;
186 parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
188 if (parm->index && (p = tcf_police_lookup(parm->index)) != NULL) {
189 a->priv = (void *)p;
190 spin_lock(&p->lock);
191 if (bind) {
192 p->bindcnt += 1;
193 p->refcnt += 1;
195 if (ovr) {
196 goto override;
198 spin_unlock(&p->lock);
199 return ret;
202 p = kmalloc(sizeof(*p), GFP_KERNEL);
203 if (p == NULL)
204 return -1;
206 memset(p, 0, sizeof(*p));
207 ret = 1;
208 p->refcnt = 1;
209 spin_lock_init(&p->lock);
210 p->stats_lock = &p->lock;
211 if (bind)
212 p->bindcnt = 1;
213 override:
214 if (parm->rate.rate) {
215 if ((p->R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1])) == NULL) {
216 goto failure;
218 if (parm->peakrate.rate &&
219 (p->P_tab = qdisc_get_rtab(&parm->peakrate, tb[TCA_POLICE_PEAKRATE-1])) == NULL) {
220 goto failure;
223 if (tb[TCA_POLICE_RESULT-1])
224 p->result = *(int*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
225 #ifdef CONFIG_NET_ESTIMATOR
226 if (tb[TCA_POLICE_AVRATE-1])
227 p->ewma_rate = *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
228 #endif
229 p->toks = p->burst = parm->burst;
230 p->mtu = parm->mtu;
231 if (p->mtu == 0) {
232 p->mtu = ~0;
233 if (p->R_tab)
234 p->mtu = 255<<p->R_tab->rate.cell_log;
236 if (p->P_tab)
237 p->ptoks = L2T_P(p, p->mtu);
238 p->action = parm->action;
240 if (ovr) {
241 spin_unlock(&p->lock);
242 return ret;
244 PSCHED_GET_TIME(p->t_c);
245 p->index = parm->index ? : tcf_police_new_index();
246 #ifdef CONFIG_NET_ESTIMATOR
247 if (est)
248 qdisc_new_estimator(&p->stats, p->stats_lock, est);
249 #endif
250 h = tcf_police_hash(p->index);
251 write_lock_bh(&police_lock);
252 p->next = tcf_police_ht[h];
253 tcf_police_ht[h] = p;
254 write_unlock_bh(&police_lock);
256 a->priv = (void *)p;
257 return ret;
259 failure:
260 if (p->R_tab)
261 qdisc_put_rtab(p->R_tab);
262 if (ovr)
263 spin_unlock(&p->lock);
264 kfree(p);
265 return -1;
268 int tcf_act_police_cleanup(struct tc_action *a, int bind)
270 struct tcf_police *p;
271 p = PRIV(a);
272 if (NULL != p)
273 return tcf_police_release(p, bind);
275 return 0;
278 int tcf_act_police_stats(struct sk_buff *skb, struct tc_action *a)
280 struct tcf_police *p;
281 p = PRIV(a);
282 if (NULL != p)
283 return qdisc_copy_stats(skb, &p->stats, p->stats_lock);
285 return 1;
288 int tcf_act_police(struct sk_buff **pskb, struct tc_action *a)
290 psched_time_t now;
291 struct sk_buff *skb = *pskb;
292 struct tcf_police *p;
293 long toks;
294 long ptoks = 0;
296 p = PRIV(a);
298 if (NULL == p) {
299 printk("BUG: tcf_police called with NULL params\n");
300 return -1;
303 spin_lock(&p->lock);
305 p->stats.bytes += skb->len;
306 p->stats.packets++;
308 #ifdef CONFIG_NET_ESTIMATOR
309 if (p->ewma_rate && p->stats.bps >= p->ewma_rate) {
310 p->stats.overlimits++;
311 spin_unlock(&p->lock);
312 return p->action;
314 #endif
316 if (skb->len <= p->mtu) {
317 if (p->R_tab == NULL) {
318 spin_unlock(&p->lock);
319 return p->result;
322 PSCHED_GET_TIME(now);
324 toks = PSCHED_TDIFF_SAFE(now, p->t_c, p->burst);
326 if (p->P_tab) {
327 ptoks = toks + p->ptoks;
328 if (ptoks > (long)L2T_P(p, p->mtu))
329 ptoks = (long)L2T_P(p, p->mtu);
330 ptoks -= L2T_P(p, skb->len);
332 toks += p->toks;
333 if (toks > (long)p->burst)
334 toks = p->burst;
335 toks -= L2T(p, skb->len);
337 if ((toks|ptoks) >= 0) {
338 p->t_c = now;
339 p->toks = toks;
340 p->ptoks = ptoks;
341 spin_unlock(&p->lock);
342 return p->result;
346 p->stats.overlimits++;
347 spin_unlock(&p->lock);
348 return p->action;
351 int tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
353 unsigned char *b = skb->tail;
354 struct tc_police opt;
355 struct tcf_police *p;
357 p = PRIV(a);
358 if (NULL == p) {
359 printk("BUG: tcf_police_dump called with NULL params\n");
360 goto rtattr_failure;
363 opt.index = p->index;
364 opt.action = p->action;
365 opt.mtu = p->mtu;
366 opt.burst = p->burst;
367 opt.refcnt = p->refcnt - ref;
368 opt.bindcnt = p->bindcnt - bind;
369 if (p->R_tab)
370 opt.rate = p->R_tab->rate;
371 else
372 memset(&opt.rate, 0, sizeof(opt.rate));
373 if (p->P_tab)
374 opt.peakrate = p->P_tab->rate;
375 else
376 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
377 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
378 if (p->result)
379 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), &p->result);
380 #ifdef CONFIG_NET_ESTIMATOR
381 if (p->ewma_rate)
382 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &p->ewma_rate);
383 #endif
384 return skb->len;
386 rtattr_failure:
387 skb_trim(skb, b - skb->data);
388 return -1;
391 MODULE_AUTHOR("Alexey Kuznetsov");
392 MODULE_DESCRIPTION("Policing actions");
393 MODULE_LICENSE("GPL");
396 static struct tc_action_ops act_police_ops = {
397 .next = NULL,
398 .kind = "police",
399 .type = TCA_ID_POLICE,
400 .capab = TCA_CAP_NONE,
401 .owner = THIS_MODULE,
402 .act = tcf_act_police,
403 .get_stats = tcf_act_police_stats,
404 .dump = tcf_act_police_dump,
405 .cleanup = tcf_act_police_cleanup,
406 .lookup = tcf_hash_search,
407 .init = tcf_act_police_locate,
408 .walk = tcf_generic_walker
411 static int __init
412 police_init_module(void)
414 return tcf_register_action(&act_police_ops);
417 static void __exit
418 police_cleanup_module(void)
420 tcf_unregister_action(&act_police_ops);
423 module_init(police_init_module);
424 module_exit(police_cleanup_module);
426 #endif
428 struct tcf_police * tcf_police_locate(struct rtattr *rta, struct rtattr *est)
430 unsigned h;
431 struct tcf_police *p;
432 struct rtattr *tb[TCA_POLICE_MAX];
433 struct tc_police *parm;
435 if (rtattr_parse(tb, TCA_POLICE_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) < 0)
436 return NULL;
438 if (tb[TCA_POLICE_TBF-1] == NULL)
439 return NULL;
441 parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
443 if (parm->index && (p = tcf_police_lookup(parm->index)) != NULL) {
444 p->refcnt++;
445 return p;
448 p = kmalloc(sizeof(*p), GFP_KERNEL);
449 if (p == NULL)
450 return NULL;
452 memset(p, 0, sizeof(*p));
453 p->refcnt = 1;
454 spin_lock_init(&p->lock);
455 p->stats_lock = &p->lock;
456 if (parm->rate.rate) {
457 if ((p->R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1])) == NULL)
458 goto failure;
459 if (parm->peakrate.rate &&
460 (p->P_tab = qdisc_get_rtab(&parm->peakrate, tb[TCA_POLICE_PEAKRATE-1])) == NULL)
461 goto failure;
463 if (tb[TCA_POLICE_RESULT-1])
464 p->result = *(int*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
465 #ifdef CONFIG_NET_ESTIMATOR
466 if (tb[TCA_POLICE_AVRATE-1])
467 p->ewma_rate = *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
468 #endif
469 p->toks = p->burst = parm->burst;
470 p->mtu = parm->mtu;
471 if (p->mtu == 0) {
472 p->mtu = ~0;
473 if (p->R_tab)
474 p->mtu = 255<<p->R_tab->rate.cell_log;
476 if (p->P_tab)
477 p->ptoks = L2T_P(p, p->mtu);
478 PSCHED_GET_TIME(p->t_c);
479 p->index = parm->index ? : tcf_police_new_index();
480 p->action = parm->action;
481 #ifdef CONFIG_NET_ESTIMATOR
482 if (est)
483 qdisc_new_estimator(&p->stats, p->stats_lock, est);
484 #endif
485 h = tcf_police_hash(p->index);
486 write_lock_bh(&police_lock);
487 p->next = tcf_police_ht[h];
488 tcf_police_ht[h] = p;
489 write_unlock_bh(&police_lock);
490 return p;
492 failure:
493 if (p->R_tab)
494 qdisc_put_rtab(p->R_tab);
495 kfree(p);
496 return NULL;
499 int tcf_police(struct sk_buff *skb, struct tcf_police *p)
501 psched_time_t now;
502 long toks;
503 long ptoks = 0;
505 spin_lock(&p->lock);
507 p->stats.bytes += skb->len;
508 p->stats.packets++;
510 #ifdef CONFIG_NET_ESTIMATOR
511 if (p->ewma_rate && p->stats.bps >= p->ewma_rate) {
512 p->stats.overlimits++;
513 spin_unlock(&p->lock);
514 return p->action;
516 #endif
518 if (skb->len <= p->mtu) {
519 if (p->R_tab == NULL) {
520 spin_unlock(&p->lock);
521 return p->result;
524 PSCHED_GET_TIME(now);
526 toks = PSCHED_TDIFF_SAFE(now, p->t_c, p->burst);
528 if (p->P_tab) {
529 ptoks = toks + p->ptoks;
530 if (ptoks > (long)L2T_P(p, p->mtu))
531 ptoks = (long)L2T_P(p, p->mtu);
532 ptoks -= L2T_P(p, skb->len);
534 toks += p->toks;
535 if (toks > (long)p->burst)
536 toks = p->burst;
537 toks -= L2T(p, skb->len);
539 if ((toks|ptoks) >= 0) {
540 p->t_c = now;
541 p->toks = toks;
542 p->ptoks = ptoks;
543 spin_unlock(&p->lock);
544 return p->result;
548 p->stats.overlimits++;
549 spin_unlock(&p->lock);
550 return p->action;
553 int tcf_police_dump(struct sk_buff *skb, struct tcf_police *p)
555 unsigned char *b = skb->tail;
556 struct tc_police opt;
558 opt.index = p->index;
559 opt.action = p->action;
560 opt.mtu = p->mtu;
561 opt.burst = p->burst;
562 if (p->R_tab)
563 opt.rate = p->R_tab->rate;
564 else
565 memset(&opt.rate, 0, sizeof(opt.rate));
566 if (p->P_tab)
567 opt.peakrate = p->P_tab->rate;
568 else
569 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
570 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
571 if (p->result)
572 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), &p->result);
573 #ifdef CONFIG_NET_ESTIMATOR
574 if (p->ewma_rate)
575 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &p->ewma_rate);
576 #endif
577 return skb->len;
579 rtattr_failure:
580 skb_trim(skb, b - skb->data);
581 return -1;
584 EXPORT_SYMBOL(tcf_police);
585 EXPORT_SYMBOL(tcf_police_destroy);
586 EXPORT_SYMBOL(tcf_police_dump);
587 EXPORT_SYMBOL(tcf_police_hash);
588 EXPORT_SYMBOL(tcf_police_ht);
589 EXPORT_SYMBOL(tcf_police_locate);
590 EXPORT_SYMBOL(tcf_police_lookup);
591 EXPORT_SYMBOL(tcf_police_new_index);