xref: /openbmc/linux/net/sched/act_gact.c (revision 4da722ca)
1 /*
2  * net/sched/act_gact.c		Generic actions
3  *
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.
8  *
9  * copyright 	Jamal Hadi Salim (2002-4)
10  *
11  */
12 
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>
25 
26 #define GACT_TAB_MASK	15
27 
28 static unsigned int gact_net_id;
29 static struct tc_action_ops act_gact_ops;
30 
31 #ifdef CONFIG_GACT_PROB
32 static int gact_net_rand(struct tcf_gact *gact)
33 {
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;
38 }
39 
40 static int gact_determ(struct tcf_gact *gact)
41 {
42 	u32 pack = atomic_inc_return(&gact->packets);
43 
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;
48 }
49 
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 */
53 
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) },
57 };
58 
59 static int tcf_gact_init(struct net *net, struct nlattr *nla,
60 			 struct nlattr *est, struct tc_action **a,
61 			 int ovr, int bind)
62 {
63 	struct tc_action_net *tn = net_generic(net, gact_net_id);
64 	struct nlattr *tb[TCA_GACT_MAX + 1];
65 	struct tc_gact *parm;
66 	struct tcf_gact *gact;
67 	int ret = 0;
68 	int err;
69 #ifdef CONFIG_GACT_PROB
70 	struct tc_gact_p *p_parm = NULL;
71 #endif
72 
73 	if (nla == NULL)
74 		return -EINVAL;
75 
76 	err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy, NULL);
77 	if (err < 0)
78 		return err;
79 
80 	if (tb[TCA_GACT_PARMS] == NULL)
81 		return -EINVAL;
82 	parm = nla_data(tb[TCA_GACT_PARMS]);
83 
84 #ifndef CONFIG_GACT_PROB
85 	if (tb[TCA_GACT_PROB] != NULL)
86 		return -EOPNOTSUPP;
87 #else
88 	if (tb[TCA_GACT_PROB]) {
89 		p_parm = nla_data(tb[TCA_GACT_PROB]);
90 		if (p_parm->ptype >= MAX_RAND)
91 			return -EINVAL;
92 	}
93 #endif
94 
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);
98 		if (ret)
99 			return ret;
100 		ret = ACT_P_CREATED;
101 	} else {
102 		if (bind)/* dont override defaults */
103 			return 0;
104 		tcf_hash_release(*a, bind);
105 		if (!ovr)
106 			return -EEXIST;
107 	}
108 
109 	gact = to_gact(*a);
110 
111 	ASSERT_RTNL();
112 	gact->tcf_action = parm->action;
113 #ifdef CONFIG_GACT_PROB
114 	if (p_parm) {
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()
119 		 */
120 		smp_wmb();
121 		gact->tcfg_ptype   = p_parm->ptype;
122 	}
123 #endif
124 	if (ret == ACT_P_CREATED)
125 		tcf_hash_insert(tn, *a);
126 	return ret;
127 }
128 
129 static int tcf_gact(struct sk_buff *skb, const struct tc_action *a,
130 		    struct tcf_result *res)
131 {
132 	struct tcf_gact *gact = to_gact(a);
133 	int action = READ_ONCE(gact->tcf_action);
134 
135 #ifdef CONFIG_GACT_PROB
136 	{
137 	u32 ptype = READ_ONCE(gact->tcfg_ptype);
138 
139 	if (ptype)
140 		action = gact_rand[ptype](gact);
141 	}
142 #endif
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));
146 
147 	tcf_lastuse_update(&gact->tcf_tm);
148 
149 	return action;
150 }
151 
152 static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets,
153 				  u64 lastuse)
154 {
155 	struct tcf_gact *gact = to_gact(a);
156 	int action = READ_ONCE(gact->tcf_action);
157 	struct tcf_t *tm = &gact->tcf_tm;
158 
159 	_bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes,
160 			   packets);
161 	if (action == TC_ACT_SHOT)
162 		this_cpu_ptr(gact->common.cpu_qstats)->drops += packets;
163 
164 	tm->lastuse = lastuse;
165 }
166 
167 static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
168 			 int bind, int ref)
169 {
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,
177 	};
178 	struct tcf_t t;
179 
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,
188 		};
189 
190 		if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
191 			goto nla_put_failure;
192 	}
193 #endif
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;
197 	return skb->len;
198 
199 nla_put_failure:
200 	nlmsg_trim(skb, b);
201 	return -1;
202 }
203 
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)
207 {
208 	struct tc_action_net *tn = net_generic(net, gact_net_id);
209 
210 	return tcf_generic_walker(tn, skb, cb, type, ops);
211 }
212 
213 static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
214 {
215 	struct tc_action_net *tn = net_generic(net, gact_net_id);
216 
217 	return tcf_hash_search(tn, a, index);
218 }
219 
220 static struct tc_action_ops act_gact_ops = {
221 	.kind		=	"gact",
222 	.type		=	TCA_ACT_GACT,
223 	.owner		=	THIS_MODULE,
224 	.act		=	tcf_gact,
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),
231 };
232 
233 static __net_init int gact_init_net(struct net *net)
234 {
235 	struct tc_action_net *tn = net_generic(net, gact_net_id);
236 
237 	return tc_action_net_init(tn, &act_gact_ops, GACT_TAB_MASK);
238 }
239 
240 static void __net_exit gact_exit_net(struct net *net)
241 {
242 	struct tc_action_net *tn = net_generic(net, gact_net_id);
243 
244 	tc_action_net_exit(tn);
245 }
246 
247 static struct pernet_operations gact_net_ops = {
248 	.init = gact_init_net,
249 	.exit = gact_exit_net,
250 	.id   = &gact_net_id,
251 	.size = sizeof(struct tc_action_net),
252 };
253 
254 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
255 MODULE_DESCRIPTION("Generic Classifier actions");
256 MODULE_LICENSE("GPL");
257 
258 static int __init gact_init_module(void)
259 {
260 #ifdef CONFIG_GACT_PROB
261 	pr_info("GACT probability on\n");
262 #else
263 	pr_info("GACT probability NOT on\n");
264 #endif
265 
266 	return tcf_register_action(&act_gact_ops, &gact_net_ops);
267 }
268 
269 static void __exit gact_cleanup_module(void)
270 {
271 	tcf_unregister_action(&act_gact_ops, &gact_net_ops);
272 }
273 
274 module_init(gact_init_module);
275 module_exit(gact_cleanup_module);
276