1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __NET_TC_POLICE_H 3 #define __NET_TC_POLICE_H 4 5 #include <net/act_api.h> 6 7 struct tcf_police_params { 8 int action; 9 int tcfp_result; 10 u32 tcfp_ewma_rate; 11 u32 tcfp_mtu; 12 s64 tcfp_burst; 13 s64 tcfp_mtu_ptoks; 14 s64 tcfp_pkt_burst; 15 struct psched_ratecfg rate; 16 bool rate_present; 17 struct psched_ratecfg peak; 18 bool peak_present; 19 struct psched_pktrate ppsrate; 20 bool pps_present; 21 struct rcu_head rcu; 22 }; 23 24 struct tcf_police { 25 struct tc_action common; 26 struct tcf_police_params __rcu *params; 27 28 spinlock_t tcfp_lock ____cacheline_aligned_in_smp; 29 s64 tcfp_toks; 30 s64 tcfp_ptoks; 31 s64 tcfp_pkttoks; 32 s64 tcfp_t_c; 33 }; 34 35 #define to_police(pc) ((struct tcf_police *)pc) 36 37 /* old policer structure from before tc actions */ 38 struct tc_police_compat { 39 u32 index; 40 int action; 41 u32 limit; 42 u32 burst; 43 u32 mtu; 44 struct tc_ratespec rate; 45 struct tc_ratespec peakrate; 46 }; 47 48 static inline u64 tcf_police_rate_bytes_ps(const struct tc_action *act) 49 { 50 struct tcf_police *police = to_police(act); 51 struct tcf_police_params *params; 52 53 params = rcu_dereference_protected(police->params, 54 lockdep_is_held(&police->tcf_lock)); 55 return params->rate.rate_bytes_ps; 56 } 57 58 static inline u32 tcf_police_burst(const struct tc_action *act) 59 { 60 struct tcf_police *police = to_police(act); 61 struct tcf_police_params *params; 62 u32 burst; 63 64 params = rcu_dereference_protected(police->params, 65 lockdep_is_held(&police->tcf_lock)); 66 67 /* 68 * "rate" bytes "burst" nanoseconds 69 * ------------ * ------------------- 70 * 1 second 2^6 ticks 71 * 72 * ------------------------------------ 73 * NSEC_PER_SEC nanoseconds 74 * ------------------------ 75 * 2^6 ticks 76 * 77 * "rate" bytes "burst" nanoseconds 2^6 ticks 78 * = ------------ * ------------------- * ------------------------ 79 * 1 second 2^6 ticks NSEC_PER_SEC nanoseconds 80 * 81 * "rate" * "burst" 82 * = ---------------- bytes/nanosecond 83 * NSEC_PER_SEC^2 84 * 85 * 86 * "rate" * "burst" 87 * = ---------------- bytes/second 88 * NSEC_PER_SEC 89 */ 90 burst = div_u64(params->tcfp_burst * params->rate.rate_bytes_ps, 91 NSEC_PER_SEC); 92 93 return burst; 94 } 95 96 static inline u64 tcf_police_rate_pkt_ps(const struct tc_action *act) 97 { 98 struct tcf_police *police = to_police(act); 99 struct tcf_police_params *params; 100 101 params = rcu_dereference_protected(police->params, 102 lockdep_is_held(&police->tcf_lock)); 103 return params->ppsrate.rate_pkts_ps; 104 } 105 106 static inline u32 tcf_police_burst_pkt(const struct tc_action *act) 107 { 108 struct tcf_police *police = to_police(act); 109 struct tcf_police_params *params; 110 u32 burst; 111 112 params = rcu_dereference_protected(police->params, 113 lockdep_is_held(&police->tcf_lock)); 114 115 /* 116 * "rate" pkts "burst" nanoseconds 117 * ------------ * ------------------- 118 * 1 second 2^6 ticks 119 * 120 * ------------------------------------ 121 * NSEC_PER_SEC nanoseconds 122 * ------------------------ 123 * 2^6 ticks 124 * 125 * "rate" pkts "burst" nanoseconds 2^6 ticks 126 * = ------------ * ------------------- * ------------------------ 127 * 1 second 2^6 ticks NSEC_PER_SEC nanoseconds 128 * 129 * "rate" * "burst" 130 * = ---------------- pkts/nanosecond 131 * NSEC_PER_SEC^2 132 * 133 * 134 * "rate" * "burst" 135 * = ---------------- pkts/second 136 * NSEC_PER_SEC 137 */ 138 burst = div_u64(params->tcfp_pkt_burst * params->ppsrate.rate_pkts_ps, 139 NSEC_PER_SEC); 140 141 return burst; 142 } 143 144 static inline u32 tcf_police_tcfp_mtu(const struct tc_action *act) 145 { 146 struct tcf_police *police = to_police(act); 147 struct tcf_police_params *params; 148 149 params = rcu_dereference_protected(police->params, 150 lockdep_is_held(&police->tcf_lock)); 151 return params->tcfp_mtu; 152 } 153 154 static inline u64 tcf_police_peakrate_bytes_ps(const struct tc_action *act) 155 { 156 struct tcf_police *police = to_police(act); 157 struct tcf_police_params *params; 158 159 params = rcu_dereference_protected(police->params, 160 lockdep_is_held(&police->tcf_lock)); 161 return params->peak.rate_bytes_ps; 162 } 163 164 static inline u32 tcf_police_tcfp_ewma_rate(const struct tc_action *act) 165 { 166 struct tcf_police *police = to_police(act); 167 struct tcf_police_params *params; 168 169 params = rcu_dereference_protected(police->params, 170 lockdep_is_held(&police->tcf_lock)); 171 return params->tcfp_ewma_rate; 172 } 173 174 static inline u16 tcf_police_rate_overhead(const struct tc_action *act) 175 { 176 struct tcf_police *police = to_police(act); 177 struct tcf_police_params *params; 178 179 params = rcu_dereference_protected(police->params, 180 lockdep_is_held(&police->tcf_lock)); 181 return params->rate.overhead; 182 } 183 184 #endif /* __NET_TC_POLICE_H */ 185