13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
19 #include <linux/rtnetlink.h>
21 #include <linux/slab.h>
25 #define L2T(p, L) qdisc_l2t((p)->tcfp_R_tab, L)
26 #define L2T_P(p, L) qdisc_l2t((p)->tcfp_P_tab, L)
28 #define POL_TAB_MASK 15
30 static u32 police_idx_gen;
34 .htab = tcf_police_ht,
53 int type,
struct tc_action *
a)
56 int err = 0,
index = -1,
i = 0, s_i = 0, n_i = 0;
72 nest = nla_nest_start(skb, a->order);
81 nla_nest_cancel(skb, nest);
84 nla_nest_end(skb, nest);
95 nla_nest_cancel(skb, nest);
99 static void tcf_police_destroy(
struct tcf_police *p)
104 for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->
tcfc_next) {
133 static int tcf_act_police_locate(
struct nlattr *
nla,
struct nlattr *est,
134 struct tc_action *a,
int ovr,
int bind)
166 police->tcf_bindcnt += 1;
167 police->tcf_refcnt += 1;
175 police = kzalloc(
sizeof(*police),
GFP_KERNEL);
179 police->tcf_refcnt = 1;
182 police->tcf_bindcnt = 1;
184 if (parm->
rate.rate) {
198 spin_lock_bh(&police->tcf_lock);
201 &police->tcf_rate_est,
202 &police->tcf_lock, est);
206 (ret == ACT_P_CREATED ||
208 &police->tcf_rate_est))) {
224 police->
tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
234 police->tcf_action = parm->
action;
239 spin_unlock_bh(&police->tcf_lock);
240 if (ret != ACT_P_CREATED)
243 police->
tcfp_t_c = psched_get_time();
244 police->tcf_index = parm->
index ? parm->
index :
248 police->tcf_next = tcf_police_ht[
h];
249 tcf_police_ht[
h] = &police->
common;
256 spin_unlock_bh(&police->tcf_lock);
262 if (ret == ACT_P_CREATED)
267 static int tcf_act_police_cleanup(
struct tc_action *a,
int bind)
277 if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) {
278 tcf_police_destroy(p);
285 static int tcf_act_police(
struct sk_buff *skb,
const struct tc_action *a,
293 spin_lock(&police->tcf_lock);
295 bstats_update(&police->tcf_bstats, skb);
299 police->tcf_qstats.overlimits++;
301 police->tcf_qstats.drops++;
302 spin_unlock(&police->tcf_lock);
303 return police->tcf_action;
306 if (qdisc_pkt_len(skb) <= police->
tcfp_mtu) {
308 spin_unlock(&police->tcf_lock);
312 now = psched_get_time();
313 toks = psched_tdiff_bounded(now, police->
tcfp_t_c,
319 ptoks -=
L2T_P(police, qdisc_pkt_len(skb));
324 toks -=
L2T(police, qdisc_pkt_len(skb));
325 if ((toks|ptoks) >= 0) {
329 spin_unlock(&police->tcf_lock);
334 police->tcf_qstats.overlimits++;
336 police->tcf_qstats.drops++;
337 spin_unlock(&police->tcf_lock);
338 return police->tcf_action;
342 tcf_act_police_dump(
struct sk_buff *skb,
struct tc_action *a,
int bind,
int ref)
344 unsigned char *
b = skb_tail_pointer(skb);
347 .index = police->tcf_index,
348 .action = police->tcf_action,
351 .refcnt = police->tcf_refcnt - ref,
352 .bindcnt = police->tcf_bindcnt - bind,
360 goto nla_put_failure;
362 nla_put_u32(skb, TCA_POLICE_RESULT, police->
tcfp_result))
363 goto nla_put_failure;
366 goto nla_put_failure;
378 static struct tc_action_ops act_police_ops = {
380 .hinfo = &police_hash_info,
382 .capab = TCA_CAP_NONE,
384 .act = tcf_act_police,
385 .dump = tcf_act_police_dump,
386 .cleanup = tcf_act_police_cleanup,
388 .init = tcf_act_police_locate,
389 .walk = tcf_act_police_walker
393 police_init_module(
void)
399 police_cleanup_module(
void)