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>
12 #include <asm/uaccess.h>
13 #include <asm/system.h>
14 #include <asm/bitops.h>
15 #include <linux/config.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
21 #include <linux/socket.h>
22 #include <linux/sockios.h>
24 #include <linux/errno.h>
25 #include <linux/interrupt.h>
26 #include <linux/netdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/init.h>
30 #include <linux/proc_fs.h>
32 #include <net/pkt_sched.h>
34 #define L2T(p,L) ((p)->R_tab->data[(L)>>(p)->R_tab->rate.cell_log])
35 #define L2T_P(p,L) ((p)->P_tab->data[(L)>>(p)->P_tab->rate.cell_log])
38 static struct tcf_police
*tcf_police_ht
[16];
39 /* Policer hash table lock */
40 static rwlock_t police_lock
= RW_LOCK_UNLOCKED
;
42 /* Each policer is serialized by its individual spinlock */
44 static __inline__
unsigned tcf_police_hash(u32 index
)
49 static __inline__
struct tcf_police
* tcf_police_lookup(u32 index
)
53 read_lock(&police_lock
);
54 for (p
= tcf_police_ht
[tcf_police_hash(index
)]; p
; p
= p
->next
) {
55 if (p
->index
== index
)
58 read_unlock(&police_lock
);
62 static __inline__ u32
tcf_police_new_index(void)
67 } while (tcf_police_lookup(idx_gen
));
73 void tcf_police_destroy(struct tcf_police
*p
)
75 unsigned h
= tcf_police_hash(p
->index
);
76 struct tcf_police
**p1p
;
78 for (p1p
= &tcf_police_ht
[h
]; *p1p
; p1p
= &(*p1p
)->next
) {
80 write_lock_bh(&police_lock
);
82 write_unlock_bh(&police_lock
);
83 #ifdef CONFIG_NET_ESTIMATOR
84 qdisc_kill_estimator(&p
->stats
);
87 qdisc_put_rtab(p
->R_tab
);
89 qdisc_put_rtab(p
->P_tab
);
97 struct tcf_police
* tcf_police_locate(struct rtattr
*rta
, struct rtattr
*est
)
100 struct tcf_police
*p
;
101 struct rtattr
*tb
[TCA_POLICE_MAX
];
102 struct tc_police
*parm
;
104 if (rtattr_parse(tb
, TCA_POLICE_MAX
, RTA_DATA(rta
), RTA_PAYLOAD(rta
)) < 0)
107 if (tb
[TCA_POLICE_TBF
-1] == NULL
)
110 parm
= RTA_DATA(tb
[TCA_POLICE_TBF
-1]);
112 if (parm
->index
&& (p
= tcf_police_lookup(parm
->index
)) != NULL
) {
117 p
= kmalloc(sizeof(*p
), GFP_KERNEL
);
121 memset(p
, 0, sizeof(*p
));
123 spin_lock_init(&p
->lock
);
124 p
->stats
.lock
= &p
->lock
;
125 if (parm
->rate
.rate
) {
126 if ((p
->R_tab
= qdisc_get_rtab(&parm
->rate
, tb
[TCA_POLICE_RATE
-1])) == NULL
)
128 if (parm
->peakrate
.rate
&&
129 (p
->P_tab
= qdisc_get_rtab(&parm
->peakrate
, tb
[TCA_POLICE_PEAKRATE
-1])) == NULL
)
132 if (tb
[TCA_POLICE_RESULT
-1])
133 p
->result
= *(int*)RTA_DATA(tb
[TCA_POLICE_RESULT
-1]);
134 #ifdef CONFIG_NET_ESTIMATOR
135 if (tb
[TCA_POLICE_AVRATE
-1])
136 p
->ewma_rate
= *(u32
*)RTA_DATA(tb
[TCA_POLICE_AVRATE
-1]);
138 p
->toks
= p
->burst
= parm
->burst
;
143 p
->mtu
= 255<<p
->R_tab
->rate
.cell_log
;
146 p
->ptoks
= L2T_P(p
, p
->mtu
);
147 PSCHED_GET_TIME(p
->t_c
);
148 p
->index
= parm
->index
? : tcf_police_new_index();
149 p
->action
= parm
->action
;
150 #ifdef CONFIG_NET_ESTIMATOR
152 qdisc_new_estimator(&p
->stats
, est
);
154 h
= tcf_police_hash(p
->index
);
155 write_lock_bh(&police_lock
);
156 p
->next
= tcf_police_ht
[h
];
157 tcf_police_ht
[h
] = p
;
158 write_unlock_bh(&police_lock
);
163 qdisc_put_rtab(p
->R_tab
);
168 int tcf_police(struct sk_buff
*skb
, struct tcf_police
*p
)
176 p
->stats
.bytes
+= skb
->len
;
179 #ifdef CONFIG_NET_ESTIMATOR
180 if (p
->ewma_rate
&& p
->stats
.bps
>= p
->ewma_rate
) {
181 p
->stats
.overlimits
++;
182 spin_unlock(&p
->lock
);
187 if (skb
->len
<= p
->mtu
) {
188 if (p
->R_tab
== NULL
) {
189 spin_unlock(&p
->lock
);
193 PSCHED_GET_TIME(now
);
195 toks
= PSCHED_TDIFF_SAFE(now
, p
->t_c
, p
->burst
, 0);
198 ptoks
= toks
+ p
->ptoks
;
199 if (ptoks
> (long)L2T_P(p
, p
->mtu
))
200 ptoks
= (long)L2T_P(p
, p
->mtu
);
201 ptoks
-= L2T_P(p
, skb
->len
);
204 if (toks
> (long)p
->burst
)
206 toks
-= L2T(p
, skb
->len
);
208 if ((toks
|ptoks
) >= 0) {
212 spin_unlock(&p
->lock
);
217 p
->stats
.overlimits
++;
218 spin_unlock(&p
->lock
);
222 #ifdef CONFIG_RTNETLINK
223 int tcf_police_dump(struct sk_buff
*skb
, struct tcf_police
*p
)
225 unsigned char *b
= skb
->tail
;
226 struct tc_police opt
;
228 opt
.index
= p
->index
;
229 opt
.action
= p
->action
;
231 opt
.burst
= p
->burst
;
233 opt
.rate
= p
->R_tab
->rate
;
235 memset(&opt
.rate
, 0, sizeof(opt
.rate
));
237 opt
.peakrate
= p
->P_tab
->rate
;
239 memset(&opt
.peakrate
, 0, sizeof(opt
.peakrate
));
240 RTA_PUT(skb
, TCA_POLICE_TBF
, sizeof(opt
), &opt
);
242 RTA_PUT(skb
, TCA_POLICE_RESULT
, sizeof(int), &p
->result
);
243 #ifdef CONFIG_NET_ESTIMATOR
245 RTA_PUT(skb
, TCA_POLICE_AVRATE
, 4, &p
->ewma_rate
);
250 skb_trim(skb
, b
- skb
->data
);