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