1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #ifndef __INCLUDE_RTE_METER_H__
6 #define __INCLUDE_RTE_METER_H__
14 * RTE Traffic Metering
16 * Traffic metering algorithms:
17 * 1. Single Rate Three Color Marker (srTCM): defined by IETF RFC 2697
18 * 2. Two Rate Three Color Marker (trTCM): defined by IETF RFC 2698
25 * Application Programmer's Interface (API)
29 /** Packet Color Set */
30 enum rte_meter_color {
31 e_RTE_METER_GREEN = 0, /**< Green */
32 e_RTE_METER_YELLOW, /**< Yellow */
33 e_RTE_METER_RED, /**< Red */
34 e_RTE_METER_COLORS /**< Number of available colors */
37 /** srTCM parameters per metered traffic flow. The CIR, CBS and EBS parameters only
38 count bytes of IP packets and do not include link specific headers. At least one of
39 the CBS or EBS parameters has to be greater than zero. */
40 struct rte_meter_srtcm_params {
41 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
42 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
43 uint64_t ebs; /**< Excess Burst Size (EBS). Measured in bytes. */
46 /** trTCM parameters per metered traffic flow. The CIR, PIR, CBS and PBS parameters
47 only count bytes of IP packets and do not include link specific headers. PIR has to
48 be greater than or equal to CIR. Both CBS or EBS have to be greater than zero. */
49 struct rte_meter_trtcm_params {
50 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
51 uint64_t pir; /**< Peak Information Rate (PIR). Measured in bytes per second. */
52 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in byes. */
53 uint64_t pbs; /**< Peak Burst Size (PBS). Measured in bytes. */
56 /** Internal data structure storing the srTCM run-time context per metered traffic flow. */
57 struct rte_meter_srtcm;
59 /** Internal data structure storing the trTCM run-time context per metered traffic flow. */
60 struct rte_meter_trtcm;
63 * srTCM configuration per metered traffic flow
66 * Pointer to pre-allocated srTCM data structure
68 * User parameters per srTCM metered traffic flow
70 * 0 upon success, error code otherwise
73 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
74 struct rte_meter_srtcm_params *params);
77 * trTCM configuration per metered traffic flow
80 * Pointer to pre-allocated trTCM data structure
82 * User parameters per trTCM metered traffic flow
84 * 0 upon success, error code otherwise
87 rte_meter_trtcm_config(struct rte_meter_trtcm *m,
88 struct rte_meter_trtcm_params *params);
91 * srTCM color blind traffic metering
94 * Handle to srTCM instance
96 * Current CPU time stamp (measured in CPU cycles)
98 * Length of the current IP packet (measured in bytes)
100 * Color assigned to the current IP packet
102 static inline enum rte_meter_color
103 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
108 * srTCM color aware traffic metering
111 * Handle to srTCM instance
113 * Current CPU time stamp (measured in CPU cycles)
115 * Length of the current IP packet (measured in bytes)
117 * Input color of the current IP packet
119 * Color assigned to the current IP packet
121 static inline enum rte_meter_color
122 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
125 enum rte_meter_color pkt_color);
128 * trTCM color blind traffic metering
131 * Handle to trTCM instance
133 * Current CPU time stamp (measured in CPU cycles)
135 * Length of the current IP packet (measured in bytes)
137 * Color assigned to the current IP packet
139 static inline enum rte_meter_color
140 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
145 * trTCM color aware traffic metering
148 * Handle to trTCM instance
150 * Current CPU time stamp (measured in CPU cycles)
152 * Length of the current IP packet (measured in bytes)
154 * Input color of the current IP packet
156 * Color assigned to the current IP packet
158 static inline enum rte_meter_color
159 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
162 enum rte_meter_color pkt_color);
165 * Inline implementation of run-time methods
169 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
170 struct rte_meter_srtcm {
171 uint64_t time; /* Time of latest update of C and E token buckets */
172 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
173 uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
174 uint64_t cbs; /* Upper limit for C token bucket */
175 uint64_t ebs; /* Upper limit for E token bucket */
176 uint64_t cir_period; /* Number of CPU cycles for one update of C and E token buckets */
177 uint64_t cir_bytes_per_period; /* Number of bytes to add to C and E token buckets on each update */
180 /* Internal data structure storing the trTCM run-time context per metered traffic flow. */
181 struct rte_meter_trtcm {
182 uint64_t time_tc; /* Time of latest update of C token bucket */
183 uint64_t time_tp; /* Time of latest update of E token bucket */
184 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
185 uint64_t tp; /* Number of bytes currently available in the peak (P) token bucket */
186 uint64_t cbs; /* Upper limit for C token bucket */
187 uint64_t pbs; /* Upper limit for P token bucket */
188 uint64_t cir_period; /* Number of CPU cycles for one update of C token bucket */
189 uint64_t cir_bytes_per_period; /* Number of bytes to add to C token bucket on each update */
190 uint64_t pir_period; /* Number of CPU cycles for one update of P token bucket */
191 uint64_t pir_bytes_per_period; /* Number of bytes to add to P token bucket on each update */
194 static inline enum rte_meter_color
195 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
199 uint64_t time_diff, n_periods, tc, te;
202 time_diff = time - m->time;
203 n_periods = time_diff / m->cir_period;
204 m->time += n_periods * m->cir_period;
206 /* Put the tokens overflowing from tc into te bucket */
207 tc = m->tc + n_periods * m->cir_bytes_per_period;
218 m->tc = tc - pkt_len;
220 return e_RTE_METER_GREEN;
225 m->te = te - pkt_len;
226 return e_RTE_METER_YELLOW;
231 return e_RTE_METER_RED;
234 static inline enum rte_meter_color
235 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
238 enum rte_meter_color pkt_color)
240 uint64_t time_diff, n_periods, tc, te;
243 time_diff = time - m->time;
244 n_periods = time_diff / m->cir_period;
245 m->time += n_periods * m->cir_period;
247 /* Put the tokens overflowing from tc into te bucket */
248 tc = m->tc + n_periods * m->cir_bytes_per_period;
258 if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
259 m->tc = tc - pkt_len;
261 return e_RTE_METER_GREEN;
264 if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
266 m->te = te - pkt_len;
267 return e_RTE_METER_YELLOW;
272 return e_RTE_METER_RED;
275 static inline enum rte_meter_color
276 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
280 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
283 time_diff_tc = time - m->time_tc;
284 time_diff_tp = time - m->time_tp;
285 n_periods_tc = time_diff_tc / m->cir_period;
286 n_periods_tp = time_diff_tp / m->pir_period;
287 m->time_tc += n_periods_tc * m->cir_period;
288 m->time_tp += n_periods_tp * m->pir_period;
290 tc = m->tc + n_periods_tc * m->cir_bytes_per_period;
294 tp = m->tp + n_periods_tp * m->pir_bytes_per_period;
302 return e_RTE_METER_RED;
307 m->tp = tp - pkt_len;
308 return e_RTE_METER_YELLOW;
311 m->tc = tc - pkt_len;
312 m->tp = tp - pkt_len;
313 return e_RTE_METER_GREEN;
316 static inline enum rte_meter_color
317 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
320 enum rte_meter_color pkt_color)
322 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
325 time_diff_tc = time - m->time_tc;
326 time_diff_tp = time - m->time_tp;
327 n_periods_tc = time_diff_tc / m->cir_period;
328 n_periods_tp = time_diff_tp / m->pir_period;
329 m->time_tc += n_periods_tc * m->cir_period;
330 m->time_tp += n_periods_tp * m->pir_period;
332 tc = m->tc + n_periods_tc * m->cir_bytes_per_period;
336 tp = m->tp + n_periods_tp * m->pir_bytes_per_period;
341 if ((pkt_color == e_RTE_METER_RED) || (tp < pkt_len)) {
344 return e_RTE_METER_RED;
347 if ((pkt_color == e_RTE_METER_YELLOW) || (tc < pkt_len)) {
349 m->tp = tp - pkt_len;
350 return e_RTE_METER_YELLOW;
353 m->tc = tc - pkt_len;
354 m->tp = tp - pkt_len;
355 return e_RTE_METER_GREEN;
362 #endif /* __INCLUDE_RTE_METER_H__ */