xref: /openbmc/linux/net/sched/act_police.c (revision abbb0d33632ce931ca9c814813ee131351f6b92f)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
24bba3925SPatrick McHardy /*
30c6965ddSJiri Pirko  * net/sched/act_police.c	Input police filter
44bba3925SPatrick McHardy  *
54bba3925SPatrick McHardy  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
64bba3925SPatrick McHardy  * 		J Hadi Salim (action changes)
74bba3925SPatrick McHardy  */
84bba3925SPatrick McHardy 
94bba3925SPatrick McHardy #include <linux/module.h>
104bba3925SPatrick McHardy #include <linux/types.h>
114bba3925SPatrick McHardy #include <linux/kernel.h>
124bba3925SPatrick McHardy #include <linux/string.h>
134bba3925SPatrick McHardy #include <linux/errno.h>
144bba3925SPatrick McHardy #include <linux/skbuff.h>
154bba3925SPatrick McHardy #include <linux/rtnetlink.h>
164bba3925SPatrick McHardy #include <linux/init.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
184bba3925SPatrick McHardy #include <net/act_api.h>
19dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
20d6124d6bSDavide Caratti #include <net/pkt_cls.h>
21fa762da9SPieter Jansen van Vuuren #include <net/tc_act/tc_police.h>
221e9b3d53SPatrick McHardy 
234bba3925SPatrick McHardy /* Each policer is serialized by its individual spinlock */
244bba3925SPatrick McHardy 
25c7d03a00SAlexey Dobriyan static unsigned int police_net_id;
26a85a970aSWANG Cong static struct tc_action_ops act_police_ops;
27ddf97ccdSWANG Cong 
282ac06347SJamal Hadi Salim static int tcf_police_walker(struct net *net, struct sk_buff *skb,
29ddf97ccdSWANG Cong 				 struct netlink_callback *cb, int type,
3041780105SAlexander Aring 				 const struct tc_action_ops *ops,
3141780105SAlexander Aring 				 struct netlink_ext_ack *extack)
324bba3925SPatrick McHardy {
33ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
344bba3925SPatrick McHardy 
35b3620145SAlexander Aring 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
364bba3925SPatrick McHardy }
374bba3925SPatrick McHardy 
3853b2bf3fSPatrick McHardy static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
3953b2bf3fSPatrick McHardy 	[TCA_POLICE_RATE]	= { .len = TC_RTAB_SIZE },
4053b2bf3fSPatrick McHardy 	[TCA_POLICE_PEAKRATE]	= { .len = TC_RTAB_SIZE },
4153b2bf3fSPatrick McHardy 	[TCA_POLICE_AVRATE]	= { .type = NLA_U32 },
4253b2bf3fSPatrick McHardy 	[TCA_POLICE_RESULT]	= { .type = NLA_U32 },
43d1967e49SDavid Dai 	[TCA_POLICE_RATE64]     = { .type = NLA_U64 },
44d1967e49SDavid Dai 	[TCA_POLICE_PEAKRATE64] = { .type = NLA_U64 },
4553b2bf3fSPatrick McHardy };
4653b2bf3fSPatrick McHardy 
472ac06347SJamal Hadi Salim static int tcf_police_init(struct net *net, struct nlattr *nla,
48a85a970aSWANG Cong 			       struct nlattr *est, struct tc_action **a,
49789871bbSVlad Buslov 			       int ovr, int bind, bool rtnl_held,
50*abbb0d33SVlad Buslov 			       struct tcf_proto *tp, u32 flags,
51589dad6dSAlexander Aring 			       struct netlink_ext_ack *extack)
524bba3925SPatrick McHardy {
53fd6d4338SDavide Caratti 	int ret = 0, tcfp_result = TC_ACT_OK, err, size;
547ba699c6SPatrick McHardy 	struct nlattr *tb[TCA_POLICE_MAX + 1];
55d6124d6bSDavide Caratti 	struct tcf_chain *goto_ch = NULL;
564bba3925SPatrick McHardy 	struct tc_police *parm;
57e9ce1cd3SDavid S. Miller 	struct tcf_police *police;
584bba3925SPatrick McHardy 	struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
59ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
602d550dbaSDavide Caratti 	struct tcf_police_params *new;
610852e455SWANG Cong 	bool exists = false;
627be8ef2cSDmytro Linkin 	u32 index;
63d1967e49SDavid Dai 	u64 rate64, prate64;
644bba3925SPatrick McHardy 
65cee63723SPatrick McHardy 	if (nla == NULL)
664bba3925SPatrick McHardy 		return -EINVAL;
674bba3925SPatrick McHardy 
688cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
698cb08174SJohannes Berg 					  police_policy, NULL);
70cee63723SPatrick McHardy 	if (err < 0)
71cee63723SPatrick McHardy 		return err;
72cee63723SPatrick McHardy 
737ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_TBF] == NULL)
741e9b3d53SPatrick McHardy 		return -EINVAL;
757ba699c6SPatrick McHardy 	size = nla_len(tb[TCA_POLICE_TBF]);
761e9b3d53SPatrick McHardy 	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
774bba3925SPatrick McHardy 		return -EINVAL;
784bba3925SPatrick McHardy 
790852e455SWANG Cong 	parm = nla_data(tb[TCA_POLICE_TBF]);
807be8ef2cSDmytro Linkin 	index = parm->index;
817be8ef2cSDmytro Linkin 	err = tcf_idr_check_alloc(tn, &index, a, bind);
820190c1d4SVlad Buslov 	if (err < 0)
830190c1d4SVlad Buslov 		return err;
840190c1d4SVlad Buslov 	exists = err;
850852e455SWANG Cong 	if (exists && bind)
860852e455SWANG Cong 		return 0;
870852e455SWANG Cong 
880852e455SWANG Cong 	if (!exists) {
897be8ef2cSDmytro Linkin 		ret = tcf_idr_create(tn, index, NULL, a,
9093be42f9SDavide Caratti 				     &act_police_ops, bind, true);
910190c1d4SVlad Buslov 		if (ret) {
927be8ef2cSDmytro Linkin 			tcf_idr_cleanup(tn, index);
93a03e6fe5SWANG Cong 			return ret;
940190c1d4SVlad Buslov 		}
95a03e6fe5SWANG Cong 		ret = ACT_P_CREATED;
96484afd1bSDavide Caratti 		spin_lock_init(&(to_police(*a)->tcfp_lock));
974e8ddd7fSVlad Buslov 	} else if (!ovr) {
9865a206c0SChris Mi 		tcf_idr_release(*a, bind);
990852e455SWANG Cong 		return -EEXIST;
100e9ce1cd3SDavid S. Miller 	}
101d6124d6bSDavide Caratti 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
102d6124d6bSDavide Caratti 	if (err < 0)
103d6124d6bSDavide Caratti 		goto release_idr;
1044bba3925SPatrick McHardy 
105a85a970aSWANG Cong 	police = to_police(*a);
1064bba3925SPatrick McHardy 	if (parm->rate.rate) {
1074bba3925SPatrick McHardy 		err = -ENOMEM;
108e9bc3fa2SAlexander Aring 		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
1094bba3925SPatrick McHardy 		if (R_tab == NULL)
1104bba3925SPatrick McHardy 			goto failure;
111c1b56878SStephen Hemminger 
1124bba3925SPatrick McHardy 		if (parm->peakrate.rate) {
1134bba3925SPatrick McHardy 			P_tab = qdisc_get_rtab(&parm->peakrate,
114e9bc3fa2SAlexander Aring 					       tb[TCA_POLICE_PEAKRATE], NULL);
11571bcb09aSStephen Hemminger 			if (P_tab == NULL)
1164bba3925SPatrick McHardy 				goto failure;
1174bba3925SPatrick McHardy 		}
1184bba3925SPatrick McHardy 	}
11971bcb09aSStephen Hemminger 
12071bcb09aSStephen Hemminger 	if (est) {
12193be42f9SDavide Caratti 		err = gen_replace_estimator(&police->tcf_bstats,
12293be42f9SDavide Caratti 					    police->common.cpu_bstats,
12371bcb09aSStephen Hemminger 					    &police->tcf_rate_est,
124edb09eb1SEric Dumazet 					    &police->tcf_lock,
125edb09eb1SEric Dumazet 					    NULL, est);
12671bcb09aSStephen Hemminger 		if (err)
12774030603SWANG Cong 			goto failure;
128a883bf56SJarek Poplawski 	} else if (tb[TCA_POLICE_AVRATE] &&
129a883bf56SJarek Poplawski 		   (ret == ACT_P_CREATED ||
1301c0d32fdSEric Dumazet 		    !gen_estimator_active(&police->tcf_rate_est))) {
131a883bf56SJarek Poplawski 		err = -EINVAL;
13274030603SWANG Cong 		goto failure;
13371bcb09aSStephen Hemminger 	}
13471bcb09aSStephen Hemminger 
135fd6d4338SDavide Caratti 	if (tb[TCA_POLICE_RESULT]) {
136fd6d4338SDavide Caratti 		tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
137fd6d4338SDavide Caratti 		if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
138fd6d4338SDavide Caratti 			NL_SET_ERR_MSG(extack,
139fd6d4338SDavide Caratti 				       "goto chain not allowed on fallback");
140fd6d4338SDavide Caratti 			err = -EINVAL;
141fd6d4338SDavide Caratti 			goto failure;
142fd6d4338SDavide Caratti 		}
143fd6d4338SDavide Caratti 	}
144fd6d4338SDavide Caratti 
1452d550dbaSDavide Caratti 	new = kzalloc(sizeof(*new), GFP_KERNEL);
1462d550dbaSDavide Caratti 	if (unlikely(!new)) {
1472d550dbaSDavide Caratti 		err = -ENOMEM;
1482d550dbaSDavide Caratti 		goto failure;
1492d550dbaSDavide Caratti 	}
1502d550dbaSDavide Caratti 
15171bcb09aSStephen Hemminger 	/* No failure allowed after this point */
152fd6d4338SDavide Caratti 	new->tcfp_result = tcfp_result;
1532d550dbaSDavide Caratti 	new->tcfp_mtu = parm->mtu;
1542d550dbaSDavide Caratti 	if (!new->tcfp_mtu) {
1552d550dbaSDavide Caratti 		new->tcfp_mtu = ~0;
156c6d14ff1SJiri Pirko 		if (R_tab)
1572d550dbaSDavide Caratti 			new->tcfp_mtu = 255 << R_tab->rate.cell_log;
1584bba3925SPatrick McHardy 	}
159c6d14ff1SJiri Pirko 	if (R_tab) {
1602d550dbaSDavide Caratti 		new->rate_present = true;
161d1967e49SDavid Dai 		rate64 = tb[TCA_POLICE_RATE64] ?
162d1967e49SDavid Dai 			 nla_get_u64(tb[TCA_POLICE_RATE64]) : 0;
163d1967e49SDavid Dai 		psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64);
164c6d14ff1SJiri Pirko 		qdisc_put_rtab(R_tab);
165c6d14ff1SJiri Pirko 	} else {
1662d550dbaSDavide Caratti 		new->rate_present = false;
167c6d14ff1SJiri Pirko 	}
168c6d14ff1SJiri Pirko 	if (P_tab) {
1692d550dbaSDavide Caratti 		new->peak_present = true;
170d1967e49SDavid Dai 		prate64 = tb[TCA_POLICE_PEAKRATE64] ?
171d1967e49SDavid Dai 			  nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0;
172d1967e49SDavid Dai 		psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64);
173c6d14ff1SJiri Pirko 		qdisc_put_rtab(P_tab);
174c6d14ff1SJiri Pirko 	} else {
1752d550dbaSDavide Caratti 		new->peak_present = false;
1764bba3925SPatrick McHardy 	}
1774bba3925SPatrick McHardy 
1782d550dbaSDavide Caratti 	new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
179f2cbd485SDavide Caratti 	if (new->peak_present)
1802d550dbaSDavide Caratti 		new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
1812d550dbaSDavide Caratti 							 new->tcfp_mtu);
1824bba3925SPatrick McHardy 
1837ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_AVRATE])
1842d550dbaSDavide Caratti 		new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
1854bba3925SPatrick McHardy 
1862d550dbaSDavide Caratti 	spin_lock_bh(&police->tcf_lock);
187f2cbd485SDavide Caratti 	spin_lock_bh(&police->tcfp_lock);
188f2cbd485SDavide Caratti 	police->tcfp_t_c = ktime_get_ns();
189f2cbd485SDavide Caratti 	police->tcfp_toks = new->tcfp_burst;
190f2cbd485SDavide Caratti 	if (new->peak_present)
191f2cbd485SDavide Caratti 		police->tcfp_ptoks = new->tcfp_mtu_ptoks;
192f2cbd485SDavide Caratti 	spin_unlock_bh(&police->tcfp_lock);
193d6124d6bSDavide Caratti 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1942d550dbaSDavide Caratti 	rcu_swap_protected(police->params,
1952d550dbaSDavide Caratti 			   new,
1962d550dbaSDavide Caratti 			   lockdep_is_held(&police->tcf_lock));
197e9ce1cd3SDavid S. Miller 	spin_unlock_bh(&police->tcf_lock);
1984bba3925SPatrick McHardy 
199d6124d6bSDavide Caratti 	if (goto_ch)
200d6124d6bSDavide Caratti 		tcf_chain_put_by_act(goto_ch);
2012d550dbaSDavide Caratti 	if (new)
2022d550dbaSDavide Caratti 		kfree_rcu(new, rcu);
2032d550dbaSDavide Caratti 
2042d550dbaSDavide Caratti 	if (ret == ACT_P_CREATED)
20565a206c0SChris Mi 		tcf_idr_insert(tn, *a);
2064bba3925SPatrick McHardy 	return ret;
2074bba3925SPatrick McHardy 
2084bba3925SPatrick McHardy failure:
20971bcb09aSStephen Hemminger 	qdisc_put_rtab(P_tab);
21071bcb09aSStephen Hemminger 	qdisc_put_rtab(R_tab);
211d6124d6bSDavide Caratti 	if (goto_ch)
212d6124d6bSDavide Caratti 		tcf_chain_put_by_act(goto_ch);
213d6124d6bSDavide Caratti release_idr:
2145bf7f818SDavide Caratti 	tcf_idr_release(*a, bind);
2154bba3925SPatrick McHardy 	return err;
2164bba3925SPatrick McHardy }
2174bba3925SPatrick McHardy 
2182ac06347SJamal Hadi Salim static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
2194bba3925SPatrick McHardy 			  struct tcf_result *res)
2204bba3925SPatrick McHardy {
221a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
2222d550dbaSDavide Caratti 	struct tcf_police_params *p;
22393be42f9SDavide Caratti 	s64 now, toks, ptoks = 0;
22493be42f9SDavide Caratti 	int ret;
22593be42f9SDavide Caratti 
22693be42f9SDavide Caratti 	tcf_lastuse_update(&police->tcf_tm);
22793be42f9SDavide Caratti 	bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
2284bba3925SPatrick McHardy 
2292d550dbaSDavide Caratti 	ret = READ_ONCE(police->tcf_action);
2302d550dbaSDavide Caratti 	p = rcu_dereference_bh(police->params);
2312d550dbaSDavide Caratti 
2322d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate) {
2331c0d32fdSEric Dumazet 		struct gnet_stats_rate_est64 sample;
2341c0d32fdSEric Dumazet 
2351c0d32fdSEric Dumazet 		if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
2362d550dbaSDavide Caratti 		    sample.bps >= p->tcfp_ewma_rate)
23793be42f9SDavide Caratti 			goto inc_overlimits;
2384bba3925SPatrick McHardy 	}
2394bba3925SPatrick McHardy 
2402d550dbaSDavide Caratti 	if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
2412d550dbaSDavide Caratti 		if (!p->rate_present) {
2422d550dbaSDavide Caratti 			ret = p->tcfp_result;
2432d550dbaSDavide Caratti 			goto end;
2444bba3925SPatrick McHardy 		}
2454bba3925SPatrick McHardy 
246d2de875cSEric Dumazet 		now = ktime_get_ns();
247f2cbd485SDavide Caratti 		spin_lock_bh(&police->tcfp_lock);
248f2cbd485SDavide Caratti 		toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
2492d550dbaSDavide Caratti 		if (p->peak_present) {
250f2cbd485SDavide Caratti 			ptoks = toks + police->tcfp_ptoks;
2512d550dbaSDavide Caratti 			if (ptoks > p->tcfp_mtu_ptoks)
2522d550dbaSDavide Caratti 				ptoks = p->tcfp_mtu_ptoks;
2532d550dbaSDavide Caratti 			ptoks -= (s64)psched_l2t_ns(&p->peak,
254c6d14ff1SJiri Pirko 						    qdisc_pkt_len(skb));
2554bba3925SPatrick McHardy 		}
256f2cbd485SDavide Caratti 		toks += police->tcfp_toks;
2572d550dbaSDavide Caratti 		if (toks > p->tcfp_burst)
2582d550dbaSDavide Caratti 			toks = p->tcfp_burst;
2592d550dbaSDavide Caratti 		toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
2604bba3925SPatrick McHardy 		if ((toks|ptoks) >= 0) {
261f2cbd485SDavide Caratti 			police->tcfp_t_c = now;
262f2cbd485SDavide Caratti 			police->tcfp_toks = toks;
263f2cbd485SDavide Caratti 			police->tcfp_ptoks = ptoks;
264f2cbd485SDavide Caratti 			spin_unlock_bh(&police->tcfp_lock);
2652d550dbaSDavide Caratti 			ret = p->tcfp_result;
26693be42f9SDavide Caratti 			goto inc_drops;
2674bba3925SPatrick McHardy 		}
268f2cbd485SDavide Caratti 		spin_unlock_bh(&police->tcfp_lock);
2694bba3925SPatrick McHardy 	}
2704bba3925SPatrick McHardy 
27193be42f9SDavide Caratti inc_overlimits:
27293be42f9SDavide Caratti 	qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
27393be42f9SDavide Caratti inc_drops:
27493be42f9SDavide Caratti 	if (ret == TC_ACT_SHOT)
27593be42f9SDavide Caratti 		qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
2762d550dbaSDavide Caratti end:
27793be42f9SDavide Caratti 	return ret;
2784bba3925SPatrick McHardy }
2794bba3925SPatrick McHardy 
2802d550dbaSDavide Caratti static void tcf_police_cleanup(struct tc_action *a)
2812d550dbaSDavide Caratti {
2822d550dbaSDavide Caratti 	struct tcf_police *police = to_police(a);
2832d550dbaSDavide Caratti 	struct tcf_police_params *p;
2842d550dbaSDavide Caratti 
2852d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params, 1);
2862d550dbaSDavide Caratti 	if (p)
2872d550dbaSDavide Caratti 		kfree_rcu(p, rcu);
2882d550dbaSDavide Caratti }
2892d550dbaSDavide Caratti 
29012f02b6bSPieter Jansen van Vuuren static void tcf_police_stats_update(struct tc_action *a,
29112f02b6bSPieter Jansen van Vuuren 				    u64 bytes, u32 packets,
29212f02b6bSPieter Jansen van Vuuren 				    u64 lastuse, bool hw)
29312f02b6bSPieter Jansen van Vuuren {
29412f02b6bSPieter Jansen van Vuuren 	struct tcf_police *police = to_police(a);
29512f02b6bSPieter Jansen van Vuuren 	struct tcf_t *tm = &police->tcf_tm;
29612f02b6bSPieter Jansen van Vuuren 
297c8ecebd0SVlad Buslov 	tcf_action_update_stats(a, bytes, packets, false, hw);
29812f02b6bSPieter Jansen van Vuuren 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
29912f02b6bSPieter Jansen van Vuuren }
30012f02b6bSPieter Jansen van Vuuren 
3012ac06347SJamal Hadi Salim static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
3025a7a5555SJamal Hadi Salim 			       int bind, int ref)
3034bba3925SPatrick McHardy {
30427a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
305a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
3062d550dbaSDavide Caratti 	struct tcf_police_params *p;
3070f04cfd0SJeff Mahoney 	struct tc_police opt = {
3080f04cfd0SJeff Mahoney 		.index = police->tcf_index,
309036bb443SVlad Buslov 		.refcnt = refcount_read(&police->tcf_refcnt) - ref,
310036bb443SVlad Buslov 		.bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
3110f04cfd0SJeff Mahoney 	};
3123d3ed181SJamal Hadi Salim 	struct tcf_t t;
3134bba3925SPatrick McHardy 
314e329bc42SVlad Buslov 	spin_lock_bh(&police->tcf_lock);
315e329bc42SVlad Buslov 	opt.action = police->tcf_action;
3162d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params,
3172d550dbaSDavide Caratti 				      lockdep_is_held(&police->tcf_lock));
3182d550dbaSDavide Caratti 	opt.mtu = p->tcfp_mtu;
3192d550dbaSDavide Caratti 	opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
320d1967e49SDavid Dai 	if (p->rate_present) {
3212d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.rate, &p->rate);
322d1967e49SDavid Dai 		if ((police->params->rate.rate_bytes_ps >= (1ULL << 32)) &&
323d1967e49SDavid Dai 		    nla_put_u64_64bit(skb, TCA_POLICE_RATE64,
324d1967e49SDavid Dai 				      police->params->rate.rate_bytes_ps,
325d1967e49SDavid Dai 				      TCA_POLICE_PAD))
326d1967e49SDavid Dai 			goto nla_put_failure;
327d1967e49SDavid Dai 	}
328d1967e49SDavid Dai 	if (p->peak_present) {
3292d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.peakrate, &p->peak);
330d1967e49SDavid Dai 		if ((police->params->peak.rate_bytes_ps >= (1ULL << 32)) &&
331d1967e49SDavid Dai 		    nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64,
332d1967e49SDavid Dai 				      police->params->peak.rate_bytes_ps,
333d1967e49SDavid Dai 				      TCA_POLICE_PAD))
334d1967e49SDavid Dai 			goto nla_put_failure;
335d1967e49SDavid Dai 	}
3361b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
3371b34ec43SDavid S. Miller 		goto nla_put_failure;
3382d550dbaSDavide Caratti 	if (p->tcfp_result &&
3392d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
3401b34ec43SDavid S. Miller 		goto nla_put_failure;
3412d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate &&
3422d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
3431b34ec43SDavid S. Miller 		goto nla_put_failure;
3443d3ed181SJamal Hadi Salim 
345985fd98aSDavide Caratti 	tcf_tm_dump(&t, &police->tcf_tm);
3463d3ed181SJamal Hadi Salim 	if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
3473d3ed181SJamal Hadi Salim 		goto nla_put_failure;
348e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
3493d3ed181SJamal Hadi Salim 
3504bba3925SPatrick McHardy 	return skb->len;
3514bba3925SPatrick McHardy 
3527ba699c6SPatrick McHardy nla_put_failure:
353e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
354dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
3554bba3925SPatrick McHardy 	return -1;
3564bba3925SPatrick McHardy }
3574bba3925SPatrick McHardy 
358f061b48cSCong Wang static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
359ddf97ccdSWANG Cong {
360ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
361ddf97ccdSWANG Cong 
36265a206c0SChris Mi 	return tcf_idr_search(tn, a, index);
363ddf97ccdSWANG Cong }
364ddf97ccdSWANG Cong 
3654bba3925SPatrick McHardy MODULE_AUTHOR("Alexey Kuznetsov");
3664bba3925SPatrick McHardy MODULE_DESCRIPTION("Policing actions");
3674bba3925SPatrick McHardy MODULE_LICENSE("GPL");
3684bba3925SPatrick McHardy 
3694bba3925SPatrick McHardy static struct tc_action_ops act_police_ops = {
3704bba3925SPatrick McHardy 	.kind		=	"police",
371eddd2cf1SEli Cohen 	.id		=	TCA_ID_POLICE,
3724bba3925SPatrick McHardy 	.owner		=	THIS_MODULE,
37312f02b6bSPieter Jansen van Vuuren 	.stats_update	=	tcf_police_stats_update,
3742ac06347SJamal Hadi Salim 	.act		=	tcf_police_act,
3752ac06347SJamal Hadi Salim 	.dump		=	tcf_police_dump,
3762ac06347SJamal Hadi Salim 	.init		=	tcf_police_init,
3772ac06347SJamal Hadi Salim 	.walk		=	tcf_police_walker,
378ddf97ccdSWANG Cong 	.lookup		=	tcf_police_search,
3792d550dbaSDavide Caratti 	.cleanup	=	tcf_police_cleanup,
380a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_police),
381ddf97ccdSWANG Cong };
382ddf97ccdSWANG Cong 
383ddf97ccdSWANG Cong static __net_init int police_init_net(struct net *net)
384ddf97ccdSWANG Cong {
385ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
386ddf97ccdSWANG Cong 
387981471bdSCong Wang 	return tc_action_net_init(net, tn, &act_police_ops);
388ddf97ccdSWANG Cong }
389ddf97ccdSWANG Cong 
390039af9c6SCong Wang static void __net_exit police_exit_net(struct list_head *net_list)
391ddf97ccdSWANG Cong {
392039af9c6SCong Wang 	tc_action_net_exit(net_list, police_net_id);
393ddf97ccdSWANG Cong }
394ddf97ccdSWANG Cong 
395ddf97ccdSWANG Cong static struct pernet_operations police_net_ops = {
396ddf97ccdSWANG Cong 	.init = police_init_net,
397039af9c6SCong Wang 	.exit_batch = police_exit_net,
398ddf97ccdSWANG Cong 	.id   = &police_net_id,
399ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
4004bba3925SPatrick McHardy };
4014bba3925SPatrick McHardy 
4025a7a5555SJamal Hadi Salim static int __init police_init_module(void)
4034bba3925SPatrick McHardy {
404ddf97ccdSWANG Cong 	return tcf_register_action(&act_police_ops, &police_net_ops);
4054bba3925SPatrick McHardy }
4064bba3925SPatrick McHardy 
4075a7a5555SJamal Hadi Salim static void __exit police_cleanup_module(void)
4084bba3925SPatrick McHardy {
409ddf97ccdSWANG Cong 	tcf_unregister_action(&act_police_ops, &police_net_ops);
4104bba3925SPatrick McHardy }
4114bba3925SPatrick McHardy 
4124bba3925SPatrick McHardy module_init(police_init_module);
4134bba3925SPatrick McHardy module_exit(police_cleanup_module);
414