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