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. */
57 * Internal data structure storing the srTCM configuration profile. Typically
58 * shared by multiple srTCM objects.
60 struct rte_meter_srtcm_profile;
63 * Internal data structure storing the trTCM configuration profile. Typically
64 * shared by multiple trTCM objects.
66 struct rte_meter_trtcm_profile;
68 /** Internal data structure storing the srTCM run-time context per metered traffic flow. */
69 struct rte_meter_srtcm;
71 /** Internal data structure storing the trTCM run-time context per metered traffic flow. */
72 struct rte_meter_trtcm;
75 * srTCM profile configuration
78 * Pointer to pre-allocated srTCM profile data structure
80 * srTCM profile parameters
82 * 0 upon success, error code otherwise
85 rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
86 struct rte_meter_srtcm_params *params);
89 * trTCM profile configuration
92 * Pointer to pre-allocated trTCM profile data structure
94 * trTCM profile parameters
96 * 0 upon success, error code otherwise
99 rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
100 struct rte_meter_trtcm_params *params);
103 * srTCM configuration per metered traffic flow
106 * Pointer to pre-allocated srTCM data structure
108 * srTCM profile. Needs to be valid.
110 * 0 upon success, error code otherwise
113 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
114 struct rte_meter_srtcm_profile *p);
117 * trTCM configuration per metered traffic flow
120 * Pointer to pre-allocated trTCM data structure
122 * trTCM profile. Needs to be valid.
124 * 0 upon success, error code otherwise
127 rte_meter_trtcm_config(struct rte_meter_trtcm *m,
128 struct rte_meter_trtcm_profile *p);
131 * srTCM color blind traffic metering
134 * Handle to srTCM instance
136 * srTCM profile specified at srTCM object creation time
138 * Current CPU time stamp (measured in CPU cycles)
140 * Length of the current IP packet (measured in bytes)
142 * Color assigned to the current IP packet
144 static inline enum rte_meter_color
145 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
146 struct rte_meter_srtcm_profile *p,
151 * srTCM color aware traffic metering
154 * Handle to srTCM instance
156 * srTCM profile specified at srTCM object creation time
158 * Current CPU time stamp (measured in CPU cycles)
160 * Length of the current IP packet (measured in bytes)
162 * Input color of the current IP packet
164 * Color assigned to the current IP packet
166 static inline enum rte_meter_color
167 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
168 struct rte_meter_srtcm_profile *p,
171 enum rte_meter_color pkt_color);
174 * trTCM color blind traffic metering
177 * Handle to trTCM instance
179 * trTCM profile specified at trTCM object creation time
181 * Current CPU time stamp (measured in CPU cycles)
183 * Length of the current IP packet (measured in bytes)
185 * Color assigned to the current IP packet
187 static inline enum rte_meter_color
188 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
189 struct rte_meter_trtcm_profile *p,
194 * trTCM color aware traffic metering
197 * Handle to trTCM instance
199 * trTCM profile specified at trTCM object creation time
201 * Current CPU time stamp (measured in CPU cycles)
203 * Length of the current IP packet (measured in bytes)
205 * Input color of the current IP packet
207 * Color assigned to the current IP packet
209 static inline enum rte_meter_color
210 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
211 struct rte_meter_trtcm_profile *p,
214 enum rte_meter_color pkt_color);
217 * Inline implementation of run-time methods
221 struct rte_meter_srtcm_profile {
223 /**< Upper limit for C token bucket */
225 /**< Upper limit for E token bucket */
227 /**< Number of CPU cycles for each update of C and E token buckets */
228 uint64_t cir_bytes_per_period;
229 /**< Number of bytes to add to C and E token buckets on each update */
232 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
233 struct rte_meter_srtcm {
234 uint64_t time; /* Time of latest update of C and E token buckets */
235 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
236 uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
239 struct rte_meter_trtcm_profile {
241 /**< Upper limit for C token bucket */
243 /**< Upper limit for P token bucket */
245 /**< Number of CPU cycles for one update of C token bucket */
246 uint64_t cir_bytes_per_period;
247 /**< Number of bytes to add to C token bucket on each update */
249 /**< Number of CPU cycles for one update of P token bucket */
250 uint64_t pir_bytes_per_period;
251 /**< Number of bytes to add to P token bucket on each update */
255 * Internal data structure storing the trTCM run-time context per metered
258 struct rte_meter_trtcm {
260 /**< Time of latest update of C token bucket */
262 /**< Time of latest update of E token bucket */
264 /**< Number of bytes currently available in committed(C) token bucket */
266 /**< Number of bytes currently available in the peak(P) token bucket */
269 static inline enum rte_meter_color
270 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
271 struct rte_meter_srtcm_profile *p,
275 uint64_t time_diff, n_periods, tc, te;
278 time_diff = time - m->time;
279 n_periods = time_diff / p->cir_period;
280 m->time += n_periods * p->cir_period;
282 /* Put the tokens overflowing from tc into te bucket */
283 tc = m->tc + n_periods * p->cir_bytes_per_period;
294 m->tc = tc - pkt_len;
296 return e_RTE_METER_GREEN;
301 m->te = te - pkt_len;
302 return e_RTE_METER_YELLOW;
307 return e_RTE_METER_RED;
310 static inline enum rte_meter_color
311 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
312 struct rte_meter_srtcm_profile *p,
315 enum rte_meter_color pkt_color)
317 uint64_t time_diff, n_periods, tc, te;
320 time_diff = time - m->time;
321 n_periods = time_diff / p->cir_period;
322 m->time += n_periods * p->cir_period;
324 /* Put the tokens overflowing from tc into te bucket */
325 tc = m->tc + n_periods * p->cir_bytes_per_period;
335 if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
336 m->tc = tc - pkt_len;
338 return e_RTE_METER_GREEN;
341 if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
343 m->te = te - pkt_len;
344 return e_RTE_METER_YELLOW;
349 return e_RTE_METER_RED;
352 static inline enum rte_meter_color
353 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
354 struct rte_meter_trtcm_profile *p,
358 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
361 time_diff_tc = time - m->time_tc;
362 time_diff_tp = time - m->time_tp;
363 n_periods_tc = time_diff_tc / p->cir_period;
364 n_periods_tp = time_diff_tp / p->pir_period;
365 m->time_tc += n_periods_tc * p->cir_period;
366 m->time_tp += n_periods_tp * p->pir_period;
368 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
372 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
380 return e_RTE_METER_RED;
385 m->tp = tp - pkt_len;
386 return e_RTE_METER_YELLOW;
389 m->tc = tc - pkt_len;
390 m->tp = tp - pkt_len;
391 return e_RTE_METER_GREEN;
394 static inline enum rte_meter_color
395 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
396 struct rte_meter_trtcm_profile *p,
399 enum rte_meter_color pkt_color)
401 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
404 time_diff_tc = time - m->time_tc;
405 time_diff_tp = time - m->time_tp;
406 n_periods_tc = time_diff_tc / p->cir_period;
407 n_periods_tp = time_diff_tp / p->pir_period;
408 m->time_tc += n_periods_tc * p->cir_period;
409 m->time_tp += n_periods_tp * p->pir_period;
411 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
415 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
420 if ((pkt_color == e_RTE_METER_RED) || (tp < pkt_len)) {
423 return e_RTE_METER_RED;
426 if ((pkt_color == e_RTE_METER_YELLOW) || (tc < pkt_len)) {
428 m->tp = tp - pkt_len;
429 return e_RTE_METER_YELLOW;
432 m->tc = tc - pkt_len;
433 m->tp = tp - pkt_len;
434 return e_RTE_METER_GREEN;
441 #endif /* __INCLUDE_RTE_METER_H__ */