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