Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __NET_TC_POLICE_H
0003 #define __NET_TC_POLICE_H
0004 
0005 #include <net/act_api.h>
0006 
0007 struct tcf_police_params {
0008     int         tcfp_result;
0009     u32         tcfp_ewma_rate;
0010     s64         tcfp_burst;
0011     u32         tcfp_mtu;
0012     s64         tcfp_mtu_ptoks;
0013     s64         tcfp_pkt_burst;
0014     struct psched_ratecfg   rate;
0015     bool            rate_present;
0016     struct psched_ratecfg   peak;
0017     bool            peak_present;
0018     struct psched_pktrate   ppsrate;
0019     bool            pps_present;
0020     struct rcu_head rcu;
0021 };
0022 
0023 struct tcf_police {
0024     struct tc_action    common;
0025     struct tcf_police_params __rcu *params;
0026 
0027     spinlock_t      tcfp_lock ____cacheline_aligned_in_smp;
0028     s64         tcfp_toks;
0029     s64         tcfp_ptoks;
0030     s64         tcfp_pkttoks;
0031     s64         tcfp_t_c;
0032 };
0033 
0034 #define to_police(pc) ((struct tcf_police *)pc)
0035 
0036 /* old policer structure from before tc actions */
0037 struct tc_police_compat {
0038     u32         index;
0039     int         action;
0040     u32         limit;
0041     u32         burst;
0042     u32         mtu;
0043     struct tc_ratespec  rate;
0044     struct tc_ratespec  peakrate;
0045 };
0046 
0047 static inline bool is_tcf_police(const struct tc_action *act)
0048 {
0049 #ifdef CONFIG_NET_CLS_ACT
0050     if (act->ops && act->ops->id == TCA_ID_POLICE)
0051         return true;
0052 #endif
0053     return false;
0054 }
0055 
0056 static inline u64 tcf_police_rate_bytes_ps(const struct tc_action *act)
0057 {
0058     struct tcf_police *police = to_police(act);
0059     struct tcf_police_params *params;
0060 
0061     params = rcu_dereference_protected(police->params,
0062                        lockdep_is_held(&police->tcf_lock));
0063     return params->rate.rate_bytes_ps;
0064 }
0065 
0066 static inline u32 tcf_police_burst(const struct tc_action *act)
0067 {
0068     struct tcf_police *police = to_police(act);
0069     struct tcf_police_params *params;
0070     u32 burst;
0071 
0072     params = rcu_dereference_protected(police->params,
0073                        lockdep_is_held(&police->tcf_lock));
0074 
0075     /*
0076      *  "rate" bytes   "burst" nanoseconds
0077      *  ------------ * -------------------
0078      *    1 second          2^6 ticks
0079      *
0080      * ------------------------------------
0081      *        NSEC_PER_SEC nanoseconds
0082      *        ------------------------
0083      *              2^6 ticks
0084      *
0085      *    "rate" bytes   "burst" nanoseconds            2^6 ticks
0086      *  = ------------ * ------------------- * ------------------------
0087      *      1 second          2^6 ticks        NSEC_PER_SEC nanoseconds
0088      *
0089      *   "rate" * "burst"
0090      * = ---------------- bytes/nanosecond
0091      *    NSEC_PER_SEC^2
0092      *
0093      *
0094      *   "rate" * "burst"
0095      * = ---------------- bytes/second
0096      *     NSEC_PER_SEC
0097      */
0098     burst = div_u64(params->tcfp_burst * params->rate.rate_bytes_ps,
0099             NSEC_PER_SEC);
0100 
0101     return burst;
0102 }
0103 
0104 static inline u64 tcf_police_rate_pkt_ps(const struct tc_action *act)
0105 {
0106     struct tcf_police *police = to_police(act);
0107     struct tcf_police_params *params;
0108 
0109     params = rcu_dereference_protected(police->params,
0110                        lockdep_is_held(&police->tcf_lock));
0111     return params->ppsrate.rate_pkts_ps;
0112 }
0113 
0114 static inline u32 tcf_police_burst_pkt(const struct tc_action *act)
0115 {
0116     struct tcf_police *police = to_police(act);
0117     struct tcf_police_params *params;
0118     u32 burst;
0119 
0120     params = rcu_dereference_protected(police->params,
0121                        lockdep_is_held(&police->tcf_lock));
0122 
0123     /*
0124      *  "rate" pkts     "burst" nanoseconds
0125      *  ------------ *  -------------------
0126      *    1 second          2^6 ticks
0127      *
0128      * ------------------------------------
0129      *        NSEC_PER_SEC nanoseconds
0130      *        ------------------------
0131      *              2^6 ticks
0132      *
0133      *    "rate" pkts    "burst" nanoseconds            2^6 ticks
0134      *  = ------------ * ------------------- * ------------------------
0135      *      1 second          2^6 ticks        NSEC_PER_SEC nanoseconds
0136      *
0137      *   "rate" * "burst"
0138      * = ---------------- pkts/nanosecond
0139      *    NSEC_PER_SEC^2
0140      *
0141      *
0142      *   "rate" * "burst"
0143      * = ---------------- pkts/second
0144      *     NSEC_PER_SEC
0145      */
0146     burst = div_u64(params->tcfp_pkt_burst * params->ppsrate.rate_pkts_ps,
0147             NSEC_PER_SEC);
0148 
0149     return burst;
0150 }
0151 
0152 static inline u32 tcf_police_tcfp_mtu(const struct tc_action *act)
0153 {
0154     struct tcf_police *police = to_police(act);
0155     struct tcf_police_params *params;
0156 
0157     params = rcu_dereference_protected(police->params,
0158                        lockdep_is_held(&police->tcf_lock));
0159     return params->tcfp_mtu;
0160 }
0161 
0162 static inline u64 tcf_police_peakrate_bytes_ps(const struct tc_action *act)
0163 {
0164     struct tcf_police *police = to_police(act);
0165     struct tcf_police_params *params;
0166 
0167     params = rcu_dereference_protected(police->params,
0168                        lockdep_is_held(&police->tcf_lock));
0169     return params->peak.rate_bytes_ps;
0170 }
0171 
0172 static inline u32 tcf_police_tcfp_ewma_rate(const struct tc_action *act)
0173 {
0174     struct tcf_police *police = to_police(act);
0175     struct tcf_police_params *params;
0176 
0177     params = rcu_dereference_protected(police->params,
0178                        lockdep_is_held(&police->tcf_lock));
0179     return params->tcfp_ewma_rate;
0180 }
0181 
0182 static inline u16 tcf_police_rate_overhead(const struct tc_action *act)
0183 {
0184     struct tcf_police *police = to_police(act);
0185     struct tcf_police_params *params;
0186 
0187     params = rcu_dereference_protected(police->params,
0188                        lockdep_is_held(&police->tcf_lock));
0189     return params->rate.overhead;
0190 }
0191 
0192 #endif /* __NET_TC_POLICE_H */