xref: /openbmc/linux/net/sched/act_gact.c (revision bc5aa3a0)
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 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);
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, packets);
160 	if (action == TC_ACT_SHOT)
161 		this_cpu_ptr(gact->common.cpu_qstats)->drops += packets;
162 
163 	tm->lastuse = lastuse;
164 }
165 
166 static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
167 			 int bind, int ref)
168 {
169 	unsigned char *b = skb_tail_pointer(skb);
170 	struct tcf_gact *gact = to_gact(a);
171 	struct tc_gact opt = {
172 		.index   = gact->tcf_index,
173 		.refcnt  = gact->tcf_refcnt - ref,
174 		.bindcnt = gact->tcf_bindcnt - bind,
175 		.action  = gact->tcf_action,
176 	};
177 	struct tcf_t t;
178 
179 	if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
180 		goto nla_put_failure;
181 #ifdef CONFIG_GACT_PROB
182 	if (gact->tcfg_ptype) {
183 		struct tc_gact_p p_opt = {
184 			.paction = gact->tcfg_paction,
185 			.pval    = gact->tcfg_pval,
186 			.ptype   = gact->tcfg_ptype,
187 		};
188 
189 		if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
190 			goto nla_put_failure;
191 	}
192 #endif
193 	tcf_tm_dump(&t, &gact->tcf_tm);
194 	if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
195 		goto nla_put_failure;
196 	return skb->len;
197 
198 nla_put_failure:
199 	nlmsg_trim(skb, b);
200 	return -1;
201 }
202 
203 static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
204 			   struct netlink_callback *cb, int type,
205 			   const struct tc_action_ops *ops)
206 {
207 	struct tc_action_net *tn = net_generic(net, gact_net_id);
208 
209 	return tcf_generic_walker(tn, skb, cb, type, ops);
210 }
211 
212 static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
213 {
214 	struct tc_action_net *tn = net_generic(net, gact_net_id);
215 
216 	return tcf_hash_search(tn, a, index);
217 }
218 
219 static struct tc_action_ops act_gact_ops = {
220 	.kind		=	"gact",
221 	.type		=	TCA_ACT_GACT,
222 	.owner		=	THIS_MODULE,
223 	.act		=	tcf_gact,
224 	.stats_update	=	tcf_gact_stats_update,
225 	.dump		=	tcf_gact_dump,
226 	.init		=	tcf_gact_init,
227 	.walk		=	tcf_gact_walker,
228 	.lookup		=	tcf_gact_search,
229 	.size		=	sizeof(struct tcf_gact),
230 };
231 
232 static __net_init int gact_init_net(struct net *net)
233 {
234 	struct tc_action_net *tn = net_generic(net, gact_net_id);
235 
236 	return tc_action_net_init(tn, &act_gact_ops, GACT_TAB_MASK);
237 }
238 
239 static void __net_exit gact_exit_net(struct net *net)
240 {
241 	struct tc_action_net *tn = net_generic(net, gact_net_id);
242 
243 	tc_action_net_exit(tn);
244 }
245 
246 static struct pernet_operations gact_net_ops = {
247 	.init = gact_init_net,
248 	.exit = gact_exit_net,
249 	.id   = &gact_net_id,
250 	.size = sizeof(struct tc_action_net),
251 };
252 
253 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
254 MODULE_DESCRIPTION("Generic Classifier actions");
255 MODULE_LICENSE("GPL");
256 
257 static int __init gact_init_module(void)
258 {
259 #ifdef CONFIG_GACT_PROB
260 	pr_info("GACT probability on\n");
261 #else
262 	pr_info("GACT probability NOT on\n");
263 #endif
264 
265 	return tcf_register_action(&act_gact_ops, &gact_net_ops);
266 }
267 
268 static void __exit gact_cleanup_module(void)
269 {
270 	tcf_unregister_action(&act_gact_ops, &gact_net_ops);
271 }
272 
273 module_init(gact_init_module);
274 module_exit(gact_cleanup_module);
275