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/gso.h>
20d6124d6bSDavide Caratti #include <net/netlink.h>
21fa762da9SPieter Jansen van Vuuren #include <net/pkt_cls.h>
22871cf386SPedro Tammela #include <net/tc_act/tc_police.h>
231e9b3d53SPatrick McHardy #include <net/tc_wrapper.h>
244bba3925SPatrick McHardy
254bba3925SPatrick McHardy /* Each policer is serialized by its individual spinlock */
26a85a970aSWANG Cong
27ddf97ccdSWANG Cong static struct tc_action_ops act_police_ops;
2853b2bf3fSPatrick McHardy
2953b2bf3fSPatrick McHardy static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
3053b2bf3fSPatrick McHardy [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE },
3153b2bf3fSPatrick McHardy [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE },
3253b2bf3fSPatrick McHardy [TCA_POLICE_AVRATE] = { .type = NLA_U32 },
33d1967e49SDavid Dai [TCA_POLICE_RESULT] = { .type = NLA_U32 },
34d1967e49SDavid Dai [TCA_POLICE_RATE64] = { .type = NLA_U64 },
352ffe0395SBaowen Zheng [TCA_POLICE_PEAKRATE64] = { .type = NLA_U64 },
362ffe0395SBaowen Zheng [TCA_POLICE_PKTRATE64] = { .type = NLA_U64, .min = 1 },
3753b2bf3fSPatrick McHardy [TCA_POLICE_PKTBURST64] = { .type = NLA_U64, .min = 1 },
3853b2bf3fSPatrick McHardy };
392ac06347SJamal Hadi Salim
tcf_police_init(struct net * net,struct nlattr * nla,struct nlattr * est,struct tc_action ** a,struct tcf_proto * tp,u32 flags,struct netlink_ext_ack * extack)40a85a970aSWANG Cong static int tcf_police_init(struct net *net, struct nlattr *nla,
41abbb0d33SVlad Buslov struct nlattr *est, struct tc_action **a,
42589dad6dSAlexander Aring struct tcf_proto *tp, u32 flags,
434bba3925SPatrick McHardy struct netlink_ext_ack *extack)
44fd6d4338SDavide Caratti {
45695176bfSCong Wang int ret = 0, tcfp_result = TC_ACT_OK, err, size;
467ba699c6SPatrick McHardy bool bind = flags & TCA_ACT_FLAGS_BIND;
47d6124d6bSDavide Caratti struct nlattr *tb[TCA_POLICE_MAX + 1];
484bba3925SPatrick McHardy struct tcf_chain *goto_ch = NULL;
49e9ce1cd3SDavid S. Miller struct tc_police *parm;
504bba3925SPatrick McHardy struct tcf_police *police;
51acd0a7abSZhengchao Shao struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
522d550dbaSDavide Caratti struct tc_action_net *tn = net_generic(net, act_police_ops.net_id);
530852e455SWANG Cong struct tcf_police_params *new;
547be8ef2cSDmytro Linkin bool exists = false;
55d1967e49SDavid Dai u32 index;
562ffe0395SBaowen Zheng u64 rate64, prate64;
574bba3925SPatrick McHardy u64 pps, ppsburst;
58cee63723SPatrick McHardy
594bba3925SPatrick McHardy if (nla == NULL)
604bba3925SPatrick McHardy return -EINVAL;
618cb08174SJohannes Berg
628cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla,
63cee63723SPatrick McHardy police_policy, NULL);
64cee63723SPatrick McHardy if (err < 0)
65cee63723SPatrick McHardy return err;
667ba699c6SPatrick McHardy
671e9b3d53SPatrick McHardy if (tb[TCA_POLICE_TBF] == NULL)
687ba699c6SPatrick McHardy return -EINVAL;
691e9b3d53SPatrick McHardy size = nla_len(tb[TCA_POLICE_TBF]);
704bba3925SPatrick McHardy if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
714bba3925SPatrick McHardy return -EINVAL;
720852e455SWANG Cong
737be8ef2cSDmytro Linkin parm = nla_data(tb[TCA_POLICE_TBF]);
747be8ef2cSDmytro Linkin index = parm->index;
750190c1d4SVlad Buslov err = tcf_idr_check_alloc(tn, &index, a, bind);
760190c1d4SVlad Buslov if (err < 0)
770190c1d4SVlad Buslov return err;
780852e455SWANG Cong exists = err;
790852e455SWANG Cong if (exists && bind)
800852e455SWANG Cong return 0;
810852e455SWANG Cong
827be8ef2cSDmytro Linkin if (!exists) {
8340bd094dSBaowen Zheng ret = tcf_idr_create(tn, index, NULL, a,
840190c1d4SVlad Buslov &act_police_ops, bind, true, flags);
857be8ef2cSDmytro Linkin if (ret) {
86a03e6fe5SWANG Cong tcf_idr_cleanup(tn, index);
870190c1d4SVlad Buslov return ret;
88a03e6fe5SWANG Cong }
89484afd1bSDavide Caratti ret = ACT_P_CREATED;
90695176bfSCong Wang spin_lock_init(&(to_police(*a)->tcfp_lock));
9165a206c0SChris Mi } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
920852e455SWANG Cong tcf_idr_release(*a, bind);
93e9ce1cd3SDavid S. Miller return -EEXIST;
94d6124d6bSDavide Caratti }
95d6124d6bSDavide Caratti err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
96d6124d6bSDavide Caratti if (err < 0)
974bba3925SPatrick McHardy goto release_idr;
98a85a970aSWANG Cong
994bba3925SPatrick McHardy police = to_police(*a);
1004bba3925SPatrick McHardy if (parm->rate.rate) {
101e9bc3fa2SAlexander Aring err = -ENOMEM;
1024bba3925SPatrick McHardy R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL);
1034bba3925SPatrick McHardy if (R_tab == NULL)
104c1b56878SStephen Hemminger goto failure;
1054bba3925SPatrick McHardy
1064bba3925SPatrick McHardy if (parm->peakrate.rate) {
107e9bc3fa2SAlexander Aring P_tab = qdisc_get_rtab(&parm->peakrate,
10871bcb09aSStephen Hemminger tb[TCA_POLICE_PEAKRATE], NULL);
1094bba3925SPatrick McHardy if (P_tab == NULL)
1104bba3925SPatrick McHardy goto failure;
1114bba3925SPatrick McHardy }
11271bcb09aSStephen Hemminger }
11371bcb09aSStephen Hemminger
11493be42f9SDavide Caratti if (est) {
11593be42f9SDavide Caratti err = gen_replace_estimator(&police->tcf_bstats,
11671bcb09aSStephen Hemminger police->common.cpu_bstats,
117edb09eb1SEric Dumazet &police->tcf_rate_est,
11829cbcd85SAhmed S. Darwish &police->tcf_lock,
11971bcb09aSStephen Hemminger false, est);
12074030603SWANG Cong if (err)
121a883bf56SJarek Poplawski goto failure;
122a883bf56SJarek Poplawski } else if (tb[TCA_POLICE_AVRATE] &&
1231c0d32fdSEric Dumazet (ret == ACT_P_CREATED ||
124a883bf56SJarek Poplawski !gen_estimator_active(&police->tcf_rate_est))) {
12574030603SWANG Cong err = -EINVAL;
12671bcb09aSStephen Hemminger goto failure;
12771bcb09aSStephen Hemminger }
128fd6d4338SDavide Caratti
129fd6d4338SDavide Caratti if (tb[TCA_POLICE_RESULT]) {
130fd6d4338SDavide Caratti tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
131fd6d4338SDavide Caratti if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) {
132fd6d4338SDavide Caratti NL_SET_ERR_MSG(extack,
133fd6d4338SDavide Caratti "goto chain not allowed on fallback");
134fd6d4338SDavide Caratti err = -EINVAL;
135fd6d4338SDavide Caratti goto failure;
136fd6d4338SDavide Caratti }
137fd6d4338SDavide Caratti }
1382ffe0395SBaowen Zheng
1392ffe0395SBaowen Zheng if ((tb[TCA_POLICE_PKTRATE64] && !tb[TCA_POLICE_PKTBURST64]) ||
1402ffe0395SBaowen Zheng (!tb[TCA_POLICE_PKTRATE64] && tb[TCA_POLICE_PKTBURST64])) {
1412ffe0395SBaowen Zheng NL_SET_ERR_MSG(extack,
1422ffe0395SBaowen Zheng "Both or neither packet-per-second burst and rate must be provided");
1432ffe0395SBaowen Zheng err = -EINVAL;
1442ffe0395SBaowen Zheng goto failure;
1452ffe0395SBaowen Zheng }
1462ffe0395SBaowen Zheng
1472ffe0395SBaowen Zheng if (tb[TCA_POLICE_PKTRATE64] && R_tab) {
1482ffe0395SBaowen Zheng NL_SET_ERR_MSG(extack,
1492ffe0395SBaowen Zheng "packet-per-second and byte-per-second rate limits not allowed in same action");
1502ffe0395SBaowen Zheng err = -EINVAL;
1512ffe0395SBaowen Zheng goto failure;
1522ffe0395SBaowen Zheng }
1532d550dbaSDavide Caratti
1542d550dbaSDavide Caratti new = kzalloc(sizeof(*new), GFP_KERNEL);
1552d550dbaSDavide Caratti if (unlikely(!new)) {
1562d550dbaSDavide Caratti err = -ENOMEM;
1572d550dbaSDavide Caratti goto failure;
1582d550dbaSDavide Caratti }
15971bcb09aSStephen Hemminger
160fd6d4338SDavide Caratti /* No failure allowed after this point */
1612d550dbaSDavide Caratti new->tcfp_result = tcfp_result;
1622d550dbaSDavide Caratti new->tcfp_mtu = parm->mtu;
1632d550dbaSDavide Caratti if (!new->tcfp_mtu) {
164c6d14ff1SJiri Pirko new->tcfp_mtu = ~0;
1652d550dbaSDavide Caratti if (R_tab)
1664bba3925SPatrick McHardy new->tcfp_mtu = 255 << R_tab->rate.cell_log;
167c6d14ff1SJiri Pirko }
1682d550dbaSDavide Caratti if (R_tab) {
169d1967e49SDavid Dai new->rate_present = true;
170d1967e49SDavid Dai rate64 = tb[TCA_POLICE_RATE64] ?
171d1967e49SDavid Dai nla_get_u64(tb[TCA_POLICE_RATE64]) : 0;
172c6d14ff1SJiri Pirko psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64);
173c6d14ff1SJiri Pirko qdisc_put_rtab(R_tab);
1742d550dbaSDavide Caratti } else {
175c6d14ff1SJiri Pirko new->rate_present = false;
176c6d14ff1SJiri Pirko }
1772d550dbaSDavide Caratti if (P_tab) {
178d1967e49SDavid Dai new->peak_present = true;
179d1967e49SDavid Dai prate64 = tb[TCA_POLICE_PEAKRATE64] ?
180d1967e49SDavid Dai nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0;
181c6d14ff1SJiri Pirko psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64);
182c6d14ff1SJiri Pirko qdisc_put_rtab(P_tab);
1832d550dbaSDavide Caratti } else {
1844bba3925SPatrick McHardy new->peak_present = false;
1854bba3925SPatrick McHardy }
1862d550dbaSDavide Caratti
187f2cbd485SDavide Caratti new->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
1882d550dbaSDavide Caratti if (new->peak_present)
1892d550dbaSDavide Caratti new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak,
1904bba3925SPatrick McHardy new->tcfp_mtu);
1917ba699c6SPatrick McHardy
1922d550dbaSDavide Caratti if (tb[TCA_POLICE_AVRATE])
1934bba3925SPatrick McHardy new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
1942ffe0395SBaowen Zheng
1952ffe0395SBaowen Zheng if (tb[TCA_POLICE_PKTRATE64]) {
1962ffe0395SBaowen Zheng pps = nla_get_u64(tb[TCA_POLICE_PKTRATE64]);
1972ffe0395SBaowen Zheng ppsburst = nla_get_u64(tb[TCA_POLICE_PKTBURST64]);
1982ffe0395SBaowen Zheng new->pps_present = true;
1992ffe0395SBaowen Zheng new->tcfp_pkt_burst = PSCHED_TICKS2NS(ppsburst);
2002ffe0395SBaowen Zheng psched_ppscfg_precompute(&new->ppsrate, pps);
2012ffe0395SBaowen Zheng }
2022d550dbaSDavide Caratti
203f2cbd485SDavide Caratti spin_lock_bh(&police->tcf_lock);
204f2cbd485SDavide Caratti spin_lock_bh(&police->tcfp_lock);
205f2cbd485SDavide Caratti police->tcfp_t_c = ktime_get_ns();
206f2cbd485SDavide Caratti police->tcfp_toks = new->tcfp_burst;
207f2cbd485SDavide Caratti if (new->peak_present)
208f2cbd485SDavide Caratti police->tcfp_ptoks = new->tcfp_mtu_ptoks;
209d6124d6bSDavide Caratti spin_unlock_bh(&police->tcfp_lock);
210445d3749SPaul E. McKenney goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
2112d550dbaSDavide Caratti new = rcu_replace_pointer(police->params,
2122d550dbaSDavide Caratti new,
213e9ce1cd3SDavid S. Miller lockdep_is_held(&police->tcf_lock));
2144bba3925SPatrick McHardy spin_unlock_bh(&police->tcf_lock);
215d6124d6bSDavide Caratti
216d6124d6bSDavide Caratti if (goto_ch)
2172d550dbaSDavide Caratti tcf_chain_put_by_act(goto_ch);
2182d550dbaSDavide Caratti if (new)
2192d550dbaSDavide Caratti kfree_rcu(new, rcu);
2204bba3925SPatrick McHardy
2214bba3925SPatrick McHardy return ret;
2224bba3925SPatrick McHardy
22371bcb09aSStephen Hemminger failure:
22471bcb09aSStephen Hemminger qdisc_put_rtab(P_tab);
225d6124d6bSDavide Caratti qdisc_put_rtab(R_tab);
226d6124d6bSDavide Caratti if (goto_ch)
227d6124d6bSDavide Caratti tcf_chain_put_by_act(goto_ch);
2285bf7f818SDavide Caratti release_idr:
2294bba3925SPatrick McHardy tcf_idr_release(*a, bind);
2304bba3925SPatrick McHardy return err;
2314bba3925SPatrick McHardy }
2324ddc844eSDavide Caratti
tcf_police_mtu_check(struct sk_buff * skb,u32 limit)2334ddc844eSDavide Caratti static bool tcf_police_mtu_check(struct sk_buff *skb, u32 limit)
2344ddc844eSDavide Caratti {
2354ddc844eSDavide Caratti u32 len;
2364ddc844eSDavide Caratti
2374ddc844eSDavide Caratti if (skb_is_gso(skb))
2384ddc844eSDavide Caratti return skb_gso_validate_mac_len(skb, limit);
2394ddc844eSDavide Caratti
2404ddc844eSDavide Caratti len = qdisc_pkt_len(skb);
2414ddc844eSDavide Caratti if (skb_at_tc_ingress(skb))
2424ddc844eSDavide Caratti len += skb->mac_len;
2434ddc844eSDavide Caratti
2444ddc844eSDavide Caratti return len <= limit;
2454ddc844eSDavide Caratti }
246871cf386SPedro Tammela
tcf_police_act(struct sk_buff * skb,const struct tc_action * a,struct tcf_result * res)247871cf386SPedro Tammela TC_INDIRECT_SCOPE int tcf_police_act(struct sk_buff *skb,
2484bba3925SPatrick McHardy const struct tc_action *a,
2494bba3925SPatrick McHardy struct tcf_result *res)
250a85a970aSWANG Cong {
2512ffe0395SBaowen Zheng struct tcf_police *police = to_police(a);
2522d550dbaSDavide Caratti s64 now, toks, ppstoks = 0, ptoks = 0;
25393be42f9SDavide Caratti struct tcf_police_params *p;
25493be42f9SDavide Caratti int ret;
25593be42f9SDavide Caratti
25650dc9a85SAhmed S. Darwish tcf_lastuse_update(&police->tcf_tm);
2574bba3925SPatrick McHardy bstats_update(this_cpu_ptr(police->common.cpu_bstats), skb);
2582d550dbaSDavide Caratti
2592d550dbaSDavide Caratti ret = READ_ONCE(police->tcf_action);
2602d550dbaSDavide Caratti p = rcu_dereference_bh(police->params);
2612d550dbaSDavide Caratti
2621c0d32fdSEric Dumazet if (p->tcfp_ewma_rate) {
2631c0d32fdSEric Dumazet struct gnet_stats_rate_est64 sample;
2641c0d32fdSEric Dumazet
2652d550dbaSDavide Caratti if (!gen_estimator_read(&police->tcf_rate_est, &sample) ||
26693be42f9SDavide Caratti sample.bps >= p->tcfp_ewma_rate)
2674bba3925SPatrick McHardy goto inc_overlimits;
2684bba3925SPatrick McHardy }
2694ddc844eSDavide Caratti
2702ffe0395SBaowen Zheng if (tcf_police_mtu_check(skb, p->tcfp_mtu)) {
2712d550dbaSDavide Caratti if (!p->rate_present && !p->pps_present) {
2722d550dbaSDavide Caratti ret = p->tcfp_result;
2734bba3925SPatrick McHardy goto end;
2744bba3925SPatrick McHardy }
275d2de875cSEric Dumazet
276f2cbd485SDavide Caratti now = ktime_get_ns();
277f2cbd485SDavide Caratti spin_lock_bh(&police->tcfp_lock);
2782d550dbaSDavide Caratti toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst);
279f2cbd485SDavide Caratti if (p->peak_present) {
2802d550dbaSDavide Caratti ptoks = toks + police->tcfp_ptoks;
2812d550dbaSDavide Caratti if (ptoks > p->tcfp_mtu_ptoks)
2822d550dbaSDavide Caratti ptoks = p->tcfp_mtu_ptoks;
283c6d14ff1SJiri Pirko ptoks -= (s64)psched_l2t_ns(&p->peak,
2844bba3925SPatrick McHardy qdisc_pkt_len(skb));
2852ffe0395SBaowen Zheng }
286f2cbd485SDavide Caratti if (p->rate_present) {
2872d550dbaSDavide Caratti toks += police->tcfp_toks;
2882d550dbaSDavide Caratti if (toks > p->tcfp_burst)
2892d550dbaSDavide Caratti toks = p->tcfp_burst;
2902ffe0395SBaowen Zheng toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb));
2912ffe0395SBaowen Zheng } else if (p->pps_present) {
2922ffe0395SBaowen Zheng ppstoks = min_t(s64, now - police->tcfp_t_c, p->tcfp_pkt_burst);
2932ffe0395SBaowen Zheng ppstoks += police->tcfp_pkttoks;
2942ffe0395SBaowen Zheng if (ppstoks > p->tcfp_pkt_burst)
2952ffe0395SBaowen Zheng ppstoks = p->tcfp_pkt_burst;
2962ffe0395SBaowen Zheng ppstoks -= (s64)psched_pkt2t_ns(&p->ppsrate, 1);
2972ffe0395SBaowen Zheng }
298f2cbd485SDavide Caratti if ((toks | ptoks | ppstoks) >= 0) {
299f2cbd485SDavide Caratti police->tcfp_t_c = now;
300f2cbd485SDavide Caratti police->tcfp_toks = toks;
3012ffe0395SBaowen Zheng police->tcfp_ptoks = ptoks;
302f2cbd485SDavide Caratti police->tcfp_pkttoks = ppstoks;
3032d550dbaSDavide Caratti spin_unlock_bh(&police->tcfp_lock);
30493be42f9SDavide Caratti ret = p->tcfp_result;
3054bba3925SPatrick McHardy goto inc_drops;
306f2cbd485SDavide Caratti }
3074bba3925SPatrick McHardy spin_unlock_bh(&police->tcfp_lock);
3084bba3925SPatrick McHardy }
30993be42f9SDavide Caratti
31093be42f9SDavide Caratti inc_overlimits:
31193be42f9SDavide Caratti qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats));
31293be42f9SDavide Caratti inc_drops:
31393be42f9SDavide Caratti if (ret == TC_ACT_SHOT)
3142d550dbaSDavide Caratti qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats));
31593be42f9SDavide Caratti end:
3164bba3925SPatrick McHardy return ret;
3174bba3925SPatrick McHardy }
3182d550dbaSDavide Caratti
tcf_police_cleanup(struct tc_action * a)3192d550dbaSDavide Caratti static void tcf_police_cleanup(struct tc_action *a)
3202d550dbaSDavide Caratti {
3212d550dbaSDavide Caratti struct tcf_police *police = to_police(a);
3222d550dbaSDavide Caratti struct tcf_police_params *p;
3232d550dbaSDavide Caratti
3242d550dbaSDavide Caratti p = rcu_dereference_protected(police->params, 1);
3252d550dbaSDavide Caratti if (p)
3262d550dbaSDavide Caratti kfree_rcu(p, rcu);
3272d550dbaSDavide Caratti }
32812f02b6bSPieter Jansen van Vuuren
tcf_police_stats_update(struct tc_action * a,u64 bytes,u64 packets,u64 drops,u64 lastuse,bool hw)3294b61d3e8SPo Liu static void tcf_police_stats_update(struct tc_action *a,
33012f02b6bSPieter Jansen van Vuuren u64 bytes, u64 packets, u64 drops,
33112f02b6bSPieter Jansen van Vuuren u64 lastuse, bool hw)
33212f02b6bSPieter Jansen van Vuuren {
33312f02b6bSPieter Jansen van Vuuren struct tcf_police *police = to_police(a);
33412f02b6bSPieter Jansen van Vuuren struct tcf_t *tm = &police->tcf_tm;
3354b61d3e8SPo Liu
33612f02b6bSPieter Jansen van Vuuren tcf_action_update_stats(a, bytes, packets, drops, hw);
33712f02b6bSPieter Jansen van Vuuren tm->lastuse = max_t(u64, tm->lastuse, lastuse);
33812f02b6bSPieter Jansen van Vuuren }
3392ac06347SJamal Hadi Salim
tcf_police_dump(struct sk_buff * skb,struct tc_action * a,int bind,int ref)3405a7a5555SJamal Hadi Salim static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a,
3414bba3925SPatrick McHardy int bind, int ref)
34227a884dcSArnaldo Carvalho de Melo {
343a85a970aSWANG Cong unsigned char *b = skb_tail_pointer(skb);
3442d550dbaSDavide Caratti struct tcf_police *police = to_police(a);
3450f04cfd0SJeff Mahoney struct tcf_police_params *p;
3460f04cfd0SJeff Mahoney struct tc_police opt = {
347036bb443SVlad Buslov .index = police->tcf_index,
348036bb443SVlad Buslov .refcnt = refcount_read(&police->tcf_refcnt) - ref,
3490f04cfd0SJeff Mahoney .bindcnt = atomic_read(&police->tcf_bindcnt) - bind,
3503d3ed181SJamal Hadi Salim };
3514bba3925SPatrick McHardy struct tcf_t t;
352e329bc42SVlad Buslov
353e329bc42SVlad Buslov spin_lock_bh(&police->tcf_lock);
3542d550dbaSDavide Caratti opt.action = police->tcf_action;
3552d550dbaSDavide Caratti p = rcu_dereference_protected(police->params,
3562d550dbaSDavide Caratti lockdep_is_held(&police->tcf_lock));
3572d550dbaSDavide Caratti opt.mtu = p->tcfp_mtu;
358d1967e49SDavid Dai opt.burst = PSCHED_NS2TICKS(p->tcfp_burst);
3592d550dbaSDavide Caratti if (p->rate_present) {
360*682881eeSEric Dumazet psched_ratecfg_getrate(&opt.rate, &p->rate);
361d1967e49SDavid Dai if ((p->rate.rate_bytes_ps >= (1ULL << 32)) &&
362*682881eeSEric Dumazet nla_put_u64_64bit(skb, TCA_POLICE_RATE64,
363d1967e49SDavid Dai p->rate.rate_bytes_ps,
364d1967e49SDavid Dai TCA_POLICE_PAD))
365d1967e49SDavid Dai goto nla_put_failure;
366d1967e49SDavid Dai }
3672d550dbaSDavide Caratti if (p->peak_present) {
368*682881eeSEric Dumazet psched_ratecfg_getrate(&opt.peakrate, &p->peak);
369d1967e49SDavid Dai if ((p->peak.rate_bytes_ps >= (1ULL << 32)) &&
370*682881eeSEric Dumazet nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64,
371d1967e49SDavid Dai p->peak.rate_bytes_ps,
372d1967e49SDavid Dai TCA_POLICE_PAD))
373d1967e49SDavid Dai goto nla_put_failure;
3742ffe0395SBaowen Zheng }
3752ffe0395SBaowen Zheng if (p->pps_present) {
376*682881eeSEric Dumazet if (nla_put_u64_64bit(skb, TCA_POLICE_PKTRATE64,
3772ffe0395SBaowen Zheng p->ppsrate.rate_pkts_ps,
3782ffe0395SBaowen Zheng TCA_POLICE_PAD))
3792ffe0395SBaowen Zheng goto nla_put_failure;
3802ffe0395SBaowen Zheng if (nla_put_u64_64bit(skb, TCA_POLICE_PKTBURST64,
3812ffe0395SBaowen Zheng PSCHED_NS2TICKS(p->tcfp_pkt_burst),
3822ffe0395SBaowen Zheng TCA_POLICE_PAD))
3832ffe0395SBaowen Zheng goto nla_put_failure;
3841b34ec43SDavid S. Miller }
3851b34ec43SDavid S. Miller if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
3862d550dbaSDavide Caratti goto nla_put_failure;
3872d550dbaSDavide Caratti if (p->tcfp_result &&
3881b34ec43SDavid S. Miller nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result))
3892d550dbaSDavide Caratti goto nla_put_failure;
3902d550dbaSDavide Caratti if (p->tcfp_ewma_rate &&
3911b34ec43SDavid S. Miller nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate))
3923d3ed181SJamal Hadi Salim goto nla_put_failure;
393985fd98aSDavide Caratti
3943d3ed181SJamal Hadi Salim tcf_tm_dump(&t, &police->tcf_tm);
3953d3ed181SJamal Hadi Salim if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD))
396e329bc42SVlad Buslov goto nla_put_failure;
3973d3ed181SJamal Hadi Salim spin_unlock_bh(&police->tcf_lock);
3984bba3925SPatrick McHardy
3994bba3925SPatrick McHardy return skb->len;
4007ba699c6SPatrick McHardy
401e329bc42SVlad Buslov nla_put_failure:
402dc5fc579SArnaldo Carvalho de Melo spin_unlock_bh(&police->tcf_lock);
4034bba3925SPatrick McHardy nlmsg_trim(skb, b);
4044bba3925SPatrick McHardy return -1;
4054bba3925SPatrick McHardy }
406b50e462bSIdo Schimmel
tcf_police_act_to_flow_act(int tc_act,u32 * extval,struct netlink_ext_ack * extack)407b50e462bSIdo Schimmel static int tcf_police_act_to_flow_act(int tc_act, u32 *extval,
408b8cd5831SJianbo Liu struct netlink_ext_ack *extack)
409b8cd5831SJianbo Liu {
410b8cd5831SJianbo Liu int act_id = -EOPNOTSUPP;
411b8cd5831SJianbo Liu
412b8cd5831SJianbo Liu if (!TC_ACT_EXT_OPCODE(tc_act)) {
413b8cd5831SJianbo Liu if (tc_act == TC_ACT_OK)
414b8cd5831SJianbo Liu act_id = FLOW_ACTION_ACCEPT;
415b8cd5831SJianbo Liu else if (tc_act == TC_ACT_SHOT)
416b8cd5831SJianbo Liu act_id = FLOW_ACTION_DROP;
417b8cd5831SJianbo Liu else if (tc_act == TC_ACT_PIPE)
418b50e462bSIdo Schimmel act_id = FLOW_ACTION_PIPE;
419b50e462bSIdo Schimmel else if (tc_act == TC_ACT_RECLASSIFY)
420b50e462bSIdo Schimmel NL_SET_ERR_MSG_MOD(extack, "Offload not supported when conform/exceed action is \"reclassify\"");
421b50e462bSIdo Schimmel else
422b8cd5831SJianbo Liu NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload");
423b8cd5831SJianbo Liu } else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_GOTO_CHAIN)) {
424b8cd5831SJianbo Liu act_id = FLOW_ACTION_GOTO;
425b8cd5831SJianbo Liu *extval = tc_act & TC_ACT_EXT_VAL_MASK;
426b8cd5831SJianbo Liu } else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_JUMP)) {
427b8cd5831SJianbo Liu act_id = FLOW_ACTION_JUMP;
428b50e462bSIdo Schimmel *extval = tc_act & TC_ACT_EXT_VAL_MASK;
429052f744fSVlad Buslov } else if (tc_act == TC_ACT_UNSPEC) {
430b50e462bSIdo Schimmel act_id = FLOW_ACTION_CONTINUE;
431b50e462bSIdo Schimmel } else {
432b8cd5831SJianbo Liu NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload");
433b8cd5831SJianbo Liu }
434b8cd5831SJianbo Liu
435b8cd5831SJianbo Liu return act_id;
436b8cd5831SJianbo Liu }
437c54e1d92SBaowen Zheng
tcf_police_offload_act_setup(struct tc_action * act,void * entry_data,u32 * index_inc,bool bind,struct netlink_ext_ack * extack)438c2ccf84eSIdo Schimmel static int tcf_police_offload_act_setup(struct tc_action *act, void *entry_data,
439c2ccf84eSIdo Schimmel u32 *index_inc, bool bind,
440c54e1d92SBaowen Zheng struct netlink_ext_ack *extack)
441c54e1d92SBaowen Zheng {
442c54e1d92SBaowen Zheng if (bind) {
443b8cd5831SJianbo Liu struct flow_action_entry *entry = entry_data;
444b8cd5831SJianbo Liu struct tcf_police *police = to_police(act);
445b8cd5831SJianbo Liu struct tcf_police_params *p;
446b8cd5831SJianbo Liu int act_id;
447b8cd5831SJianbo Liu
448b8cd5831SJianbo Liu p = rcu_dereference_protected(police->params,
449c54e1d92SBaowen Zheng lockdep_is_held(&police->tcf_lock));
450c54e1d92SBaowen Zheng
451c54e1d92SBaowen Zheng entry->id = FLOW_ACTION_POLICE;
452c54e1d92SBaowen Zheng entry->police.burst = tcf_police_burst(act);
453c54e1d92SBaowen Zheng entry->police.rate_bytes_ps =
454b8cd5831SJianbo Liu tcf_police_rate_bytes_ps(act);
455b8cd5831SJianbo Liu entry->police.peakrate_bytes_ps = tcf_police_peakrate_bytes_ps(act);
456b8cd5831SJianbo Liu entry->police.avrate = tcf_police_tcfp_ewma_rate(act);
457c54e1d92SBaowen Zheng entry->police.overhead = tcf_police_rate_overhead(act);
458c54e1d92SBaowen Zheng entry->police.burst_pkt = tcf_police_burst_pkt(act);
459c54e1d92SBaowen Zheng entry->police.rate_pkt_ps =
460c54e1d92SBaowen Zheng tcf_police_rate_pkt_ps(act);
461b8cd5831SJianbo Liu entry->police.mtu = tcf_police_tcfp_mtu(act);
462b8cd5831SJianbo Liu
463b50e462bSIdo Schimmel act_id = tcf_police_act_to_flow_act(police->tcf_action,
464b50e462bSIdo Schimmel &entry->police.exceed.extval,
465b8cd5831SJianbo Liu extack);
466b8cd5831SJianbo Liu if (act_id < 0)
467b8cd5831SJianbo Liu return act_id;
468b8cd5831SJianbo Liu
469b8cd5831SJianbo Liu entry->police.exceed.act_id = act_id;
470b8cd5831SJianbo Liu
471b50e462bSIdo Schimmel act_id = tcf_police_act_to_flow_act(p->tcfp_result,
472b50e462bSIdo Schimmel &entry->police.notexceed.extval,
473b8cd5831SJianbo Liu extack);
474b8cd5831SJianbo Liu if (act_id < 0)
475b8cd5831SJianbo Liu return act_id;
476b8cd5831SJianbo Liu
477b8cd5831SJianbo Liu entry->police.notexceed.act_id = act_id;
478c54e1d92SBaowen Zheng
479c54e1d92SBaowen Zheng *index_inc = 1;
4808cbfe939SBaowen Zheng } else {
4818cbfe939SBaowen Zheng struct flow_offload_action *fl_action = entry_data;
4828cbfe939SBaowen Zheng
483c54e1d92SBaowen Zheng fl_action->id = FLOW_ACTION_POLICE;
484c54e1d92SBaowen Zheng }
485c54e1d92SBaowen Zheng
486c54e1d92SBaowen Zheng return 0;
487c54e1d92SBaowen Zheng }
4884bba3925SPatrick McHardy
4894bba3925SPatrick McHardy MODULE_AUTHOR("Alexey Kuznetsov");
4904bba3925SPatrick McHardy MODULE_DESCRIPTION("Policing actions");
4914bba3925SPatrick McHardy MODULE_LICENSE("GPL");
4924bba3925SPatrick McHardy
4934bba3925SPatrick McHardy static struct tc_action_ops act_police_ops = {
494eddd2cf1SEli Cohen .kind = "police",
4954bba3925SPatrick McHardy .id = TCA_ID_POLICE,
49612f02b6bSPieter Jansen van Vuuren .owner = THIS_MODULE,
4972ac06347SJamal Hadi Salim .stats_update = tcf_police_stats_update,
4982ac06347SJamal Hadi Salim .act = tcf_police_act,
4992ac06347SJamal Hadi Salim .dump = tcf_police_dump,
5002d550dbaSDavide Caratti .init = tcf_police_init,
501c54e1d92SBaowen Zheng .cleanup = tcf_police_cleanup,
502a85a970aSWANG Cong .offload_act_setup = tcf_police_offload_act_setup,
503ddf97ccdSWANG Cong .size = sizeof(struct tcf_police),
504ddf97ccdSWANG Cong };
505ddf97ccdSWANG Cong
police_init_net(struct net * net)506ddf97ccdSWANG Cong static __net_init int police_init_net(struct net *net)
507acd0a7abSZhengchao Shao {
508ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, act_police_ops.net_id);
509981471bdSCong Wang
510ddf97ccdSWANG Cong return tc_action_net_init(net, tn, &act_police_ops);
511ddf97ccdSWANG Cong }
512039af9c6SCong Wang
police_exit_net(struct list_head * net_list)513ddf97ccdSWANG Cong static void __net_exit police_exit_net(struct list_head *net_list)
514acd0a7abSZhengchao Shao {
515ddf97ccdSWANG Cong tc_action_net_exit(net_list, act_police_ops.net_id);
516ddf97ccdSWANG Cong }
517ddf97ccdSWANG Cong
518ddf97ccdSWANG Cong static struct pernet_operations police_net_ops = {
519039af9c6SCong Wang .init = police_init_net,
520acd0a7abSZhengchao Shao .exit_batch = police_exit_net,
521ddf97ccdSWANG Cong .id = &act_police_ops.net_id,
5224bba3925SPatrick McHardy .size = sizeof(struct tc_action_net),
5234bba3925SPatrick McHardy };
5245a7a5555SJamal Hadi Salim
police_init_module(void)5254bba3925SPatrick McHardy static int __init police_init_module(void)
526ddf97ccdSWANG Cong {
5274bba3925SPatrick McHardy return tcf_register_action(&act_police_ops, &police_net_ops);
5284bba3925SPatrick McHardy }
5295a7a5555SJamal Hadi Salim
police_cleanup_module(void)5304bba3925SPatrick McHardy static void __exit police_cleanup_module(void)
531ddf97ccdSWANG Cong {
5324bba3925SPatrick McHardy tcf_unregister_action(&act_police_ops, &police_net_ops);
5334bba3925SPatrick McHardy }
5344bba3925SPatrick McHardy
5354bba3925SPatrick McHardy module_init(police_init_module);
536 module_exit(police_cleanup_module);
537