xref: /linux/net/sched/act_gact.c (revision 987b741c52c7c6c68d46fbaeb95b8d1087f10b7f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/act_gact.c		Generic actions
4  *
5  * copyright 	Jamal Hadi Salim (2002-4)
6  */
7 
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/skbuff.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <net/netlink.h>
17 #include <net/pkt_sched.h>
18 #include <net/pkt_cls.h>
19 #include <linux/tc_act/tc_gact.h>
20 #include <net/tc_act/tc_gact.h>
21 
22 static unsigned int gact_net_id;
23 static struct tc_action_ops act_gact_ops;
24 
25 #ifdef CONFIG_GACT_PROB
26 static int gact_net_rand(struct tcf_gact *gact)
27 {
28 	smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
29 	if (prandom_u32() % gact->tcfg_pval)
30 		return gact->tcf_action;
31 	return gact->tcfg_paction;
32 }
33 
34 static int gact_determ(struct tcf_gact *gact)
35 {
36 	u32 pack = atomic_inc_return(&gact->packets);
37 
38 	smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
39 	if (pack % gact->tcfg_pval)
40 		return gact->tcf_action;
41 	return gact->tcfg_paction;
42 }
43 
44 typedef int (*g_rand)(struct tcf_gact *gact);
45 static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
46 #endif /* CONFIG_GACT_PROB */
47 
48 static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
49 	[TCA_GACT_PARMS]	= { .len = sizeof(struct tc_gact) },
50 	[TCA_GACT_PROB]		= { .len = sizeof(struct tc_gact_p) },
51 };
52 
53 static int tcf_gact_init(struct net *net, struct nlattr *nla,
54 			 struct nlattr *est, struct tc_action **a,
55 			 int ovr, int bind, bool rtnl_held,
56 			 struct tcf_proto *tp, u32 flags,
57 			 struct netlink_ext_ack *extack)
58 {
59 	struct tc_action_net *tn = net_generic(net, gact_net_id);
60 	struct nlattr *tb[TCA_GACT_MAX + 1];
61 	struct tcf_chain *goto_ch = NULL;
62 	struct tc_gact *parm;
63 	struct tcf_gact *gact;
64 	int ret = 0;
65 	u32 index;
66 	int err;
67 #ifdef CONFIG_GACT_PROB
68 	struct tc_gact_p *p_parm = NULL;
69 #endif
70 
71 	if (nla == NULL)
72 		return -EINVAL;
73 
74 	err = nla_parse_nested_deprecated(tb, TCA_GACT_MAX, nla, gact_policy,
75 					  NULL);
76 	if (err < 0)
77 		return err;
78 
79 	if (tb[TCA_GACT_PARMS] == NULL)
80 		return -EINVAL;
81 	parm = nla_data(tb[TCA_GACT_PARMS]);
82 	index = parm->index;
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 		if (TC_ACT_EXT_CMP(p_parm->paction, TC_ACT_GOTO_CHAIN)) {
93 			NL_SET_ERR_MSG(extack,
94 				       "goto chain not allowed on fallback");
95 			return -EINVAL;
96 		}
97 	}
98 #endif
99 
100 	err = tcf_idr_check_alloc(tn, &index, a, bind);
101 	if (!err) {
102 		ret = tcf_idr_create_from_flags(tn, index, est, a,
103 						&act_gact_ops, bind, flags);
104 		if (ret) {
105 			tcf_idr_cleanup(tn, index);
106 			return ret;
107 		}
108 		ret = ACT_P_CREATED;
109 	} else if (err > 0) {
110 		if (bind)/* dont override defaults */
111 			return 0;
112 		if (!ovr) {
113 			tcf_idr_release(*a, bind);
114 			return -EEXIST;
115 		}
116 	} else {
117 		return err;
118 	}
119 
120 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
121 	if (err < 0)
122 		goto release_idr;
123 	gact = to_gact(*a);
124 
125 	spin_lock_bh(&gact->tcf_lock);
126 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
127 #ifdef CONFIG_GACT_PROB
128 	if (p_parm) {
129 		gact->tcfg_paction = p_parm->paction;
130 		gact->tcfg_pval    = max_t(u16, 1, p_parm->pval);
131 		/* Make sure tcfg_pval is written before tcfg_ptype
132 		 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
133 		 */
134 		smp_wmb();
135 		gact->tcfg_ptype   = p_parm->ptype;
136 	}
137 #endif
138 	spin_unlock_bh(&gact->tcf_lock);
139 
140 	if (goto_ch)
141 		tcf_chain_put_by_act(goto_ch);
142 
143 	return ret;
144 release_idr:
145 	tcf_idr_release(*a, bind);
146 	return err;
147 }
148 
149 static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
150 			struct tcf_result *res)
151 {
152 	struct tcf_gact *gact = to_gact(a);
153 	int action = READ_ONCE(gact->tcf_action);
154 
155 #ifdef CONFIG_GACT_PROB
156 	{
157 	u32 ptype = READ_ONCE(gact->tcfg_ptype);
158 
159 	if (ptype)
160 		action = gact_rand[ptype](gact);
161 	}
162 #endif
163 	tcf_action_update_bstats(&gact->common, skb);
164 	if (action == TC_ACT_SHOT)
165 		tcf_action_inc_drop_qstats(&gact->common);
166 
167 	tcf_lastuse_update(&gact->tcf_tm);
168 
169 	return action;
170 }
171 
172 static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u64 packets,
173 				  u64 drops, u64 lastuse, bool hw)
174 {
175 	struct tcf_gact *gact = to_gact(a);
176 	int action = READ_ONCE(gact->tcf_action);
177 	struct tcf_t *tm = &gact->tcf_tm;
178 
179 	tcf_action_update_stats(a, bytes, packets,
180 				action == TC_ACT_SHOT ? packets : drops, hw);
181 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
182 }
183 
184 static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
185 			 int bind, int ref)
186 {
187 	unsigned char *b = skb_tail_pointer(skb);
188 	struct tcf_gact *gact = to_gact(a);
189 	struct tc_gact opt = {
190 		.index   = gact->tcf_index,
191 		.refcnt  = refcount_read(&gact->tcf_refcnt) - ref,
192 		.bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
193 	};
194 	struct tcf_t t;
195 
196 	spin_lock_bh(&gact->tcf_lock);
197 	opt.action = gact->tcf_action;
198 	if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
199 		goto nla_put_failure;
200 #ifdef CONFIG_GACT_PROB
201 	if (gact->tcfg_ptype) {
202 		struct tc_gact_p p_opt = {
203 			.paction = gact->tcfg_paction,
204 			.pval    = gact->tcfg_pval,
205 			.ptype   = gact->tcfg_ptype,
206 		};
207 
208 		if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
209 			goto nla_put_failure;
210 	}
211 #endif
212 	tcf_tm_dump(&t, &gact->tcf_tm);
213 	if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
214 		goto nla_put_failure;
215 	spin_unlock_bh(&gact->tcf_lock);
216 
217 	return skb->len;
218 
219 nla_put_failure:
220 	spin_unlock_bh(&gact->tcf_lock);
221 	nlmsg_trim(skb, b);
222 	return -1;
223 }
224 
225 static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
226 			   struct netlink_callback *cb, int type,
227 			   const struct tc_action_ops *ops,
228 			   struct netlink_ext_ack *extack)
229 {
230 	struct tc_action_net *tn = net_generic(net, gact_net_id);
231 
232 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
233 }
234 
235 static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
236 {
237 	struct tc_action_net *tn = net_generic(net, gact_net_id);
238 
239 	return tcf_idr_search(tn, a, index);
240 }
241 
242 static size_t tcf_gact_get_fill_size(const struct tc_action *act)
243 {
244 	size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
245 
246 #ifdef CONFIG_GACT_PROB
247 	if (to_gact(act)->tcfg_ptype)
248 		/* TCA_GACT_PROB */
249 		sz += nla_total_size(sizeof(struct tc_gact_p));
250 #endif
251 
252 	return sz;
253 }
254 
255 static struct tc_action_ops act_gact_ops = {
256 	.kind		=	"gact",
257 	.id		=	TCA_ID_GACT,
258 	.owner		=	THIS_MODULE,
259 	.act		=	tcf_gact_act,
260 	.stats_update	=	tcf_gact_stats_update,
261 	.dump		=	tcf_gact_dump,
262 	.init		=	tcf_gact_init,
263 	.walk		=	tcf_gact_walker,
264 	.lookup		=	tcf_gact_search,
265 	.get_fill_size	=	tcf_gact_get_fill_size,
266 	.size		=	sizeof(struct tcf_gact),
267 };
268 
269 static __net_init int gact_init_net(struct net *net)
270 {
271 	struct tc_action_net *tn = net_generic(net, gact_net_id);
272 
273 	return tc_action_net_init(net, tn, &act_gact_ops);
274 }
275 
276 static void __net_exit gact_exit_net(struct list_head *net_list)
277 {
278 	tc_action_net_exit(net_list, gact_net_id);
279 }
280 
281 static struct pernet_operations gact_net_ops = {
282 	.init = gact_init_net,
283 	.exit_batch = gact_exit_net,
284 	.id   = &gact_net_id,
285 	.size = sizeof(struct tc_action_net),
286 };
287 
288 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
289 MODULE_DESCRIPTION("Generic Classifier actions");
290 MODULE_LICENSE("GPL");
291 
292 static int __init gact_init_module(void)
293 {
294 #ifdef CONFIG_GACT_PROB
295 	pr_info("GACT probability on\n");
296 #else
297 	pr_info("GACT probability NOT on\n");
298 #endif
299 
300 	return tcf_register_action(&act_gact_ops, &gact_net_ops);
301 }
302 
303 static void __exit gact_cleanup_module(void)
304 {
305 	tcf_unregister_action(&act_gact_ops, &gact_net_ops);
306 }
307 
308 module_init(gact_init_module);
309 module_exit(gact_cleanup_module);
310