xref: /linux/net/sched/act_police.c (revision 0b8061c340b643e01da431dd60c75a41bb1d31ec)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/act_police.c	Input police filter
4  *
5  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  * 		J Hadi Salim (action changes)
7  */
8 
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/errno.h>
14 #include <linux/skbuff.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <net/act_api.h>
19 #include <net/netlink.h>
20 #include <net/pkt_cls.h>
21 #include <net/tc_act/tc_police.h>
22 
23 /* Each policer is serialized by its individual spinlock */
24 
25 static unsigned int police_net_id;
26 static struct tc_action_ops act_police_ops;
27 
28 static int tcf_police_walker(struct net *net, struct sk_buff *skb,
29 				 struct netlink_callback *cb, int type,
30 				 const struct tc_action_ops *ops,
31 				 struct netlink_ext_ack *extack)
32 {
33 	struct tc_action_net *tn = net_generic(net, police_net_id);
34 
35 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
36 }
37 
38 static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
39 	[TCA_POLICE_RATE]	= { .len = TC_RTAB_SIZE },
40 	[TCA_POLICE_PEAKRATE]	= { .len = TC_RTAB_SIZE },
41 	[TCA_POLICE_AVRATE]	= { .type = NLA_U32 },
42 	[TCA_POLICE_RESULT]	= { .type = NLA_U32 },
43 	[TCA_POLICE_RATE64]     = { .type = NLA_U64 },
44 	[TCA_POLICE_PEAKRATE64] = { .type = NLA_U64 },
45 };
46 
47 static int tcf_police_init(struct net *net, struct nlattr *nla,
48 			       struct nlattr *est, struct tc_action **a,
49 			       int ovr, int bind, bool rtnl_held,
50 			       struct tcf_proto *tp, u32 flags,
51 			       struct netlink_ext_ack *extack)
52 {
53 	int ret = 0, tcfp_result = TC_ACT_OK, err, size;
54 	struct nlattr *tb[TCA_POLICE_MAX + 1];
55 	struct tcf_chain *goto_ch = NULL;
56 	struct tc_police *parm;
57 	struct tcf_police *police;
58 	struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
59 	struct tc_action_net *tn = net_generic(net, police_net_id);
60 	struct tcf_police_params *new;
61 	bool exists = false;
62 	u32 index;
63 	u64 rate64, prate64;
64 
65 	if (nla == NULL)
66 		return -EINVAL;
67 
68 	err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
69 					  police_policy, NULL);
70 	if (err < 0)
71 		return err;
72 
73 	if (tb[TCA_POLICE_TBF] == NULL)
74 		return -EINVAL;
75 	size = nla_len(tb[TCA_POLICE_TBF]);
76 	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
77 		return -EINVAL;
78 
79 	parm = nla_data(tb[TCA_POLICE_TBF]);
80 	index = parm->index;
81 	err = tcf_idr_check_alloc(tn, &index, a, bind);
82 	if (err < 0)
83 		return err;
84 	exists = err;
85 	if (exists && bind)
86 		return 0;
87 
88 	if (!exists) {
89 		ret = tcf_idr_create(tn, index, NULL, a,
90 				     &act_police_ops, bind, true, 0);
91 		if (ret) {
92 			tcf_idr_cleanup(tn, index);
93 			return ret;
94 		}
95 		ret = ACT_P_CREATED;
96 		spin_lock_init(&(to_police(*a)->tcfp_lock));
97 	} else if (!ovr) {
98 		tcf_idr_release(*a, bind);
99 		return -EEXIST;
100 	}
101 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
102 	if (err < 0)
103 		goto release_idr;
104 
105 	police = to_police(*a);
106 	if (parm->rate.rate) {
107 		err = -ENOMEM;
108 		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
109 		if (R_tab == NULL)
110 			goto failure;
111 
112 		if (parm->peakrate.rate) {
113 			P_tab = qdisc_get_rtab(&parm->peakrate,
114 					       tb[TCA_POLICE_PEAKRATE], NULL);
115 			if (P_tab == NULL)
116 				goto failure;
117 		}
118 	}
119 
120 	if (est) {
121 		err = gen_replace_estimator(&police->tcf_bstats,
122 					    police->common.cpu_bstats,
123 					    &police->tcf_rate_est,
124 					    &police->tcf_lock,
125 					    NULL, est);
126 		if (err)
127 			goto failure;
128 	} else if (tb[TCA_POLICE_AVRATE] &&
129 		   (ret == ACT_P_CREATED ||
130 		    !gen_estimator_active(&police->tcf_rate_est))) {
131 		err = -EINVAL;
132 		goto failure;
133 	}
134 
135 	if (tb[TCA_POLICE_RESULT]) {
136 		tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
137 		if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
138 			NL_SET_ERR_MSG(extack,
139 				       "goto chain not allowed on fallback");
140 			err = -EINVAL;
141 			goto failure;
142 		}
143 	}
144 
145 	new = kzalloc(sizeof(*new), GFP_KERNEL);
146 	if (unlikely(!new)) {
147 		err = -ENOMEM;
148 		goto failure;
149 	}
150 
151 	/* No failure allowed after this point */
152 	new->tcfp_result = tcfp_result;
153 	new->tcfp_mtu = parm->mtu;
154 	if (!new->tcfp_mtu) {
155 		new->tcfp_mtu = ~0;
156 		if (R_tab)
157 			new->tcfp_mtu = 255 << R_tab->rate.cell_log;
158 	}
159 	if (R_tab) {
160 		new->rate_present = true;
161 		rate64 = tb[TCA_POLICE_RATE64] ?
162 			 nla_get_u64(tb[TCA_POLICE_RATE64]) : 0;
163 		psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64);
164 		qdisc_put_rtab(R_tab);
165 	} else {
166 		new->rate_present = false;
167 	}
168 	if (P_tab) {
169 		new->peak_present = true;
170 		prate64 = tb[TCA_POLICE_PEAKRATE64] ?
171 			  nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0;
172 		psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64);
173 		qdisc_put_rtab(P_tab);
174 	} else {
175 		new->peak_present = false;
176 	}
177 
178 	new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
179 	if (new->peak_present)
180 		new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
181 							 new->tcfp_mtu);
182 
183 	if (tb[TCA_POLICE_AVRATE])
184 		new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
185 
186 	spin_lock_bh(&police->tcf_lock);
187 	spin_lock_bh(&police->tcfp_lock);
188 	police->tcfp_t_c = ktime_get_ns();
189 	police->tcfp_toks = new->tcfp_burst;
190 	if (new->peak_present)
191 		police->tcfp_ptoks = new->tcfp_mtu_ptoks;
192 	spin_unlock_bh(&police->tcfp_lock);
193 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
194 	new = rcu_replace_pointer(police->params,
195 				  new,
196 				  lockdep_is_held(&police->tcf_lock));
197 	spin_unlock_bh(&police->tcf_lock);
198 
199 	if (goto_ch)
200 		tcf_chain_put_by_act(goto_ch);
201 	if (new)
202 		kfree_rcu(new, rcu);
203 
204 	return ret;
205 
206 failure:
207 	qdisc_put_rtab(P_tab);
208 	qdisc_put_rtab(R_tab);
209 	if (goto_ch)
210 		tcf_chain_put_by_act(goto_ch);
211 release_idr:
212 	tcf_idr_release(*a, bind);
213 	return err;
214 }
215 
216 static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
217 			  struct tcf_result *res)
218 {
219 	struct tcf_police *police = to_police(a);
220 	struct tcf_police_params *p;
221 	s64 now, toks, ptoks = 0;
222 	int ret;
223 
224 	tcf_lastuse_update(&police->tcf_tm);
225 	bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
226 
227 	ret = READ_ONCE(police->tcf_action);
228 	p = rcu_dereference_bh(police->params);
229 
230 	if (p->tcfp_ewma_rate) {
231 		struct gnet_stats_rate_est64 sample;
232 
233 		if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
234 		    sample.bps >= p->tcfp_ewma_rate)
235 			goto inc_overlimits;
236 	}
237 
238 	if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
239 		if (!p->rate_present) {
240 			ret = p->tcfp_result;
241 			goto end;
242 		}
243 
244 		now = ktime_get_ns();
245 		spin_lock_bh(&police->tcfp_lock);
246 		toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
247 		if (p->peak_present) {
248 			ptoks = toks + police->tcfp_ptoks;
249 			if (ptoks > p->tcfp_mtu_ptoks)
250 				ptoks = p->tcfp_mtu_ptoks;
251 			ptoks -= (s64)psched_l2t_ns(&p->peak,
252 						    qdisc_pkt_len(skb));
253 		}
254 		toks += police->tcfp_toks;
255 		if (toks > p->tcfp_burst)
256 			toks = p->tcfp_burst;
257 		toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
258 		if ((toks|ptoks) >= 0) {
259 			police->tcfp_t_c = now;
260 			police->tcfp_toks = toks;
261 			police->tcfp_ptoks = ptoks;
262 			spin_unlock_bh(&police->tcfp_lock);
263 			ret = p->tcfp_result;
264 			goto inc_drops;
265 		}
266 		spin_unlock_bh(&police->tcfp_lock);
267 	}
268 
269 inc_overlimits:
270 	qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
271 inc_drops:
272 	if (ret == TC_ACT_SHOT)
273 		qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
274 end:
275 	return ret;
276 }
277 
278 static void tcf_police_cleanup(struct tc_action *a)
279 {
280 	struct tcf_police *police = to_police(a);
281 	struct tcf_police_params *p;
282 
283 	p = rcu_dereference_protected(police->params, 1);
284 	if (p)
285 		kfree_rcu(p, rcu);
286 }
287 
288 static void tcf_police_stats_update(struct tc_action *a,
289 				    u64 bytes, u64 packets, u64 drops,
290 				    u64 lastuse, bool hw)
291 {
292 	struct tcf_police *police = to_police(a);
293 	struct tcf_t *tm = &police->tcf_tm;
294 
295 	tcf_action_update_stats(a, bytes, packets, drops, hw);
296 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
297 }
298 
299 static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
300 			       int bind, int ref)
301 {
302 	unsigned char *b = skb_tail_pointer(skb);
303 	struct tcf_police *police = to_police(a);
304 	struct tcf_police_params *p;
305 	struct tc_police opt = {
306 		.index = police->tcf_index,
307 		.refcnt = refcount_read(&police->tcf_refcnt) - ref,
308 		.bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
309 	};
310 	struct tcf_t t;
311 
312 	spin_lock_bh(&police->tcf_lock);
313 	opt.action = police->tcf_action;
314 	p = rcu_dereference_protected(police->params,
315 				      lockdep_is_held(&police->tcf_lock));
316 	opt.mtu = p->tcfp_mtu;
317 	opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
318 	if (p->rate_present) {
319 		psched_ratecfg_getrate(&opt.rate, &p->rate);
320 		if ((police->params->rate.rate_bytes_ps >= (1ULL << 32)) &&
321 		    nla_put_u64_64bit(skb, TCA_POLICE_RATE64,
322 				      police->params->rate.rate_bytes_ps,
323 				      TCA_POLICE_PAD))
324 			goto nla_put_failure;
325 	}
326 	if (p->peak_present) {
327 		psched_ratecfg_getrate(&opt.peakrate, &p->peak);
328 		if ((police->params->peak.rate_bytes_ps >= (1ULL << 32)) &&
329 		    nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64,
330 				      police->params->peak.rate_bytes_ps,
331 				      TCA_POLICE_PAD))
332 			goto nla_put_failure;
333 	}
334 	if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
335 		goto nla_put_failure;
336 	if (p->tcfp_result &&
337 	    nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
338 		goto nla_put_failure;
339 	if (p->tcfp_ewma_rate &&
340 	    nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
341 		goto nla_put_failure;
342 
343 	tcf_tm_dump(&t, &police->tcf_tm);
344 	if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
345 		goto nla_put_failure;
346 	spin_unlock_bh(&police->tcf_lock);
347 
348 	return skb->len;
349 
350 nla_put_failure:
351 	spin_unlock_bh(&police->tcf_lock);
352 	nlmsg_trim(skb, b);
353 	return -1;
354 }
355 
356 static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
357 {
358 	struct tc_action_net *tn = net_generic(net, police_net_id);
359 
360 	return tcf_idr_search(tn, a, index);
361 }
362 
363 MODULE_AUTHOR("Alexey Kuznetsov");
364 MODULE_DESCRIPTION("Policing actions");
365 MODULE_LICENSE("GPL");
366 
367 static struct tc_action_ops act_police_ops = {
368 	.kind		=	"police",
369 	.id		=	TCA_ID_POLICE,
370 	.owner		=	THIS_MODULE,
371 	.stats_update	=	tcf_police_stats_update,
372 	.act		=	tcf_police_act,
373 	.dump		=	tcf_police_dump,
374 	.init		=	tcf_police_init,
375 	.walk		=	tcf_police_walker,
376 	.lookup		=	tcf_police_search,
377 	.cleanup	=	tcf_police_cleanup,
378 	.size		=	sizeof(struct tcf_police),
379 };
380 
381 static __net_init int police_init_net(struct net *net)
382 {
383 	struct tc_action_net *tn = net_generic(net, police_net_id);
384 
385 	return tc_action_net_init(net, tn, &act_police_ops);
386 }
387 
388 static void __net_exit police_exit_net(struct list_head *net_list)
389 {
390 	tc_action_net_exit(net_list, police_net_id);
391 }
392 
393 static struct pernet_operations police_net_ops = {
394 	.init = police_init_net,
395 	.exit_batch = police_exit_net,
396 	.id   = &police_net_id,
397 	.size = sizeof(struct tc_action_net),
398 };
399 
400 static int __init police_init_module(void)
401 {
402 	return tcf_register_action(&act_police_ops, &police_net_ops);
403 }
404 
405 static void __exit police_cleanup_module(void)
406 {
407 	tcf_unregister_action(&act_police_ops, &police_net_ops);
408 }
409 
410 module_init(police_init_module);
411 module_exit(police_cleanup_module);
412