4 * Copyright(c) 2010-2014 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.
34 #ifndef __INCLUDE_RTE_METER_H__
35 #define __INCLUDE_RTE_METER_H__
43 * RTE Traffic Metering
45 * Traffic metering algorithms:
46 * 1. Single Rate Three Color Marker (srTCM): defined by IETF RFC 2697
47 * 2. Two Rate Three Color Marker (trTCM): defined by IETF RFC 2698
54 * Application Programmer's Interface (API)
58 /** Packet Color Set */
59 enum rte_meter_color {
60 e_RTE_METER_GREEN = 0, /**< Green */
61 e_RTE_METER_YELLOW, /**< Yellow */
62 e_RTE_METER_RED, /**< Red */
63 e_RTE_METER_COLORS /**< Number of available colors */
66 /** srTCM parameters per metered traffic flow. The CIR, CBS and EBS parameters only
67 count bytes of IP packets and do not include link specific headers. At least one of
68 the CBS or EBS parameters has to be greater than zero. */
69 struct rte_meter_srtcm_params {
70 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
71 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
72 uint64_t ebs; /**< Excess Burst Size (EBS). Measured in bytes. */
75 /** trTCM parameters per metered traffic flow. The CIR, PIR, CBS and PBS parameters
76 only count bytes of IP packets and do not include link specific headers. PIR has to
77 be greater than or equal to CIR. Both CBS or EBS have to be greater than zero. */
78 struct rte_meter_trtcm_params {
79 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
80 uint64_t pir; /**< Peak Information Rate (PIR). Measured in bytes per second. */
81 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in byes. */
82 uint64_t pbs; /**< Peak Burst Size (PBS). Measured in bytes. */
85 /** Internal data structure storing the srTCM run-time context per metered traffic flow. */
86 struct rte_meter_srtcm;
88 /** Internal data structure storing the trTCM run-time context per metered traffic flow. */
89 struct rte_meter_trtcm;
92 * srTCM configuration per metered traffic flow
95 * Pointer to pre-allocated srTCM data structure
97 * User parameters per srTCM metered traffic flow
99 * 0 upon success, error code otherwise
102 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
103 struct rte_meter_srtcm_params *params);
106 * trTCM configuration per metered traffic flow
109 * Pointer to pre-allocated trTCM data structure
111 * User parameters per trTCM metered traffic flow
113 * 0 upon success, error code otherwise
116 rte_meter_trtcm_config(struct rte_meter_trtcm *m,
117 struct rte_meter_trtcm_params *params);
120 * srTCM color blind traffic metering
123 * Handle to srTCM instance
125 * Current CPU time stamp (measured in CPU cycles)
127 * Length of the current IP packet (measured in bytes)
129 * Color assigned to the current IP packet
131 static inline enum rte_meter_color
132 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
137 * srTCM color aware traffic metering
140 * Handle to srTCM instance
142 * Current CPU time stamp (measured in CPU cycles)
144 * Length of the current IP packet (measured in bytes)
146 * Input color of the current IP packet
148 * Color assigned to the current IP packet
150 static inline enum rte_meter_color
151 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
154 enum rte_meter_color pkt_color);
157 * trTCM color blind traffic metering
160 * Handle to trTCM instance
162 * Current CPU time stamp (measured in CPU cycles)
164 * Length of the current IP packet (measured in bytes)
166 * Color assigned to the current IP packet
168 static inline enum rte_meter_color
169 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
174 * trTCM color aware traffic metering
177 * Handle to trTCM instance
179 * Current CPU time stamp (measured in CPU cycles)
181 * Length of the current IP packet (measured in bytes)
183 * Input color of the current IP packet
185 * Color assigned to the current IP packet
187 static inline enum rte_meter_color
188 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
191 enum rte_meter_color pkt_color);
194 * Inline implementation of run-time methods
198 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
199 struct rte_meter_srtcm {
200 uint64_t time; /* Time of latest update of C and E token buckets */
201 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
202 uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
203 uint64_t cbs; /* Upper limit for C token bucket */
204 uint64_t ebs; /* Upper limit for E token bucket */
205 uint64_t cir_period; /* Number of CPU cycles for one update of C and E token buckets */
206 uint64_t cir_bytes_per_period; /* Number of bytes to add to C and E token buckets on each update */
209 /* Internal data structure storing the trTCM run-time context per metered traffic flow. */
210 struct rte_meter_trtcm {
211 uint64_t time_tc; /* Time of latest update of C token bucket */
212 uint64_t time_tp; /* Time of latest update of E token bucket */
213 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
214 uint64_t tp; /* Number of bytes currently available in the peak (P) token bucket */
215 uint64_t cbs; /* Upper limit for C token bucket */
216 uint64_t pbs; /* Upper limit for P token bucket */
217 uint64_t cir_period; /* Number of CPU cycles for one update of C token bucket */
218 uint64_t cir_bytes_per_period; /* Number of bytes to add to C token bucket on each update */
219 uint64_t pir_period; /* Number of CPU cycles for one update of P token bucket */
220 uint64_t pir_bytes_per_period; /* Number of bytes to add to P token bucket on each update */
223 static inline enum rte_meter_color
224 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
228 uint64_t time_diff, n_periods, tc, te;
231 time_diff = time - m->time;
232 n_periods = time_diff / m->cir_period;
233 m->time += n_periods * m->cir_period;
235 tc = m->tc + n_periods * m->cir_bytes_per_period;
239 te = m->te + n_periods * m->cir_bytes_per_period;
245 m->tc = tc - pkt_len;
247 return e_RTE_METER_GREEN;
252 m->te = te - pkt_len;
253 return e_RTE_METER_YELLOW;
258 return e_RTE_METER_RED;
261 static inline enum rte_meter_color
262 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
265 enum rte_meter_color pkt_color)
267 uint64_t time_diff, n_periods, tc, te;
270 time_diff = time - m->time;
271 n_periods = time_diff / m->cir_period;
272 m->time += n_periods * m->cir_period;
274 tc = m->tc + n_periods * m->cir_bytes_per_period;
278 te = m->te + n_periods * m->cir_bytes_per_period;
283 if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
284 m->tc = tc - pkt_len;
286 return e_RTE_METER_GREEN;
289 if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
291 m->te = te - pkt_len;
292 return e_RTE_METER_YELLOW;
297 return e_RTE_METER_RED;
300 static inline enum rte_meter_color
301 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
305 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
308 time_diff_tc = time - m->time_tc;
309 time_diff_tp = time - m->time_tp;
310 n_periods_tc = time_diff_tc / m->cir_period;
311 n_periods_tp = time_diff_tp / m->pir_period;
312 m->time_tc += n_periods_tc * m->cir_period;
313 m->time_tp += n_periods_tp * m->pir_period;
315 tc = m->tc + n_periods_tc * m->cir_bytes_per_period;
319 tp = m->tp + n_periods_tp * m->pir_bytes_per_period;
327 return e_RTE_METER_RED;
332 m->tp = tp - pkt_len;
333 return e_RTE_METER_YELLOW;
336 m->tc = tc - pkt_len;
337 m->tp = tp - pkt_len;
338 return e_RTE_METER_GREEN;
341 static inline enum rte_meter_color
342 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
345 enum rte_meter_color pkt_color)
347 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
350 time_diff_tc = time - m->time_tc;
351 time_diff_tp = time - m->time_tp;
352 n_periods_tc = time_diff_tc / m->cir_period;
353 n_periods_tp = time_diff_tp / m->pir_period;
354 m->time_tc += n_periods_tc * m->cir_period;
355 m->time_tp += n_periods_tp * m->pir_period;
357 tc = m->tc + n_periods_tc * m->cir_bytes_per_period;
361 tp = m->tp + n_periods_tp * m->pir_bytes_per_period;
366 if ((pkt_color == e_RTE_METER_RED) || (tp < pkt_len)) {
369 return e_RTE_METER_RED;
372 if ((pkt_color == e_RTE_METER_YELLOW) || (tc < pkt_len)) {
374 m->tp = tp - pkt_len;
375 return e_RTE_METER_YELLOW;
378 m->tc = tc - pkt_len;
379 m->tp = tp - pkt_len;
380 return e_RTE_METER_GREEN;
387 #endif /* __INCLUDE_RTE_METER_H__ */