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 <net/pkt_cls.h>
24 #include <linux/tc_act/tc_gact.h>
25 #include <net/tc_act/tc_gact.h>
27 static unsigned int gact_net_id;
28 static struct tc_action_ops act_gact_ops;
30 #ifdef CONFIG_GACT_PROB
31 static int gact_net_rand(struct tcf_gact *gact)
33 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
34 if (prandom_u32() % gact->tcfg_pval)
35 return gact->tcf_action;
36 return gact->tcfg_paction;
39 static int gact_determ(struct tcf_gact *gact)
41 u32 pack = atomic_inc_return(&gact->packets);
43 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
44 if (pack % gact->tcfg_pval)
45 return gact->tcf_action;
46 return gact->tcfg_paction;
49 typedef int (*g_rand)(struct tcf_gact *gact);
50 static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
51 #endif /* CONFIG_GACT_PROB */
53 static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
54 [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) },
55 [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) },
58 static int tcf_gact_init(struct net *net, struct nlattr *nla,
59 struct nlattr *est, struct tc_action **a,
60 int ovr, int bind, bool rtnl_held,
61 struct tcf_proto *tp, struct netlink_ext_ack *extack)
63 struct tc_action_net *tn = net_generic(net, gact_net_id);
64 struct nlattr *tb[TCA_GACT_MAX + 1];
65 struct tcf_chain *goto_ch = NULL;
67 struct tcf_gact *gact;
70 #ifdef CONFIG_GACT_PROB
71 struct tc_gact_p *p_parm = NULL;
77 err = nla_parse_nested_deprecated(tb, TCA_GACT_MAX, nla, gact_policy,
82 if (tb[TCA_GACT_PARMS] == NULL)
84 parm = nla_data(tb[TCA_GACT_PARMS]);
86 #ifndef CONFIG_GACT_PROB
87 if (tb[TCA_GACT_PROB] != NULL)
90 if (tb[TCA_GACT_PROB]) {
91 p_parm = nla_data(tb[TCA_GACT_PROB]);
92 if (p_parm->ptype >= MAX_RAND)
94 if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) {
95 NL_SET_ERR_MSG(extack,
96 "goto chain not allowed on fallback");
102 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
104 ret = tcf_idr_create(tn, parm->index, est, a,
105 &act_gact_ops, bind, true);
107 tcf_idr_cleanup(tn, parm->index);
111 } else if (err > 0) {
112 if (bind)/* dont override defaults */
115 tcf_idr_release(*a, bind);
122 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
127 spin_lock_bh(&gact->tcf_lock);
128 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
129 #ifdef CONFIG_GACT_PROB
131 gact->tcfg_paction = p_parm->paction;
132 gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
133 /* Make sure tcfg_pval is written before tcfg_ptype
134 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
137 gact->tcfg_ptype = p_parm->ptype;
140 spin_unlock_bh(&gact->tcf_lock);
143 tcf_chain_put_by_act(goto_ch);
145 if (ret == ACT_P_CREATED)
146 tcf_idr_insert(tn, *a);
149 tcf_idr_release(*a, bind);
153 static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
154 struct tcf_result *res)
156 struct tcf_gact *gact = to_gact(a);
157 int action = READ_ONCE(gact->tcf_action);
159 #ifdef CONFIG_GACT_PROB
161 u32 ptype = READ_ONCE(gact->tcfg_ptype);
164 action = gact_rand[ptype](gact);
167 bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb);
168 if (action == TC_ACT_SHOT)
169 qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats));
171 tcf_lastuse_update(&gact->tcf_tm);
176 static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets,
177 u64 lastuse, bool hw)
179 struct tcf_gact *gact = to_gact(a);
180 int action = READ_ONCE(gact->tcf_action);
181 struct tcf_t *tm = &gact->tcf_tm;
183 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes,
185 if (action == TC_ACT_SHOT)
186 this_cpu_ptr(gact->common.cpu_qstats)->drops += packets;
189 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats_hw),
192 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
195 static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
198 unsigned char *b = skb_tail_pointer(skb);
199 struct tcf_gact *gact = to_gact(a);
200 struct tc_gact opt = {
201 .index = gact->tcf_index,
202 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
203 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
207 spin_lock_bh(&gact->tcf_lock);
208 opt.action = gact->tcf_action;
209 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
210 goto nla_put_failure;
211 #ifdef CONFIG_GACT_PROB
212 if (gact->tcfg_ptype) {
213 struct tc_gact_p p_opt = {
214 .paction = gact->tcfg_paction,
215 .pval = gact->tcfg_pval,
216 .ptype = gact->tcfg_ptype,
219 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
220 goto nla_put_failure;
223 tcf_tm_dump(&t, &gact->tcf_tm);
224 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
225 goto nla_put_failure;
226 spin_unlock_bh(&gact->tcf_lock);
231 spin_unlock_bh(&gact->tcf_lock);
236 static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
237 struct netlink_callback *cb, int type,
238 const struct tc_action_ops *ops,
239 struct netlink_ext_ack *extack)
241 struct tc_action_net *tn = net_generic(net, gact_net_id);
243 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
246 static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
248 struct tc_action_net *tn = net_generic(net, gact_net_id);
250 return tcf_idr_search(tn, a, index);
253 static size_t tcf_gact_get_fill_size(const struct tc_action *act)
255 size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
257 #ifdef CONFIG_GACT_PROB
258 if (to_gact(act)->tcfg_ptype)
260 sz += nla_total_size(sizeof(struct tc_gact_p));
266 static struct tc_action_ops act_gact_ops = {
269 .owner = THIS_MODULE,
271 .stats_update = tcf_gact_stats_update,
272 .dump = tcf_gact_dump,
273 .init = tcf_gact_init,
274 .walk = tcf_gact_walker,
275 .lookup = tcf_gact_search,
276 .get_fill_size = tcf_gact_get_fill_size,
277 .size = sizeof(struct tcf_gact),
280 static __net_init int gact_init_net(struct net *net)
282 struct tc_action_net *tn = net_generic(net, gact_net_id);
284 return tc_action_net_init(tn, &act_gact_ops);
287 static void __net_exit gact_exit_net(struct list_head *net_list)
289 tc_action_net_exit(net_list, gact_net_id);
292 static struct pernet_operations gact_net_ops = {
293 .init = gact_init_net,
294 .exit_batch = gact_exit_net,
296 .size = sizeof(struct tc_action_net),
299 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
300 MODULE_DESCRIPTION("Generic Classifier actions");
301 MODULE_LICENSE("GPL");
303 static int __init gact_init_module(void)
305 #ifdef CONFIG_GACT_PROB
306 pr_info("GACT probability on\n");
308 pr_info("GACT probability NOT on\n");
311 return tcf_register_action(&act_gact_ops, &gact_net_ops);
314 static void __exit gact_cleanup_module(void)
316 tcf_unregister_action(&act_gact_ops, &gact_net_ops);
319 module_init(gact_init_module);
320 module_exit(gact_cleanup_module);