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(tb, TCA_GACT_MAX, nla, gact_policy, NULL);
81 if (tb[TCA_GACT_PARMS] == NULL)
83 parm = nla_data(tb[TCA_GACT_PARMS]);
85 #ifndef CONFIG_GACT_PROB
86 if (tb[TCA_GACT_PROB] != NULL)
89 if (tb[TCA_GACT_PROB]) {
90 p_parm = nla_data(tb[TCA_GACT_PROB]);
91 if (p_parm->ptype >= MAX_RAND)
93 if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) {
94 NL_SET_ERR_MSG(extack,
95 "goto chain not allowed on fallback");
101 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
103 ret = tcf_idr_create(tn, parm->index, est, a,
104 &act_gact_ops, bind, true);
106 tcf_idr_cleanup(tn, parm->index);
110 } else if (err > 0) {
111 if (bind)/* dont override defaults */
114 tcf_idr_release(*a, bind);
121 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
126 spin_lock_bh(&gact->tcf_lock);
127 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
128 #ifdef CONFIG_GACT_PROB
130 gact->tcfg_paction = p_parm->paction;
131 gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
132 /* Make sure tcfg_pval is written before tcfg_ptype
133 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
136 gact->tcfg_ptype = p_parm->ptype;
139 spin_unlock_bh(&gact->tcf_lock);
142 tcf_chain_put_by_act(goto_ch);
144 if (ret == ACT_P_CREATED)
145 tcf_idr_insert(tn, *a);
148 tcf_idr_release(*a, bind);
152 static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
153 struct tcf_result *res)
155 struct tcf_gact *gact = to_gact(a);
156 int action = READ_ONCE(gact->tcf_action);
158 #ifdef CONFIG_GACT_PROB
160 u32 ptype = READ_ONCE(gact->tcfg_ptype);
163 action = gact_rand[ptype](gact);
166 bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb);
167 if (action == TC_ACT_SHOT)
168 qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats));
170 tcf_lastuse_update(&gact->tcf_tm);
175 static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets,
176 u64 lastuse, bool hw)
178 struct tcf_gact *gact = to_gact(a);
179 int action = READ_ONCE(gact->tcf_action);
180 struct tcf_t *tm = &gact->tcf_tm;
182 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes,
184 if (action == TC_ACT_SHOT)
185 this_cpu_ptr(gact->common.cpu_qstats)->drops += packets;
188 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats_hw),
191 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
194 static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
197 unsigned char *b = skb_tail_pointer(skb);
198 struct tcf_gact *gact = to_gact(a);
199 struct tc_gact opt = {
200 .index = gact->tcf_index,
201 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
202 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
206 spin_lock_bh(&gact->tcf_lock);
207 opt.action = gact->tcf_action;
208 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
209 goto nla_put_failure;
210 #ifdef CONFIG_GACT_PROB
211 if (gact->tcfg_ptype) {
212 struct tc_gact_p p_opt = {
213 .paction = gact->tcfg_paction,
214 .pval = gact->tcfg_pval,
215 .ptype = gact->tcfg_ptype,
218 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
219 goto nla_put_failure;
222 tcf_tm_dump(&t, &gact->tcf_tm);
223 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
224 goto nla_put_failure;
225 spin_unlock_bh(&gact->tcf_lock);
230 spin_unlock_bh(&gact->tcf_lock);
235 static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
236 struct netlink_callback *cb, int type,
237 const struct tc_action_ops *ops,
238 struct netlink_ext_ack *extack)
240 struct tc_action_net *tn = net_generic(net, gact_net_id);
242 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
245 static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
247 struct tc_action_net *tn = net_generic(net, gact_net_id);
249 return tcf_idr_search(tn, a, index);
252 static size_t tcf_gact_get_fill_size(const struct tc_action *act)
254 size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
256 #ifdef CONFIG_GACT_PROB
257 if (to_gact(act)->tcfg_ptype)
259 sz += nla_total_size(sizeof(struct tc_gact_p));
265 static struct tc_action_ops act_gact_ops = {
268 .owner = THIS_MODULE,
270 .stats_update = tcf_gact_stats_update,
271 .dump = tcf_gact_dump,
272 .init = tcf_gact_init,
273 .walk = tcf_gact_walker,
274 .lookup = tcf_gact_search,
275 .get_fill_size = tcf_gact_get_fill_size,
276 .size = sizeof(struct tcf_gact),
279 static __net_init int gact_init_net(struct net *net)
281 struct tc_action_net *tn = net_generic(net, gact_net_id);
283 return tc_action_net_init(tn, &act_gact_ops);
286 static void __net_exit gact_exit_net(struct list_head *net_list)
288 tc_action_net_exit(net_list, gact_net_id);
291 static struct pernet_operations gact_net_ops = {
292 .init = gact_init_net,
293 .exit_batch = gact_exit_net,
295 .size = sizeof(struct tc_action_net),
298 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
299 MODULE_DESCRIPTION("Generic Classifier actions");
300 MODULE_LICENSE("GPL");
302 static int __init gact_init_module(void)
304 #ifdef CONFIG_GACT_PROB
305 pr_info("GACT probability on\n");
307 pr_info("GACT probability NOT on\n");
310 return tcf_register_action(&act_gact_ops, &gact_net_ops);
313 static void __exit gact_cleanup_module(void)
315 tcf_unregister_action(&act_gact_ops, &gact_net_ops);
318 module_init(gact_init_module);
319 module_exit(gact_cleanup_module);