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, 93e3822678SVlad Buslov &act_police_ops, bind, true, 0); 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, 128edb09eb1SEric Dumazet NULL, 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 2422ac06347SJamal Hadi Salim static int tcf_police_act(struct sk_buff *skb, const struct tc_action *a, 2434bba3925SPatrick McHardy struct tcf_result *res) 2444bba3925SPatrick McHardy { 245a85a970aSWANG Cong struct tcf_police *police = to_police(a); 2462ffe0395SBaowen Zheng s64 now, toks, ppstoks = 0, ptoks = 0; 2472d550dbaSDavide Caratti struct tcf_police_params *p; 24893be42f9SDavide Caratti int ret; 24993be42f9SDavide Caratti 25093be42f9SDavide Caratti tcf_lastuse_update(&police->tcf_tm); 251*50dc9a85SAhmed S. Darwish bstats_update(this_cpu_ptr(police->common.cpu_bstats), skb); 2524bba3925SPatrick McHardy 2532d550dbaSDavide Caratti ret = READ_ONCE(police->tcf_action); 2542d550dbaSDavide Caratti p = rcu_dereference_bh(police->params); 2552d550dbaSDavide Caratti 2562d550dbaSDavide Caratti if (p->tcfp_ewma_rate) { 2571c0d32fdSEric Dumazet struct gnet_stats_rate_est64 sample; 2581c0d32fdSEric Dumazet 2591c0d32fdSEric Dumazet if (!gen_estimator_read(&police->tcf_rate_est, &sample) || 2602d550dbaSDavide Caratti sample.bps >= p->tcfp_ewma_rate) 26193be42f9SDavide Caratti goto inc_overlimits; 2624bba3925SPatrick McHardy } 2634bba3925SPatrick McHardy 2642d550dbaSDavide Caratti if (qdisc_pkt_len(skb) <= p->tcfp_mtu) { 2652ffe0395SBaowen Zheng if (!p->rate_present && !p->pps_present) { 2662d550dbaSDavide Caratti ret = p->tcfp_result; 2672d550dbaSDavide Caratti goto end; 2684bba3925SPatrick McHardy } 2694bba3925SPatrick McHardy 270d2de875cSEric Dumazet now = ktime_get_ns(); 271f2cbd485SDavide Caratti spin_lock_bh(&police->tcfp_lock); 272f2cbd485SDavide Caratti toks = min_t(s64, now - police->tcfp_t_c, p->tcfp_burst); 2732d550dbaSDavide Caratti if (p->peak_present) { 274f2cbd485SDavide Caratti ptoks = toks + police->tcfp_ptoks; 2752d550dbaSDavide Caratti if (ptoks > p->tcfp_mtu_ptoks) 2762d550dbaSDavide Caratti ptoks = p->tcfp_mtu_ptoks; 2772d550dbaSDavide Caratti ptoks -= (s64)psched_l2t_ns(&p->peak, 278c6d14ff1SJiri Pirko qdisc_pkt_len(skb)); 2794bba3925SPatrick McHardy } 2802ffe0395SBaowen Zheng if (p->rate_present) { 281f2cbd485SDavide Caratti toks += police->tcfp_toks; 2822d550dbaSDavide Caratti if (toks > p->tcfp_burst) 2832d550dbaSDavide Caratti toks = p->tcfp_burst; 2842d550dbaSDavide Caratti toks -= (s64)psched_l2t_ns(&p->rate, qdisc_pkt_len(skb)); 2852ffe0395SBaowen Zheng } else if (p->pps_present) { 2862ffe0395SBaowen Zheng ppstoks = min_t(s64, now - police->tcfp_t_c, p->tcfp_pkt_burst); 2872ffe0395SBaowen Zheng ppstoks += police->tcfp_pkttoks; 2882ffe0395SBaowen Zheng if (ppstoks > p->tcfp_pkt_burst) 2892ffe0395SBaowen Zheng ppstoks = p->tcfp_pkt_burst; 2902ffe0395SBaowen Zheng ppstoks -= (s64)psched_pkt2t_ns(&p->ppsrate, 1); 2912ffe0395SBaowen Zheng } 2922ffe0395SBaowen Zheng if ((toks | ptoks | ppstoks) >= 0) { 293f2cbd485SDavide Caratti police->tcfp_t_c = now; 294f2cbd485SDavide Caratti police->tcfp_toks = toks; 295f2cbd485SDavide Caratti police->tcfp_ptoks = ptoks; 2962ffe0395SBaowen Zheng police->tcfp_pkttoks = ppstoks; 297f2cbd485SDavide Caratti spin_unlock_bh(&police->tcfp_lock); 2982d550dbaSDavide Caratti ret = p->tcfp_result; 29993be42f9SDavide Caratti goto inc_drops; 3004bba3925SPatrick McHardy } 301f2cbd485SDavide Caratti spin_unlock_bh(&police->tcfp_lock); 3024bba3925SPatrick McHardy } 3034bba3925SPatrick McHardy 30493be42f9SDavide Caratti inc_overlimits: 30593be42f9SDavide Caratti qstats_overlimit_inc(this_cpu_ptr(police->common.cpu_qstats)); 30693be42f9SDavide Caratti inc_drops: 30793be42f9SDavide Caratti if (ret == TC_ACT_SHOT) 30893be42f9SDavide Caratti qstats_drop_inc(this_cpu_ptr(police->common.cpu_qstats)); 3092d550dbaSDavide Caratti end: 31093be42f9SDavide Caratti return ret; 3114bba3925SPatrick McHardy } 3124bba3925SPatrick McHardy 3132d550dbaSDavide Caratti static void tcf_police_cleanup(struct tc_action *a) 3142d550dbaSDavide Caratti { 3152d550dbaSDavide Caratti struct tcf_police *police = to_police(a); 3162d550dbaSDavide Caratti struct tcf_police_params *p; 3172d550dbaSDavide Caratti 3182d550dbaSDavide Caratti p = rcu_dereference_protected(police->params, 1); 3192d550dbaSDavide Caratti if (p) 3202d550dbaSDavide Caratti kfree_rcu(p, rcu); 3212d550dbaSDavide Caratti } 3222d550dbaSDavide Caratti 32312f02b6bSPieter Jansen van Vuuren static void tcf_police_stats_update(struct tc_action *a, 3244b61d3e8SPo Liu u64 bytes, u64 packets, u64 drops, 32512f02b6bSPieter Jansen van Vuuren u64 lastuse, bool hw) 32612f02b6bSPieter Jansen van Vuuren { 32712f02b6bSPieter Jansen van Vuuren struct tcf_police *police = to_police(a); 32812f02b6bSPieter Jansen van Vuuren struct tcf_t *tm = &police->tcf_tm; 32912f02b6bSPieter Jansen van Vuuren 3304b61d3e8SPo Liu tcf_action_update_stats(a, bytes, packets, drops, hw); 33112f02b6bSPieter Jansen van Vuuren tm->lastuse = max_t(u64, tm->lastuse, lastuse); 33212f02b6bSPieter Jansen van Vuuren } 33312f02b6bSPieter Jansen van Vuuren 3342ac06347SJamal Hadi Salim static int tcf_police_dump(struct sk_buff *skb, struct tc_action *a, 3355a7a5555SJamal Hadi Salim int bind, int ref) 3364bba3925SPatrick McHardy { 33727a884dcSArnaldo Carvalho de Melo unsigned char *b = skb_tail_pointer(skb); 338a85a970aSWANG Cong struct tcf_police *police = to_police(a); 3392d550dbaSDavide Caratti struct tcf_police_params *p; 3400f04cfd0SJeff Mahoney struct tc_police opt = { 3410f04cfd0SJeff Mahoney .index = police->tcf_index, 342036bb443SVlad Buslov .refcnt = refcount_read(&police->tcf_refcnt) - ref, 343036bb443SVlad Buslov .bindcnt = atomic_read(&police->tcf_bindcnt) - bind, 3440f04cfd0SJeff Mahoney }; 3453d3ed181SJamal Hadi Salim struct tcf_t t; 3464bba3925SPatrick McHardy 347e329bc42SVlad Buslov spin_lock_bh(&police->tcf_lock); 348e329bc42SVlad Buslov opt.action = police->tcf_action; 3492d550dbaSDavide Caratti p = rcu_dereference_protected(police->params, 3502d550dbaSDavide Caratti lockdep_is_held(&police->tcf_lock)); 3512d550dbaSDavide Caratti opt.mtu = p->tcfp_mtu; 3522d550dbaSDavide Caratti opt.burst = PSCHED_NS2TICKS(p->tcfp_burst); 353d1967e49SDavid Dai if (p->rate_present) { 3542d550dbaSDavide Caratti psched_ratecfg_getrate(&opt.rate, &p->rate); 355d1967e49SDavid Dai if ((police->params->rate.rate_bytes_ps >= (1ULL << 32)) && 356d1967e49SDavid Dai nla_put_u64_64bit(skb, TCA_POLICE_RATE64, 357d1967e49SDavid Dai police->params->rate.rate_bytes_ps, 358d1967e49SDavid Dai TCA_POLICE_PAD)) 359d1967e49SDavid Dai goto nla_put_failure; 360d1967e49SDavid Dai } 361d1967e49SDavid Dai if (p->peak_present) { 3622d550dbaSDavide Caratti psched_ratecfg_getrate(&opt.peakrate, &p->peak); 363d1967e49SDavid Dai if ((police->params->peak.rate_bytes_ps >= (1ULL << 32)) && 364d1967e49SDavid Dai nla_put_u64_64bit(skb, TCA_POLICE_PEAKRATE64, 365d1967e49SDavid Dai police->params->peak.rate_bytes_ps, 366d1967e49SDavid Dai TCA_POLICE_PAD)) 367d1967e49SDavid Dai goto nla_put_failure; 368d1967e49SDavid Dai } 3692ffe0395SBaowen Zheng if (p->pps_present) { 3702ffe0395SBaowen Zheng if (nla_put_u64_64bit(skb, TCA_POLICE_PKTRATE64, 3712ffe0395SBaowen Zheng police->params->ppsrate.rate_pkts_ps, 3722ffe0395SBaowen Zheng TCA_POLICE_PAD)) 3732ffe0395SBaowen Zheng goto nla_put_failure; 3742ffe0395SBaowen Zheng if (nla_put_u64_64bit(skb, TCA_POLICE_PKTBURST64, 3752ffe0395SBaowen Zheng PSCHED_NS2TICKS(p->tcfp_pkt_burst), 3762ffe0395SBaowen Zheng TCA_POLICE_PAD)) 3772ffe0395SBaowen Zheng goto nla_put_failure; 3782ffe0395SBaowen Zheng } 3791b34ec43SDavid S. Miller if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt)) 3801b34ec43SDavid S. Miller goto nla_put_failure; 3812d550dbaSDavide Caratti if (p->tcfp_result && 3822d550dbaSDavide Caratti nla_put_u32(skb, TCA_POLICE_RESULT, p->tcfp_result)) 3831b34ec43SDavid S. Miller goto nla_put_failure; 3842d550dbaSDavide Caratti if (p->tcfp_ewma_rate && 3852d550dbaSDavide Caratti nla_put_u32(skb, TCA_POLICE_AVRATE, p->tcfp_ewma_rate)) 3861b34ec43SDavid S. Miller goto nla_put_failure; 3873d3ed181SJamal Hadi Salim 388985fd98aSDavide Caratti tcf_tm_dump(&t, &police->tcf_tm); 3893d3ed181SJamal Hadi Salim if (nla_put_64bit(skb, TCA_POLICE_TM, sizeof(t), &t, TCA_POLICE_PAD)) 3903d3ed181SJamal Hadi Salim goto nla_put_failure; 391e329bc42SVlad Buslov spin_unlock_bh(&police->tcf_lock); 3923d3ed181SJamal Hadi Salim 3934bba3925SPatrick McHardy return skb->len; 3944bba3925SPatrick McHardy 3957ba699c6SPatrick McHardy nla_put_failure: 396e329bc42SVlad Buslov spin_unlock_bh(&police->tcf_lock); 397dc5fc579SArnaldo Carvalho de Melo nlmsg_trim(skb, b); 3984bba3925SPatrick McHardy return -1; 3994bba3925SPatrick McHardy } 4004bba3925SPatrick McHardy 401f061b48cSCong Wang static int tcf_police_search(struct net *net, struct tc_action **a, u32 index) 402ddf97ccdSWANG Cong { 403ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, police_net_id); 404ddf97ccdSWANG Cong 40565a206c0SChris Mi return tcf_idr_search(tn, a, index); 406ddf97ccdSWANG Cong } 407ddf97ccdSWANG Cong 4084bba3925SPatrick McHardy MODULE_AUTHOR("Alexey Kuznetsov"); 4094bba3925SPatrick McHardy MODULE_DESCRIPTION("Policing actions"); 4104bba3925SPatrick McHardy MODULE_LICENSE("GPL"); 4114bba3925SPatrick McHardy 4124bba3925SPatrick McHardy static struct tc_action_ops act_police_ops = { 4134bba3925SPatrick McHardy .kind = "police", 414eddd2cf1SEli Cohen .id = TCA_ID_POLICE, 4154bba3925SPatrick McHardy .owner = THIS_MODULE, 41612f02b6bSPieter Jansen van Vuuren .stats_update = tcf_police_stats_update, 4172ac06347SJamal Hadi Salim .act = tcf_police_act, 4182ac06347SJamal Hadi Salim .dump = tcf_police_dump, 4192ac06347SJamal Hadi Salim .init = tcf_police_init, 4202ac06347SJamal Hadi Salim .walk = tcf_police_walker, 421ddf97ccdSWANG Cong .lookup = tcf_police_search, 4222d550dbaSDavide Caratti .cleanup = tcf_police_cleanup, 423a85a970aSWANG Cong .size = sizeof(struct tcf_police), 424ddf97ccdSWANG Cong }; 425ddf97ccdSWANG Cong 426ddf97ccdSWANG Cong static __net_init int police_init_net(struct net *net) 427ddf97ccdSWANG Cong { 428ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, police_net_id); 429ddf97ccdSWANG Cong 430981471bdSCong Wang return tc_action_net_init(net, tn, &act_police_ops); 431ddf97ccdSWANG Cong } 432ddf97ccdSWANG Cong 433039af9c6SCong Wang static void __net_exit police_exit_net(struct list_head *net_list) 434ddf97ccdSWANG Cong { 435039af9c6SCong Wang tc_action_net_exit(net_list, police_net_id); 436ddf97ccdSWANG Cong } 437ddf97ccdSWANG Cong 438ddf97ccdSWANG Cong static struct pernet_operations police_net_ops = { 439ddf97ccdSWANG Cong .init = police_init_net, 440039af9c6SCong Wang .exit_batch = police_exit_net, 441ddf97ccdSWANG Cong .id = &police_net_id, 442ddf97ccdSWANG Cong .size = sizeof(struct tc_action_net), 4434bba3925SPatrick McHardy }; 4444bba3925SPatrick McHardy 4455a7a5555SJamal Hadi Salim static int __init police_init_module(void) 4464bba3925SPatrick McHardy { 447ddf97ccdSWANG Cong return tcf_register_action(&act_police_ops, &police_net_ops); 4484bba3925SPatrick McHardy } 4494bba3925SPatrick McHardy 4505a7a5555SJamal Hadi Salim static void __exit police_cleanup_module(void) 4514bba3925SPatrick McHardy { 452ddf97ccdSWANG Cong tcf_unregister_action(&act_police_ops, &police_net_ops); 4534bba3925SPatrick McHardy } 4544bba3925SPatrick McHardy 4554bba3925SPatrick McHardy module_init(police_init_module); 4564bba3925SPatrick McHardy module_exit(police_cleanup_module); 457