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 <linux/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>
23 #include <linux/socket.h>
24 #include <linux/sockios.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>
34 #include <net/act_api.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
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)
55 static __inline__ struct tcf_police * tcf_police_lookup(u32 index)
59 read_lock(&police_lock);
60 for (p = tcf_police_ht[tcf_police_hash(index)]; p; p = p->next) {
61 if (p->index == index)
64 read_unlock(&police_lock);
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)
72 int err =0, index = -1,i= 0, s_i = 0, n_i = 0;
75 read_lock(&police_lock);
79 for (i = 0; i < MY_TAB_SIZE; i++) {
80 p = tcf_police_ht[tcf_police_hash(i)];
82 for (; p; p = p->next) {
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);
93 err = tcf_action_dump_1(skb, a, 0, 0);
96 skb_trim(skb, (u8*)r - skb->data);
99 r->rta_len = skb->tail - (u8*)r;
104 read_unlock(&police_lock);
110 skb_trim(skb, (u8*)r - skb->data);
115 tcf_hash_search(struct tc_action *a, u32 index)
117 struct tcf_police *p = tcf_police_lookup(index);
130 static __inline__ u32 tcf_police_new_index(void)
135 } while (tcf_police_lookup(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) {
148 write_lock_bh(&police_lock);
150 write_unlock_bh(&police_lock);
151 #ifdef CONFIG_NET_ESTIMATOR
152 gen_kill_estimator(&p->bstats, &p->rate_est);
155 qdisc_put_rtab(p->R_tab);
157 qdisc_put_rtab(p->P_tab);
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)
170 struct rtattr *tb[TCA_POLICE_MAX];
171 struct tc_police *parm;
172 struct tcf_police *p;
176 printk("BUG: tcf_police_locate called with NULL params\n");
180 if (rtattr_parse(tb, TCA_POLICE_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) < 0)
183 if (tb[TCA_POLICE_TBF-1] == NULL)
186 parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
188 if (parm->index && (p = tcf_police_lookup(parm->index)) != NULL) {
198 spin_unlock(&p->lock);
202 p = kmalloc(sizeof(*p), GFP_KERNEL);
206 memset(p, 0, sizeof(*p));
209 spin_lock_init(&p->lock);
210 p->stats_lock = &p->lock;
214 if (parm->rate.rate) {
215 if ((p->R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1])) == NULL) {
218 if (parm->peakrate.rate &&
219 (p->P_tab = qdisc_get_rtab(&parm->peakrate, tb[TCA_POLICE_PEAKRATE-1])) == NULL) {
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]);
229 p->toks = p->burst = parm->burst;
234 p->mtu = 255<<p->R_tab->rate.cell_log;
237 p->ptoks = L2T_P(p, p->mtu);
238 p->action = parm->action;
241 spin_unlock(&p->lock);
244 PSCHED_GET_TIME(p->t_c);
245 p->index = parm->index ? : tcf_police_new_index();
246 #ifdef CONFIG_NET_ESTIMATOR
248 gen_new_estimator(&p->bstats, &p->rate_est, p->stats_lock, est);
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);
261 qdisc_put_rtab(p->R_tab);
263 spin_unlock(&p->lock);
268 int tcf_act_police_cleanup(struct tc_action *a, int bind)
270 struct tcf_police *p;
273 return tcf_police_release(p, bind);
278 int tcf_act_police(struct sk_buff **pskb, struct tc_action *a)
281 struct sk_buff *skb = *pskb;
282 struct tcf_police *p;
289 printk("BUG: tcf_police called with NULL params\n");
295 p->bstats.bytes += skb->len;
298 #ifdef CONFIG_NET_ESTIMATOR
299 if (p->ewma_rate && p->rate_est.bps >= p->ewma_rate) {
300 p->qstats.overlimits++;
301 spin_unlock(&p->lock);
306 if (skb->len <= p->mtu) {
307 if (p->R_tab == NULL) {
308 spin_unlock(&p->lock);
312 PSCHED_GET_TIME(now);
314 toks = PSCHED_TDIFF_SAFE(now, p->t_c, p->burst);
317 ptoks = toks + p->ptoks;
318 if (ptoks > (long)L2T_P(p, p->mtu))
319 ptoks = (long)L2T_P(p, p->mtu);
320 ptoks -= L2T_P(p, skb->len);
323 if (toks > (long)p->burst)
325 toks -= L2T(p, skb->len);
327 if ((toks|ptoks) >= 0) {
331 spin_unlock(&p->lock);
336 p->qstats.overlimits++;
337 spin_unlock(&p->lock);
341 int tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
343 unsigned char *b = skb->tail;
344 struct tc_police opt;
345 struct tcf_police *p;
349 printk("BUG: tcf_police_dump called with NULL params\n");
353 opt.index = p->index;
354 opt.action = p->action;
356 opt.burst = p->burst;
357 opt.refcnt = p->refcnt - ref;
358 opt.bindcnt = p->bindcnt - bind;
360 opt.rate = p->R_tab->rate;
362 memset(&opt.rate, 0, sizeof(opt.rate));
364 opt.peakrate = p->P_tab->rate;
366 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
367 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
369 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), &p->result);
370 #ifdef CONFIG_NET_ESTIMATOR
372 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &p->ewma_rate);
377 skb_trim(skb, b - skb->data);
381 MODULE_AUTHOR("Alexey Kuznetsov");
382 MODULE_DESCRIPTION("Policing actions");
383 MODULE_LICENSE("GPL");
386 static struct tc_action_ops act_police_ops = {
389 .type = TCA_ID_POLICE,
390 .capab = TCA_CAP_NONE,
391 .owner = THIS_MODULE,
392 .act = tcf_act_police,
393 .dump = tcf_act_police_dump,
394 .cleanup = tcf_act_police_cleanup,
395 .lookup = tcf_hash_search,
396 .init = tcf_act_police_locate,
397 .walk = tcf_generic_walker
401 police_init_module(void)
403 return tcf_register_action(&act_police_ops);
407 police_cleanup_module(void)
409 tcf_unregister_action(&act_police_ops);
412 module_init(police_init_module);
413 module_exit(police_cleanup_module);
417 struct tcf_police * tcf_police_locate(struct rtattr *rta, struct rtattr *est)
420 struct tcf_police *p;
421 struct rtattr *tb[TCA_POLICE_MAX];
422 struct tc_police *parm;
424 if (rtattr_parse(tb, TCA_POLICE_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta)) < 0)
427 if (tb[TCA_POLICE_TBF-1] == NULL)
430 parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
432 if (parm->index && (p = tcf_police_lookup(parm->index)) != NULL) {
437 p = kmalloc(sizeof(*p), GFP_KERNEL);
441 memset(p, 0, sizeof(*p));
443 spin_lock_init(&p->lock);
444 p->stats_lock = &p->lock;
445 if (parm->rate.rate) {
446 if ((p->R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1])) == NULL)
448 if (parm->peakrate.rate &&
449 (p->P_tab = qdisc_get_rtab(&parm->peakrate, tb[TCA_POLICE_PEAKRATE-1])) == NULL)
452 if (tb[TCA_POLICE_RESULT-1])
453 p->result = *(int*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
454 #ifdef CONFIG_NET_ESTIMATOR
455 if (tb[TCA_POLICE_AVRATE-1])
456 p->ewma_rate = *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
458 p->toks = p->burst = parm->burst;
463 p->mtu = 255<<p->R_tab->rate.cell_log;
466 p->ptoks = L2T_P(p, p->mtu);
467 PSCHED_GET_TIME(p->t_c);
468 p->index = parm->index ? : tcf_police_new_index();
469 p->action = parm->action;
470 #ifdef CONFIG_NET_ESTIMATOR
472 gen_new_estimator(&p->bstats, &p->rate_est, p->stats_lock, est);
474 h = tcf_police_hash(p->index);
475 write_lock_bh(&police_lock);
476 p->next = tcf_police_ht[h];
477 tcf_police_ht[h] = p;
478 write_unlock_bh(&police_lock);
483 qdisc_put_rtab(p->R_tab);
488 int tcf_police(struct sk_buff *skb, struct tcf_police *p)
496 p->bstats.bytes += skb->len;
499 #ifdef CONFIG_NET_ESTIMATOR
500 if (p->ewma_rate && p->rate_est.bps >= p->ewma_rate) {
501 p->qstats.overlimits++;
502 spin_unlock(&p->lock);
507 if (skb->len <= p->mtu) {
508 if (p->R_tab == NULL) {
509 spin_unlock(&p->lock);
513 PSCHED_GET_TIME(now);
515 toks = PSCHED_TDIFF_SAFE(now, p->t_c, p->burst);
518 ptoks = toks + p->ptoks;
519 if (ptoks > (long)L2T_P(p, p->mtu))
520 ptoks = (long)L2T_P(p, p->mtu);
521 ptoks -= L2T_P(p, skb->len);
524 if (toks > (long)p->burst)
526 toks -= L2T(p, skb->len);
528 if ((toks|ptoks) >= 0) {
532 spin_unlock(&p->lock);
537 p->qstats.overlimits++;
538 spin_unlock(&p->lock);
542 int tcf_police_dump(struct sk_buff *skb, struct tcf_police *p)
544 unsigned char *b = skb->tail;
545 struct tc_police opt;
547 opt.index = p->index;
548 opt.action = p->action;
550 opt.burst = p->burst;
552 opt.rate = p->R_tab->rate;
554 memset(&opt.rate, 0, sizeof(opt.rate));
556 opt.peakrate = p->P_tab->rate;
558 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
559 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
561 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), &p->result);
562 #ifdef CONFIG_NET_ESTIMATOR
564 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &p->ewma_rate);
569 skb_trim(skb, b - skb->data);
573 int tcf_police_dump_stats(struct sk_buff *skb, struct tcf_police *p)
577 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
578 TCA_XSTATS, p->stats_lock, &d) < 0)
581 if (gnet_stats_copy_basic(&d, &p->bstats) < 0 ||
582 #ifdef CONFIG_NET_ESTIMATOR
583 gnet_stats_copy_rate_est(&d, &p->rate_est) < 0 ||
585 gnet_stats_copy_queue(&d, &p->qstats) < 0)
588 if (gnet_stats_finish_copy(&d) < 0)
598 EXPORT_SYMBOL(tcf_police);
599 EXPORT_SYMBOL(tcf_police_destroy);
600 EXPORT_SYMBOL(tcf_police_dump);
601 EXPORT_SYMBOL(tcf_police_dump_stats);
602 EXPORT_SYMBOL(tcf_police_hash);
603 EXPORT_SYMBOL(tcf_police_ht);
604 EXPORT_SYMBOL(tcf_police_locate);
605 EXPORT_SYMBOL(tcf_police_lookup);
606 EXPORT_SYMBOL(tcf_police_new_index);