4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef __INCLUDE_RTE_METER_H__
36 #define __INCLUDE_RTE_METER_H__
44 * RTE Traffic Metering
46 * Traffic metering algorithms:
47 * 1. Single Rate Three Color Marker (srTCM): defined by IETF RFC 2697
48 * 2. Two Rate Three Color Marker (trTCM): defined by IETF RFC 2698
55 * Application Programmer's Interface (API)
59 /** Packet Color Set */
60 enum rte_meter_color {
61 e_RTE_METER_GREEN = 0, /**< Green */
62 e_RTE_METER_YELLOW, /**< Yellow */
63 e_RTE_METER_RED, /**< Red */
64 e_RTE_METER_COLORS /**< Number of available colors */
67 /** srTCM parameters per metered traffic flow. The CIR, CBS and EBS parameters only
68 count bytes of IP packets and do not include link specific headers. At least one of
69 the CBS or EBS parameters has to be greater than zero. */
70 struct rte_meter_srtcm_params {
71 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
72 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
73 uint64_t ebs; /**< Excess Burst Size (EBS). Measured in bytes. */
76 /** trTCM parameters per metered traffic flow. The CIR, PIR, CBS and PBS parameters
77 only count bytes of IP packets and do not include link specific headers. PIR has to
78 be greater than or equal to CIR. Both CBS or EBS have to be greater than zero. */
79 struct rte_meter_trtcm_params {
80 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
81 uint64_t pir; /**< Peak Information Rate (PIR). Measured in bytes per second. */
82 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in byes. */
83 uint64_t pbs; /**< Peak Burst Size (PBS). Measured in bytes. */
86 /** Internal data structure storing the srTCM run-time context per metered traffic flow. */
87 struct rte_meter_srtcm;
89 /** Internal data structure storing the trTCM run-time context per metered traffic flow. */
90 struct rte_meter_trtcm;
93 * srTCM configuration per metered traffic flow
96 * Pointer to pre-allocated srTCM data structure
98 * User parameters per srTCM metered traffic flow
100 * 0 upon success, error code otherwise
103 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
104 struct rte_meter_srtcm_params *params);
107 * trTCM configuration per metered traffic flow
110 * Pointer to pre-allocated trTCM data structure
112 * User parameters per trTCM metered traffic flow
114 * 0 upon success, error code otherwise
117 rte_meter_trtcm_config(struct rte_meter_trtcm *m,
118 struct rte_meter_trtcm_params *params);
121 * srTCM color blind traffic metering
124 * Handle to srTCM instance
126 * Current CPU time stamp (measured in CPU cycles)
128 * Length of the current IP packet (measured in bytes)
130 * Color assigned to the current IP packet
132 static inline enum rte_meter_color
133 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
138 * srTCM color aware traffic metering
141 * Handle to srTCM instance
143 * Current CPU time stamp (measured in CPU cycles)
145 * Length of the current IP packet (measured in bytes)
147 * Input color of the current IP packet
149 * Color assigned to the current IP packet
151 static inline enum rte_meter_color
152 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
155 enum rte_meter_color pkt_color);
158 * trTCM color blind traffic metering
161 * Handle to trTCM instance
163 * Current CPU time stamp (measured in CPU cycles)
165 * Length of the current IP packet (measured in bytes)
167 * Color assigned to the current IP packet
169 static inline enum rte_meter_color
170 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
175 * trTCM color aware traffic metering
178 * Handle to trTCM instance
180 * Current CPU time stamp (measured in CPU cycles)
182 * Length of the current IP packet (measured in bytes)
184 * Input color of the current IP packet
186 * Color assigned to the current IP packet
188 static inline enum rte_meter_color
189 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
192 enum rte_meter_color pkt_color);
195 * Inline implementation of run-time methods
199 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
200 struct rte_meter_srtcm {
201 uint64_t time; /* Time of latest update of C and E token buckets */
202 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
203 uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
204 uint64_t cbs; /* Upper limit for C token bucket */
205 uint64_t ebs; /* Upper limit for E token bucket */
206 uint64_t cir_period; /* Number of CPU cycles for one update of C and E token buckets */
207 uint64_t cir_bytes_per_period; /* Number of bytes to add to C and E token buckets on each update */
210 /* Internal data structure storing the trTCM run-time context per metered traffic flow. */
211 struct rte_meter_trtcm {
212 uint64_t time_tc; /* Time of latest update of C token bucket */
213 uint64_t time_tp; /* Time of latest update of E token bucket */
214 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
215 uint64_t tp; /* Number of bytes currently available in the peak (P) token bucket */
216 uint64_t cbs; /* Upper limit for C token bucket */
217 uint64_t pbs; /* Upper limit for P token bucket */
218 uint64_t cir_period; /* Number of CPU cycles for one update of C token bucket */
219 uint64_t cir_bytes_per_period; /* Number of bytes to add to C token bucket on each update */
220 uint64_t pir_period; /* Number of CPU cycles for one update of P token bucket */
221 uint64_t pir_bytes_per_period; /* Number of bytes to add to P token bucket on each update */
224 static inline enum rte_meter_color
225 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
229 uint64_t time_diff, n_periods, tc, te;
232 time_diff = time - m->time;
233 n_periods = time_diff / m->cir_period;
234 m->time += n_periods * m->cir_period;
236 tc = m->tc + n_periods * m->cir_bytes_per_period;
240 te = m->te + n_periods * m->cir_bytes_per_period;
246 m->tc = tc - pkt_len;
248 return e_RTE_METER_GREEN;
253 m->te = te - pkt_len;
254 return e_RTE_METER_YELLOW;
259 return e_RTE_METER_RED;
262 static inline enum rte_meter_color
263 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
266 enum rte_meter_color pkt_color)
268 uint64_t time_diff, n_periods, tc, te;
271 time_diff = time - m->time;
272 n_periods = time_diff / m->cir_period;
273 m->time += n_periods * m->cir_period;
275 tc = m->tc + n_periods * m->cir_bytes_per_period;
279 te = m->te + n_periods * m->cir_bytes_per_period;
284 if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
285 m->tc = tc - pkt_len;
287 return e_RTE_METER_GREEN;
290 if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
292 m->te = te - pkt_len;
293 return e_RTE_METER_YELLOW;
298 return e_RTE_METER_RED;
301 static inline enum rte_meter_color
302 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
306 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
309 time_diff_tc = time - m->time_tc;
310 time_diff_tp = time - m->time_tp;
311 n_periods_tc = time_diff_tc / m->cir_period;
312 n_periods_tp = time_diff_tp / m->pir_period;
313 m->time_tc += n_periods_tc * m->cir_period;
314 m->time_tp += n_periods_tp * m->pir_period;
316 tc = m->tc + n_periods_tc * m->cir_bytes_per_period;
320 tp = m->tp + n_periods_tp * m->pir_bytes_per_period;
328 return e_RTE_METER_RED;
333 m->tp = tp - pkt_len;
334 return e_RTE_METER_YELLOW;
337 m->tc = tc - pkt_len;
338 m->tp = tp - pkt_len;
339 return e_RTE_METER_GREEN;
342 static inline enum rte_meter_color
343 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
346 enum rte_meter_color pkt_color)
348 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
351 time_diff_tc = time - m->time_tc;
352 time_diff_tp = time - m->time_tp;
353 n_periods_tc = time_diff_tc / m->cir_period;
354 n_periods_tp = time_diff_tp / m->pir_period;
355 m->time_tc += n_periods_tc * m->cir_period;
356 m->time_tp += n_periods_tp * m->pir_period;
358 tc = m->tc + n_periods_tc * m->cir_bytes_per_period;
362 tp = m->tp + n_periods_tp * m->pir_bytes_per_period;
367 if ((pkt_color == e_RTE_METER_RED) || (tp < pkt_len)) {
370 return e_RTE_METER_RED;
373 if ((pkt_color == e_RTE_METER_YELLOW) || (tc < pkt_len)) {
375 m->tp = tp - pkt_len;
376 return e_RTE_METER_YELLOW;
379 m->tc = tc - pkt_len;
380 m->tp = tp - pkt_len;
381 return e_RTE_METER_GREEN;
388 #endif /* __INCLUDE_RTE_METER_H__ */