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)
33 RTE_COLOR_GREEN = 0, /**< Green */
34 RTE_COLOR_YELLOW, /**< Yellow */
35 RTE_COLOR_RED, /**< Red */
36 RTE_COLORS /**< Number of colors */
39 /* New rte_color is defined and used to deprecate rte_meter_color soon. */
40 #define rte_meter_color rte_color
41 #define e_RTE_METER_GREEN RTE_COLOR_GREEN
42 #define e_RTE_METER_YELLOW RTE_COLOR_YELLOW
43 #define e_RTE_METER_RED RTE_COLOR_RED
44 #define e_RTE_METER_COLORS RTE_COLORS
46 /** srTCM parameters per metered traffic flow. The CIR, CBS and EBS parameters only
47 count bytes of IP packets and do not include link specific headers. At least one of
48 the CBS or EBS parameters has to be greater than zero. */
49 struct rte_meter_srtcm_params {
50 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
51 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
52 uint64_t ebs; /**< Excess Burst Size (EBS). Measured in bytes. */
55 /** trTCM parameters per metered traffic flow. The CIR, PIR, CBS and PBS parameters
56 only count bytes of IP packets and do not include link specific headers. PIR has to
57 be greater than or equal to CIR. Both CBS or EBS have to be greater than zero. */
58 struct rte_meter_trtcm_params {
59 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
60 uint64_t pir; /**< Peak Information Rate (PIR). Measured in bytes per second. */
61 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in byes. */
62 uint64_t pbs; /**< Peak Burst Size (PBS). Measured in bytes. */
66 * Internal data structure storing the srTCM configuration profile. Typically
67 * shared by multiple srTCM objects.
69 struct rte_meter_srtcm_profile;
72 * Internal data structure storing the trTCM configuration profile. Typically
73 * shared by multiple trTCM objects.
75 struct rte_meter_trtcm_profile;
77 /** Internal data structure storing the srTCM run-time context per metered traffic flow. */
78 struct rte_meter_srtcm;
80 /** Internal data structure storing the trTCM run-time context per metered traffic flow. */
81 struct rte_meter_trtcm;
84 * srTCM profile configuration
87 * Pointer to pre-allocated srTCM profile data structure
89 * srTCM profile parameters
91 * 0 upon success, error code otherwise
94 rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
95 struct rte_meter_srtcm_params *params);
98 * trTCM profile configuration
101 * Pointer to pre-allocated trTCM profile data structure
103 * trTCM profile parameters
105 * 0 upon success, error code otherwise
108 rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
109 struct rte_meter_trtcm_params *params);
112 * srTCM configuration per metered traffic flow
115 * Pointer to pre-allocated srTCM data structure
117 * srTCM profile. Needs to be valid.
119 * 0 upon success, error code otherwise
122 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
123 struct rte_meter_srtcm_profile *p);
126 * trTCM configuration per metered traffic flow
129 * Pointer to pre-allocated trTCM data structure
131 * trTCM profile. Needs to be valid.
133 * 0 upon success, error code otherwise
136 rte_meter_trtcm_config(struct rte_meter_trtcm *m,
137 struct rte_meter_trtcm_profile *p);
140 * srTCM color blind traffic metering
143 * Handle to srTCM instance
145 * srTCM profile specified at srTCM object creation time
147 * Current CPU time stamp (measured in CPU cycles)
149 * Length of the current IP packet (measured in bytes)
151 * Color assigned to the current IP packet
153 static inline enum rte_meter_color
154 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
155 struct rte_meter_srtcm_profile *p,
160 * srTCM color aware traffic metering
163 * Handle to srTCM instance
165 * srTCM profile specified at srTCM object creation time
167 * Current CPU time stamp (measured in CPU cycles)
169 * Length of the current IP packet (measured in bytes)
171 * Input color of the current IP packet
173 * Color assigned to the current IP packet
175 static inline enum rte_meter_color
176 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
177 struct rte_meter_srtcm_profile *p,
180 enum rte_meter_color pkt_color);
183 * trTCM color blind traffic metering
186 * Handle to trTCM instance
188 * trTCM profile specified at trTCM object creation time
190 * Current CPU time stamp (measured in CPU cycles)
192 * Length of the current IP packet (measured in bytes)
194 * Color assigned to the current IP packet
196 static inline enum rte_meter_color
197 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
198 struct rte_meter_trtcm_profile *p,
203 * trTCM color aware traffic metering
206 * Handle to trTCM instance
208 * trTCM profile specified at trTCM object creation time
210 * Current CPU time stamp (measured in CPU cycles)
212 * Length of the current IP packet (measured in bytes)
214 * Input color of the current IP packet
216 * Color assigned to the current IP packet
218 static inline enum rte_meter_color
219 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
220 struct rte_meter_trtcm_profile *p,
223 enum rte_meter_color pkt_color);
226 * Inline implementation of run-time methods
230 struct rte_meter_srtcm_profile {
232 /**< Upper limit for C token bucket */
234 /**< Upper limit for E token bucket */
236 /**< Number of CPU cycles for each update of C and E token buckets */
237 uint64_t cir_bytes_per_period;
238 /**< Number of bytes to add to C and E token buckets on each update */
241 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
242 struct rte_meter_srtcm {
243 uint64_t time; /* Time of latest update of C and E token buckets */
244 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
245 uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
248 struct rte_meter_trtcm_profile {
250 /**< Upper limit for C token bucket */
252 /**< Upper limit for P token bucket */
254 /**< Number of CPU cycles for one update of C token bucket */
255 uint64_t cir_bytes_per_period;
256 /**< Number of bytes to add to C token bucket on each update */
258 /**< Number of CPU cycles for one update of P token bucket */
259 uint64_t pir_bytes_per_period;
260 /**< Number of bytes to add to P token bucket on each update */
264 * Internal data structure storing the trTCM run-time context per metered
267 struct rte_meter_trtcm {
269 /**< Time of latest update of C token bucket */
271 /**< Time of latest update of E token bucket */
273 /**< Number of bytes currently available in committed(C) token bucket */
275 /**< Number of bytes currently available in the peak(P) token bucket */
278 static inline enum rte_meter_color
279 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
280 struct rte_meter_srtcm_profile *p,
284 uint64_t time_diff, n_periods, tc, te;
287 time_diff = time - m->time;
288 n_periods = time_diff / p->cir_period;
289 m->time += n_periods * p->cir_period;
291 /* Put the tokens overflowing from tc into te bucket */
292 tc = m->tc + n_periods * p->cir_bytes_per_period;
303 m->tc = tc - pkt_len;
305 return e_RTE_METER_GREEN;
310 m->te = te - pkt_len;
311 return e_RTE_METER_YELLOW;
316 return e_RTE_METER_RED;
319 static inline enum rte_meter_color
320 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
321 struct rte_meter_srtcm_profile *p,
324 enum rte_meter_color pkt_color)
326 uint64_t time_diff, n_periods, tc, te;
329 time_diff = time - m->time;
330 n_periods = time_diff / p->cir_period;
331 m->time += n_periods * p->cir_period;
333 /* Put the tokens overflowing from tc into te bucket */
334 tc = m->tc + n_periods * p->cir_bytes_per_period;
344 if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
345 m->tc = tc - pkt_len;
347 return e_RTE_METER_GREEN;
350 if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
352 m->te = te - pkt_len;
353 return e_RTE_METER_YELLOW;
358 return e_RTE_METER_RED;
361 static inline enum rte_meter_color
362 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
363 struct rte_meter_trtcm_profile *p,
367 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
370 time_diff_tc = time - m->time_tc;
371 time_diff_tp = time - m->time_tp;
372 n_periods_tc = time_diff_tc / p->cir_period;
373 n_periods_tp = time_diff_tp / p->pir_period;
374 m->time_tc += n_periods_tc * p->cir_period;
375 m->time_tp += n_periods_tp * p->pir_period;
377 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
381 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
389 return e_RTE_METER_RED;
394 m->tp = tp - pkt_len;
395 return e_RTE_METER_YELLOW;
398 m->tc = tc - pkt_len;
399 m->tp = tp - pkt_len;
400 return e_RTE_METER_GREEN;
403 static inline enum rte_meter_color
404 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
405 struct rte_meter_trtcm_profile *p,
408 enum rte_meter_color pkt_color)
410 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
413 time_diff_tc = time - m->time_tc;
414 time_diff_tp = time - m->time_tp;
415 n_periods_tc = time_diff_tc / p->cir_period;
416 n_periods_tp = time_diff_tp / p->pir_period;
417 m->time_tc += n_periods_tc * p->cir_period;
418 m->time_tp += n_periods_tp * p->pir_period;
420 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
424 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
429 if ((pkt_color == e_RTE_METER_RED) || (tp < pkt_len)) {
432 return e_RTE_METER_RED;
435 if ((pkt_color == e_RTE_METER_YELLOW) || (tc < pkt_len)) {
437 m->tp = tp - pkt_len;
438 return e_RTE_METER_YELLOW;
441 m->tc = tc - pkt_len;
442 m->tp = tp - pkt_len;
443 return e_RTE_METER_GREEN;
450 #endif /* __INCLUDE_RTE_METER_H__ */