xref: /openbmc/linux/net/sched/act_police.c (revision eddd2cf195d6fb5e4bbc91a0fe4be55110f559ab)
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>
244bba3925SPatrick McHardy 
252d550dbaSDavide Caratti struct tcf_police_params {
260e243218SJiri Pirko 	int			tcfp_result;
270e243218SJiri Pirko 	u32			tcfp_ewma_rate;
28c6d14ff1SJiri Pirko 	s64			tcfp_burst;
290e243218SJiri Pirko 	u32			tcfp_mtu;
30c6d14ff1SJiri Pirko 	s64			tcfp_mtu_ptoks;
31c6d14ff1SJiri Pirko 	struct psched_ratecfg	rate;
32c6d14ff1SJiri Pirko 	bool			rate_present;
33c6d14ff1SJiri Pirko 	struct psched_ratecfg	peak;
34c6d14ff1SJiri Pirko 	bool			peak_present;
352d550dbaSDavide Caratti 	struct rcu_head rcu;
362d550dbaSDavide Caratti };
372d550dbaSDavide Caratti 
382d550dbaSDavide Caratti struct tcf_police {
392d550dbaSDavide Caratti 	struct tc_action	common;
402d550dbaSDavide Caratti 	struct tcf_police_params __rcu *params;
41f2cbd485SDavide Caratti 
42f2cbd485SDavide Caratti 	spinlock_t		tcfp_lock ____cacheline_aligned_in_smp;
43f2cbd485SDavide Caratti 	s64			tcfp_toks;
44f2cbd485SDavide Caratti 	s64			tcfp_ptoks;
45f2cbd485SDavide Caratti 	s64			tcfp_t_c;
460e243218SJiri Pirko };
47a85a970aSWANG Cong 
48a85a970aSWANG Cong #define to_police(pc) ((struct tcf_police *)pc)
490e243218SJiri Pirko 
501e9b3d53SPatrick McHardy /* old policer structure from before tc actions */
51cc7ec456SEric Dumazet struct tc_police_compat {
521e9b3d53SPatrick McHardy 	u32			index;
531e9b3d53SPatrick McHardy 	int			action;
541e9b3d53SPatrick McHardy 	u32			limit;
551e9b3d53SPatrick McHardy 	u32			burst;
561e9b3d53SPatrick McHardy 	u32			mtu;
571e9b3d53SPatrick McHardy 	struct tc_ratespec	rate;
581e9b3d53SPatrick McHardy 	struct tc_ratespec	peakrate;
591e9b3d53SPatrick McHardy };
601e9b3d53SPatrick McHardy 
614bba3925SPatrick McHardy /* Each policer is serialized by its individual spinlock */
624bba3925SPatrick McHardy 
63c7d03a00SAlexey Dobriyan static unsigned int police_net_id;
64a85a970aSWANG Cong static struct tc_action_ops act_police_ops;
65ddf97ccdSWANG Cong 
662ac06347SJamal Hadi Salim static int tcf_police_walker(struct net *net, struct sk_buff *skb,
67ddf97ccdSWANG Cong 				 struct netlink_callback *cb, int type,
6841780105SAlexander Aring 				 const struct tc_action_ops *ops,
6941780105SAlexander Aring 				 struct netlink_ext_ack *extack)
704bba3925SPatrick McHardy {
71ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
724bba3925SPatrick McHardy 
73b3620145SAlexander Aring 	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
744bba3925SPatrick McHardy }
754bba3925SPatrick McHardy 
7653b2bf3fSPatrick McHardy static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
7753b2bf3fSPatrick McHardy 	[TCA_POLICE_RATE]	= { .len = TC_RTAB_SIZE },
7853b2bf3fSPatrick McHardy 	[TCA_POLICE_PEAKRATE]	= { .len = TC_RTAB_SIZE },
7953b2bf3fSPatrick McHardy 	[TCA_POLICE_AVRATE]	= { .type = NLA_U32 },
8053b2bf3fSPatrick McHardy 	[TCA_POLICE_RESULT]	= { .type = NLA_U32 },
8153b2bf3fSPatrick McHardy };
8253b2bf3fSPatrick McHardy 
832ac06347SJamal Hadi Salim static int tcf_police_init(struct net *net, struct nlattr *nla,
84a85a970aSWANG Cong 			       struct nlattr *est, struct tc_action **a,
85789871bbSVlad Buslov 			       int ovr, int bind, bool rtnl_held,
86589dad6dSAlexander Aring 			       struct netlink_ext_ack *extack)
874bba3925SPatrick McHardy {
88fd6d4338SDavide Caratti 	int ret = 0, tcfp_result = TC_ACT_OK, err, size;
897ba699c6SPatrick McHardy 	struct nlattr *tb[TCA_POLICE_MAX + 1];
904bba3925SPatrick McHardy 	struct tc_police *parm;
91e9ce1cd3SDavid S. Miller 	struct tcf_police *police;
924bba3925SPatrick McHardy 	struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
93ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
942d550dbaSDavide Caratti 	struct tcf_police_params *new;
950852e455SWANG Cong 	bool exists = false;
964bba3925SPatrick McHardy 
97cee63723SPatrick McHardy 	if (nla == NULL)
984bba3925SPatrick McHardy 		return -EINVAL;
994bba3925SPatrick McHardy 
100fceb6435SJohannes Berg 	err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy, NULL);
101cee63723SPatrick McHardy 	if (err < 0)
102cee63723SPatrick McHardy 		return err;
103cee63723SPatrick McHardy 
1047ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_TBF] == NULL)
1051e9b3d53SPatrick McHardy 		return -EINVAL;
1067ba699c6SPatrick McHardy 	size = nla_len(tb[TCA_POLICE_TBF]);
1071e9b3d53SPatrick McHardy 	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
1084bba3925SPatrick McHardy 		return -EINVAL;
1094bba3925SPatrick McHardy 
1100852e455SWANG Cong 	parm = nla_data(tb[TCA_POLICE_TBF]);
1110190c1d4SVlad Buslov 	err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
1120190c1d4SVlad Buslov 	if (err < 0)
1130190c1d4SVlad Buslov 		return err;
1140190c1d4SVlad Buslov 	exists = err;
1150852e455SWANG Cong 	if (exists && bind)
1160852e455SWANG Cong 		return 0;
1170852e455SWANG Cong 
1180852e455SWANG Cong 	if (!exists) {
11965a206c0SChris Mi 		ret = tcf_idr_create(tn, parm->index, NULL, a,
12093be42f9SDavide Caratti 				     &act_police_ops, bind, true);
1210190c1d4SVlad Buslov 		if (ret) {
1220190c1d4SVlad Buslov 			tcf_idr_cleanup(tn, parm->index);
123a03e6fe5SWANG Cong 			return ret;
1240190c1d4SVlad Buslov 		}
125a03e6fe5SWANG Cong 		ret = ACT_P_CREATED;
126484afd1bSDavide Caratti 		spin_lock_init(&(to_police(*a)->tcfp_lock));
1274e8ddd7fSVlad Buslov 	} else if (!ovr) {
12865a206c0SChris Mi 		tcf_idr_release(*a, bind);
1290852e455SWANG Cong 		return -EEXIST;
130e9ce1cd3SDavid S. Miller 	}
1314bba3925SPatrick McHardy 
132a85a970aSWANG Cong 	police = to_police(*a);
1334bba3925SPatrick McHardy 	if (parm->rate.rate) {
1344bba3925SPatrick McHardy 		err = -ENOMEM;
135e9bc3fa2SAlexander Aring 		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
1364bba3925SPatrick McHardy 		if (R_tab == NULL)
1374bba3925SPatrick McHardy 			goto failure;
138c1b56878SStephen Hemminger 
1394bba3925SPatrick McHardy 		if (parm->peakrate.rate) {
1404bba3925SPatrick McHardy 			P_tab = qdisc_get_rtab(&parm->peakrate,
141e9bc3fa2SAlexander Aring 					       tb[TCA_POLICE_PEAKRATE], NULL);
14271bcb09aSStephen Hemminger 			if (P_tab == NULL)
1434bba3925SPatrick McHardy 				goto failure;
1444bba3925SPatrick McHardy 		}
1454bba3925SPatrick McHardy 	}
14671bcb09aSStephen Hemminger 
14771bcb09aSStephen Hemminger 	if (est) {
14893be42f9SDavide Caratti 		err = gen_replace_estimator(&police->tcf_bstats,
14993be42f9SDavide Caratti 					    police->common.cpu_bstats,
15071bcb09aSStephen Hemminger 					    &police->tcf_rate_est,
151edb09eb1SEric Dumazet 					    &police->tcf_lock,
152edb09eb1SEric Dumazet 					    NULL, est);
15371bcb09aSStephen Hemminger 		if (err)
15474030603SWANG Cong 			goto failure;
155a883bf56SJarek Poplawski 	} else if (tb[TCA_POLICE_AVRATE] &&
156a883bf56SJarek Poplawski 		   (ret == ACT_P_CREATED ||
1571c0d32fdSEric Dumazet 		    !gen_estimator_active(&police->tcf_rate_est))) {
158a883bf56SJarek Poplawski 		err = -EINVAL;
15974030603SWANG Cong 		goto failure;
16071bcb09aSStephen Hemminger 	}
16171bcb09aSStephen Hemminger 
162fd6d4338SDavide Caratti 	if (tb[TCA_POLICE_RESULT]) {
163fd6d4338SDavide Caratti 		tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
164fd6d4338SDavide Caratti 		if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
165fd6d4338SDavide Caratti 			NL_SET_ERR_MSG(extack,
166fd6d4338SDavide Caratti 				       "goto chain not allowed on fallback");
167fd6d4338SDavide Caratti 			err = -EINVAL;
168fd6d4338SDavide Caratti 			goto failure;
169fd6d4338SDavide Caratti 		}
170fd6d4338SDavide Caratti 	}
171fd6d4338SDavide Caratti 
1722d550dbaSDavide Caratti 	new = kzalloc(sizeof(*new), GFP_KERNEL);
1732d550dbaSDavide Caratti 	if (unlikely(!new)) {
1742d550dbaSDavide Caratti 		err = -ENOMEM;
1752d550dbaSDavide Caratti 		goto failure;
1762d550dbaSDavide Caratti 	}
1772d550dbaSDavide Caratti 
17871bcb09aSStephen Hemminger 	/* No failure allowed after this point */
179fd6d4338SDavide Caratti 	new->tcfp_result = tcfp_result;
1802d550dbaSDavide Caratti 	new->tcfp_mtu = parm->mtu;
1812d550dbaSDavide Caratti 	if (!new->tcfp_mtu) {
1822d550dbaSDavide Caratti 		new->tcfp_mtu = ~0;
183c6d14ff1SJiri Pirko 		if (R_tab)
1842d550dbaSDavide Caratti 			new->tcfp_mtu = 255 << R_tab->rate.cell_log;
1854bba3925SPatrick McHardy 	}
186c6d14ff1SJiri Pirko 	if (R_tab) {
1872d550dbaSDavide Caratti 		new->rate_present = true;
1882d550dbaSDavide Caratti 		psched_ratecfg_precompute(&new->rate, &R_tab->rate, 0);
189c6d14ff1SJiri Pirko 		qdisc_put_rtab(R_tab);
190c6d14ff1SJiri Pirko 	} else {
1912d550dbaSDavide Caratti 		new->rate_present = false;
192c6d14ff1SJiri Pirko 	}
193c6d14ff1SJiri Pirko 	if (P_tab) {
1942d550dbaSDavide Caratti 		new->peak_present = true;
1952d550dbaSDavide Caratti 		psched_ratecfg_precompute(&new->peak, &P_tab->rate, 0);
196c6d14ff1SJiri Pirko 		qdisc_put_rtab(P_tab);
197c6d14ff1SJiri Pirko 	} else {
1982d550dbaSDavide Caratti 		new->peak_present = false;
1994bba3925SPatrick McHardy 	}
2004bba3925SPatrick McHardy 
2012d550dbaSDavide Caratti 	new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
202f2cbd485SDavide Caratti 	if (new->peak_present)
2032d550dbaSDavide Caratti 		new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
2042d550dbaSDavide Caratti 							 new->tcfp_mtu);
2054bba3925SPatrick McHardy 
2067ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_AVRATE])
2072d550dbaSDavide Caratti 		new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
2084bba3925SPatrick McHardy 
2092d550dbaSDavide Caratti 	spin_lock_bh(&police->tcf_lock);
210f2cbd485SDavide Caratti 	spin_lock_bh(&police->tcfp_lock);
211f2cbd485SDavide Caratti 	police->tcfp_t_c = ktime_get_ns();
212f2cbd485SDavide Caratti 	police->tcfp_toks = new->tcfp_burst;
213f2cbd485SDavide Caratti 	if (new->peak_present)
214f2cbd485SDavide Caratti 		police->tcfp_ptoks = new->tcfp_mtu_ptoks;
215f2cbd485SDavide Caratti 	spin_unlock_bh(&police->tcfp_lock);
2162d550dbaSDavide Caratti 	police->tcf_action = parm->action;
2172d550dbaSDavide Caratti 	rcu_swap_protected(police->params,
2182d550dbaSDavide Caratti 			   new,
2192d550dbaSDavide Caratti 			   lockdep_is_held(&police->tcf_lock));
220e9ce1cd3SDavid S. Miller 	spin_unlock_bh(&police->tcf_lock);
2214bba3925SPatrick McHardy 
2222d550dbaSDavide Caratti 	if (new)
2232d550dbaSDavide Caratti 		kfree_rcu(new, rcu);
2242d550dbaSDavide Caratti 
2252d550dbaSDavide Caratti 	if (ret == ACT_P_CREATED)
22665a206c0SChris Mi 		tcf_idr_insert(tn, *a);
2274bba3925SPatrick McHardy 	return ret;
2284bba3925SPatrick McHardy 
2294bba3925SPatrick McHardy failure:
23071bcb09aSStephen Hemminger 	qdisc_put_rtab(P_tab);
23171bcb09aSStephen Hemminger 	qdisc_put_rtab(R_tab);
2325bf7f818SDavide Caratti 	tcf_idr_release(*a, bind);
2334bba3925SPatrick McHardy 	return err;
2344bba3925SPatrick McHardy }
2354bba3925SPatrick McHardy 
2362ac06347SJamal Hadi Salim static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
2374bba3925SPatrick McHardy 			  struct tcf_result *res)
2384bba3925SPatrick McHardy {
239a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
2402d550dbaSDavide Caratti 	struct tcf_police_params *p;
24193be42f9SDavide Caratti 	s64 now, toks, ptoks = 0;
24293be42f9SDavide Caratti 	int ret;
24393be42f9SDavide Caratti 
24493be42f9SDavide Caratti 	tcf_lastuse_update(&police->tcf_tm);
24593be42f9SDavide Caratti 	bstats_cpu_update(this_cpu_ptr(police->common.cpu_bstats), skb);
2464bba3925SPatrick McHardy 
2472d550dbaSDavide Caratti 	ret = READ_ONCE(police->tcf_action);
2482d550dbaSDavide Caratti 	p = rcu_dereference_bh(police->params);
2492d550dbaSDavide Caratti 
2502d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate) {
2511c0d32fdSEric Dumazet 		struct gnet_stats_rate_est64 sample;
2521c0d32fdSEric Dumazet 
2531c0d32fdSEric Dumazet 		if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
2542d550dbaSDavide Caratti 		    sample.bps >= p->tcfp_ewma_rate)
25593be42f9SDavide Caratti 			goto inc_overlimits;
2564bba3925SPatrick McHardy 	}
2574bba3925SPatrick McHardy 
2582d550dbaSDavide Caratti 	if (qdisc_pkt_len(skb) <= p->tcfp_mtu) {
2592d550dbaSDavide Caratti 		if (!p->rate_present) {
2602d550dbaSDavide Caratti 			ret = p->tcfp_result;
2612d550dbaSDavide Caratti 			goto end;
2624bba3925SPatrick McHardy 		}
2634bba3925SPatrick McHardy 
264d2de875cSEric Dumazet 		now = ktime_get_ns();
265f2cbd485SDavide Caratti 		spin_lock_bh(&police->tcfp_lock);
266f2cbd485SDavide Caratti 		toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
2672d550dbaSDavide Caratti 		if (p->peak_present) {
268f2cbd485SDavide Caratti 			ptoks = toks + police->tcfp_ptoks;
2692d550dbaSDavide Caratti 			if (ptoks > p->tcfp_mtu_ptoks)
2702d550dbaSDavide Caratti 				ptoks = p->tcfp_mtu_ptoks;
2712d550dbaSDavide Caratti 			ptoks -= (s64)psched_l2t_ns(&p->peak,
272c6d14ff1SJiri Pirko 						    qdisc_pkt_len(skb));
2734bba3925SPatrick McHardy 		}
274f2cbd485SDavide Caratti 		toks += police->tcfp_toks;
2752d550dbaSDavide Caratti 		if (toks > p->tcfp_burst)
2762d550dbaSDavide Caratti 			toks = p->tcfp_burst;
2772d550dbaSDavide Caratti 		toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
2784bba3925SPatrick McHardy 		if ((toks|ptoks) >= 0) {
279f2cbd485SDavide Caratti 			police->tcfp_t_c = now;
280f2cbd485SDavide Caratti 			police->tcfp_toks = toks;
281f2cbd485SDavide Caratti 			police->tcfp_ptoks = ptoks;
282f2cbd485SDavide Caratti 			spin_unlock_bh(&police->tcfp_lock);
2832d550dbaSDavide Caratti 			ret = p->tcfp_result;
28493be42f9SDavide Caratti 			goto inc_drops;
2854bba3925SPatrick McHardy 		}
286f2cbd485SDavide Caratti 		spin_unlock_bh(&police->tcfp_lock);
2874bba3925SPatrick McHardy 	}
2884bba3925SPatrick McHardy 
28993be42f9SDavide Caratti inc_overlimits:
29093be42f9SDavide Caratti 	qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
29193be42f9SDavide Caratti inc_drops:
29293be42f9SDavide Caratti 	if (ret == TC_ACT_SHOT)
29393be42f9SDavide Caratti 		qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
2942d550dbaSDavide Caratti end:
29593be42f9SDavide Caratti 	return ret;
2964bba3925SPatrick McHardy }
2974bba3925SPatrick McHardy 
2982d550dbaSDavide Caratti static void tcf_police_cleanup(struct tc_action *a)
2992d550dbaSDavide Caratti {
3002d550dbaSDavide Caratti 	struct tcf_police *police = to_police(a);
3012d550dbaSDavide Caratti 	struct tcf_police_params *p;
3022d550dbaSDavide Caratti 
3032d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params, 1);
3042d550dbaSDavide Caratti 	if (p)
3052d550dbaSDavide Caratti 		kfree_rcu(p, rcu);
3062d550dbaSDavide Caratti }
3072d550dbaSDavide Caratti 
3082ac06347SJamal Hadi Salim static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
3095a7a5555SJamal Hadi Salim 			       int bind, int ref)
3104bba3925SPatrick McHardy {
31127a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
312a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
3132d550dbaSDavide Caratti 	struct tcf_police_params *p;
3140f04cfd0SJeff Mahoney 	struct tc_police opt = {
3150f04cfd0SJeff Mahoney 		.index = police->tcf_index,
316036bb443SVlad Buslov 		.refcnt = refcount_read(&police->tcf_refcnt) - ref,
317036bb443SVlad Buslov 		.bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
3180f04cfd0SJeff Mahoney 	};
3193d3ed181SJamal Hadi Salim 	struct tcf_t t;
3204bba3925SPatrick McHardy 
321e329bc42SVlad Buslov 	spin_lock_bh(&police->tcf_lock);
322e329bc42SVlad Buslov 	opt.action = police->tcf_action;
3232d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params,
3242d550dbaSDavide Caratti 				      lockdep_is_held(&police->tcf_lock));
3252d550dbaSDavide Caratti 	opt.mtu = p->tcfp_mtu;
3262d550dbaSDavide Caratti 	opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
3272d550dbaSDavide Caratti 	if (p->rate_present)
3282d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.rate, &p->rate);
3292d550dbaSDavide Caratti 	if (p->peak_present)
3302d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.peakrate, &p->peak);
3311b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
3321b34ec43SDavid S. Miller 		goto nla_put_failure;
3332d550dbaSDavide Caratti 	if (p->tcfp_result &&
3342d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
3351b34ec43SDavid S. Miller 		goto nla_put_failure;
3362d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate &&
3372d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
3381b34ec43SDavid S. Miller 		goto nla_put_failure;
3393d3ed181SJamal Hadi Salim 
3403d3ed181SJamal Hadi Salim 	t.install = jiffies_to_clock_t(jiffies - police->tcf_tm.install);
3413d3ed181SJamal Hadi Salim 	t.lastuse = jiffies_to_clock_t(jiffies - police->tcf_tm.lastuse);
34253eb440fSJamal Hadi Salim 	t.firstuse = jiffies_to_clock_t(jiffies - police->tcf_tm.firstuse);
3433d3ed181SJamal Hadi Salim 	t.expires = jiffies_to_clock_t(police->tcf_tm.expires);
3443d3ed181SJamal Hadi Salim 	if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
3453d3ed181SJamal Hadi Salim 		goto nla_put_failure;
346e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
3473d3ed181SJamal Hadi Salim 
3484bba3925SPatrick McHardy 	return skb->len;
3494bba3925SPatrick McHardy 
3507ba699c6SPatrick McHardy nla_put_failure:
351e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
352dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
3534bba3925SPatrick McHardy 	return -1;
3544bba3925SPatrick McHardy }
3554bba3925SPatrick McHardy 
356f061b48cSCong Wang static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
357ddf97ccdSWANG Cong {
358ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
359ddf97ccdSWANG Cong 
36065a206c0SChris Mi 	return tcf_idr_search(tn, a, index);
361ddf97ccdSWANG Cong }
362ddf97ccdSWANG Cong 
3634bba3925SPatrick McHardy MODULE_AUTHOR("Alexey Kuznetsov");
3644bba3925SPatrick McHardy MODULE_DESCRIPTION("Policing actions");
3654bba3925SPatrick McHardy MODULE_LICENSE("GPL");
3664bba3925SPatrick McHardy 
3674bba3925SPatrick McHardy static struct tc_action_ops act_police_ops = {
3684bba3925SPatrick McHardy 	.kind		=	"police",
369*eddd2cf1SEli Cohen 	.id		=	TCA_ID_POLICE,
3704bba3925SPatrick McHardy 	.owner		=	THIS_MODULE,
3712ac06347SJamal Hadi Salim 	.act		=	tcf_police_act,
3722ac06347SJamal Hadi Salim 	.dump		=	tcf_police_dump,
3732ac06347SJamal Hadi Salim 	.init		=	tcf_police_init,
3742ac06347SJamal Hadi Salim 	.walk		=	tcf_police_walker,
375ddf97ccdSWANG Cong 	.lookup		=	tcf_police_search,
3762d550dbaSDavide Caratti 	.cleanup	=	tcf_police_cleanup,
377a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_police),
378ddf97ccdSWANG Cong };
379ddf97ccdSWANG Cong 
380ddf97ccdSWANG Cong static __net_init int police_init_net(struct net *net)
381ddf97ccdSWANG Cong {
382ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
383ddf97ccdSWANG Cong 
384c7e460ceSCong Wang 	return tc_action_net_init(tn, &act_police_ops);
385ddf97ccdSWANG Cong }
386ddf97ccdSWANG Cong 
387039af9c6SCong Wang static void __net_exit police_exit_net(struct list_head *net_list)
388ddf97ccdSWANG Cong {
389039af9c6SCong Wang 	tc_action_net_exit(net_list, police_net_id);
390ddf97ccdSWANG Cong }
391ddf97ccdSWANG Cong 
392ddf97ccdSWANG Cong static struct pernet_operations police_net_ops = {
393ddf97ccdSWANG Cong 	.init = police_init_net,
394039af9c6SCong Wang 	.exit_batch = police_exit_net,
395ddf97ccdSWANG Cong 	.id   = &police_net_id,
396ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
3974bba3925SPatrick McHardy };
3984bba3925SPatrick McHardy 
3995a7a5555SJamal Hadi Salim static int __init police_init_module(void)
4004bba3925SPatrick McHardy {
401ddf97ccdSWANG Cong 	return tcf_register_action(&act_police_ops, &police_net_ops);
4024bba3925SPatrick McHardy }
4034bba3925SPatrick McHardy 
4045a7a5555SJamal Hadi Salim static void __exit police_cleanup_module(void)
4054bba3925SPatrick McHardy {
406ddf97ccdSWANG Cong 	tcf_unregister_action(&act_police_ops, &police_net_ops);
4074bba3925SPatrick McHardy }
4084bba3925SPatrick McHardy 
4094bba3925SPatrick McHardy module_init(police_init_module);
4104bba3925SPatrick McHardy module_exit(police_cleanup_module);
411