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>
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/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
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 qdisc_kill_estimator(&p
->stats
);
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 qdisc_new_estimator(&p
->stats
, 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_stats(struct sk_buff
*skb
, struct tc_action
*a
)
280 struct tcf_police
*p
;
283 return qdisc_copy_stats(skb
, &p
->stats
, p
->stats_lock
);
288 int tcf_act_police(struct sk_buff
**pskb
, struct tc_action
*a
)
291 struct sk_buff
*skb
= *pskb
;
292 struct tcf_police
*p
;
299 printk("BUG: tcf_police called with NULL params\n");
305 p
->stats
.bytes
+= skb
->len
;
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
);
316 if (skb
->len
<= p
->mtu
) {
317 if (p
->R_tab
== NULL
) {
318 spin_unlock(&p
->lock
);
322 PSCHED_GET_TIME(now
);
324 toks
= PSCHED_TDIFF_SAFE(now
, p
->t_c
, p
->burst
);
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
);
333 if (toks
> (long)p
->burst
)
335 toks
-= L2T(p
, skb
->len
);
337 if ((toks
|ptoks
) >= 0) {
341 spin_unlock(&p
->lock
);
346 p
->stats
.overlimits
++;
347 spin_unlock(&p
->lock
);
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
;
359 printk("BUG: tcf_police_dump called with NULL params\n");
363 opt
.index
= p
->index
;
364 opt
.action
= p
->action
;
366 opt
.burst
= p
->burst
;
367 opt
.refcnt
= p
->refcnt
- ref
;
368 opt
.bindcnt
= p
->bindcnt
- bind
;
370 opt
.rate
= p
->R_tab
->rate
;
372 memset(&opt
.rate
, 0, sizeof(opt
.rate
));
374 opt
.peakrate
= p
->P_tab
->rate
;
376 memset(&opt
.peakrate
, 0, sizeof(opt
.peakrate
));
377 RTA_PUT(skb
, TCA_POLICE_TBF
, sizeof(opt
), &opt
);
379 RTA_PUT(skb
, TCA_POLICE_RESULT
, sizeof(int), &p
->result
);
380 #ifdef CONFIG_NET_ESTIMATOR
382 RTA_PUT(skb
, TCA_POLICE_AVRATE
, 4, &p
->ewma_rate
);
387 skb_trim(skb
, b
- skb
->data
);
391 MODULE_AUTHOR("Alexey Kuznetsov");
392 MODULE_DESCRIPTION("Policing actions");
393 MODULE_LICENSE("GPL");
396 static struct tc_action_ops act_police_ops
= {
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
412 police_init_module(void)
414 return tcf_register_action(&act_police_ops
);
418 police_cleanup_module(void)
420 tcf_unregister_action(&act_police_ops
);
423 module_init(police_init_module
);
424 module_exit(police_cleanup_module
);
428 struct tcf_police
* tcf_police_locate(struct rtattr
*rta
, struct rtattr
*est
)
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)
438 if (tb
[TCA_POLICE_TBF
-1] == NULL
)
441 parm
= RTA_DATA(tb
[TCA_POLICE_TBF
-1]);
443 if (parm
->index
&& (p
= tcf_police_lookup(parm
->index
)) != NULL
) {
448 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
452 memset(p
, 0, sizeof(*p
));
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
)
459 if (parm
->peakrate
.rate
&&
460 (p
->P_tab
= qdisc_get_rtab(&parm
->peakrate
, tb
[TCA_POLICE_PEAKRATE
-1])) == NULL
)
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]);
469 p
->toks
= p
->burst
= parm
->burst
;
474 p
->mtu
= 255<<p
->R_tab
->rate
.cell_log
;
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
483 qdisc_new_estimator(&p
->stats
, p
->stats_lock
, est
);
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
);
494 qdisc_put_rtab(p
->R_tab
);
499 int tcf_police(struct sk_buff
*skb
, struct tcf_police
*p
)
507 p
->stats
.bytes
+= skb
->len
;
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
);
518 if (skb
->len
<= p
->mtu
) {
519 if (p
->R_tab
== NULL
) {
520 spin_unlock(&p
->lock
);
524 PSCHED_GET_TIME(now
);
526 toks
= PSCHED_TDIFF_SAFE(now
, p
->t_c
, p
->burst
);
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
);
535 if (toks
> (long)p
->burst
)
537 toks
-= L2T(p
, skb
->len
);
539 if ((toks
|ptoks
) >= 0) {
543 spin_unlock(&p
->lock
);
548 p
->stats
.overlimits
++;
549 spin_unlock(&p
->lock
);
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
;
561 opt
.burst
= p
->burst
;
563 opt
.rate
= p
->R_tab
->rate
;
565 memset(&opt
.rate
, 0, sizeof(opt
.rate
));
567 opt
.peakrate
= p
->P_tab
->rate
;
569 memset(&opt
.peakrate
, 0, sizeof(opt
.peakrate
));
570 RTA_PUT(skb
, TCA_POLICE_TBF
, sizeof(opt
), &opt
);
572 RTA_PUT(skb
, TCA_POLICE_RESULT
, sizeof(int), &p
->result
);
573 #ifdef CONFIG_NET_ESTIMATOR
575 RTA_PUT(skb
, TCA_POLICE_AVRATE
, 4, &p
->ewma_rate
);
580 skb_trim(skb
, b
- skb
->data
);
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
);