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 25a85a970aSWANG Cong static struct tc_action_ops act_police_ops; 26ddf97ccdSWANG Cong 272ac06347SJamal Hadi Salim static int tcf_police_walker(struct net *net, struct sk_buff *skb, 28ddf97ccdSWANG Cong struct netlink_callback *cb, int type, 2941780105SAlexander Aring const struct tc_action_ops *ops, 3041780105SAlexander Aring struct netlink_ext_ack *extack) 314bba3925SPatrick McHardy { 32*acd0a7abSZhengchao Shao struct tc_action_net *tn = net_generic(net, act_police_ops.net_id); 334bba3925SPatrick McHardy 34b3620145SAlexander Aring return tcf_generic_walker(tn, skb, cb, type, ops, extack); 354bba3925SPatrick McHardy } 364bba3925SPatrick McHardy 3753b2bf3fSPatrick McHardy static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = { 3853b2bf3fSPatrick McHardy [TCA_POLICE_RATE] = { .len = TC_RTAB_SIZE }, 3953b2bf3fSPatrick McHardy [TCA_POLICE_PEAKRATE] = { .len = TC_RTAB_SIZE }, 4053b2bf3fSPatrick McHardy [TCA_POLICE_AVRATE] = { .type = NLA_U32 }, 4153b2bf3fSPatrick McHardy [TCA_POLICE_RESULT] = { .type = NLA_U32 }, 42d1967e49SDavid Dai [TCA_POLICE_RATE64] = { .type = NLA_U64 }, 43d1967e49SDavid Dai [TCA_POLICE_PEAKRATE64] = { .type = NLA_U64 }, 442ffe0395SBaowen Zheng [TCA_POLICE_PKTRATE64] = { .type = NLA_U64, .min = 1 }, 452ffe0395SBaowen Zheng [TCA_POLICE_PKTBURST64] = { .type = NLA_U64, .min = 1 }, 4653b2bf3fSPatrick McHardy }; 4753b2bf3fSPatrick McHardy 482ac06347SJamal Hadi Salim static int tcf_police_init(struct net *net, struct nlattr *nla, 49a85a970aSWANG Cong struct nlattr *est, struct tc_action **a, 50abbb0d33SVlad Buslov struct tcf_proto *tp, u32 flags, 51589dad6dSAlexander Aring struct netlink_ext_ack *extack) 524bba3925SPatrick McHardy { 53fd6d4338SDavide Caratti int ret = 0, tcfp_result = TC_ACT_OK, err, size; 54695176bfSCong Wang bool bind = flags & TCA_ACT_FLAGS_BIND; 557ba699c6SPatrick McHardy struct nlattr *tb[TCA_POLICE_MAX + 1]; 56d6124d6bSDavide Caratti struct tcf_chain *goto_ch = NULL; 574bba3925SPatrick McHardy struct tc_police *parm; 58e9ce1cd3SDavid S. Miller struct tcf_police *police; 594bba3925SPatrick McHardy struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL; 60*acd0a7abSZhengchao Shao struct tc_action_net *tn = net_generic(net, act_police_ops.net_id); 612d550dbaSDavide Caratti struct tcf_police_params *new; 620852e455SWANG Cong bool exists = false; 637be8ef2cSDmytro Linkin u32 index; 64d1967e49SDavid Dai u64 rate64, prate64; 652ffe0395SBaowen Zheng u64 pps, ppsburst; 664bba3925SPatrick McHardy 67cee63723SPatrick McHardy if (nla == NULL) 684bba3925SPatrick McHardy return -EINVAL; 694bba3925SPatrick McHardy 708cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_POLICE_MAX, nla, 718cb08174SJohannes Berg police_policy, NULL); 72cee63723SPatrick McHardy if (err < 0) 73cee63723SPatrick McHardy return err; 74cee63723SPatrick McHardy 757ba699c6SPatrick McHardy if (tb[TCA_POLICE_TBF] == NULL) 761e9b3d53SPatrick McHardy return -EINVAL; 777ba699c6SPatrick McHardy size = nla_len(tb[TCA_POLICE_TBF]); 781e9b3d53SPatrick McHardy if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat)) 794bba3925SPatrick McHardy return -EINVAL; 804bba3925SPatrick McHardy 810852e455SWANG Cong parm = nla_data(tb[TCA_POLICE_TBF]); 827be8ef2cSDmytro Linkin index = parm->index; 837be8ef2cSDmytro Linkin err = tcf_idr_check_alloc(tn, &index, a, bind); 840190c1d4SVlad Buslov if (err < 0) 850190c1d4SVlad Buslov return err; 860190c1d4SVlad Buslov exists = err; 870852e455SWANG Cong if (exists && bind) 880852e455SWANG Cong return 0; 890852e455SWANG Cong 900852e455SWANG Cong if (!exists) { 917be8ef2cSDmytro Linkin ret = tcf_idr_create(tn, index, NULL, a, 9240bd094dSBaowen Zheng &act_police_ops, bind, true, flags); 930190c1d4SVlad Buslov if (ret) { 947be8ef2cSDmytro Linkin tcf_idr_cleanup(tn, index); 95a03e6fe5SWANG Cong return ret; 960190c1d4SVlad Buslov } 97a03e6fe5SWANG Cong ret = ACT_P_CREATED; 98484afd1bSDavide Caratti spin_lock_init(&(to_police(*a)->tcfp_lock)); 99695176bfSCong Wang } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) { 10065a206c0SChris Mi tcf_idr_release(*a, bind); 1010852e455SWANG Cong return -EEXIST; 102e9ce1cd3SDavid S. Miller } 103d6124d6bSDavide Caratti err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 104d6124d6bSDavide Caratti if (err < 0) 105d6124d6bSDavide Caratti goto release_idr; 1064bba3925SPatrick McHardy 107a85a970aSWANG Cong police = to_police(*a); 1084bba3925SPatrick McHardy if (parm->rate.rate) { 1094bba3925SPatrick McHardy err = -ENOMEM; 110e9bc3fa2SAlexander Aring R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE], NULL); 1114bba3925SPatrick McHardy if (R_tab == NULL) 1124bba3925SPatrick McHardy goto failure; 113c1b56878SStephen Hemminger 1144bba3925SPatrick McHardy if (parm->peakrate.rate) { 1154bba3925SPatrick McHardy P_tab = qdisc_get_rtab(&parm->peakrate, 116e9bc3fa2SAlexander Aring tb[TCA_POLICE_PEAKRATE], NULL); 11771bcb09aSStephen Hemminger if (P_tab == NULL) 1184bba3925SPatrick McHardy goto failure; 1194bba3925SPatrick McHardy } 1204bba3925SPatrick McHardy } 12171bcb09aSStephen Hemminger 12271bcb09aSStephen Hemminger if (est) { 12393be42f9SDavide Caratti err = gen_replace_estimator(&police->tcf_bstats, 12493be42f9SDavide Caratti police->common.cpu_bstats, 12571bcb09aSStephen Hemminger &police->tcf_rate_est, 126edb09eb1SEric Dumazet &police->tcf_lock, 12729cbcd85SAhmed S. Darwish false, est); 12871bcb09aSStephen Hemminger if (err) 12974030603SWANG Cong goto failure; 130a883bf56SJarek Poplawski } else if (tb[TCA_POLICE_AVRATE] && 131a883bf56SJarek Poplawski (ret == ACT_P_CREATED || 1321c0d32fdSEric Dumazet !gen_estimator_active(&police->tcf_rate_est))) { 133a883bf56SJarek Poplawski err = -EINVAL; 13474030603SWANG Cong goto failure; 13571bcb09aSStephen Hemminger } 13671bcb09aSStephen Hemminger 137fd6d4338SDavide Caratti if (tb[TCA_POLICE_RESULT]) { 138fd6d4338SDavide Caratti tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]); 139fd6d4338SDavide Caratti if (TC_ACT_EXT_CMP(tcfp_result, TC_ACT_GOTO_CHAIN)) { 140fd6d4338SDavide Caratti NL_SET_ERR_MSG(extack, 141fd6d4338SDavide Caratti "goto chain not allowed on fallback"); 142fd6d4338SDavide Caratti err = -EINVAL; 143fd6d4338SDavide Caratti goto failure; 144fd6d4338SDavide Caratti } 145fd6d4338SDavide Caratti } 146fd6d4338SDavide Caratti 1472ffe0395SBaowen Zheng if ((tb[TCA_POLICE_PKTRATE64] && !tb[TCA_POLICE_PKTBURST64]) || 1482ffe0395SBaowen Zheng (!tb[TCA_POLICE_PKTRATE64] && tb[TCA_POLICE_PKTBURST64])) { 1492ffe0395SBaowen Zheng NL_SET_ERR_MSG(extack, 1502ffe0395SBaowen Zheng "Both or neither packet-per-second burst and rate must be provided"); 1512ffe0395SBaowen Zheng err = -EINVAL; 1522ffe0395SBaowen Zheng goto failure; 1532ffe0395SBaowen Zheng } 1542ffe0395SBaowen Zheng 1552ffe0395SBaowen Zheng if (tb[TCA_POLICE_PKTRATE64] && R_tab) { 1562ffe0395SBaowen Zheng NL_SET_ERR_MSG(extack, 1572ffe0395SBaowen Zheng "packet-per-second and byte-per-second rate limits not allowed in same action"); 1582ffe0395SBaowen Zheng err = -EINVAL; 1592ffe0395SBaowen Zheng goto failure; 1602ffe0395SBaowen Zheng } 1612ffe0395SBaowen Zheng 1622d550dbaSDavide Caratti new = kzalloc(sizeof(*new), GFP_KERNEL); 1632d550dbaSDavide Caratti if (unlikely(!new)) { 1642d550dbaSDavide Caratti err = -ENOMEM; 1652d550dbaSDavide Caratti goto failure; 1662d550dbaSDavide Caratti } 1672d550dbaSDavide Caratti 16871bcb09aSStephen Hemminger /* No failure allowed after this point */ 169fd6d4338SDavide Caratti new->tcfp_result = tcfp_result; 1702d550dbaSDavide Caratti new->tcfp_mtu = parm->mtu; 1712d550dbaSDavide Caratti if (!new->tcfp_mtu) { 1722d550dbaSDavide Caratti new->tcfp_mtu = ~0; 173c6d14ff1SJiri Pirko if (R_tab) 1742d550dbaSDavide Caratti new->tcfp_mtu = 255 << R_tab->rate.cell_log; 1754bba3925SPatrick McHardy } 176c6d14ff1SJiri Pirko if (R_tab) { 1772d550dbaSDavide Caratti new->rate_present = true; 178d1967e49SDavid Dai rate64 = tb[TCA_POLICE_RATE64] ? 179d1967e49SDavid Dai nla_get_u64(tb[TCA_POLICE_RATE64]) : 0; 180d1967e49SDavid Dai psched_ratecfg_precompute(&new->rate, &R_tab->rate, rate64); 181c6d14ff1SJiri Pirko qdisc_put_rtab(R_tab); 182c6d14ff1SJiri Pirko } else { 1832d550dbaSDavide Caratti new->rate_present = false; 184c6d14ff1SJiri Pirko } 185c6d14ff1SJiri Pirko if (P_tab) { 1862d550dbaSDavide Caratti new->peak_present = true; 187d1967e49SDavid Dai prate64 = tb[TCA_POLICE_PEAKRATE64] ? 188d1967e49SDavid Dai nla_get_u64(tb[TCA_POLICE_PEAKRATE64]) : 0; 189d1967e49SDavid Dai psched_ratecfg_precompute(&new->peak, &P_tab->rate, prate64); 190c6d14ff1SJiri Pirko qdisc_put_rtab(P_tab); 191c6d14ff1SJiri Pirko } else { 1922d550dbaSDavide Caratti new->peak_present = false; 1934bba3925SPatrick McHardy } 1944bba3925SPatrick McHardy 1952d550dbaSDavide Caratti new->tcfp_burst = PSCHED_TICKS2NS(parm->burst); 196f2cbd485SDavide Caratti if (new->peak_present) 1972d550dbaSDavide Caratti new->tcfp_mtu_ptoks = (s64)psched_l2t_ns(&new->peak, 1982d550dbaSDavide Caratti new->tcfp_mtu); 1994bba3925SPatrick McHardy 2007ba699c6SPatrick McHardy if (tb[TCA_POLICE_AVRATE]) 2012d550dbaSDavide Caratti new->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]); 2024bba3925SPatrick McHardy 2032ffe0395SBaowen Zheng if (tb[TCA_POLICE_PKTRATE64]) { 2042ffe0395SBaowen Zheng pps = nla_get_u64(tb[TCA_POLICE_PKTRATE64]); 2052ffe0395SBaowen Zheng ppsburst = nla_get_u64(tb[TCA_POLICE_PKTBURST64]); 2062ffe0395SBaowen Zheng new->pps_present = true; 2072ffe0395SBaowen Zheng new->tcfp_pkt_burst = PSCHED_TICKS2NS(ppsburst); 2082ffe0395SBaowen Zheng psched_ppscfg_precompute(&new->ppsrate, pps); 2092ffe0395SBaowen Zheng } 2102ffe0395SBaowen Zheng 2112d550dbaSDavide Caratti spin_lock_bh(&police->tcf_lock); 212f2cbd485SDavide Caratti spin_lock_bh(&police->tcfp_lock); 213f2cbd485SDavide Caratti police->tcfp_t_c = ktime_get_ns(); 214f2cbd485SDavide Caratti police->tcfp_toks = new->tcfp_burst; 215f2cbd485SDavide Caratti if (new->peak_present) 216f2cbd485SDavide Caratti police->tcfp_ptoks = new->tcfp_mtu_ptoks; 217f2cbd485SDavide Caratti spin_unlock_bh(&police->tcfp_lock); 218d6124d6bSDavide Caratti goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 219445d3749SPaul E. McKenney new = rcu_replace_pointer(police->params, 2202d550dbaSDavide Caratti new, 2212d550dbaSDavide Caratti lockdep_is_held(&police->tcf_lock)); 222e9ce1cd3SDavid S. Miller spin_unlock_bh(&police->tcf_lock); 2234bba3925SPatrick McHardy 224d6124d6bSDavide Caratti if (goto_ch) 225d6124d6bSDavide Caratti tcf_chain_put_by_act(goto_ch); 2262d550dbaSDavide Caratti if (new) 2272d550dbaSDavide Caratti kfree_rcu(new, rcu); 2282d550dbaSDavide Caratti 2294bba3925SPatrick McHardy return ret; 2304bba3925SPatrick McHardy 2314bba3925SPatrick McHardy failure: 23271bcb09aSStephen Hemminger qdisc_put_rtab(P_tab); 23371bcb09aSStephen Hemminger qdisc_put_rtab(R_tab); 234d6124d6bSDavide Caratti if (goto_ch) 235d6124d6bSDavide Caratti tcf_chain_put_by_act(goto_ch); 236d6124d6bSDavide Caratti release_idr: 2375bf7f818SDavide Caratti tcf_idr_release(*a, bind); 2384bba3925SPatrick McHardy return err; 2394bba3925SPatrick McHardy } 2404bba3925SPatrick McHardy 2414ddc844eSDavide Caratti static bool tcf_police_mtu_check(struct sk_buff *skb, u32 limit) 2424ddc844eSDavide Caratti { 2434ddc844eSDavide Caratti u32 len; 2444ddc844eSDavide Caratti 2454ddc844eSDavide Caratti if (skb_is_gso(skb)) 2464ddc844eSDavide Caratti return skb_gso_validate_mac_len(skb, limit); 2474ddc844eSDavide Caratti 2484ddc844eSDavide Caratti len = qdisc_pkt_len(skb); 2494ddc844eSDavide Caratti if (skb_at_tc_ingress(skb)) 2504ddc844eSDavide Caratti len += skb->mac_len; 2514ddc844eSDavide Caratti 2524ddc844eSDavide Caratti return len <= limit; 2534ddc844eSDavide Caratti } 2544ddc844eSDavide Caratti 2552ac06347SJamal Hadi Salim static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a, 2564bba3925SPatrick McHardy struct tcf_result *res) 2574bba3925SPatrick McHardy { 258a85a970aSWANG Cong struct tcf_police *police = to_police(a); 2592ffe0395SBaowen Zheng s64 now, toks, ppstoks = 0, ptoks = 0; 2602d550dbaSDavide Caratti struct tcf_police_params *p; 26193be42f9SDavide Caratti int ret; 26293be42f9SDavide Caratti 26393be42f9SDavide Caratti tcf_lastuse_update(&police->tcf_tm); 26450dc9a85SAhmed S. Darwish bstats_update(this_cpu_ptr(police->common.cpu_bstats), skb); 2654bba3925SPatrick McHardy 2662d550dbaSDavide Caratti ret = READ_ONCE(police->tcf_action); 2672d550dbaSDavide Caratti p = rcu_dereference_bh(police->params); 2682d550dbaSDavide Caratti 2692d550dbaSDavide Caratti if (p->tcfp_ewma_rate) { 2701c0d32fdSEric Dumazet struct gnet_stats_rate_est64 sample; 2711c0d32fdSEric Dumazet 2721c0d32fdSEric Dumazet if (!gen_estimator_read(&police->tcf_rate_est, &sample) || 2732d550dbaSDavide Caratti sample.bps >= p->tcfp_ewma_rate) 27493be42f9SDavide Caratti goto inc_overlimits; 2754bba3925SPatrick McHardy } 2764bba3925SPatrick McHardy 2774ddc844eSDavide Caratti if (tcf_police_mtu_check(skb, p->tcfp_mtu)) { 2782ffe0395SBaowen Zheng if (!p->rate_present && !p->pps_present) { 2792d550dbaSDavide Caratti ret = p->tcfp_result; 2802d550dbaSDavide Caratti goto end; 2814bba3925SPatrick McHardy } 2824bba3925SPatrick McHardy 283d2de875cSEric Dumazet now = ktime_get_ns(); 284f2cbd485SDavide Caratti spin_lock_bh(&police->tcfp_lock); 285f2cbd485SDavide Caratti toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst); 2862d550dbaSDavide Caratti if (p->peak_present) { 287f2cbd485SDavide Caratti ptoks = toks + police->tcfp_ptoks; 2882d550dbaSDavide Caratti if (ptoks > p->tcfp_mtu_ptoks) 2892d550dbaSDavide Caratti ptoks = p->tcfp_mtu_ptoks; 2902d550dbaSDavide Caratti ptoks -= (s64)psched_l2t_ns(&p->peak, 291c6d14ff1SJiri Pirko qdisc_pkt_len(skb)); 2924bba3925SPatrick McHardy } 2932ffe0395SBaowen Zheng if (p->rate_present) { 294f2cbd485SDavide Caratti toks += police->tcfp_toks; 2952d550dbaSDavide Caratti if (toks > p->tcfp_burst) 2962d550dbaSDavide Caratti toks = p->tcfp_burst; 2972d550dbaSDavide Caratti toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb)); 2982ffe0395SBaowen Zheng } else if (p->pps_present) { 2992ffe0395SBaowen Zheng ppstoks = min_t(s64, now - police->tcfp_t_c, p->tcfp_pkt_burst); 3002ffe0395SBaowen Zheng ppstoks += police->tcfp_pkttoks; 3012ffe0395SBaowen Zheng if (ppstoks > p->tcfp_pkt_burst) 3022ffe0395SBaowen Zheng ppstoks = p->tcfp_pkt_burst; 3032ffe0395SBaowen Zheng ppstoks -= (s64)psched_pkt2t_ns(&p->ppsrate, 1); 3042ffe0395SBaowen Zheng } 3052ffe0395SBaowen Zheng if ((toks | ptoks | ppstoks) >= 0) { 306f2cbd485SDavide Caratti police->tcfp_t_c = now; 307f2cbd485SDavide Caratti police->tcfp_toks = toks; 308f2cbd485SDavide Caratti police->tcfp_ptoks = ptoks; 3092ffe0395SBaowen Zheng police->tcfp_pkttoks = ppstoks; 310f2cbd485SDavide Caratti spin_unlock_bh(&police->tcfp_lock); 3112d550dbaSDavide Caratti ret = p->tcfp_result; 31293be42f9SDavide Caratti goto inc_drops; 3134bba3925SPatrick McHardy } 314f2cbd485SDavide Caratti spin_unlock_bh(&police->tcfp_lock); 3154bba3925SPatrick McHardy } 3164bba3925SPatrick McHardy 31793be42f9SDavide Caratti inc_overlimits: 31893be42f9SDavide Caratti qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats)); 31993be42f9SDavide Caratti inc_drops: 32093be42f9SDavide Caratti if (ret == TC_ACT_SHOT) 32193be42f9SDavide Caratti qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats)); 3222d550dbaSDavide Caratti end: 32393be42f9SDavide Caratti return ret; 3244bba3925SPatrick McHardy } 3254bba3925SPatrick McHardy 3262d550dbaSDavide Caratti static void tcf_police_cleanup(struct tc_action *a) 3272d550dbaSDavide Caratti { 3282d550dbaSDavide Caratti struct tcf_police *police = to_police(a); 3292d550dbaSDavide Caratti struct tcf_police_params *p; 3302d550dbaSDavide Caratti 3312d550dbaSDavide Caratti p = rcu_dereference_protected(police->params, 1); 3322d550dbaSDavide Caratti if (p) 3332d550dbaSDavide Caratti kfree_rcu(p, rcu); 3342d550dbaSDavide Caratti } 3352d550dbaSDavide Caratti 33612f02b6bSPieter Jansen van Vuuren static void tcf_police_stats_update(struct tc_action *a, 3374b61d3e8SPo Liu u64 bytes, u64 packets, u64 drops, 33812f02b6bSPieter Jansen van Vuuren u64 lastuse, bool hw) 33912f02b6bSPieter Jansen van Vuuren { 34012f02b6bSPieter Jansen van Vuuren struct tcf_police *police = to_police(a); 34112f02b6bSPieter Jansen van Vuuren struct tcf_t *tm = &police->tcf_tm; 34212f02b6bSPieter Jansen van Vuuren 3434b61d3e8SPo Liu tcf_action_update_stats(a, bytes, packets, drops, hw); 34412f02b6bSPieter Jansen van Vuuren tm->lastuse = max_t(u64, tm->lastuse, lastuse); 34512f02b6bSPieter Jansen van Vuuren } 34612f02b6bSPieter Jansen van Vuuren 3472ac06347SJamal Hadi Salim static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a, 3485a7a5555SJamal Hadi Salim int bind, int ref) 3494bba3925SPatrick McHardy { 35027a884dcSArnaldo Carvalho de Melo unsigned char *b = skb_tail_pointer(skb); 351a85a970aSWANG Cong struct tcf_police *police = to_police(a); 3522d550dbaSDavide Caratti struct tcf_police_params *p; 3530f04cfd0SJeff Mahoney struct tc_police opt = { 3540f04cfd0SJeff Mahoney .index = police->tcf_index, 355036bb443SVlad Buslov .refcnt = refcount_read(&police->tcf_refcnt) - ref, 356036bb443SVlad Buslov .bindcnt = atomic_read(&police->tcf_bindcnt) - bind, 3570f04cfd0SJeff Mahoney }; 3583d3ed181SJamal Hadi Salim struct tcf_t t; 3594bba3925SPatrick McHardy 360e329bc42SVlad Buslov spin_lock_bh(&police->tcf_lock); 361e329bc42SVlad Buslov opt.action = police->tcf_action; 3622d550dbaSDavide Caratti p = rcu_dereference_protected(police->params, 3632d550dbaSDavide Caratti lockdep_is_held(&police->tcf_lock)); 3642d550dbaSDavide Caratti opt.mtu = p->tcfp_mtu; 3652d550dbaSDavide Caratti opt.burst = PSCHED_NS2TICKS(p->tcfp_burst); 366d1967e49SDavid Dai if (p->rate_present) { 3672d550dbaSDavide Caratti psched_ratecfg_getrate(&opt.rate, &p->rate); 368d1967e49SDavid Dai if ((police->params->rate.rate_bytes_ps >= (1ULL << 32)) && 369d1967e49SDavid Dai nla_put_u64_64bit(skb, TCA_POLICE_RATE64, 370d1967e49SDavid Dai police->params->rate.rate_bytes_ps, 371d1967e49SDavid Dai TCA_POLICE_PAD)) 372d1967e49SDavid Dai goto nla_put_failure; 373d1967e49SDavid Dai } 374d1967e49SDavid Dai if (p->peak_present) { 3752d550dbaSDavide Caratti psched_ratecfg_getrate(&opt.peakrate, &p->peak); 376d1967e49SDavid Dai if ((police->params->peak.rate_bytes_ps >= (1ULL << 32)) && 377d1967e49SDavid Dai nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64, 378d1967e49SDavid Dai police->params->peak.rate_bytes_ps, 379d1967e49SDavid Dai TCA_POLICE_PAD)) 380d1967e49SDavid Dai goto nla_put_failure; 381d1967e49SDavid Dai } 3822ffe0395SBaowen Zheng if (p->pps_present) { 3832ffe0395SBaowen Zheng if (nla_put_u64_64bit(skb, TCA_POLICE_PKTRATE64, 3842ffe0395SBaowen Zheng police->params->ppsrate.rate_pkts_ps, 3852ffe0395SBaowen Zheng TCA_POLICE_PAD)) 3862ffe0395SBaowen Zheng goto nla_put_failure; 3872ffe0395SBaowen Zheng if (nla_put_u64_64bit(skb, TCA_POLICE_PKTBURST64, 3882ffe0395SBaowen Zheng PSCHED_NS2TICKS(p->tcfp_pkt_burst), 3892ffe0395SBaowen Zheng TCA_POLICE_PAD)) 3902ffe0395SBaowen Zheng goto nla_put_failure; 3912ffe0395SBaowen Zheng } 3921b34ec43SDavid S. Miller if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt)) 3931b34ec43SDavid S. Miller goto nla_put_failure; 3942d550dbaSDavide Caratti if (p->tcfp_result && 3952d550dbaSDavide Caratti nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result)) 3961b34ec43SDavid S. Miller goto nla_put_failure; 3972d550dbaSDavide Caratti if (p->tcfp_ewma_rate && 3982d550dbaSDavide Caratti nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate)) 3991b34ec43SDavid S. Miller goto nla_put_failure; 4003d3ed181SJamal Hadi Salim 401985fd98aSDavide Caratti tcf_tm_dump(&t, &police->tcf_tm); 4023d3ed181SJamal Hadi Salim if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD)) 4033d3ed181SJamal Hadi Salim goto nla_put_failure; 404e329bc42SVlad Buslov spin_unlock_bh(&police->tcf_lock); 4053d3ed181SJamal Hadi Salim 4064bba3925SPatrick McHardy return skb->len; 4074bba3925SPatrick McHardy 4087ba699c6SPatrick McHardy nla_put_failure: 409e329bc42SVlad Buslov spin_unlock_bh(&police->tcf_lock); 410dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 4114bba3925SPatrick McHardy return -1; 4124bba3925SPatrick McHardy } 4134bba3925SPatrick McHardy 414f061b48cSCong Wang static int tcf_police_search(struct net *net, struct tc_action **a, u32 index) 415ddf97ccdSWANG Cong { 416*acd0a7abSZhengchao Shao struct tc_action_net *tn = net_generic(net, act_police_ops.net_id); 417ddf97ccdSWANG Cong 41865a206c0SChris Mi return tcf_idr_search(tn, a, index); 419ddf97ccdSWANG Cong } 420ddf97ccdSWANG Cong 421b50e462bSIdo Schimmel static int tcf_police_act_to_flow_act(int tc_act, u32 *extval, 422b50e462bSIdo Schimmel struct netlink_ext_ack *extack) 423b8cd5831SJianbo Liu { 424b8cd5831SJianbo Liu int act_id = -EOPNOTSUPP; 425b8cd5831SJianbo Liu 426b8cd5831SJianbo Liu if (!TC_ACT_EXT_OPCODE(tc_act)) { 427b8cd5831SJianbo Liu if (tc_act == TC_ACT_OK) 428b8cd5831SJianbo Liu act_id = FLOW_ACTION_ACCEPT; 429b8cd5831SJianbo Liu else if (tc_act == TC_ACT_SHOT) 430b8cd5831SJianbo Liu act_id = FLOW_ACTION_DROP; 431b8cd5831SJianbo Liu else if (tc_act == TC_ACT_PIPE) 432b8cd5831SJianbo Liu act_id = FLOW_ACTION_PIPE; 433b50e462bSIdo Schimmel else if (tc_act == TC_ACT_RECLASSIFY) 434b50e462bSIdo Schimmel NL_SET_ERR_MSG_MOD(extack, "Offload not supported when conform/exceed action is \"reclassify\""); 435b50e462bSIdo Schimmel else 436b50e462bSIdo Schimmel NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload"); 437b8cd5831SJianbo Liu } else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_GOTO_CHAIN)) { 438b8cd5831SJianbo Liu act_id = FLOW_ACTION_GOTO; 439b8cd5831SJianbo Liu *extval = tc_act & TC_ACT_EXT_VAL_MASK; 440b8cd5831SJianbo Liu } else if (TC_ACT_EXT_CMP(tc_act, TC_ACT_JUMP)) { 441b8cd5831SJianbo Liu act_id = FLOW_ACTION_JUMP; 442b8cd5831SJianbo Liu *extval = tc_act & TC_ACT_EXT_VAL_MASK; 443b50e462bSIdo Schimmel } else if (tc_act == TC_ACT_UNSPEC) { 444052f744fSVlad Buslov act_id = FLOW_ACTION_CONTINUE; 445b50e462bSIdo Schimmel } else { 446b50e462bSIdo Schimmel NL_SET_ERR_MSG_MOD(extack, "Unsupported conform/exceed action offload"); 447b8cd5831SJianbo Liu } 448b8cd5831SJianbo Liu 449b8cd5831SJianbo Liu return act_id; 450b8cd5831SJianbo Liu } 451b8cd5831SJianbo Liu 452c54e1d92SBaowen Zheng static int tcf_police_offload_act_setup(struct tc_action *act, void *entry_data, 453c2ccf84eSIdo Schimmel u32 *index_inc, bool bind, 454c2ccf84eSIdo Schimmel struct netlink_ext_ack *extack) 455c54e1d92SBaowen Zheng { 456c54e1d92SBaowen Zheng if (bind) { 457c54e1d92SBaowen Zheng struct flow_action_entry *entry = entry_data; 458b8cd5831SJianbo Liu struct tcf_police *police = to_police(act); 459b8cd5831SJianbo Liu struct tcf_police_params *p; 460b8cd5831SJianbo Liu int act_id; 461b8cd5831SJianbo Liu 462b8cd5831SJianbo Liu p = rcu_dereference_protected(police->params, 463b8cd5831SJianbo Liu lockdep_is_held(&police->tcf_lock)); 464c54e1d92SBaowen Zheng 465c54e1d92SBaowen Zheng entry->id = FLOW_ACTION_POLICE; 466c54e1d92SBaowen Zheng entry->police.burst = tcf_police_burst(act); 467c54e1d92SBaowen Zheng entry->police.rate_bytes_ps = 468c54e1d92SBaowen Zheng tcf_police_rate_bytes_ps(act); 469b8cd5831SJianbo Liu entry->police.peakrate_bytes_ps = tcf_police_peakrate_bytes_ps(act); 470b8cd5831SJianbo Liu entry->police.avrate = tcf_police_tcfp_ewma_rate(act); 471b8cd5831SJianbo Liu entry->police.overhead = tcf_police_rate_overhead(act); 472c54e1d92SBaowen Zheng entry->police.burst_pkt = tcf_police_burst_pkt(act); 473c54e1d92SBaowen Zheng entry->police.rate_pkt_ps = 474c54e1d92SBaowen Zheng tcf_police_rate_pkt_ps(act); 475c54e1d92SBaowen Zheng entry->police.mtu = tcf_police_tcfp_mtu(act); 476b8cd5831SJianbo Liu 477b8cd5831SJianbo Liu act_id = tcf_police_act_to_flow_act(police->tcf_action, 478b50e462bSIdo Schimmel &entry->police.exceed.extval, 479b50e462bSIdo Schimmel extack); 480b8cd5831SJianbo Liu if (act_id < 0) 481b8cd5831SJianbo Liu return act_id; 482b8cd5831SJianbo Liu 483b8cd5831SJianbo Liu entry->police.exceed.act_id = act_id; 484b8cd5831SJianbo Liu 485b8cd5831SJianbo Liu act_id = tcf_police_act_to_flow_act(p->tcfp_result, 486b50e462bSIdo Schimmel &entry->police.notexceed.extval, 487b50e462bSIdo Schimmel extack); 488b8cd5831SJianbo Liu if (act_id < 0) 489b8cd5831SJianbo Liu return act_id; 490b8cd5831SJianbo Liu 491b8cd5831SJianbo Liu entry->police.notexceed.act_id = act_id; 492b8cd5831SJianbo Liu 493c54e1d92SBaowen Zheng *index_inc = 1; 494c54e1d92SBaowen Zheng } else { 4958cbfe939SBaowen Zheng struct flow_offload_action *fl_action = entry_data; 4968cbfe939SBaowen Zheng 4978cbfe939SBaowen Zheng fl_action->id = FLOW_ACTION_POLICE; 498c54e1d92SBaowen Zheng } 499c54e1d92SBaowen Zheng 500c54e1d92SBaowen Zheng return 0; 501c54e1d92SBaowen Zheng } 502c54e1d92SBaowen Zheng 5034bba3925SPatrick McHardy MODULE_AUTHOR("Alexey Kuznetsov"); 5044bba3925SPatrick McHardy MODULE_DESCRIPTION("Policing actions"); 5054bba3925SPatrick McHardy MODULE_LICENSE("GPL"); 5064bba3925SPatrick McHardy 5074bba3925SPatrick McHardy static struct tc_action_ops act_police_ops = { 5084bba3925SPatrick McHardy .kind = "police", 509eddd2cf1SEli Cohen .id = TCA_ID_POLICE, 5104bba3925SPatrick McHardy .owner = THIS_MODULE, 51112f02b6bSPieter Jansen van Vuuren .stats_update = tcf_police_stats_update, 5122ac06347SJamal Hadi Salim .act = tcf_police_act, 5132ac06347SJamal Hadi Salim .dump = tcf_police_dump, 5142ac06347SJamal Hadi Salim .init = tcf_police_init, 5152ac06347SJamal Hadi Salim .walk = tcf_police_walker, 516ddf97ccdSWANG Cong .lookup = tcf_police_search, 5172d550dbaSDavide Caratti .cleanup = tcf_police_cleanup, 518c54e1d92SBaowen Zheng .offload_act_setup = tcf_police_offload_act_setup, 519a85a970aSWANG Cong .size = sizeof(struct tcf_police), 520ddf97ccdSWANG Cong }; 521ddf97ccdSWANG Cong 522ddf97ccdSWANG Cong static __net_init int police_init_net(struct net *net) 523ddf97ccdSWANG Cong { 524*acd0a7abSZhengchao Shao struct tc_action_net *tn = net_generic(net, act_police_ops.net_id); 525ddf97ccdSWANG Cong 526981471bdSCong Wang return tc_action_net_init(net, tn, &act_police_ops); 527ddf97ccdSWANG Cong } 528ddf97ccdSWANG Cong 529039af9c6SCong Wang static void __net_exit police_exit_net(struct list_head *net_list) 530ddf97ccdSWANG Cong { 531*acd0a7abSZhengchao Shao tc_action_net_exit(net_list, act_police_ops.net_id); 532ddf97ccdSWANG Cong } 533ddf97ccdSWANG Cong 534ddf97ccdSWANG Cong static struct pernet_operations police_net_ops = { 535ddf97ccdSWANG Cong .init = police_init_net, 536039af9c6SCong Wang .exit_batch = police_exit_net, 537*acd0a7abSZhengchao Shao .id = &act_police_ops.net_id, 538ddf97ccdSWANG Cong .size = sizeof(struct tc_action_net), 5394bba3925SPatrick McHardy }; 5404bba3925SPatrick McHardy 5415a7a5555SJamal Hadi Salim static int __init police_init_module(void) 5424bba3925SPatrick McHardy { 543ddf97ccdSWANG Cong return tcf_register_action(&act_police_ops, &police_net_ops); 5444bba3925SPatrick McHardy } 5454bba3925SPatrick McHardy 5465a7a5555SJamal Hadi Salim static void __exit police_cleanup_module(void) 5474bba3925SPatrick McHardy { 548ddf97ccdSWANG Cong tcf_unregister_action(&act_police_ops, &police_net_ops); 5494bba3925SPatrick McHardy } 5504bba3925SPatrick McHardy 5514bba3925SPatrick McHardy module_init(police_init_module); 5524bba3925SPatrick McHardy module_exit(police_cleanup_module); 553