1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_TC_POLICE_H
3 #define __NET_TC_POLICE_H
5 #include <net/act_api.h>
7 struct tcf_police_params {
14 struct psched_ratecfg rate;
16 struct psched_ratecfg peak;
18 struct psched_pktrate ppsrate;
24 struct tc_action common;
25 struct tcf_police_params __rcu *params;
27 spinlock_t tcfp_lock ____cacheline_aligned_in_smp;
34 #define to_police(pc) ((struct tcf_police *)pc)
36 /* old policer structure from before tc actions */
37 struct tc_police_compat {
43 struct tc_ratespec rate;
44 struct tc_ratespec peakrate;
47 static inline bool is_tcf_police(const struct tc_action *act)
49 #ifdef CONFIG_NET_CLS_ACT
50 if (act->ops && act->ops->id == TCA_ID_POLICE)
56 static inline u64 tcf_police_rate_bytes_ps(const struct tc_action *act)
58 struct tcf_police *police = to_police(act);
59 struct tcf_police_params *params;
61 params = rcu_dereference_protected(police->params,
62 lockdep_is_held(&police->tcf_lock));
63 return params->rate.rate_bytes_ps;
66 static inline u32 tcf_police_burst(const struct tc_action *act)
68 struct tcf_police *police = to_police(act);
69 struct tcf_police_params *params;
72 params = rcu_dereference_protected(police->params,
73 lockdep_is_held(&police->tcf_lock));
76 * "rate" bytes "burst" nanoseconds
77 * ------------ * -------------------
80 * ------------------------------------
81 * NSEC_PER_SEC nanoseconds
82 * ------------------------
85 * "rate" bytes "burst" nanoseconds 2^6 ticks
86 * = ------------ * ------------------- * ------------------------
87 * 1 second 2^6 ticks NSEC_PER_SEC nanoseconds
90 * = ---------------- bytes/nanosecond
95 * = ---------------- bytes/second
98 burst = div_u64(params->tcfp_burst * params->rate.rate_bytes_ps,
104 static inline u64 tcf_police_rate_pkt_ps(const struct tc_action *act)
106 struct tcf_police *police = to_police(act);
107 struct tcf_police_params *params;
109 params = rcu_dereference_protected(police->params,
110 lockdep_is_held(&police->tcf_lock));
111 return params->ppsrate.rate_pkts_ps;
114 static inline u32 tcf_police_burst_pkt(const struct tc_action *act)
116 struct tcf_police *police = to_police(act);
117 struct tcf_police_params *params;
120 params = rcu_dereference_protected(police->params,
121 lockdep_is_held(&police->tcf_lock));
124 * "rate" pkts "burst" nanoseconds
125 * ------------ * -------------------
128 * ------------------------------------
129 * NSEC_PER_SEC nanoseconds
130 * ------------------------
133 * "rate" pkts "burst" nanoseconds 2^6 ticks
134 * = ------------ * ------------------- * ------------------------
135 * 1 second 2^6 ticks NSEC_PER_SEC nanoseconds
138 * = ---------------- pkts/nanosecond
143 * = ---------------- pkts/second
146 burst = div_u64(params->tcfp_pkt_burst * params->ppsrate.rate_pkts_ps,
152 static inline u32 tcf_police_tcfp_mtu(const struct tc_action *act)
154 struct tcf_police *police = to_police(act);
155 struct tcf_police_params *params;
157 params = rcu_dereference_protected(police->params,
158 lockdep_is_held(&police->tcf_lock));
159 return params->tcfp_mtu;
162 static inline u64 tcf_police_peakrate_bytes_ps(const struct tc_action *act)
164 struct tcf_police *police = to_police(act);
165 struct tcf_police_params *params;
167 params = rcu_dereference_protected(police->params,
168 lockdep_is_held(&police->tcf_lock));
169 return params->peak.rate_bytes_ps;
172 static inline u32 tcf_police_tcfp_ewma_rate(const struct tc_action *act)
174 struct tcf_police *police = to_police(act);
175 struct tcf_police_params *params;
177 params = rcu_dereference_protected(police->params,
178 lockdep_is_held(&police->tcf_lock));
179 return params->tcfp_ewma_rate;
182 static inline u16 tcf_police_rate_overhead(const struct tc_action *act)
184 struct tcf_police *police = to_police(act);
185 struct tcf_police_params *params;
187 params = rcu_dereference_protected(police->params,
188 lockdep_is_held(&police->tcf_lock));
189 return params->rate.overhead;
192 #endif /* __NET_TC_POLICE_H */