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
23 #include <rte_compat.h>
26 * Application Programmer's Interface (API)
30 /** Packet Color Set */
31 enum rte_meter_color {
32 e_RTE_METER_GREEN = 0, /**< Green */
33 e_RTE_METER_YELLOW, /**< Yellow */
34 e_RTE_METER_RED, /**< Red */
35 e_RTE_METER_COLORS /**< Number of available colors */
38 /** srTCM parameters per metered traffic flow. The CIR, CBS and EBS parameters only
39 count bytes of IP packets and do not include link specific headers. At least one of
40 the CBS or EBS parameters has to be greater than zero. */
41 struct rte_meter_srtcm_params {
42 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
43 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
44 uint64_t ebs; /**< Excess Burst Size (EBS). Measured in bytes. */
47 /** trTCM parameters per metered traffic flow. The CIR, PIR, CBS and PBS parameters
48 only count bytes of IP packets and do not include link specific headers. PIR has to
49 be greater than or equal to CIR. Both CBS or EBS have to be greater than zero. */
50 struct rte_meter_trtcm_params {
51 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
52 uint64_t pir; /**< Peak Information Rate (PIR). Measured in bytes per second. */
53 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in byes. */
54 uint64_t pbs; /**< Peak Burst Size (PBS). Measured in bytes. */
58 * Internal data structure storing the srTCM configuration profile. Typically
59 * shared by multiple srTCM objects.
61 struct rte_meter_srtcm_profile;
64 * Internal data structure storing the trTCM configuration profile. Typically
65 * shared by multiple trTCM objects.
67 struct rte_meter_trtcm_profile;
69 /** Internal data structure storing the srTCM run-time context per metered traffic flow. */
70 struct rte_meter_srtcm;
72 /** Internal data structure storing the trTCM run-time context per metered traffic flow. */
73 struct rte_meter_trtcm;
76 * srTCM profile configuration
79 * Pointer to pre-allocated srTCM profile data structure
81 * srTCM profile parameters
83 * 0 upon success, error code otherwise
85 int __rte_experimental
86 rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
87 struct rte_meter_srtcm_params *params);
90 * trTCM profile configuration
93 * Pointer to pre-allocated trTCM profile data structure
95 * trTCM profile parameters
97 * 0 upon success, error code otherwise
99 int __rte_experimental
100 rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
101 struct rte_meter_trtcm_params *params);
104 * srTCM configuration per metered traffic flow
107 * Pointer to pre-allocated srTCM data structure
109 * srTCM profile. Needs to be valid.
111 * 0 upon success, error code otherwise
114 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
115 struct rte_meter_srtcm_profile *p);
118 * trTCM configuration per metered traffic flow
121 * Pointer to pre-allocated trTCM data structure
123 * trTCM profile. Needs to be valid.
125 * 0 upon success, error code otherwise
128 rte_meter_trtcm_config(struct rte_meter_trtcm *m,
129 struct rte_meter_trtcm_profile *p);
132 * srTCM color blind traffic metering
135 * Handle to srTCM instance
137 * srTCM profile specified at srTCM object creation time
139 * Current CPU time stamp (measured in CPU cycles)
141 * Length of the current IP packet (measured in bytes)
143 * Color assigned to the current IP packet
145 static inline enum rte_meter_color
146 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
147 struct rte_meter_srtcm_profile *p,
152 * srTCM color aware traffic metering
155 * Handle to srTCM instance
157 * srTCM profile specified at srTCM object creation time
159 * Current CPU time stamp (measured in CPU cycles)
161 * Length of the current IP packet (measured in bytes)
163 * Input color of the current IP packet
165 * Color assigned to the current IP packet
167 static inline enum rte_meter_color
168 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
169 struct rte_meter_srtcm_profile *p,
172 enum rte_meter_color pkt_color);
175 * trTCM color blind traffic metering
178 * Handle to trTCM instance
180 * trTCM profile specified at trTCM object creation time
182 * Current CPU time stamp (measured in CPU cycles)
184 * Length of the current IP packet (measured in bytes)
186 * Color assigned to the current IP packet
188 static inline enum rte_meter_color
189 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
190 struct rte_meter_trtcm_profile *p,
195 * trTCM color aware traffic metering
198 * Handle to trTCM instance
200 * trTCM profile specified at trTCM object creation time
202 * Current CPU time stamp (measured in CPU cycles)
204 * Length of the current IP packet (measured in bytes)
206 * Input color of the current IP packet
208 * Color assigned to the current IP packet
210 static inline enum rte_meter_color
211 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
212 struct rte_meter_trtcm_profile *p,
215 enum rte_meter_color pkt_color);
218 * Inline implementation of run-time methods
222 struct rte_meter_srtcm_profile {
224 /**< Upper limit for C token bucket */
226 /**< Upper limit for E token bucket */
228 /**< Number of CPU cycles for each update of C and E token buckets */
229 uint64_t cir_bytes_per_period;
230 /**< Number of bytes to add to C and E token buckets on each update */
233 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
234 struct rte_meter_srtcm {
235 uint64_t time; /* Time of latest update of C and E token buckets */
236 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
237 uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
240 struct rte_meter_trtcm_profile {
242 /**< Upper limit for C token bucket */
244 /**< Upper limit for P token bucket */
246 /**< Number of CPU cycles for one update of C token bucket */
247 uint64_t cir_bytes_per_period;
248 /**< Number of bytes to add to C token bucket on each update */
250 /**< Number of CPU cycles for one update of P token bucket */
251 uint64_t pir_bytes_per_period;
252 /**< Number of bytes to add to P token bucket on each update */
256 * Internal data structure storing the trTCM run-time context per metered
259 struct rte_meter_trtcm {
261 /**< Time of latest update of C token bucket */
263 /**< Time of latest update of E token bucket */
265 /**< Number of bytes currently available in committed(C) token bucket */
267 /**< Number of bytes currently available in the peak(P) token bucket */
270 static inline enum rte_meter_color
271 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
272 struct rte_meter_srtcm_profile *p,
276 uint64_t time_diff, n_periods, tc, te;
279 time_diff = time - m->time;
280 n_periods = time_diff / p->cir_period;
281 m->time += n_periods * p->cir_period;
283 /* Put the tokens overflowing from tc into te bucket */
284 tc = m->tc + n_periods * p->cir_bytes_per_period;
295 m->tc = tc - pkt_len;
297 return e_RTE_METER_GREEN;
302 m->te = te - pkt_len;
303 return e_RTE_METER_YELLOW;
308 return e_RTE_METER_RED;
311 static inline enum rte_meter_color
312 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
313 struct rte_meter_srtcm_profile *p,
316 enum rte_meter_color pkt_color)
318 uint64_t time_diff, n_periods, tc, te;
321 time_diff = time - m->time;
322 n_periods = time_diff / p->cir_period;
323 m->time += n_periods * p->cir_period;
325 /* Put the tokens overflowing from tc into te bucket */
326 tc = m->tc + n_periods * p->cir_bytes_per_period;
336 if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
337 m->tc = tc - pkt_len;
339 return e_RTE_METER_GREEN;
342 if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
344 m->te = te - pkt_len;
345 return e_RTE_METER_YELLOW;
350 return e_RTE_METER_RED;
353 static inline enum rte_meter_color
354 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
355 struct rte_meter_trtcm_profile *p,
359 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
362 time_diff_tc = time - m->time_tc;
363 time_diff_tp = time - m->time_tp;
364 n_periods_tc = time_diff_tc / p->cir_period;
365 n_periods_tp = time_diff_tp / p->pir_period;
366 m->time_tc += n_periods_tc * p->cir_period;
367 m->time_tp += n_periods_tp * p->pir_period;
369 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
373 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
381 return e_RTE_METER_RED;
386 m->tp = tp - pkt_len;
387 return e_RTE_METER_YELLOW;
390 m->tc = tc - pkt_len;
391 m->tp = tp - pkt_len;
392 return e_RTE_METER_GREEN;
395 static inline enum rte_meter_color
396 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
397 struct rte_meter_trtcm_profile *p,
400 enum rte_meter_color pkt_color)
402 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
405 time_diff_tc = time - m->time_tc;
406 time_diff_tp = time - m->time_tp;
407 n_periods_tc = time_diff_tc / p->cir_period;
408 n_periods_tp = time_diff_tp / p->pir_period;
409 m->time_tc += n_periods_tc * p->cir_period;
410 m->time_tp += n_periods_tp * p->pir_period;
412 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
416 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
421 if ((pkt_color == e_RTE_METER_RED) || (tp < pkt_len)) {
424 return e_RTE_METER_RED;
427 if ((pkt_color == e_RTE_METER_YELLOW) || (tc < pkt_len)) {
429 m->tp = tp - pkt_len;
430 return e_RTE_METER_YELLOW;
433 m->tc = tc - pkt_len;
434 m->tp = tp - pkt_len;
435 return e_RTE_METER_GREEN;
442 #endif /* __INCLUDE_RTE_METER_H__ */