xref: /openbmc/linux/net/sched/act_police.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas 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 },
4353b2bf3fSPatrick McHardy };
4453b2bf3fSPatrick McHardy 
452ac06347SJamal Hadi Salim static int tcf_police_init(struct net *net, struct nlattr *nla,
46a85a970aSWANG Cong 			       struct nlattr *est, struct tc_action **a,
47789871bbSVlad Buslov 			       int ovr, int bind, bool rtnl_held,
4885d0966fSDavide Caratti 			       struct tcf_proto *tp,
49589dad6dSAlexander Aring 			       struct netlink_ext_ack *extack)
504bba3925SPatrick McHardy {
51fd6d4338SDavide Caratti 	int ret = 0, tcfp_result = TC_ACT_OK, err, size;
527ba699c6SPatrick McHardy 	struct nlattr *tb[TCA_POLICE_MAX + 1];
53d6124d6bSDavide Caratti 	struct tcf_chain *goto_ch = NULL;
544bba3925SPatrick McHardy 	struct tc_police *parm;
55e9ce1cd3SDavid S. Miller 	struct tcf_police *police;
564bba3925SPatrick McHardy 	struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
57ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
582d550dbaSDavide Caratti 	struct tcf_police_params *new;
590852e455SWANG Cong 	bool exists = false;
604bba3925SPatrick McHardy 
61cee63723SPatrick McHardy 	if (nla == NULL)
624bba3925SPatrick McHardy 		return -EINVAL;
634bba3925SPatrick McHardy 
648cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
658cb08174SJohannes Berg 					  police_policy, NULL);
66cee63723SPatrick McHardy 	if (err < 0)
67cee63723SPatrick McHardy 		return err;
68cee63723SPatrick McHardy 
697ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_TBF] == NULL)
701e9b3d53SPatrick McHardy 		return -EINVAL;
717ba699c6SPatrick McHardy 	size = nla_len(tb[TCA_POLICE_TBF]);
721e9b3d53SPatrick McHardy 	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
734bba3925SPatrick McHardy 		return -EINVAL;
744bba3925SPatrick McHardy 
750852e455SWANG Cong 	parm = nla_data(tb[TCA_POLICE_TBF]);
760190c1d4SVlad Buslov 	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
770190c1d4SVlad Buslov 	if (err < 0)
780190c1d4SVlad Buslov 		return err;
790190c1d4SVlad Buslov 	exists = err;
800852e455SWANG Cong 	if (exists && bind)
810852e455SWANG Cong 		return 0;
820852e455SWANG Cong 
830852e455SWANG Cong 	if (!exists) {
8465a206c0SChris Mi 		ret = tcf_idr_create(tn, parm->index, NULL, a,
8593be42f9SDavide Caratti 				     &act_police_ops, bind, true);
860190c1d4SVlad Buslov 		if (ret) {
870190c1d4SVlad Buslov 			tcf_idr_cleanup(tn, parm->index);
88a03e6fe5SWANG Cong 			return ret;
890190c1d4SVlad Buslov 		}
90a03e6fe5SWANG Cong 		ret = ACT_P_CREATED;
91484afd1bSDavide Caratti 		spin_lock_init(&(to_police(*a)->tcfp_lock));
924e8ddd7fSVlad Buslov 	} else if (!ovr) {
9365a206c0SChris Mi 		tcf_idr_release(*a, bind);
940852e455SWANG Cong 		return -EEXIST;
95e9ce1cd3SDavid S. Miller 	}
96d6124d6bSDavide Caratti 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
97d6124d6bSDavide Caratti 	if (err < 0)
98d6124d6bSDavide Caratti 		goto release_idr;
994bba3925SPatrick McHardy 
100a85a970aSWANG Cong 	police = to_police(*a);
1014bba3925SPatrick McHardy 	if (parm->rate.rate) {
1024bba3925SPatrick McHardy 		err = -ENOMEM;
103e9bc3fa2SAlexander Aring 		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
1044bba3925SPatrick McHardy 		if (R_tab == NULL)
1054bba3925SPatrick McHardy 			goto failure;
106c1b56878SStephen Hemminger 
1074bba3925SPatrick McHardy 		if (parm->peakrate.rate) {
1084bba3925SPatrick McHardy 			P_tab = qdisc_get_rtab(&parm->peakrate,
109e9bc3fa2SAlexander Aring 					       tb[TCA_POLICE_PEAKRATE], NULL);
11071bcb09aSStephen Hemminger 			if (P_tab == NULL)
1114bba3925SPatrick McHardy 				goto failure;
1124bba3925SPatrick McHardy 		}
1134bba3925SPatrick McHardy 	}
11471bcb09aSStephen Hemminger 
11571bcb09aSStephen Hemminger 	if (est) {
11693be42f9SDavide Caratti 		err = gen_replace_estimator(&police->tcf_bstats,
11793be42f9SDavide Caratti 					    police->common.cpu_bstats,
11871bcb09aSStephen Hemminger 					    &police->tcf_rate_est,
119edb09eb1SEric Dumazet 					    &police->tcf_lock,
120edb09eb1SEric Dumazet 					    NULL, est);
12171bcb09aSStephen Hemminger 		if (err)
12274030603SWANG Cong 			goto failure;
123a883bf56SJarek Poplawski 	} else if (tb[TCA_POLICE_AVRATE] &&
124a883bf56SJarek Poplawski 		   (ret == ACT_P_CREATED ||
1251c0d32fdSEric Dumazet 		    !gen_estimator_active(&police->tcf_rate_est))) {
126a883bf56SJarek Poplawski 		err = -EINVAL;
12774030603SWANG Cong 		goto failure;
12871bcb09aSStephen Hemminger 	}
12971bcb09aSStephen Hemminger 
130fd6d4338SDavide Caratti 	if (tb[TCA_POLICE_RESULT]) {
131fd6d4338SDavide Caratti 		tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
132fd6d4338SDavide Caratti 		if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
133fd6d4338SDavide Caratti 			NL_SET_ERR_MSG(extack,
134fd6d4338SDavide Caratti 				       "goto chain not allowed on fallback");
135fd6d4338SDavide Caratti 			err = -EINVAL;
136fd6d4338SDavide Caratti 			goto failure;
137fd6d4338SDavide Caratti 		}
138fd6d4338SDavide Caratti 	}
139fd6d4338SDavide Caratti 
1402d550dbaSDavide Caratti 	new = kzalloc(sizeof(*new), GFP_KERNEL);
1412d550dbaSDavide Caratti 	if (unlikely(!new)) {
1422d550dbaSDavide Caratti 		err = -ENOMEM;
1432d550dbaSDavide Caratti 		goto failure;
1442d550dbaSDavide Caratti 	}
1452d550dbaSDavide Caratti 
14671bcb09aSStephen Hemminger 	/* No failure allowed after this point */
147fd6d4338SDavide Caratti 	new->tcfp_result = tcfp_result;
1482d550dbaSDavide Caratti 	new->tcfp_mtu = parm->mtu;
1492d550dbaSDavide Caratti 	if (!new->tcfp_mtu) {
1502d550dbaSDavide Caratti 		new->tcfp_mtu = ~0;
151c6d14ff1SJiri Pirko 		if (R_tab)
1522d550dbaSDavide Caratti 			new->tcfp_mtu = 255 << R_tab->rate.cell_log;
1534bba3925SPatrick McHardy 	}
154c6d14ff1SJiri Pirko 	if (R_tab) {
1552d550dbaSDavide Caratti 		new->rate_present = true;
1562d550dbaSDavide Caratti 		psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
157c6d14ff1SJiri Pirko 		qdisc_put_rtab(R_tab);
158c6d14ff1SJiri Pirko 	} else {
1592d550dbaSDavide Caratti 		new->rate_present = false;
160c6d14ff1SJiri Pirko 	}
161c6d14ff1SJiri Pirko 	if (P_tab) {
1622d550dbaSDavide Caratti 		new->peak_present = true;
1632d550dbaSDavide Caratti 		psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
164c6d14ff1SJiri Pirko 		qdisc_put_rtab(P_tab);
165c6d14ff1SJiri Pirko 	} else {
1662d550dbaSDavide Caratti 		new->peak_present = false;
1674bba3925SPatrick McHardy 	}
1684bba3925SPatrick McHardy 
1692d550dbaSDavide Caratti 	new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
170f2cbd485SDavide Caratti 	if (new->peak_present)
1712d550dbaSDavide Caratti 		new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
1722d550dbaSDavide Caratti 							 new->tcfp_mtu);
1734bba3925SPatrick McHardy 
1747ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_AVRATE])
1752d550dbaSDavide Caratti 		new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
1764bba3925SPatrick McHardy 
1772d550dbaSDavide Caratti 	spin_lock_bh(&police->tcf_lock);
178f2cbd485SDavide Caratti 	spin_lock_bh(&police->tcfp_lock);
179f2cbd485SDavide Caratti 	police->tcfp_t_c = ktime_get_ns();
180f2cbd485SDavide Caratti 	police->tcfp_toks = new->tcfp_burst;
181f2cbd485SDavide Caratti 	if (new->peak_present)
182f2cbd485SDavide Caratti 		police->tcfp_ptoks = new->tcfp_mtu_ptoks;
183f2cbd485SDavide Caratti 	spin_unlock_bh(&police->tcfp_lock);
184d6124d6bSDavide Caratti 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1852d550dbaSDavide Caratti 	rcu_swap_protected(police->params,
1862d550dbaSDavide Caratti 			   new,
1872d550dbaSDavide Caratti 			   lockdep_is_held(&police->tcf_lock));
188e9ce1cd3SDavid S. Miller 	spin_unlock_bh(&police->tcf_lock);
1894bba3925SPatrick McHardy 
190d6124d6bSDavide Caratti 	if (goto_ch)
191d6124d6bSDavide Caratti 		tcf_chain_put_by_act(goto_ch);
1922d550dbaSDavide Caratti 	if (new)
1932d550dbaSDavide Caratti 		kfree_rcu(new, rcu);
1942d550dbaSDavide Caratti 
1952d550dbaSDavide Caratti 	if (ret == ACT_P_CREATED)
19665a206c0SChris Mi 		tcf_idr_insert(tn, *a);
1974bba3925SPatrick McHardy 	return ret;
1984bba3925SPatrick McHardy 
1994bba3925SPatrick McHardy failure:
20071bcb09aSStephen Hemminger 	qdisc_put_rtab(P_tab);
20171bcb09aSStephen Hemminger 	qdisc_put_rtab(R_tab);
202d6124d6bSDavide Caratti 	if (goto_ch)
203d6124d6bSDavide Caratti 		tcf_chain_put_by_act(goto_ch);
204d6124d6bSDavide Caratti release_idr:
2055bf7f818SDavide Caratti 	tcf_idr_release(*a, bind);
2064bba3925SPatrick McHardy 	return err;
2074bba3925SPatrick McHardy }
2084bba3925SPatrick McHardy 
2092ac06347SJamal Hadi Salim static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
2104bba3925SPatrick McHardy 			  struct tcf_result *res)
2114bba3925SPatrick McHardy {
212a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
2132d550dbaSDavide Caratti 	struct tcf_police_params *p;
21493be42f9SDavide Caratti 	s64 now, toks, ptoks = 0;
21593be42f9SDavide Caratti 	int ret;
21693be42f9SDavide Caratti 
21793be42f9SDavide Caratti 	tcf_lastuse_update(&police->tcf_tm);
21893be42f9SDavide Caratti 	bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
2194bba3925SPatrick McHardy 
2202d550dbaSDavide Caratti 	ret = READ_ONCE(police->tcf_action);
2212d550dbaSDavide Caratti 	p = rcu_dereference_bh(police->params);
2222d550dbaSDavide Caratti 
2232d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate) {
2241c0d32fdSEric Dumazet 		struct gnet_stats_rate_est64 sample;
2251c0d32fdSEric Dumazet 
2261c0d32fdSEric Dumazet 		if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
2272d550dbaSDavide Caratti 		    sample.bps >= p->tcfp_ewma_rate)
22893be42f9SDavide Caratti 			goto inc_overlimits;
2294bba3925SPatrick McHardy 	}
2304bba3925SPatrick McHardy 
2312d550dbaSDavide Caratti 	if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
2322d550dbaSDavide Caratti 		if (!p->rate_present) {
2332d550dbaSDavide Caratti 			ret = p->tcfp_result;
2342d550dbaSDavide Caratti 			goto end;
2354bba3925SPatrick McHardy 		}
2364bba3925SPatrick McHardy 
237d2de875cSEric Dumazet 		now = ktime_get_ns();
238f2cbd485SDavide Caratti 		spin_lock_bh(&police->tcfp_lock);
239f2cbd485SDavide Caratti 		toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
2402d550dbaSDavide Caratti 		if (p->peak_present) {
241f2cbd485SDavide Caratti 			ptoks = toks + police->tcfp_ptoks;
2422d550dbaSDavide Caratti 			if (ptoks > p->tcfp_mtu_ptoks)
2432d550dbaSDavide Caratti 				ptoks = p->tcfp_mtu_ptoks;
2442d550dbaSDavide Caratti 			ptoks -= (s64)psched_l2t_ns(&p->peak,
245c6d14ff1SJiri Pirko 						    qdisc_pkt_len(skb));
2464bba3925SPatrick McHardy 		}
247f2cbd485SDavide Caratti 		toks += police->tcfp_toks;
2482d550dbaSDavide Caratti 		if (toks > p->tcfp_burst)
2492d550dbaSDavide Caratti 			toks = p->tcfp_burst;
2502d550dbaSDavide Caratti 		toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
2514bba3925SPatrick McHardy 		if ((toks|ptoks) >= 0) {
252f2cbd485SDavide Caratti 			police->tcfp_t_c = now;
253f2cbd485SDavide Caratti 			police->tcfp_toks = toks;
254f2cbd485SDavide Caratti 			police->tcfp_ptoks = ptoks;
255f2cbd485SDavide Caratti 			spin_unlock_bh(&police->tcfp_lock);
2562d550dbaSDavide Caratti 			ret = p->tcfp_result;
25793be42f9SDavide Caratti 			goto inc_drops;
2584bba3925SPatrick McHardy 		}
259f2cbd485SDavide Caratti 		spin_unlock_bh(&police->tcfp_lock);
2604bba3925SPatrick McHardy 	}
2614bba3925SPatrick McHardy 
26293be42f9SDavide Caratti inc_overlimits:
26393be42f9SDavide Caratti 	qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
26493be42f9SDavide Caratti inc_drops:
26593be42f9SDavide Caratti 	if (ret == TC_ACT_SHOT)
26693be42f9SDavide Caratti 		qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
2672d550dbaSDavide Caratti end:
26893be42f9SDavide Caratti 	return ret;
2694bba3925SPatrick McHardy }
2704bba3925SPatrick McHardy 
2712d550dbaSDavide Caratti static void tcf_police_cleanup(struct tc_action *a)
2722d550dbaSDavide Caratti {
2732d550dbaSDavide Caratti 	struct tcf_police *police = to_police(a);
2742d550dbaSDavide Caratti 	struct tcf_police_params *p;
2752d550dbaSDavide Caratti 
2762d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params, 1);
2772d550dbaSDavide Caratti 	if (p)
2782d550dbaSDavide Caratti 		kfree_rcu(p, rcu);
2792d550dbaSDavide Caratti }
2802d550dbaSDavide Caratti 
28112f02b6bSPieter Jansen van Vuuren static void tcf_police_stats_update(struct tc_action *a,
28212f02b6bSPieter Jansen van Vuuren 				    u64 bytes, u32 packets,
28312f02b6bSPieter Jansen van Vuuren 				    u64 lastuse, bool hw)
28412f02b6bSPieter Jansen van Vuuren {
28512f02b6bSPieter Jansen van Vuuren 	struct tcf_police *police = to_police(a);
28612f02b6bSPieter Jansen van Vuuren 	struct tcf_t *tm = &police->tcf_tm;
28712f02b6bSPieter Jansen van Vuuren 
28812f02b6bSPieter Jansen van Vuuren 	_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
28912f02b6bSPieter Jansen van Vuuren 	if (hw)
29012f02b6bSPieter Jansen van Vuuren 		_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
29112f02b6bSPieter Jansen van Vuuren 				   bytes, packets);
29212f02b6bSPieter Jansen van Vuuren 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
29312f02b6bSPieter Jansen van Vuuren }
29412f02b6bSPieter Jansen van Vuuren 
2952ac06347SJamal Hadi Salim static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
2965a7a5555SJamal Hadi Salim 			       int bind, int ref)
2974bba3925SPatrick McHardy {
29827a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
299a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
3002d550dbaSDavide Caratti 	struct tcf_police_params *p;
3010f04cfd0SJeff Mahoney 	struct tc_police opt = {
3020f04cfd0SJeff Mahoney 		.index = police->tcf_index,
303036bb443SVlad Buslov 		.refcnt = refcount_read(&police->tcf_refcnt) - ref,
304036bb443SVlad Buslov 		.bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
3050f04cfd0SJeff Mahoney 	};
3063d3ed181SJamal Hadi Salim 	struct tcf_t t;
3074bba3925SPatrick McHardy 
308e329bc42SVlad Buslov 	spin_lock_bh(&police->tcf_lock);
309e329bc42SVlad Buslov 	opt.action = police->tcf_action;
3102d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params,
3112d550dbaSDavide Caratti 				      lockdep_is_held(&police->tcf_lock));
3122d550dbaSDavide Caratti 	opt.mtu = p->tcfp_mtu;
3132d550dbaSDavide Caratti 	opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
3142d550dbaSDavide Caratti 	if (p->rate_present)
3152d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.rate, &p->rate);
3162d550dbaSDavide Caratti 	if (p->peak_present)
3172d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.peakrate, &p->peak);
3181b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
3191b34ec43SDavid S. Miller 		goto nla_put_failure;
3202d550dbaSDavide Caratti 	if (p->tcfp_result &&
3212d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
3221b34ec43SDavid S. Miller 		goto nla_put_failure;
3232d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate &&
3242d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
3251b34ec43SDavid S. Miller 		goto nla_put_failure;
3263d3ed181SJamal Hadi Salim 
3273d3ed181SJamal Hadi Salim 	t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
3283d3ed181SJamal Hadi Salim 	t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
32953eb440fSJamal Hadi Salim 	t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
3303d3ed181SJamal Hadi Salim 	t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
3313d3ed181SJamal Hadi Salim 	if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
3323d3ed181SJamal Hadi Salim 		goto nla_put_failure;
333e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
3343d3ed181SJamal Hadi Salim 
3354bba3925SPatrick McHardy 	return skb->len;
3364bba3925SPatrick McHardy 
3377ba699c6SPatrick McHardy nla_put_failure:
338e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
339dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
3404bba3925SPatrick McHardy 	return -1;
3414bba3925SPatrick McHardy }
3424bba3925SPatrick McHardy 
343f061b48cSCong Wang static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
344ddf97ccdSWANG Cong {
345ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
346ddf97ccdSWANG Cong 
34765a206c0SChris Mi 	return tcf_idr_search(tn, a, index);
348ddf97ccdSWANG Cong }
349ddf97ccdSWANG Cong 
3504bba3925SPatrick McHardy MODULE_AUTHOR("Alexey Kuznetsov");
3514bba3925SPatrick McHardy MODULE_DESCRIPTION("Policing actions");
3524bba3925SPatrick McHardy MODULE_LICENSE("GPL");
3534bba3925SPatrick McHardy 
3544bba3925SPatrick McHardy static struct tc_action_ops act_police_ops = {
3554bba3925SPatrick McHardy 	.kind		=	"police",
356eddd2cf1SEli Cohen 	.id		=	TCA_ID_POLICE,
3574bba3925SPatrick McHardy 	.owner		=	THIS_MODULE,
35812f02b6bSPieter Jansen van Vuuren 	.stats_update	=	tcf_police_stats_update,
3592ac06347SJamal Hadi Salim 	.act		=	tcf_police_act,
3602ac06347SJamal Hadi Salim 	.dump		=	tcf_police_dump,
3612ac06347SJamal Hadi Salim 	.init		=	tcf_police_init,
3622ac06347SJamal Hadi Salim 	.walk		=	tcf_police_walker,
363ddf97ccdSWANG Cong 	.lookup		=	tcf_police_search,
3642d550dbaSDavide Caratti 	.cleanup	=	tcf_police_cleanup,
365a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_police),
366ddf97ccdSWANG Cong };
367ddf97ccdSWANG Cong 
368ddf97ccdSWANG Cong static __net_init int police_init_net(struct net *net)
369ddf97ccdSWANG Cong {
370ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
371ddf97ccdSWANG Cong 
372c7e460ceSCong Wang 	return tc_action_net_init(tn, &act_police_ops);
373ddf97ccdSWANG Cong }
374ddf97ccdSWANG Cong 
375039af9c6SCong Wang static void __net_exit police_exit_net(struct list_head *net_list)
376ddf97ccdSWANG Cong {
377039af9c6SCong Wang 	tc_action_net_exit(net_list, police_net_id);
378ddf97ccdSWANG Cong }
379ddf97ccdSWANG Cong 
380ddf97ccdSWANG Cong static struct pernet_operations police_net_ops = {
381ddf97ccdSWANG Cong 	.init = police_init_net,
382039af9c6SCong Wang 	.exit_batch = police_exit_net,
383ddf97ccdSWANG Cong 	.id   = &police_net_id,
384ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
3854bba3925SPatrick McHardy };
3864bba3925SPatrick McHardy 
3875a7a5555SJamal Hadi Salim static int __init police_init_module(void)
3884bba3925SPatrick McHardy {
389ddf97ccdSWANG Cong 	return tcf_register_action(&act_police_ops, &police_net_ops);
3904bba3925SPatrick McHardy }
3914bba3925SPatrick McHardy 
3925a7a5555SJamal Hadi Salim static void __exit police_cleanup_module(void)
3934bba3925SPatrick McHardy {
394ddf97ccdSWANG Cong 	tcf_unregister_action(&act_police_ops, &police_net_ops);
3954bba3925SPatrick McHardy }
3964bba3925SPatrick McHardy 
3974bba3925SPatrick McHardy module_init(police_init_module);
3984bba3925SPatrick McHardy module_exit(police_cleanup_module);
399