xref: /openbmc/linux/net/sched/act_police.c (revision 12f02b6b1548367fb548e61105fac6778c1a9173)
14bba3925SPatrick McHardy /*
20c6965ddSJiri Pirko  * net/sched/act_police.c	Input police filter
34bba3925SPatrick McHardy  *
44bba3925SPatrick McHardy  *		This program is free software; you can redistribute it and/or
54bba3925SPatrick McHardy  *		modify it under the terms of the GNU General Public License
64bba3925SPatrick McHardy  *		as published by the Free Software Foundation; either version
74bba3925SPatrick McHardy  *		2 of the License, or (at your option) any later version.
84bba3925SPatrick McHardy  *
94bba3925SPatrick McHardy  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
104bba3925SPatrick McHardy  * 		J Hadi Salim (action changes)
114bba3925SPatrick McHardy  */
124bba3925SPatrick McHardy 
134bba3925SPatrick McHardy #include <linux/module.h>
144bba3925SPatrick McHardy #include <linux/types.h>
154bba3925SPatrick McHardy #include <linux/kernel.h>
164bba3925SPatrick McHardy #include <linux/string.h>
174bba3925SPatrick McHardy #include <linux/errno.h>
184bba3925SPatrick McHardy #include <linux/skbuff.h>
194bba3925SPatrick McHardy #include <linux/rtnetlink.h>
204bba3925SPatrick McHardy #include <linux/init.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
224bba3925SPatrick McHardy #include <net/act_api.h>
23dc5fc579SArnaldo Carvalho de Melo #include <net/netlink.h>
24d6124d6bSDavide Caratti #include <net/pkt_cls.h>
25fa762da9SPieter Jansen van Vuuren #include <net/tc_act/tc_police.h>
261e9b3d53SPatrick McHardy 
274bba3925SPatrick McHardy /* Each policer is serialized by its individual spinlock */
284bba3925SPatrick McHardy 
29c7d03a00SAlexey Dobriyan static unsigned int police_net_id;
30a85a970aSWANG Cong static struct tc_action_ops act_police_ops;
31ddf97ccdSWANG Cong 
322ac06347SJamal Hadi Salim static int tcf_police_walker(struct net *net, struct sk_buff *skb,
33ddf97ccdSWANG Cong 				 struct netlink_callback *cb, int type,
3441780105SAlexander Aring 				 const struct tc_action_ops *ops,
3541780105SAlexander Aring 				 struct netlink_ext_ack *extack)
364bba3925SPatrick McHardy {
37ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
384bba3925SPatrick McHardy 
39b3620145SAlexander Aring 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
404bba3925SPatrick McHardy }
414bba3925SPatrick McHardy 
4253b2bf3fSPatrick McHardy static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
4353b2bf3fSPatrick McHardy 	[TCA_POLICE_RATE]	= { .len = TC_RTAB_SIZE },
4453b2bf3fSPatrick McHardy 	[TCA_POLICE_PEAKRATE]	= { .len = TC_RTAB_SIZE },
4553b2bf3fSPatrick McHardy 	[TCA_POLICE_AVRATE]	= { .type = NLA_U32 },
4653b2bf3fSPatrick McHardy 	[TCA_POLICE_RESULT]	= { .type = NLA_U32 },
4753b2bf3fSPatrick McHardy };
4853b2bf3fSPatrick McHardy 
492ac06347SJamal Hadi Salim static int tcf_police_init(struct net *net, struct nlattr *nla,
50a85a970aSWANG Cong 			       struct nlattr *est, struct tc_action **a,
51789871bbSVlad Buslov 			       int ovr, int bind, bool rtnl_held,
5285d0966fSDavide Caratti 			       struct tcf_proto *tp,
53589dad6dSAlexander Aring 			       struct netlink_ext_ack *extack)
544bba3925SPatrick McHardy {
55fd6d4338SDavide Caratti 	int ret = 0, tcfp_result = TC_ACT_OK, err, size;
567ba699c6SPatrick McHardy 	struct nlattr *tb[TCA_POLICE_MAX + 1];
57d6124d6bSDavide Caratti 	struct tcf_chain *goto_ch = NULL;
584bba3925SPatrick McHardy 	struct tc_police *parm;
59e9ce1cd3SDavid S. Miller 	struct tcf_police *police;
604bba3925SPatrick McHardy 	struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
61ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
622d550dbaSDavide Caratti 	struct tcf_police_params *new;
630852e455SWANG Cong 	bool exists = false;
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]);
800190c1d4SVlad Buslov 	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
810190c1d4SVlad Buslov 	if (err < 0)
820190c1d4SVlad Buslov 		return err;
830190c1d4SVlad Buslov 	exists = err;
840852e455SWANG Cong 	if (exists && bind)
850852e455SWANG Cong 		return 0;
860852e455SWANG Cong 
870852e455SWANG Cong 	if (!exists) {
8865a206c0SChris Mi 		ret = tcf_idr_create(tn, parm->index, NULL, a,
8993be42f9SDavide Caratti 				     &act_police_ops, bind, true);
900190c1d4SVlad Buslov 		if (ret) {
910190c1d4SVlad Buslov 			tcf_idr_cleanup(tn, parm->index);
92a03e6fe5SWANG Cong 			return ret;
930190c1d4SVlad Buslov 		}
94a03e6fe5SWANG Cong 		ret = ACT_P_CREATED;
95484afd1bSDavide Caratti 		spin_lock_init(&(to_police(*a)->tcfp_lock));
964e8ddd7fSVlad Buslov 	} else if (!ovr) {
9765a206c0SChris Mi 		tcf_idr_release(*a, bind);
980852e455SWANG Cong 		return -EEXIST;
99e9ce1cd3SDavid S. Miller 	}
100d6124d6bSDavide Caratti 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
101d6124d6bSDavide Caratti 	if (err < 0)
102d6124d6bSDavide Caratti 		goto release_idr;
1034bba3925SPatrick McHardy 
104a85a970aSWANG Cong 	police = to_police(*a);
1054bba3925SPatrick McHardy 	if (parm->rate.rate) {
1064bba3925SPatrick McHardy 		err = -ENOMEM;
107e9bc3fa2SAlexander Aring 		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
1084bba3925SPatrick McHardy 		if (R_tab == NULL)
1094bba3925SPatrick McHardy 			goto failure;
110c1b56878SStephen Hemminger 
1114bba3925SPatrick McHardy 		if (parm->peakrate.rate) {
1124bba3925SPatrick McHardy 			P_tab = qdisc_get_rtab(&parm->peakrate,
113e9bc3fa2SAlexander Aring 					       tb[TCA_POLICE_PEAKRATE], NULL);
11471bcb09aSStephen Hemminger 			if (P_tab == NULL)
1154bba3925SPatrick McHardy 				goto failure;
1164bba3925SPatrick McHardy 		}
1174bba3925SPatrick McHardy 	}
11871bcb09aSStephen Hemminger 
11971bcb09aSStephen Hemminger 	if (est) {
12093be42f9SDavide Caratti 		err = gen_replace_estimator(&police->tcf_bstats,
12193be42f9SDavide Caratti 					    police->common.cpu_bstats,
12271bcb09aSStephen Hemminger 					    &police->tcf_rate_est,
123edb09eb1SEric Dumazet 					    &police->tcf_lock,
124edb09eb1SEric Dumazet 					    NULL, est);
12571bcb09aSStephen Hemminger 		if (err)
12674030603SWANG Cong 			goto failure;
127a883bf56SJarek Poplawski 	} else if (tb[TCA_POLICE_AVRATE] &&
128a883bf56SJarek Poplawski 		   (ret == ACT_P_CREATED ||
1291c0d32fdSEric Dumazet 		    !gen_estimator_active(&police->tcf_rate_est))) {
130a883bf56SJarek Poplawski 		err = -EINVAL;
13174030603SWANG Cong 		goto failure;
13271bcb09aSStephen Hemminger 	}
13371bcb09aSStephen Hemminger 
134fd6d4338SDavide Caratti 	if (tb[TCA_POLICE_RESULT]) {
135fd6d4338SDavide Caratti 		tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
136fd6d4338SDavide Caratti 		if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
137fd6d4338SDavide Caratti 			NL_SET_ERR_MSG(extack,
138fd6d4338SDavide Caratti 				       "goto chain not allowed on fallback");
139fd6d4338SDavide Caratti 			err = -EINVAL;
140fd6d4338SDavide Caratti 			goto failure;
141fd6d4338SDavide Caratti 		}
142fd6d4338SDavide Caratti 	}
143fd6d4338SDavide Caratti 
1442d550dbaSDavide Caratti 	new = kzalloc(sizeof(*new), GFP_KERNEL);
1452d550dbaSDavide Caratti 	if (unlikely(!new)) {
1462d550dbaSDavide Caratti 		err = -ENOMEM;
1472d550dbaSDavide Caratti 		goto failure;
1482d550dbaSDavide Caratti 	}
1492d550dbaSDavide Caratti 
15071bcb09aSStephen Hemminger 	/* No failure allowed after this point */
151fd6d4338SDavide Caratti 	new->tcfp_result = tcfp_result;
1522d550dbaSDavide Caratti 	new->tcfp_mtu = parm->mtu;
1532d550dbaSDavide Caratti 	if (!new->tcfp_mtu) {
1542d550dbaSDavide Caratti 		new->tcfp_mtu = ~0;
155c6d14ff1SJiri Pirko 		if (R_tab)
1562d550dbaSDavide Caratti 			new->tcfp_mtu = 255 << R_tab->rate.cell_log;
1574bba3925SPatrick McHardy 	}
158c6d14ff1SJiri Pirko 	if (R_tab) {
1592d550dbaSDavide Caratti 		new->rate_present = true;
1602d550dbaSDavide Caratti 		psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
161c6d14ff1SJiri Pirko 		qdisc_put_rtab(R_tab);
162c6d14ff1SJiri Pirko 	} else {
1632d550dbaSDavide Caratti 		new->rate_present = false;
164c6d14ff1SJiri Pirko 	}
165c6d14ff1SJiri Pirko 	if (P_tab) {
1662d550dbaSDavide Caratti 		new->peak_present = true;
1672d550dbaSDavide Caratti 		psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
168c6d14ff1SJiri Pirko 		qdisc_put_rtab(P_tab);
169c6d14ff1SJiri Pirko 	} else {
1702d550dbaSDavide Caratti 		new->peak_present = false;
1714bba3925SPatrick McHardy 	}
1724bba3925SPatrick McHardy 
1732d550dbaSDavide Caratti 	new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
174f2cbd485SDavide Caratti 	if (new->peak_present)
1752d550dbaSDavide Caratti 		new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
1762d550dbaSDavide Caratti 							 new->tcfp_mtu);
1774bba3925SPatrick McHardy 
1787ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_AVRATE])
1792d550dbaSDavide Caratti 		new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
1804bba3925SPatrick McHardy 
1812d550dbaSDavide Caratti 	spin_lock_bh(&police->tcf_lock);
182f2cbd485SDavide Caratti 	spin_lock_bh(&police->tcfp_lock);
183f2cbd485SDavide Caratti 	police->tcfp_t_c = ktime_get_ns();
184f2cbd485SDavide Caratti 	police->tcfp_toks = new->tcfp_burst;
185f2cbd485SDavide Caratti 	if (new->peak_present)
186f2cbd485SDavide Caratti 		police->tcfp_ptoks = new->tcfp_mtu_ptoks;
187f2cbd485SDavide Caratti 	spin_unlock_bh(&police->tcfp_lock);
188d6124d6bSDavide Caratti 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1892d550dbaSDavide Caratti 	rcu_swap_protected(police->params,
1902d550dbaSDavide Caratti 			   new,
1912d550dbaSDavide Caratti 			   lockdep_is_held(&police->tcf_lock));
192e9ce1cd3SDavid S. Miller 	spin_unlock_bh(&police->tcf_lock);
1934bba3925SPatrick McHardy 
194d6124d6bSDavide Caratti 	if (goto_ch)
195d6124d6bSDavide Caratti 		tcf_chain_put_by_act(goto_ch);
1962d550dbaSDavide Caratti 	if (new)
1972d550dbaSDavide Caratti 		kfree_rcu(new, rcu);
1982d550dbaSDavide Caratti 
1992d550dbaSDavide Caratti 	if (ret == ACT_P_CREATED)
20065a206c0SChris Mi 		tcf_idr_insert(tn, *a);
2014bba3925SPatrick McHardy 	return ret;
2024bba3925SPatrick McHardy 
2034bba3925SPatrick McHardy failure:
20471bcb09aSStephen Hemminger 	qdisc_put_rtab(P_tab);
20571bcb09aSStephen Hemminger 	qdisc_put_rtab(R_tab);
206d6124d6bSDavide Caratti 	if (goto_ch)
207d6124d6bSDavide Caratti 		tcf_chain_put_by_act(goto_ch);
208d6124d6bSDavide Caratti release_idr:
2095bf7f818SDavide Caratti 	tcf_idr_release(*a, bind);
2104bba3925SPatrick McHardy 	return err;
2114bba3925SPatrick McHardy }
2124bba3925SPatrick McHardy 
2132ac06347SJamal Hadi Salim static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
2144bba3925SPatrick McHardy 			  struct tcf_result *res)
2154bba3925SPatrick McHardy {
216a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
2172d550dbaSDavide Caratti 	struct tcf_police_params *p;
21893be42f9SDavide Caratti 	s64 now, toks, ptoks = 0;
21993be42f9SDavide Caratti 	int ret;
22093be42f9SDavide Caratti 
22193be42f9SDavide Caratti 	tcf_lastuse_update(&police->tcf_tm);
22293be42f9SDavide Caratti 	bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
2234bba3925SPatrick McHardy 
2242d550dbaSDavide Caratti 	ret = READ_ONCE(police->tcf_action);
2252d550dbaSDavide Caratti 	p = rcu_dereference_bh(police->params);
2262d550dbaSDavide Caratti 
2272d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate) {
2281c0d32fdSEric Dumazet 		struct gnet_stats_rate_est64 sample;
2291c0d32fdSEric Dumazet 
2301c0d32fdSEric Dumazet 		if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
2312d550dbaSDavide Caratti 		    sample.bps >= p->tcfp_ewma_rate)
23293be42f9SDavide Caratti 			goto inc_overlimits;
2334bba3925SPatrick McHardy 	}
2344bba3925SPatrick McHardy 
2352d550dbaSDavide Caratti 	if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
2362d550dbaSDavide Caratti 		if (!p->rate_present) {
2372d550dbaSDavide Caratti 			ret = p->tcfp_result;
2382d550dbaSDavide Caratti 			goto end;
2394bba3925SPatrick McHardy 		}
2404bba3925SPatrick McHardy 
241d2de875cSEric Dumazet 		now = ktime_get_ns();
242f2cbd485SDavide Caratti 		spin_lock_bh(&police->tcfp_lock);
243f2cbd485SDavide Caratti 		toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
2442d550dbaSDavide Caratti 		if (p->peak_present) {
245f2cbd485SDavide Caratti 			ptoks = toks + police->tcfp_ptoks;
2462d550dbaSDavide Caratti 			if (ptoks > p->tcfp_mtu_ptoks)
2472d550dbaSDavide Caratti 				ptoks = p->tcfp_mtu_ptoks;
2482d550dbaSDavide Caratti 			ptoks -= (s64)psched_l2t_ns(&p->peak,
249c6d14ff1SJiri Pirko 						    qdisc_pkt_len(skb));
2504bba3925SPatrick McHardy 		}
251f2cbd485SDavide Caratti 		toks += police->tcfp_toks;
2522d550dbaSDavide Caratti 		if (toks > p->tcfp_burst)
2532d550dbaSDavide Caratti 			toks = p->tcfp_burst;
2542d550dbaSDavide Caratti 		toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
2554bba3925SPatrick McHardy 		if ((toks|ptoks) >= 0) {
256f2cbd485SDavide Caratti 			police->tcfp_t_c = now;
257f2cbd485SDavide Caratti 			police->tcfp_toks = toks;
258f2cbd485SDavide Caratti 			police->tcfp_ptoks = ptoks;
259f2cbd485SDavide Caratti 			spin_unlock_bh(&police->tcfp_lock);
2602d550dbaSDavide Caratti 			ret = p->tcfp_result;
26193be42f9SDavide Caratti 			goto inc_drops;
2624bba3925SPatrick McHardy 		}
263f2cbd485SDavide Caratti 		spin_unlock_bh(&police->tcfp_lock);
2644bba3925SPatrick McHardy 	}
2654bba3925SPatrick McHardy 
26693be42f9SDavide Caratti inc_overlimits:
26793be42f9SDavide Caratti 	qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
26893be42f9SDavide Caratti inc_drops:
26993be42f9SDavide Caratti 	if (ret == TC_ACT_SHOT)
27093be42f9SDavide Caratti 		qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
2712d550dbaSDavide Caratti end:
27293be42f9SDavide Caratti 	return ret;
2734bba3925SPatrick McHardy }
2744bba3925SPatrick McHardy 
2752d550dbaSDavide Caratti static void tcf_police_cleanup(struct tc_action *a)
2762d550dbaSDavide Caratti {
2772d550dbaSDavide Caratti 	struct tcf_police *police = to_police(a);
2782d550dbaSDavide Caratti 	struct tcf_police_params *p;
2792d550dbaSDavide Caratti 
2802d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params, 1);
2812d550dbaSDavide Caratti 	if (p)
2822d550dbaSDavide Caratti 		kfree_rcu(p, rcu);
2832d550dbaSDavide Caratti }
2842d550dbaSDavide Caratti 
285*12f02b6bSPieter Jansen van Vuuren static void tcf_police_stats_update(struct tc_action *a,
286*12f02b6bSPieter Jansen van Vuuren 				    u64 bytes, u32 packets,
287*12f02b6bSPieter Jansen van Vuuren 				    u64 lastuse, bool hw)
288*12f02b6bSPieter Jansen van Vuuren {
289*12f02b6bSPieter Jansen van Vuuren 	struct tcf_police *police = to_police(a);
290*12f02b6bSPieter Jansen van Vuuren 	struct tcf_t *tm = &police->tcf_tm;
291*12f02b6bSPieter Jansen van Vuuren 
292*12f02b6bSPieter Jansen van Vuuren 	_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
293*12f02b6bSPieter Jansen van Vuuren 	if (hw)
294*12f02b6bSPieter Jansen van Vuuren 		_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
295*12f02b6bSPieter Jansen van Vuuren 				   bytes, packets);
296*12f02b6bSPieter Jansen van Vuuren 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
297*12f02b6bSPieter Jansen van Vuuren }
298*12f02b6bSPieter Jansen van Vuuren 
2992ac06347SJamal Hadi Salim static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
3005a7a5555SJamal Hadi Salim 			       int bind, int ref)
3014bba3925SPatrick McHardy {
30227a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
303a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
3042d550dbaSDavide Caratti 	struct tcf_police_params *p;
3050f04cfd0SJeff Mahoney 	struct tc_police opt = {
3060f04cfd0SJeff Mahoney 		.index = police->tcf_index,
307036bb443SVlad Buslov 		.refcnt = refcount_read(&police->tcf_refcnt) - ref,
308036bb443SVlad Buslov 		.bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
3090f04cfd0SJeff Mahoney 	};
3103d3ed181SJamal Hadi Salim 	struct tcf_t t;
3114bba3925SPatrick McHardy 
312e329bc42SVlad Buslov 	spin_lock_bh(&police->tcf_lock);
313e329bc42SVlad Buslov 	opt.action = police->tcf_action;
3142d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params,
3152d550dbaSDavide Caratti 				      lockdep_is_held(&police->tcf_lock));
3162d550dbaSDavide Caratti 	opt.mtu = p->tcfp_mtu;
3172d550dbaSDavide Caratti 	opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
3182d550dbaSDavide Caratti 	if (p->rate_present)
3192d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.rate, &p->rate);
3202d550dbaSDavide Caratti 	if (p->peak_present)
3212d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.peakrate, &p->peak);
3221b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
3231b34ec43SDavid S. Miller 		goto nla_put_failure;
3242d550dbaSDavide Caratti 	if (p->tcfp_result &&
3252d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
3261b34ec43SDavid S. Miller 		goto nla_put_failure;
3272d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate &&
3282d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
3291b34ec43SDavid S. Miller 		goto nla_put_failure;
3303d3ed181SJamal Hadi Salim 
3313d3ed181SJamal Hadi Salim 	t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
3323d3ed181SJamal Hadi Salim 	t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
33353eb440fSJamal Hadi Salim 	t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
3343d3ed181SJamal Hadi Salim 	t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
3353d3ed181SJamal Hadi Salim 	if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
3363d3ed181SJamal Hadi Salim 		goto nla_put_failure;
337e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
3383d3ed181SJamal Hadi Salim 
3394bba3925SPatrick McHardy 	return skb->len;
3404bba3925SPatrick McHardy 
3417ba699c6SPatrick McHardy nla_put_failure:
342e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
343dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
3444bba3925SPatrick McHardy 	return -1;
3454bba3925SPatrick McHardy }
3464bba3925SPatrick McHardy 
347f061b48cSCong Wang static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
348ddf97ccdSWANG Cong {
349ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
350ddf97ccdSWANG Cong 
35165a206c0SChris Mi 	return tcf_idr_search(tn, a, index);
352ddf97ccdSWANG Cong }
353ddf97ccdSWANG Cong 
3544bba3925SPatrick McHardy MODULE_AUTHOR("Alexey Kuznetsov");
3554bba3925SPatrick McHardy MODULE_DESCRIPTION("Policing actions");
3564bba3925SPatrick McHardy MODULE_LICENSE("GPL");
3574bba3925SPatrick McHardy 
3584bba3925SPatrick McHardy static struct tc_action_ops act_police_ops = {
3594bba3925SPatrick McHardy 	.kind		=	"police",
360eddd2cf1SEli Cohen 	.id		=	TCA_ID_POLICE,
3614bba3925SPatrick McHardy 	.owner		=	THIS_MODULE,
362*12f02b6bSPieter Jansen van Vuuren 	.stats_update	=	tcf_police_stats_update,
3632ac06347SJamal Hadi Salim 	.act		=	tcf_police_act,
3642ac06347SJamal Hadi Salim 	.dump		=	tcf_police_dump,
3652ac06347SJamal Hadi Salim 	.init		=	tcf_police_init,
3662ac06347SJamal Hadi Salim 	.walk		=	tcf_police_walker,
367ddf97ccdSWANG Cong 	.lookup		=	tcf_police_search,
3682d550dbaSDavide Caratti 	.cleanup	=	tcf_police_cleanup,
369a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_police),
370ddf97ccdSWANG Cong };
371ddf97ccdSWANG Cong 
372ddf97ccdSWANG Cong static __net_init int police_init_net(struct net *net)
373ddf97ccdSWANG Cong {
374ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
375ddf97ccdSWANG Cong 
376c7e460ceSCong Wang 	return tc_action_net_init(tn, &act_police_ops);
377ddf97ccdSWANG Cong }
378ddf97ccdSWANG Cong 
379039af9c6SCong Wang static void __net_exit police_exit_net(struct list_head *net_list)
380ddf97ccdSWANG Cong {
381039af9c6SCong Wang 	tc_action_net_exit(net_list, police_net_id);
382ddf97ccdSWANG Cong }
383ddf97ccdSWANG Cong 
384ddf97ccdSWANG Cong static struct pernet_operations police_net_ops = {
385ddf97ccdSWANG Cong 	.init = police_init_net,
386039af9c6SCong Wang 	.exit_batch = police_exit_net,
387ddf97ccdSWANG Cong 	.id   = &police_net_id,
388ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
3894bba3925SPatrick McHardy };
3904bba3925SPatrick McHardy 
3915a7a5555SJamal Hadi Salim static int __init police_init_module(void)
3924bba3925SPatrick McHardy {
393ddf97ccdSWANG Cong 	return tcf_register_action(&act_police_ops, &police_net_ops);
3944bba3925SPatrick McHardy }
3954bba3925SPatrick McHardy 
3965a7a5555SJamal Hadi Salim static void __exit police_cleanup_module(void)
3974bba3925SPatrick McHardy {
398ddf97ccdSWANG Cong 	tcf_unregister_action(&act_police_ops, &police_net_ops);
3994bba3925SPatrick McHardy }
4004bba3925SPatrick McHardy 
4014bba3925SPatrick McHardy module_init(police_init_module);
4024bba3925SPatrick McHardy module_exit(police_cleanup_module);
403