2 * net/sched/act_gact.c Generic actions
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 * copyright Jamal Hadi Salim (2002-4)
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/errno.h>
17 #include <linux/skbuff.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <net/netlink.h>
22 #include <net/pkt_sched.h>
23 #include <linux/tc_act/tc_gact.h>
24 #include <net/tc_act/tc_gact.h>
26 #define GACT_TAB_MASK 15
28 static unsigned int gact_net_id
;
29 static struct tc_action_ops act_gact_ops
;
31 #ifdef CONFIG_GACT_PROB
32 static int gact_net_rand(struct tcf_gact
*gact
)
34 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
35 if (prandom_u32() % gact
->tcfg_pval
)
36 return gact
->tcf_action
;
37 return gact
->tcfg_paction
;
40 static int gact_determ(struct tcf_gact
*gact
)
42 u32 pack
= atomic_inc_return(&gact
->packets
);
44 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
45 if (pack
% gact
->tcfg_pval
)
46 return gact
->tcf_action
;
47 return gact
->tcfg_paction
;
50 typedef int (*g_rand
)(struct tcf_gact
*gact
);
51 static g_rand gact_rand
[MAX_RAND
] = { NULL
, gact_net_rand
, gact_determ
};
52 #endif /* CONFIG_GACT_PROB */
54 static const struct nla_policy gact_policy
[TCA_GACT_MAX
+ 1] = {
55 [TCA_GACT_PARMS
] = { .len
= sizeof(struct tc_gact
) },
56 [TCA_GACT_PROB
] = { .len
= sizeof(struct tc_gact_p
) },
59 static int tcf_gact_init(struct net
*net
, struct nlattr
*nla
,
60 struct nlattr
*est
, struct tc_action
**a
,
63 struct tc_action_net
*tn
= net_generic(net
, gact_net_id
);
64 struct nlattr
*tb
[TCA_GACT_MAX
+ 1];
66 struct tcf_gact
*gact
;
69 #ifdef CONFIG_GACT_PROB
70 struct tc_gact_p
*p_parm
= NULL
;
76 err
= nla_parse_nested(tb
, TCA_GACT_MAX
, nla
, gact_policy
);
80 if (tb
[TCA_GACT_PARMS
] == NULL
)
82 parm
= nla_data(tb
[TCA_GACT_PARMS
]);
84 #ifndef CONFIG_GACT_PROB
85 if (tb
[TCA_GACT_PROB
] != NULL
)
88 if (tb
[TCA_GACT_PROB
]) {
89 p_parm
= nla_data(tb
[TCA_GACT_PROB
]);
90 if (p_parm
->ptype
>= MAX_RAND
)
95 if (!tcf_hash_check(tn
, parm
->index
, a
, bind
)) {
96 ret
= tcf_hash_create(tn
, parm
->index
, est
, a
,
97 &act_gact_ops
, bind
, true);
102 if (bind
)/* dont override defaults */
104 tcf_hash_release(*a
, bind
);
112 gact
->tcf_action
= parm
->action
;
113 #ifdef CONFIG_GACT_PROB
115 gact
->tcfg_paction
= p_parm
->paction
;
116 gact
->tcfg_pval
= max_t(u16
, 1, p_parm
->pval
);
117 /* Make sure tcfg_pval is written before tcfg_ptype
118 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
121 gact
->tcfg_ptype
= p_parm
->ptype
;
124 if (ret
== ACT_P_CREATED
)
125 tcf_hash_insert(tn
, *a
);
129 static int tcf_gact(struct sk_buff
*skb
, const struct tc_action
*a
,
130 struct tcf_result
*res
)
132 struct tcf_gact
*gact
= to_gact(a
);
133 int action
= READ_ONCE(gact
->tcf_action
);
135 #ifdef CONFIG_GACT_PROB
137 u32 ptype
= READ_ONCE(gact
->tcfg_ptype
);
140 action
= gact_rand
[ptype
](gact
);
143 bstats_cpu_update(this_cpu_ptr(gact
->common
.cpu_bstats
), skb
);
144 if (action
== TC_ACT_SHOT
)
145 qstats_drop_inc(this_cpu_ptr(gact
->common
.cpu_qstats
));
147 tcf_lastuse_update(&gact
->tcf_tm
);
152 static void tcf_gact_stats_update(struct tc_action
*a
, u64 bytes
, u32 packets
,
155 struct tcf_gact
*gact
= to_gact(a
);
156 int action
= READ_ONCE(gact
->tcf_action
);
157 struct tcf_t
*tm
= &gact
->tcf_tm
;
159 _bstats_cpu_update(this_cpu_ptr(gact
->common
.cpu_bstats
), bytes
,
161 if (action
== TC_ACT_SHOT
)
162 this_cpu_ptr(gact
->common
.cpu_qstats
)->drops
+= packets
;
164 tm
->lastuse
= lastuse
;
167 static int tcf_gact_dump(struct sk_buff
*skb
, struct tc_action
*a
,
170 unsigned char *b
= skb_tail_pointer(skb
);
171 struct tcf_gact
*gact
= to_gact(a
);
172 struct tc_gact opt
= {
173 .index
= gact
->tcf_index
,
174 .refcnt
= gact
->tcf_refcnt
- ref
,
175 .bindcnt
= gact
->tcf_bindcnt
- bind
,
176 .action
= gact
->tcf_action
,
180 if (nla_put(skb
, TCA_GACT_PARMS
, sizeof(opt
), &opt
))
181 goto nla_put_failure
;
182 #ifdef CONFIG_GACT_PROB
183 if (gact
->tcfg_ptype
) {
184 struct tc_gact_p p_opt
= {
185 .paction
= gact
->tcfg_paction
,
186 .pval
= gact
->tcfg_pval
,
187 .ptype
= gact
->tcfg_ptype
,
190 if (nla_put(skb
, TCA_GACT_PROB
, sizeof(p_opt
), &p_opt
))
191 goto nla_put_failure
;
194 tcf_tm_dump(&t
, &gact
->tcf_tm
);
195 if (nla_put_64bit(skb
, TCA_GACT_TM
, sizeof(t
), &t
, TCA_GACT_PAD
))
196 goto nla_put_failure
;
204 static int tcf_gact_walker(struct net
*net
, struct sk_buff
*skb
,
205 struct netlink_callback
*cb
, int type
,
206 const struct tc_action_ops
*ops
)
208 struct tc_action_net
*tn
= net_generic(net
, gact_net_id
);
210 return tcf_generic_walker(tn
, skb
, cb
, type
, ops
);
213 static int tcf_gact_search(struct net
*net
, struct tc_action
**a
, u32 index
)
215 struct tc_action_net
*tn
= net_generic(net
, gact_net_id
);
217 return tcf_hash_search(tn
, a
, index
);
220 static struct tc_action_ops act_gact_ops
= {
222 .type
= TCA_ACT_GACT
,
223 .owner
= THIS_MODULE
,
225 .stats_update
= tcf_gact_stats_update
,
226 .dump
= tcf_gact_dump
,
227 .init
= tcf_gact_init
,
228 .walk
= tcf_gact_walker
,
229 .lookup
= tcf_gact_search
,
230 .size
= sizeof(struct tcf_gact
),
233 static __net_init
int gact_init_net(struct net
*net
)
235 struct tc_action_net
*tn
= net_generic(net
, gact_net_id
);
237 return tc_action_net_init(tn
, &act_gact_ops
, GACT_TAB_MASK
);
240 static void __net_exit
gact_exit_net(struct net
*net
)
242 struct tc_action_net
*tn
= net_generic(net
, gact_net_id
);
244 tc_action_net_exit(tn
);
247 static struct pernet_operations gact_net_ops
= {
248 .init
= gact_init_net
,
249 .exit
= gact_exit_net
,
251 .size
= sizeof(struct tc_action_net
),
254 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
255 MODULE_DESCRIPTION("Generic Classifier actions");
256 MODULE_LICENSE("GPL");
258 static int __init
gact_init_module(void)
260 #ifdef CONFIG_GACT_PROB
261 pr_info("GACT probability on\n");
263 pr_info("GACT probability NOT on\n");
266 return tcf_register_action(&act_gact_ops
, &gact_net_ops
);
269 static void __exit
gact_cleanup_module(void)
271 tcf_unregister_action(&act_gact_ops
, &gact_net_ops
);
274 module_init(gact_init_module
);
275 module_exit(gact_cleanup_module
);