xref: /openbmc/linux/net/sched/act_police.c (revision 981471bd3abf4d572097645d765391533aac327d)
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 },
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;
607be8ef2cSDmytro Linkin 	u32 index;
614bba3925SPatrick McHardy 
62cee63723SPatrick McHardy 	if (nla == NULL)
634bba3925SPatrick McHardy 		return -EINVAL;
644bba3925SPatrick McHardy 
658cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
668cb08174SJohannes Berg 					  police_policy, NULL);
67cee63723SPatrick McHardy 	if (err < 0)
68cee63723SPatrick McHardy 		return err;
69cee63723SPatrick McHardy 
707ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_TBF] == NULL)
711e9b3d53SPatrick McHardy 		return -EINVAL;
727ba699c6SPatrick McHardy 	size = nla_len(tb[TCA_POLICE_TBF]);
731e9b3d53SPatrick McHardy 	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
744bba3925SPatrick McHardy 		return -EINVAL;
754bba3925SPatrick McHardy 
760852e455SWANG Cong 	parm = nla_data(tb[TCA_POLICE_TBF]);
777be8ef2cSDmytro Linkin 	index = parm->index;
787be8ef2cSDmytro Linkin 	err = tcf_idr_check_alloc(tn, &index, a, bind);
790190c1d4SVlad Buslov 	if (err < 0)
800190c1d4SVlad Buslov 		return err;
810190c1d4SVlad Buslov 	exists = err;
820852e455SWANG Cong 	if (exists && bind)
830852e455SWANG Cong 		return 0;
840852e455SWANG Cong 
850852e455SWANG Cong 	if (!exists) {
867be8ef2cSDmytro Linkin 		ret = tcf_idr_create(tn, index, NULL, a,
8793be42f9SDavide Caratti 				     &act_police_ops, bind, true);
880190c1d4SVlad Buslov 		if (ret) {
897be8ef2cSDmytro Linkin 			tcf_idr_cleanup(tn, index);
90a03e6fe5SWANG Cong 			return ret;
910190c1d4SVlad Buslov 		}
92a03e6fe5SWANG Cong 		ret = ACT_P_CREATED;
93484afd1bSDavide Caratti 		spin_lock_init(&(to_police(*a)->tcfp_lock));
944e8ddd7fSVlad Buslov 	} else if (!ovr) {
9565a206c0SChris Mi 		tcf_idr_release(*a, bind);
960852e455SWANG Cong 		return -EEXIST;
97e9ce1cd3SDavid S. Miller 	}
98d6124d6bSDavide Caratti 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
99d6124d6bSDavide Caratti 	if (err < 0)
100d6124d6bSDavide Caratti 		goto release_idr;
1014bba3925SPatrick McHardy 
102a85a970aSWANG Cong 	police = to_police(*a);
1034bba3925SPatrick McHardy 	if (parm->rate.rate) {
1044bba3925SPatrick McHardy 		err = -ENOMEM;
105e9bc3fa2SAlexander Aring 		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
1064bba3925SPatrick McHardy 		if (R_tab == NULL)
1074bba3925SPatrick McHardy 			goto failure;
108c1b56878SStephen Hemminger 
1094bba3925SPatrick McHardy 		if (parm->peakrate.rate) {
1104bba3925SPatrick McHardy 			P_tab = qdisc_get_rtab(&parm->peakrate,
111e9bc3fa2SAlexander Aring 					       tb[TCA_POLICE_PEAKRATE], NULL);
11271bcb09aSStephen Hemminger 			if (P_tab == NULL)
1134bba3925SPatrick McHardy 				goto failure;
1144bba3925SPatrick McHardy 		}
1154bba3925SPatrick McHardy 	}
11671bcb09aSStephen Hemminger 
11771bcb09aSStephen Hemminger 	if (est) {
11893be42f9SDavide Caratti 		err = gen_replace_estimator(&police->tcf_bstats,
11993be42f9SDavide Caratti 					    police->common.cpu_bstats,
12071bcb09aSStephen Hemminger 					    &police->tcf_rate_est,
121edb09eb1SEric Dumazet 					    &police->tcf_lock,
122edb09eb1SEric Dumazet 					    NULL, est);
12371bcb09aSStephen Hemminger 		if (err)
12474030603SWANG Cong 			goto failure;
125a883bf56SJarek Poplawski 	} else if (tb[TCA_POLICE_AVRATE] &&
126a883bf56SJarek Poplawski 		   (ret == ACT_P_CREATED ||
1271c0d32fdSEric Dumazet 		    !gen_estimator_active(&police->tcf_rate_est))) {
128a883bf56SJarek Poplawski 		err = -EINVAL;
12974030603SWANG Cong 		goto failure;
13071bcb09aSStephen Hemminger 	}
13171bcb09aSStephen Hemminger 
132fd6d4338SDavide Caratti 	if (tb[TCA_POLICE_RESULT]) {
133fd6d4338SDavide Caratti 		tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
134fd6d4338SDavide Caratti 		if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
135fd6d4338SDavide Caratti 			NL_SET_ERR_MSG(extack,
136fd6d4338SDavide Caratti 				       "goto chain not allowed on fallback");
137fd6d4338SDavide Caratti 			err = -EINVAL;
138fd6d4338SDavide Caratti 			goto failure;
139fd6d4338SDavide Caratti 		}
140fd6d4338SDavide Caratti 	}
141fd6d4338SDavide Caratti 
1422d550dbaSDavide Caratti 	new = kzalloc(sizeof(*new), GFP_KERNEL);
1432d550dbaSDavide Caratti 	if (unlikely(!new)) {
1442d550dbaSDavide Caratti 		err = -ENOMEM;
1452d550dbaSDavide Caratti 		goto failure;
1462d550dbaSDavide Caratti 	}
1472d550dbaSDavide Caratti 
14871bcb09aSStephen Hemminger 	/* No failure allowed after this point */
149fd6d4338SDavide Caratti 	new->tcfp_result = tcfp_result;
1502d550dbaSDavide Caratti 	new->tcfp_mtu = parm->mtu;
1512d550dbaSDavide Caratti 	if (!new->tcfp_mtu) {
1522d550dbaSDavide Caratti 		new->tcfp_mtu = ~0;
153c6d14ff1SJiri Pirko 		if (R_tab)
1542d550dbaSDavide Caratti 			new->tcfp_mtu = 255 << R_tab->rate.cell_log;
1554bba3925SPatrick McHardy 	}
156c6d14ff1SJiri Pirko 	if (R_tab) {
1572d550dbaSDavide Caratti 		new->rate_present = true;
1582d550dbaSDavide Caratti 		psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
159c6d14ff1SJiri Pirko 		qdisc_put_rtab(R_tab);
160c6d14ff1SJiri Pirko 	} else {
1612d550dbaSDavide Caratti 		new->rate_present = false;
162c6d14ff1SJiri Pirko 	}
163c6d14ff1SJiri Pirko 	if (P_tab) {
1642d550dbaSDavide Caratti 		new->peak_present = true;
1652d550dbaSDavide Caratti 		psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
166c6d14ff1SJiri Pirko 		qdisc_put_rtab(P_tab);
167c6d14ff1SJiri Pirko 	} else {
1682d550dbaSDavide Caratti 		new->peak_present = false;
1694bba3925SPatrick McHardy 	}
1704bba3925SPatrick McHardy 
1712d550dbaSDavide Caratti 	new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
172f2cbd485SDavide Caratti 	if (new->peak_present)
1732d550dbaSDavide Caratti 		new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
1742d550dbaSDavide Caratti 							 new->tcfp_mtu);
1754bba3925SPatrick McHardy 
1767ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_AVRATE])
1772d550dbaSDavide Caratti 		new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
1784bba3925SPatrick McHardy 
1792d550dbaSDavide Caratti 	spin_lock_bh(&police->tcf_lock);
180f2cbd485SDavide Caratti 	spin_lock_bh(&police->tcfp_lock);
181f2cbd485SDavide Caratti 	police->tcfp_t_c = ktime_get_ns();
182f2cbd485SDavide Caratti 	police->tcfp_toks = new->tcfp_burst;
183f2cbd485SDavide Caratti 	if (new->peak_present)
184f2cbd485SDavide Caratti 		police->tcfp_ptoks = new->tcfp_mtu_ptoks;
185f2cbd485SDavide Caratti 	spin_unlock_bh(&police->tcfp_lock);
186d6124d6bSDavide Caratti 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
1872d550dbaSDavide Caratti 	rcu_swap_protected(police->params,
1882d550dbaSDavide Caratti 			   new,
1892d550dbaSDavide Caratti 			   lockdep_is_held(&police->tcf_lock));
190e9ce1cd3SDavid S. Miller 	spin_unlock_bh(&police->tcf_lock);
1914bba3925SPatrick McHardy 
192d6124d6bSDavide Caratti 	if (goto_ch)
193d6124d6bSDavide Caratti 		tcf_chain_put_by_act(goto_ch);
1942d550dbaSDavide Caratti 	if (new)
1952d550dbaSDavide Caratti 		kfree_rcu(new, rcu);
1962d550dbaSDavide Caratti 
1972d550dbaSDavide Caratti 	if (ret == ACT_P_CREATED)
19865a206c0SChris Mi 		tcf_idr_insert(tn, *a);
1994bba3925SPatrick McHardy 	return ret;
2004bba3925SPatrick McHardy 
2014bba3925SPatrick McHardy failure:
20271bcb09aSStephen Hemminger 	qdisc_put_rtab(P_tab);
20371bcb09aSStephen Hemminger 	qdisc_put_rtab(R_tab);
204d6124d6bSDavide Caratti 	if (goto_ch)
205d6124d6bSDavide Caratti 		tcf_chain_put_by_act(goto_ch);
206d6124d6bSDavide Caratti release_idr:
2075bf7f818SDavide Caratti 	tcf_idr_release(*a, bind);
2084bba3925SPatrick McHardy 	return err;
2094bba3925SPatrick McHardy }
2104bba3925SPatrick McHardy 
2112ac06347SJamal Hadi Salim static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
2124bba3925SPatrick McHardy 			  struct tcf_result *res)
2134bba3925SPatrick McHardy {
214a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
2152d550dbaSDavide Caratti 	struct tcf_police_params *p;
21693be42f9SDavide Caratti 	s64 now, toks, ptoks = 0;
21793be42f9SDavide Caratti 	int ret;
21893be42f9SDavide Caratti 
21993be42f9SDavide Caratti 	tcf_lastuse_update(&police->tcf_tm);
22093be42f9SDavide Caratti 	bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
2214bba3925SPatrick McHardy 
2222d550dbaSDavide Caratti 	ret = READ_ONCE(police->tcf_action);
2232d550dbaSDavide Caratti 	p = rcu_dereference_bh(police->params);
2242d550dbaSDavide Caratti 
2252d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate) {
2261c0d32fdSEric Dumazet 		struct gnet_stats_rate_est64 sample;
2271c0d32fdSEric Dumazet 
2281c0d32fdSEric Dumazet 		if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
2292d550dbaSDavide Caratti 		    sample.bps >= p->tcfp_ewma_rate)
23093be42f9SDavide Caratti 			goto inc_overlimits;
2314bba3925SPatrick McHardy 	}
2324bba3925SPatrick McHardy 
2332d550dbaSDavide Caratti 	if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
2342d550dbaSDavide Caratti 		if (!p->rate_present) {
2352d550dbaSDavide Caratti 			ret = p->tcfp_result;
2362d550dbaSDavide Caratti 			goto end;
2374bba3925SPatrick McHardy 		}
2384bba3925SPatrick McHardy 
239d2de875cSEric Dumazet 		now = ktime_get_ns();
240f2cbd485SDavide Caratti 		spin_lock_bh(&police->tcfp_lock);
241f2cbd485SDavide Caratti 		toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
2422d550dbaSDavide Caratti 		if (p->peak_present) {
243f2cbd485SDavide Caratti 			ptoks = toks + police->tcfp_ptoks;
2442d550dbaSDavide Caratti 			if (ptoks > p->tcfp_mtu_ptoks)
2452d550dbaSDavide Caratti 				ptoks = p->tcfp_mtu_ptoks;
2462d550dbaSDavide Caratti 			ptoks -= (s64)psched_l2t_ns(&p->peak,
247c6d14ff1SJiri Pirko 						    qdisc_pkt_len(skb));
2484bba3925SPatrick McHardy 		}
249f2cbd485SDavide Caratti 		toks += police->tcfp_toks;
2502d550dbaSDavide Caratti 		if (toks > p->tcfp_burst)
2512d550dbaSDavide Caratti 			toks = p->tcfp_burst;
2522d550dbaSDavide Caratti 		toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
2534bba3925SPatrick McHardy 		if ((toks|ptoks) >= 0) {
254f2cbd485SDavide Caratti 			police->tcfp_t_c = now;
255f2cbd485SDavide Caratti 			police->tcfp_toks = toks;
256f2cbd485SDavide Caratti 			police->tcfp_ptoks = ptoks;
257f2cbd485SDavide Caratti 			spin_unlock_bh(&police->tcfp_lock);
2582d550dbaSDavide Caratti 			ret = p->tcfp_result;
25993be42f9SDavide Caratti 			goto inc_drops;
2604bba3925SPatrick McHardy 		}
261f2cbd485SDavide Caratti 		spin_unlock_bh(&police->tcfp_lock);
2624bba3925SPatrick McHardy 	}
2634bba3925SPatrick McHardy 
26493be42f9SDavide Caratti inc_overlimits:
26593be42f9SDavide Caratti 	qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
26693be42f9SDavide Caratti inc_drops:
26793be42f9SDavide Caratti 	if (ret == TC_ACT_SHOT)
26893be42f9SDavide Caratti 		qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
2692d550dbaSDavide Caratti end:
27093be42f9SDavide Caratti 	return ret;
2714bba3925SPatrick McHardy }
2724bba3925SPatrick McHardy 
2732d550dbaSDavide Caratti static void tcf_police_cleanup(struct tc_action *a)
2742d550dbaSDavide Caratti {
2752d550dbaSDavide Caratti 	struct tcf_police *police = to_police(a);
2762d550dbaSDavide Caratti 	struct tcf_police_params *p;
2772d550dbaSDavide Caratti 
2782d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params, 1);
2792d550dbaSDavide Caratti 	if (p)
2802d550dbaSDavide Caratti 		kfree_rcu(p, rcu);
2812d550dbaSDavide Caratti }
2822d550dbaSDavide Caratti 
28312f02b6bSPieter Jansen van Vuuren static void tcf_police_stats_update(struct tc_action *a,
28412f02b6bSPieter Jansen van Vuuren 				    u64 bytes, u32 packets,
28512f02b6bSPieter Jansen van Vuuren 				    u64 lastuse, bool hw)
28612f02b6bSPieter Jansen van Vuuren {
28712f02b6bSPieter Jansen van Vuuren 	struct tcf_police *police = to_police(a);
28812f02b6bSPieter Jansen van Vuuren 	struct tcf_t *tm = &police->tcf_tm;
28912f02b6bSPieter Jansen van Vuuren 
29012f02b6bSPieter Jansen van Vuuren 	_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats), bytes, packets);
29112f02b6bSPieter Jansen van Vuuren 	if (hw)
29212f02b6bSPieter Jansen van Vuuren 		_bstats_cpu_update(this_cpu_ptr(a->cpu_bstats_hw),
29312f02b6bSPieter Jansen van Vuuren 				   bytes, packets);
29412f02b6bSPieter Jansen van Vuuren 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
29512f02b6bSPieter Jansen van Vuuren }
29612f02b6bSPieter Jansen van Vuuren 
2972ac06347SJamal Hadi Salim static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
2985a7a5555SJamal Hadi Salim 			       int bind, int ref)
2994bba3925SPatrick McHardy {
30027a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
301a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
3022d550dbaSDavide Caratti 	struct tcf_police_params *p;
3030f04cfd0SJeff Mahoney 	struct tc_police opt = {
3040f04cfd0SJeff Mahoney 		.index = police->tcf_index,
305036bb443SVlad Buslov 		.refcnt = refcount_read(&police->tcf_refcnt) - ref,
306036bb443SVlad Buslov 		.bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
3070f04cfd0SJeff Mahoney 	};
3083d3ed181SJamal Hadi Salim 	struct tcf_t t;
3094bba3925SPatrick McHardy 
310e329bc42SVlad Buslov 	spin_lock_bh(&police->tcf_lock);
311e329bc42SVlad Buslov 	opt.action = police->tcf_action;
3122d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params,
3132d550dbaSDavide Caratti 				      lockdep_is_held(&police->tcf_lock));
3142d550dbaSDavide Caratti 	opt.mtu = p->tcfp_mtu;
3152d550dbaSDavide Caratti 	opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
3162d550dbaSDavide Caratti 	if (p->rate_present)
3172d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.rate, &p->rate);
3182d550dbaSDavide Caratti 	if (p->peak_present)
3192d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.peakrate, &p->peak);
3201b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
3211b34ec43SDavid S. Miller 		goto nla_put_failure;
3222d550dbaSDavide Caratti 	if (p->tcfp_result &&
3232d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
3241b34ec43SDavid S. Miller 		goto nla_put_failure;
3252d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate &&
3262d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
3271b34ec43SDavid S. Miller 		goto nla_put_failure;
3283d3ed181SJamal Hadi Salim 
3293d3ed181SJamal Hadi Salim 	t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
3303d3ed181SJamal Hadi Salim 	t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
33153eb440fSJamal Hadi Salim 	t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
3323d3ed181SJamal Hadi Salim 	t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
3333d3ed181SJamal Hadi Salim 	if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
3343d3ed181SJamal Hadi Salim 		goto nla_put_failure;
335e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
3363d3ed181SJamal Hadi Salim 
3374bba3925SPatrick McHardy 	return skb->len;
3384bba3925SPatrick McHardy 
3397ba699c6SPatrick McHardy nla_put_failure:
340e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
341dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
3424bba3925SPatrick McHardy 	return -1;
3434bba3925SPatrick McHardy }
3444bba3925SPatrick McHardy 
345f061b48cSCong Wang static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
346ddf97ccdSWANG Cong {
347ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
348ddf97ccdSWANG Cong 
34965a206c0SChris Mi 	return tcf_idr_search(tn, a, index);
350ddf97ccdSWANG Cong }
351ddf97ccdSWANG Cong 
3524bba3925SPatrick McHardy MODULE_AUTHOR("Alexey Kuznetsov");
3534bba3925SPatrick McHardy MODULE_DESCRIPTION("Policing actions");
3544bba3925SPatrick McHardy MODULE_LICENSE("GPL");
3554bba3925SPatrick McHardy 
3564bba3925SPatrick McHardy static struct tc_action_ops act_police_ops = {
3574bba3925SPatrick McHardy 	.kind		=	"police",
358eddd2cf1SEli Cohen 	.id		=	TCA_ID_POLICE,
3594bba3925SPatrick McHardy 	.owner		=	THIS_MODULE,
36012f02b6bSPieter Jansen van Vuuren 	.stats_update	=	tcf_police_stats_update,
3612ac06347SJamal Hadi Salim 	.act		=	tcf_police_act,
3622ac06347SJamal Hadi Salim 	.dump		=	tcf_police_dump,
3632ac06347SJamal Hadi Salim 	.init		=	tcf_police_init,
3642ac06347SJamal Hadi Salim 	.walk		=	tcf_police_walker,
365ddf97ccdSWANG Cong 	.lookup		=	tcf_police_search,
3662d550dbaSDavide Caratti 	.cleanup	=	tcf_police_cleanup,
367a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_police),
368ddf97ccdSWANG Cong };
369ddf97ccdSWANG Cong 
370ddf97ccdSWANG Cong static __net_init int police_init_net(struct net *net)
371ddf97ccdSWANG Cong {
372ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
373ddf97ccdSWANG Cong 
374*981471bdSCong Wang 	return tc_action_net_init(net, tn, &act_police_ops);
375ddf97ccdSWANG Cong }
376ddf97ccdSWANG Cong 
377039af9c6SCong Wang static void __net_exit police_exit_net(struct list_head *net_list)
378ddf97ccdSWANG Cong {
379039af9c6SCong Wang 	tc_action_net_exit(net_list, police_net_id);
380ddf97ccdSWANG Cong }
381ddf97ccdSWANG Cong 
382ddf97ccdSWANG Cong static struct pernet_operations police_net_ops = {
383ddf97ccdSWANG Cong 	.init = police_init_net,
384039af9c6SCong Wang 	.exit_batch = police_exit_net,
385ddf97ccdSWANG Cong 	.id   = &police_net_id,
386ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
3874bba3925SPatrick McHardy };
3884bba3925SPatrick McHardy 
3895a7a5555SJamal Hadi Salim static int __init police_init_module(void)
3904bba3925SPatrick McHardy {
391ddf97ccdSWANG Cong 	return tcf_register_action(&act_police_ops, &police_net_ops);
3924bba3925SPatrick McHardy }
3934bba3925SPatrick McHardy 
3945a7a5555SJamal Hadi Salim static void __exit police_cleanup_module(void)
3954bba3925SPatrick McHardy {
396ddf97ccdSWANG Cong 	tcf_unregister_action(&act_police_ops, &police_net_ops);
3974bba3925SPatrick McHardy }
3984bba3925SPatrick McHardy 
3994bba3925SPatrick McHardy module_init(police_init_module);
4004bba3925SPatrick McHardy module_exit(police_cleanup_module);
401