xref: /openbmc/linux/net/sched/act_police.c (revision b50e462bc22df4488ec04d85606646e3db5952b8)
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 },
452ffe0395SBaowen Zheng 	[TCA_POLICE_PKTRATE64]  = { .type = NLA_U64, .min = 1 },
462ffe0395SBaowen Zheng 	[TCA_POLICE_PKTBURST64] = { .type = NLA_U64, .min = 1 },
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,
51abbb0d33SVlad Buslov 			       struct tcf_proto *tp, u32 flags,
52589dad6dSAlexander Aring 			       struct netlink_ext_ack *extack)
534bba3925SPatrick McHardy {
54fd6d4338SDavide Caratti 	int ret = 0, tcfp_result = TC_ACT_OK, err, size;
55695176bfSCong Wang 	bool bind = flags & TCA_ACT_FLAGS_BIND;
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;
647be8ef2cSDmytro Linkin 	u32 index;
65d1967e49SDavid Dai 	u64 rate64, prate64;
662ffe0395SBaowen Zheng 	u64 pps, ppsburst;
674bba3925SPatrick McHardy 
68cee63723SPatrick McHardy 	if (nla == NULL)
694bba3925SPatrick McHardy 		return -EINVAL;
704bba3925SPatrick McHardy 
718cb08174SJohannes Berg 	err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
728cb08174SJohannes Berg 					  police_policy, NULL);
73cee63723SPatrick McHardy 	if (err < 0)
74cee63723SPatrick McHardy 		return err;
75cee63723SPatrick McHardy 
767ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_TBF] == NULL)
771e9b3d53SPatrick McHardy 		return -EINVAL;
787ba699c6SPatrick McHardy 	size = nla_len(tb[TCA_POLICE_TBF]);
791e9b3d53SPatrick McHardy 	if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
804bba3925SPatrick McHardy 		return -EINVAL;
814bba3925SPatrick McHardy 
820852e455SWANG Cong 	parm = nla_data(tb[TCA_POLICE_TBF]);
837be8ef2cSDmytro Linkin 	index = parm->index;
847be8ef2cSDmytro Linkin 	err = tcf_idr_check_alloc(tn, &index, a, bind);
850190c1d4SVlad Buslov 	if (err < 0)
860190c1d4SVlad Buslov 		return err;
870190c1d4SVlad Buslov 	exists = err;
880852e455SWANG Cong 	if (exists && bind)
890852e455SWANG Cong 		return 0;
900852e455SWANG Cong 
910852e455SWANG Cong 	if (!exists) {
927be8ef2cSDmytro Linkin 		ret = tcf_idr_create(tn, index, NULL, a,
9340bd094dSBaowen Zheng 				     &act_police_ops, bind, true, flags);
940190c1d4SVlad Buslov 		if (ret) {
957be8ef2cSDmytro Linkin 			tcf_idr_cleanup(tn, index);
96a03e6fe5SWANG Cong 			return ret;
970190c1d4SVlad Buslov 		}
98a03e6fe5SWANG Cong 		ret = ACT_P_CREATED;
99484afd1bSDavide Caratti 		spin_lock_init(&(to_police(*a)->tcfp_lock));
100695176bfSCong Wang 	} else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
10165a206c0SChris Mi 		tcf_idr_release(*a, bind);
1020852e455SWANG Cong 		return -EEXIST;
103e9ce1cd3SDavid S. Miller 	}
104d6124d6bSDavide Caratti 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
105d6124d6bSDavide Caratti 	if (err < 0)
106d6124d6bSDavide Caratti 		goto release_idr;
1074bba3925SPatrick McHardy 
108a85a970aSWANG Cong 	police = to_police(*a);
1094bba3925SPatrick McHardy 	if (parm->rate.rate) {
1104bba3925SPatrick McHardy 		err = -ENOMEM;
111e9bc3fa2SAlexander Aring 		R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
1124bba3925SPatrick McHardy 		if (R_tab == NULL)
1134bba3925SPatrick McHardy 			goto failure;
114c1b56878SStephen Hemminger 
1154bba3925SPatrick McHardy 		if (parm->peakrate.rate) {
1164bba3925SPatrick McHardy 			P_tab = qdisc_get_rtab(&parm->peakrate,
117e9bc3fa2SAlexander Aring 					       tb[TCA_POLICE_PEAKRATE], NULL);
11871bcb09aSStephen Hemminger 			if (P_tab == NULL)
1194bba3925SPatrick McHardy 				goto failure;
1204bba3925SPatrick McHardy 		}
1214bba3925SPatrick McHardy 	}
12271bcb09aSStephen Hemminger 
12371bcb09aSStephen Hemminger 	if (est) {
12493be42f9SDavide Caratti 		err = gen_replace_estimator(&police->tcf_bstats,
12593be42f9SDavide Caratti 					    police->common.cpu_bstats,
12671bcb09aSStephen Hemminger 					    &police->tcf_rate_est,
127edb09eb1SEric Dumazet 					    &police->tcf_lock,
12829cbcd85SAhmed S. Darwish 					    false, est);
12971bcb09aSStephen Hemminger 		if (err)
13074030603SWANG Cong 			goto failure;
131a883bf56SJarek Poplawski 	} else if (tb[TCA_POLICE_AVRATE] &&
132a883bf56SJarek Poplawski 		   (ret == ACT_P_CREATED ||
1331c0d32fdSEric Dumazet 		    !gen_estimator_active(&police->tcf_rate_est))) {
134a883bf56SJarek Poplawski 		err = -EINVAL;
13574030603SWANG Cong 		goto failure;
13671bcb09aSStephen Hemminger 	}
13771bcb09aSStephen Hemminger 
138fd6d4338SDavide Caratti 	if (tb[TCA_POLICE_RESULT]) {
139fd6d4338SDavide Caratti 		tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
140fd6d4338SDavide Caratti 		if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
141fd6d4338SDavide Caratti 			NL_SET_ERR_MSG(extack,
142fd6d4338SDavide Caratti 				       "goto chain not allowed on fallback");
143fd6d4338SDavide Caratti 			err = -EINVAL;
144fd6d4338SDavide Caratti 			goto failure;
145fd6d4338SDavide Caratti 		}
146fd6d4338SDavide Caratti 	}
147fd6d4338SDavide Caratti 
1482ffe0395SBaowen Zheng 	if ((tb[TCA_POLICE_PKTRATE64] && !tb[TCA_POLICE_PKTBURST64]) ||
1492ffe0395SBaowen Zheng 	    (!tb[TCA_POLICE_PKTRATE64] && tb[TCA_POLICE_PKTBURST64])) {
1502ffe0395SBaowen Zheng 		NL_SET_ERR_MSG(extack,
1512ffe0395SBaowen Zheng 			       "Both or neither packet-per-second burst and rate must be provided");
1522ffe0395SBaowen Zheng 		err = -EINVAL;
1532ffe0395SBaowen Zheng 		goto failure;
1542ffe0395SBaowen Zheng 	}
1552ffe0395SBaowen Zheng 
1562ffe0395SBaowen Zheng 	if (tb[TCA_POLICE_PKTRATE64] && R_tab) {
1572ffe0395SBaowen Zheng 		NL_SET_ERR_MSG(extack,
1582ffe0395SBaowen Zheng 			       "packet-per-second and byte-per-second rate limits not allowed in same action");
1592ffe0395SBaowen Zheng 		err = -EINVAL;
1602ffe0395SBaowen Zheng 		goto failure;
1612ffe0395SBaowen Zheng 	}
1622ffe0395SBaowen Zheng 
1632d550dbaSDavide Caratti 	new = kzalloc(sizeof(*new), GFP_KERNEL);
1642d550dbaSDavide Caratti 	if (unlikely(!new)) {
1652d550dbaSDavide Caratti 		err = -ENOMEM;
1662d550dbaSDavide Caratti 		goto failure;
1672d550dbaSDavide Caratti 	}
1682d550dbaSDavide Caratti 
16971bcb09aSStephen Hemminger 	/* No failure allowed after this point */
170fd6d4338SDavide Caratti 	new->tcfp_result = tcfp_result;
1712d550dbaSDavide Caratti 	new->tcfp_mtu = parm->mtu;
1722d550dbaSDavide Caratti 	if (!new->tcfp_mtu) {
1732d550dbaSDavide Caratti 		new->tcfp_mtu = ~0;
174c6d14ff1SJiri Pirko 		if (R_tab)
1752d550dbaSDavide Caratti 			new->tcfp_mtu = 255 << R_tab->rate.cell_log;
1764bba3925SPatrick McHardy 	}
177c6d14ff1SJiri Pirko 	if (R_tab) {
1782d550dbaSDavide Caratti 		new->rate_present = true;
179d1967e49SDavid Dai 		rate64 = tb[TCA_POLICE_RATE64] ?
180d1967e49SDavid Dai 			 nla_get_u64(tb[TCA_POLICE_RATE64]) : 0;
181d1967e49SDavid Dai 		psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64);
182c6d14ff1SJiri Pirko 		qdisc_put_rtab(R_tab);
183c6d14ff1SJiri Pirko 	} else {
1842d550dbaSDavide Caratti 		new->rate_present = false;
185c6d14ff1SJiri Pirko 	}
186c6d14ff1SJiri Pirko 	if (P_tab) {
1872d550dbaSDavide Caratti 		new->peak_present = true;
188d1967e49SDavid Dai 		prate64 = tb[TCA_POLICE_PEAKRATE64] ?
189d1967e49SDavid Dai 			  nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0;
190d1967e49SDavid Dai 		psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64);
191c6d14ff1SJiri Pirko 		qdisc_put_rtab(P_tab);
192c6d14ff1SJiri Pirko 	} else {
1932d550dbaSDavide Caratti 		new->peak_present = false;
1944bba3925SPatrick McHardy 	}
1954bba3925SPatrick McHardy 
1962d550dbaSDavide Caratti 	new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
197f2cbd485SDavide Caratti 	if (new->peak_present)
1982d550dbaSDavide Caratti 		new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
1992d550dbaSDavide Caratti 							 new->tcfp_mtu);
2004bba3925SPatrick McHardy 
2017ba699c6SPatrick McHardy 	if (tb[TCA_POLICE_AVRATE])
2022d550dbaSDavide Caratti 		new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
2034bba3925SPatrick McHardy 
2042ffe0395SBaowen Zheng 	if (tb[TCA_POLICE_PKTRATE64]) {
2052ffe0395SBaowen Zheng 		pps = nla_get_u64(tb[TCA_POLICE_PKTRATE64]);
2062ffe0395SBaowen Zheng 		ppsburst = nla_get_u64(tb[TCA_POLICE_PKTBURST64]);
2072ffe0395SBaowen Zheng 		new->pps_present = true;
2082ffe0395SBaowen Zheng 		new->tcfp_pkt_burst = PSCHED_TICKS2NS(ppsburst);
2092ffe0395SBaowen Zheng 		psched_ppscfg_precompute(&new->ppsrate, pps);
2102ffe0395SBaowen Zheng 	}
2112ffe0395SBaowen Zheng 
2122d550dbaSDavide Caratti 	spin_lock_bh(&police->tcf_lock);
213f2cbd485SDavide Caratti 	spin_lock_bh(&police->tcfp_lock);
214f2cbd485SDavide Caratti 	police->tcfp_t_c = ktime_get_ns();
215f2cbd485SDavide Caratti 	police->tcfp_toks = new->tcfp_burst;
216f2cbd485SDavide Caratti 	if (new->peak_present)
217f2cbd485SDavide Caratti 		police->tcfp_ptoks = new->tcfp_mtu_ptoks;
218f2cbd485SDavide Caratti 	spin_unlock_bh(&police->tcfp_lock);
219d6124d6bSDavide Caratti 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
220445d3749SPaul E. McKenney 	new = rcu_replace_pointer(police->params,
2212d550dbaSDavide Caratti 				  new,
2222d550dbaSDavide Caratti 				  lockdep_is_held(&police->tcf_lock));
223e9ce1cd3SDavid S. Miller 	spin_unlock_bh(&police->tcf_lock);
2244bba3925SPatrick McHardy 
225d6124d6bSDavide Caratti 	if (goto_ch)
226d6124d6bSDavide Caratti 		tcf_chain_put_by_act(goto_ch);
2272d550dbaSDavide Caratti 	if (new)
2282d550dbaSDavide Caratti 		kfree_rcu(new, rcu);
2292d550dbaSDavide Caratti 
2304bba3925SPatrick McHardy 	return ret;
2314bba3925SPatrick McHardy 
2324bba3925SPatrick McHardy failure:
23371bcb09aSStephen Hemminger 	qdisc_put_rtab(P_tab);
23471bcb09aSStephen Hemminger 	qdisc_put_rtab(R_tab);
235d6124d6bSDavide Caratti 	if (goto_ch)
236d6124d6bSDavide Caratti 		tcf_chain_put_by_act(goto_ch);
237d6124d6bSDavide Caratti release_idr:
2385bf7f818SDavide Caratti 	tcf_idr_release(*a, bind);
2394bba3925SPatrick McHardy 	return err;
2404bba3925SPatrick McHardy }
2414bba3925SPatrick McHardy 
2424ddc844eSDavide Caratti static bool tcf_police_mtu_check(struct sk_buff *skb, u32 limit)
2434ddc844eSDavide Caratti {
2444ddc844eSDavide Caratti 	u32 len;
2454ddc844eSDavide Caratti 
2464ddc844eSDavide Caratti 	if (skb_is_gso(skb))
2474ddc844eSDavide Caratti 		return skb_gso_validate_mac_len(skb, limit);
2484ddc844eSDavide Caratti 
2494ddc844eSDavide Caratti 	len = qdisc_pkt_len(skb);
2504ddc844eSDavide Caratti 	if (skb_at_tc_ingress(skb))
2514ddc844eSDavide Caratti 		len += skb->mac_len;
2524ddc844eSDavide Caratti 
2534ddc844eSDavide Caratti 	return len <= limit;
2544ddc844eSDavide Caratti }
2554ddc844eSDavide Caratti 
2562ac06347SJamal Hadi Salim static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a,
2574bba3925SPatrick McHardy 			  struct tcf_result *res)
2584bba3925SPatrick McHardy {
259a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
2602ffe0395SBaowen Zheng 	s64 now, toks, ppstoks = 0, ptoks = 0;
2612d550dbaSDavide Caratti 	struct tcf_police_params *p;
26293be42f9SDavide Caratti 	int ret;
26393be42f9SDavide Caratti 
26493be42f9SDavide Caratti 	tcf_lastuse_update(&police->tcf_tm);
26550dc9a85SAhmed S. Darwish 	bstats_update(this_cpu_ptr(police->common.cpu_bstats), skb);
2664bba3925SPatrick McHardy 
2672d550dbaSDavide Caratti 	ret = READ_ONCE(police->tcf_action);
2682d550dbaSDavide Caratti 	p = rcu_dereference_bh(police->params);
2692d550dbaSDavide Caratti 
2702d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate) {
2711c0d32fdSEric Dumazet 		struct gnet_stats_rate_est64 sample;
2721c0d32fdSEric Dumazet 
2731c0d32fdSEric Dumazet 		if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
2742d550dbaSDavide Caratti 		    sample.bps >= p->tcfp_ewma_rate)
27593be42f9SDavide Caratti 			goto inc_overlimits;
2764bba3925SPatrick McHardy 	}
2774bba3925SPatrick McHardy 
2784ddc844eSDavide Caratti 	if (tcf_police_mtu_check(skb, p->tcfp_mtu)) {
2792ffe0395SBaowen Zheng 		if (!p->rate_present && !p->pps_present) {
2802d550dbaSDavide Caratti 			ret = p->tcfp_result;
2812d550dbaSDavide Caratti 			goto end;
2824bba3925SPatrick McHardy 		}
2834bba3925SPatrick McHardy 
284d2de875cSEric Dumazet 		now = ktime_get_ns();
285f2cbd485SDavide Caratti 		spin_lock_bh(&police->tcfp_lock);
286f2cbd485SDavide Caratti 		toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
2872d550dbaSDavide Caratti 		if (p->peak_present) {
288f2cbd485SDavide Caratti 			ptoks = toks + police->tcfp_ptoks;
2892d550dbaSDavide Caratti 			if (ptoks > p->tcfp_mtu_ptoks)
2902d550dbaSDavide Caratti 				ptoks = p->tcfp_mtu_ptoks;
2912d550dbaSDavide Caratti 			ptoks -= (s64)psched_l2t_ns(&p->peak,
292c6d14ff1SJiri Pirko 						    qdisc_pkt_len(skb));
2934bba3925SPatrick McHardy 		}
2942ffe0395SBaowen Zheng 		if (p->rate_present) {
295f2cbd485SDavide Caratti 			toks += police->tcfp_toks;
2962d550dbaSDavide Caratti 			if (toks > p->tcfp_burst)
2972d550dbaSDavide Caratti 				toks = p->tcfp_burst;
2982d550dbaSDavide Caratti 			toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
2992ffe0395SBaowen Zheng 		} else if (p->pps_present) {
3002ffe0395SBaowen Zheng 			ppstoks = min_t(s64, now - police->tcfp_t_c, p->tcfp_pkt_burst);
3012ffe0395SBaowen Zheng 			ppstoks += police->tcfp_pkttoks;
3022ffe0395SBaowen Zheng 			if (ppstoks > p->tcfp_pkt_burst)
3032ffe0395SBaowen Zheng 				ppstoks = p->tcfp_pkt_burst;
3042ffe0395SBaowen Zheng 			ppstoks -= (s64)psched_pkt2t_ns(&p->ppsrate, 1);
3052ffe0395SBaowen Zheng 		}
3062ffe0395SBaowen Zheng 		if ((toks | ptoks | ppstoks) >= 0) {
307f2cbd485SDavide Caratti 			police->tcfp_t_c = now;
308f2cbd485SDavide Caratti 			police->tcfp_toks = toks;
309f2cbd485SDavide Caratti 			police->tcfp_ptoks = ptoks;
3102ffe0395SBaowen Zheng 			police->tcfp_pkttoks = ppstoks;
311f2cbd485SDavide Caratti 			spin_unlock_bh(&police->tcfp_lock);
3122d550dbaSDavide Caratti 			ret = p->tcfp_result;
31393be42f9SDavide Caratti 			goto inc_drops;
3144bba3925SPatrick McHardy 		}
315f2cbd485SDavide Caratti 		spin_unlock_bh(&police->tcfp_lock);
3164bba3925SPatrick McHardy 	}
3174bba3925SPatrick McHardy 
31893be42f9SDavide Caratti inc_overlimits:
31993be42f9SDavide Caratti 	qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
32093be42f9SDavide Caratti inc_drops:
32193be42f9SDavide Caratti 	if (ret == TC_ACT_SHOT)
32293be42f9SDavide Caratti 		qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
3232d550dbaSDavide Caratti end:
32493be42f9SDavide Caratti 	return ret;
3254bba3925SPatrick McHardy }
3264bba3925SPatrick McHardy 
3272d550dbaSDavide Caratti static void tcf_police_cleanup(struct tc_action *a)
3282d550dbaSDavide Caratti {
3292d550dbaSDavide Caratti 	struct tcf_police *police = to_police(a);
3302d550dbaSDavide Caratti 	struct tcf_police_params *p;
3312d550dbaSDavide Caratti 
3322d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params, 1);
3332d550dbaSDavide Caratti 	if (p)
3342d550dbaSDavide Caratti 		kfree_rcu(p, rcu);
3352d550dbaSDavide Caratti }
3362d550dbaSDavide Caratti 
33712f02b6bSPieter Jansen van Vuuren static void tcf_police_stats_update(struct tc_action *a,
3384b61d3e8SPo Liu 				    u64 bytes, u64 packets, u64 drops,
33912f02b6bSPieter Jansen van Vuuren 				    u64 lastuse, bool hw)
34012f02b6bSPieter Jansen van Vuuren {
34112f02b6bSPieter Jansen van Vuuren 	struct tcf_police *police = to_police(a);
34212f02b6bSPieter Jansen van Vuuren 	struct tcf_t *tm = &police->tcf_tm;
34312f02b6bSPieter Jansen van Vuuren 
3444b61d3e8SPo Liu 	tcf_action_update_stats(a, bytes, packets, drops, hw);
34512f02b6bSPieter Jansen van Vuuren 	tm->lastuse = max_t(u64, tm->lastuse, lastuse);
34612f02b6bSPieter Jansen van Vuuren }
34712f02b6bSPieter Jansen van Vuuren 
3482ac06347SJamal Hadi Salim static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
3495a7a5555SJamal Hadi Salim 			       int bind, int ref)
3504bba3925SPatrick McHardy {
35127a884dcSArnaldo Carvalho de Melo 	unsigned char *b = skb_tail_pointer(skb);
352a85a970aSWANG Cong 	struct tcf_police *police = to_police(a);
3532d550dbaSDavide Caratti 	struct tcf_police_params *p;
3540f04cfd0SJeff Mahoney 	struct tc_police opt = {
3550f04cfd0SJeff Mahoney 		.index = police->tcf_index,
356036bb443SVlad Buslov 		.refcnt = refcount_read(&police->tcf_refcnt) - ref,
357036bb443SVlad Buslov 		.bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
3580f04cfd0SJeff Mahoney 	};
3593d3ed181SJamal Hadi Salim 	struct tcf_t t;
3604bba3925SPatrick McHardy 
361e329bc42SVlad Buslov 	spin_lock_bh(&police->tcf_lock);
362e329bc42SVlad Buslov 	opt.action = police->tcf_action;
3632d550dbaSDavide Caratti 	p = rcu_dereference_protected(police->params,
3642d550dbaSDavide Caratti 				      lockdep_is_held(&police->tcf_lock));
3652d550dbaSDavide Caratti 	opt.mtu = p->tcfp_mtu;
3662d550dbaSDavide Caratti 	opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
367d1967e49SDavid Dai 	if (p->rate_present) {
3682d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.rate, &p->rate);
369d1967e49SDavid Dai 		if ((police->params->rate.rate_bytes_ps >= (1ULL << 32)) &&
370d1967e49SDavid Dai 		    nla_put_u64_64bit(skb, TCA_POLICE_RATE64,
371d1967e49SDavid Dai 				      police->params->rate.rate_bytes_ps,
372d1967e49SDavid Dai 				      TCA_POLICE_PAD))
373d1967e49SDavid Dai 			goto nla_put_failure;
374d1967e49SDavid Dai 	}
375d1967e49SDavid Dai 	if (p->peak_present) {
3762d550dbaSDavide Caratti 		psched_ratecfg_getrate(&opt.peakrate, &p->peak);
377d1967e49SDavid Dai 		if ((police->params->peak.rate_bytes_ps >= (1ULL << 32)) &&
378d1967e49SDavid Dai 		    nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64,
379d1967e49SDavid Dai 				      police->params->peak.rate_bytes_ps,
380d1967e49SDavid Dai 				      TCA_POLICE_PAD))
381d1967e49SDavid Dai 			goto nla_put_failure;
382d1967e49SDavid Dai 	}
3832ffe0395SBaowen Zheng 	if (p->pps_present) {
3842ffe0395SBaowen Zheng 		if (nla_put_u64_64bit(skb, TCA_POLICE_PKTRATE64,
3852ffe0395SBaowen Zheng 				      police->params->ppsrate.rate_pkts_ps,
3862ffe0395SBaowen Zheng 				      TCA_POLICE_PAD))
3872ffe0395SBaowen Zheng 			goto nla_put_failure;
3882ffe0395SBaowen Zheng 		if (nla_put_u64_64bit(skb, TCA_POLICE_PKTBURST64,
3892ffe0395SBaowen Zheng 				      PSCHED_NS2TICKS(p->tcfp_pkt_burst),
3902ffe0395SBaowen Zheng 				      TCA_POLICE_PAD))
3912ffe0395SBaowen Zheng 			goto nla_put_failure;
3922ffe0395SBaowen Zheng 	}
3931b34ec43SDavid S. Miller 	if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
3941b34ec43SDavid S. Miller 		goto nla_put_failure;
3952d550dbaSDavide Caratti 	if (p->tcfp_result &&
3962d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
3971b34ec43SDavid S. Miller 		goto nla_put_failure;
3982d550dbaSDavide Caratti 	if (p->tcfp_ewma_rate &&
3992d550dbaSDavide Caratti 	    nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
4001b34ec43SDavid S. Miller 		goto nla_put_failure;
4013d3ed181SJamal Hadi Salim 
402985fd98aSDavide Caratti 	tcf_tm_dump(&t, &police->tcf_tm);
4033d3ed181SJamal Hadi Salim 	if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
4043d3ed181SJamal Hadi Salim 		goto nla_put_failure;
405e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
4063d3ed181SJamal Hadi Salim 
4074bba3925SPatrick McHardy 	return skb->len;
4084bba3925SPatrick McHardy 
4097ba699c6SPatrick McHardy nla_put_failure:
410e329bc42SVlad Buslov 	spin_unlock_bh(&police->tcf_lock);
411dc5fc579SArnaldo Carvalho de Melo 	nlmsg_trim(skb, b);
4124bba3925SPatrick McHardy 	return -1;
4134bba3925SPatrick McHardy }
4144bba3925SPatrick McHardy 
415f061b48cSCong Wang static int tcf_police_search(struct net *net, struct tc_action **a, u32 index)
416ddf97ccdSWANG Cong {
417ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
418ddf97ccdSWANG Cong 
41965a206c0SChris Mi 	return tcf_idr_search(tn, a, index);
420ddf97ccdSWANG Cong }
421ddf97ccdSWANG Cong 
422*b50e462bSIdo Schimmel static int tcf_police_act_to_flow_act(int tc_act, u32 *extval,
423*b50e462bSIdo Schimmel 				      struct netlink_ext_ack *extack)
424b8cd5831SJianbo Liu {
425b8cd5831SJianbo Liu 	int act_id = -EOPNOTSUPP;
426b8cd5831SJianbo Liu 
427b8cd5831SJianbo Liu 	if (!TC_ACT_EXT_OPCODE(tc_act)) {
428b8cd5831SJianbo Liu 		if (tc_act == TC_ACT_OK)
429b8cd5831SJianbo Liu 			act_id = FLOW_ACTION_ACCEPT;
430b8cd5831SJianbo Liu 		else if (tc_act ==  TC_ACT_SHOT)
431b8cd5831SJianbo Liu 			act_id = FLOW_ACTION_DROP;
432b8cd5831SJianbo Liu 		else if (tc_act == TC_ACT_PIPE)
433b8cd5831SJianbo Liu 			act_id = FLOW_ACTION_PIPE;
434*b50e462bSIdo Schimmel 		else if (tc_act == TC_ACT_RECLASSIFY)
435*b50e462bSIdo Schimmel 			NL_SET_ERR_MSG_MOD(extack, "Offload not supported when conform/exceed action is \"reclassify\"");
436*b50e462bSIdo Schimmel 		else
437*b50e462bSIdo Schimmel 			NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload");
438b8cd5831SJianbo Liu 	} else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_GOTO_CHAIN)) {
439b8cd5831SJianbo Liu 		act_id = FLOW_ACTION_GOTO;
440b8cd5831SJianbo Liu 		*extval = tc_act & TC_ACT_EXT_VAL_MASK;
441b8cd5831SJianbo Liu 	} else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_JUMP)) {
442b8cd5831SJianbo Liu 		act_id = FLOW_ACTION_JUMP;
443b8cd5831SJianbo Liu 		*extval = tc_act & TC_ACT_EXT_VAL_MASK;
444*b50e462bSIdo Schimmel 	} else if (tc_act == TC_ACT_UNSPEC) {
445*b50e462bSIdo Schimmel 		NL_SET_ERR_MSG_MOD(extack, "Offload not supported when conform/exceed action is \"continue\"");
446*b50e462bSIdo Schimmel 	} else {
447*b50e462bSIdo Schimmel 		NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload");
448b8cd5831SJianbo Liu 	}
449b8cd5831SJianbo Liu 
450b8cd5831SJianbo Liu 	return act_id;
451b8cd5831SJianbo Liu }
452b8cd5831SJianbo Liu 
453c54e1d92SBaowen Zheng static int tcf_police_offload_act_setup(struct tc_action *act, void *entry_data,
454c2ccf84eSIdo Schimmel 					u32 *index_inc, bool bind,
455c2ccf84eSIdo Schimmel 					struct netlink_ext_ack *extack)
456c54e1d92SBaowen Zheng {
457c54e1d92SBaowen Zheng 	if (bind) {
458c54e1d92SBaowen Zheng 		struct flow_action_entry *entry = entry_data;
459b8cd5831SJianbo Liu 		struct tcf_police *police = to_police(act);
460b8cd5831SJianbo Liu 		struct tcf_police_params *p;
461b8cd5831SJianbo Liu 		int act_id;
462b8cd5831SJianbo Liu 
463b8cd5831SJianbo Liu 		p = rcu_dereference_protected(police->params,
464b8cd5831SJianbo Liu 					      lockdep_is_held(&police->tcf_lock));
465c54e1d92SBaowen Zheng 
466c54e1d92SBaowen Zheng 		entry->id = FLOW_ACTION_POLICE;
467c54e1d92SBaowen Zheng 		entry->police.burst = tcf_police_burst(act);
468c54e1d92SBaowen Zheng 		entry->police.rate_bytes_ps =
469c54e1d92SBaowen Zheng 			tcf_police_rate_bytes_ps(act);
470b8cd5831SJianbo Liu 		entry->police.peakrate_bytes_ps = tcf_police_peakrate_bytes_ps(act);
471b8cd5831SJianbo Liu 		entry->police.avrate = tcf_police_tcfp_ewma_rate(act);
472b8cd5831SJianbo Liu 		entry->police.overhead = tcf_police_rate_overhead(act);
473c54e1d92SBaowen Zheng 		entry->police.burst_pkt = tcf_police_burst_pkt(act);
474c54e1d92SBaowen Zheng 		entry->police.rate_pkt_ps =
475c54e1d92SBaowen Zheng 			tcf_police_rate_pkt_ps(act);
476c54e1d92SBaowen Zheng 		entry->police.mtu = tcf_police_tcfp_mtu(act);
477b8cd5831SJianbo Liu 
478b8cd5831SJianbo Liu 		act_id = tcf_police_act_to_flow_act(police->tcf_action,
479*b50e462bSIdo Schimmel 						    &entry->police.exceed.extval,
480*b50e462bSIdo Schimmel 						    extack);
481b8cd5831SJianbo Liu 		if (act_id < 0)
482b8cd5831SJianbo Liu 			return act_id;
483b8cd5831SJianbo Liu 
484b8cd5831SJianbo Liu 		entry->police.exceed.act_id = act_id;
485b8cd5831SJianbo Liu 
486b8cd5831SJianbo Liu 		act_id = tcf_police_act_to_flow_act(p->tcfp_result,
487*b50e462bSIdo Schimmel 						    &entry->police.notexceed.extval,
488*b50e462bSIdo Schimmel 						    extack);
489b8cd5831SJianbo Liu 		if (act_id < 0)
490b8cd5831SJianbo Liu 			return act_id;
491b8cd5831SJianbo Liu 
492b8cd5831SJianbo Liu 		entry->police.notexceed.act_id = act_id;
493b8cd5831SJianbo Liu 
494c54e1d92SBaowen Zheng 		*index_inc = 1;
495c54e1d92SBaowen Zheng 	} else {
4968cbfe939SBaowen Zheng 		struct flow_offload_action *fl_action = entry_data;
4978cbfe939SBaowen Zheng 
4988cbfe939SBaowen Zheng 		fl_action->id = FLOW_ACTION_POLICE;
499c54e1d92SBaowen Zheng 	}
500c54e1d92SBaowen Zheng 
501c54e1d92SBaowen Zheng 	return 0;
502c54e1d92SBaowen Zheng }
503c54e1d92SBaowen Zheng 
5044bba3925SPatrick McHardy MODULE_AUTHOR("Alexey Kuznetsov");
5054bba3925SPatrick McHardy MODULE_DESCRIPTION("Policing actions");
5064bba3925SPatrick McHardy MODULE_LICENSE("GPL");
5074bba3925SPatrick McHardy 
5084bba3925SPatrick McHardy static struct tc_action_ops act_police_ops = {
5094bba3925SPatrick McHardy 	.kind		=	"police",
510eddd2cf1SEli Cohen 	.id		=	TCA_ID_POLICE,
5114bba3925SPatrick McHardy 	.owner		=	THIS_MODULE,
51212f02b6bSPieter Jansen van Vuuren 	.stats_update	=	tcf_police_stats_update,
5132ac06347SJamal Hadi Salim 	.act		=	tcf_police_act,
5142ac06347SJamal Hadi Salim 	.dump		=	tcf_police_dump,
5152ac06347SJamal Hadi Salim 	.init		=	tcf_police_init,
5162ac06347SJamal Hadi Salim 	.walk		=	tcf_police_walker,
517ddf97ccdSWANG Cong 	.lookup		=	tcf_police_search,
5182d550dbaSDavide Caratti 	.cleanup	=	tcf_police_cleanup,
519c54e1d92SBaowen Zheng 	.offload_act_setup =	tcf_police_offload_act_setup,
520a85a970aSWANG Cong 	.size		=	sizeof(struct tcf_police),
521ddf97ccdSWANG Cong };
522ddf97ccdSWANG Cong 
523ddf97ccdSWANG Cong static __net_init int police_init_net(struct net *net)
524ddf97ccdSWANG Cong {
525ddf97ccdSWANG Cong 	struct tc_action_net *tn = net_generic(net, police_net_id);
526ddf97ccdSWANG Cong 
527981471bdSCong Wang 	return tc_action_net_init(net, tn, &act_police_ops);
528ddf97ccdSWANG Cong }
529ddf97ccdSWANG Cong 
530039af9c6SCong Wang static void __net_exit police_exit_net(struct list_head *net_list)
531ddf97ccdSWANG Cong {
532039af9c6SCong Wang 	tc_action_net_exit(net_list, police_net_id);
533ddf97ccdSWANG Cong }
534ddf97ccdSWANG Cong 
535ddf97ccdSWANG Cong static struct pernet_operations police_net_ops = {
536ddf97ccdSWANG Cong 	.init = police_init_net,
537039af9c6SCong Wang 	.exit_batch = police_exit_net,
538ddf97ccdSWANG Cong 	.id   = &police_net_id,
539ddf97ccdSWANG Cong 	.size = sizeof(struct tc_action_net),
5404bba3925SPatrick McHardy };
5414bba3925SPatrick McHardy 
5425a7a5555SJamal Hadi Salim static int __init police_init_module(void)
5434bba3925SPatrick McHardy {
544ddf97ccdSWANG Cong 	return tcf_register_action(&act_police_ops, &police_net_ops);
5454bba3925SPatrick McHardy }
5464bba3925SPatrick McHardy 
5475a7a5555SJamal Hadi Salim static void __exit police_cleanup_module(void)
5484bba3925SPatrick McHardy {
549ddf97ccdSWANG Cong 	tcf_unregister_action(&act_police_ops, &police_net_ops);
5504bba3925SPatrick McHardy }
5514bba3925SPatrick McHardy 
5524bba3925SPatrick McHardy module_init(police_init_module);
5534bba3925SPatrick McHardy module_exit(police_cleanup_module);
554