2 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright(c) 2010-2014 Intel Corporation
6 #ifndef __INCLUDE_RTE_METER_H__
7 #define __INCLUDE_RTE_METER_H__
15 * RTE Traffic Metering
17 * Traffic metering algorithms:
18 * 1. Single Rate Three Color Marker (srTCM): defined by IETF RFC 2697
19 * 2. Two Rate Three Color Marker (trTCM): defined by IETF RFC 2698
20 * 3. Two Rate Three Color Marker (trTCM): defined by IETF RFC 4115
26 #include "rte_compat.h"
29 * Application Programmer's Interface (API)
37 RTE_COLOR_GREEN = 0, /**< Green */
38 RTE_COLOR_YELLOW, /**< Yellow */
39 RTE_COLOR_RED, /**< Red */
40 RTE_COLORS /**< Number of colors */
43 /** srTCM parameters per metered traffic flow. The CIR, CBS and EBS parameters only
44 count bytes of IP packets and do not include link specific headers. At least one of
45 the CBS or EBS parameters has to be greater than zero. */
46 struct rte_meter_srtcm_params {
47 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
48 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
49 uint64_t ebs; /**< Excess Burst Size (EBS). Measured in bytes. */
52 /** trTCM parameters per metered traffic flow. The CIR, PIR, CBS and PBS parameters
53 only count bytes of IP packets and do not include link specific headers. PIR has to
54 be greater than or equal to CIR. Both CBS or EBS have to be greater than zero. */
55 struct rte_meter_trtcm_params {
56 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
57 uint64_t pir; /**< Peak Information Rate (PIR). Measured in bytes per second. */
58 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
59 uint64_t pbs; /**< Peak Burst Size (PBS). Measured in bytes. */
62 /** trTCM parameters per metered traffic flow. The CIR, EIR, CBS and EBS
63 parameters only count bytes of IP packets and do not include link specific
64 headers. The CBS and EBS need to be greater than zero if CIR and EIR are
65 none-zero respectively.*/
66 struct rte_meter_trtcm_rfc4115_params {
67 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
68 uint64_t eir; /**< Excess Information Rate (EIR). Measured in bytes per second. */
69 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
70 uint64_t ebs; /**< Excess Burst Size (EBS). Measured in bytes. */
74 * Internal data structure storing the srTCM configuration profile. Typically
75 * shared by multiple srTCM objects.
77 struct rte_meter_srtcm_profile;
80 * Internal data structure storing the trTCM configuration profile. Typically
81 * shared by multiple trTCM objects.
83 struct rte_meter_trtcm_profile;
86 * Internal data structure storing the trTCM RFC4115 configuration profile.
87 * Typically shared by multiple trTCM objects.
89 struct rte_meter_trtcm_rfc4115_profile;
91 /** Internal data structure storing the srTCM run-time context per metered traffic flow. */
92 struct rte_meter_srtcm;
94 /** Internal data structure storing the trTCM run-time context per metered traffic flow. */
95 struct rte_meter_trtcm;
98 * Internal data structure storing the trTCM RFC4115 run-time context per
99 * metered traffic flow.
101 struct rte_meter_trtcm_rfc4115;
104 * srTCM profile configuration
107 * Pointer to pre-allocated srTCM profile data structure
109 * srTCM profile parameters
111 * 0 upon success, error code otherwise
114 rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
115 struct rte_meter_srtcm_params *params);
118 * trTCM profile configuration
121 * Pointer to pre-allocated trTCM profile data structure
123 * trTCM profile parameters
125 * 0 upon success, error code otherwise
128 rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
129 struct rte_meter_trtcm_params *params);
132 * @b EXPERIMENTAL: this API may change without prior notice
134 * trTCM RFC 4115 profile configuration
137 * Pointer to pre-allocated trTCM profile data structure
139 * trTCM profile parameters
141 * 0 upon success, error code otherwise
145 rte_meter_trtcm_rfc4115_profile_config(
146 struct rte_meter_trtcm_rfc4115_profile *p,
147 struct rte_meter_trtcm_rfc4115_params *params);
150 * srTCM configuration per metered traffic flow
153 * Pointer to pre-allocated srTCM data structure
155 * srTCM profile. Needs to be valid.
157 * 0 upon success, error code otherwise
160 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
161 struct rte_meter_srtcm_profile *p);
164 * trTCM configuration per metered traffic flow
167 * Pointer to pre-allocated trTCM data structure
169 * trTCM profile. Needs to be valid.
171 * 0 upon success, error code otherwise
174 rte_meter_trtcm_config(struct rte_meter_trtcm *m,
175 struct rte_meter_trtcm_profile *p);
179 * @b EXPERIMENTAL: this API may change without prior notice
181 * trTCM RFC 4115 configuration per metered traffic flow
184 * Pointer to pre-allocated trTCM data structure
186 * trTCM profile. Needs to be valid.
188 * 0 upon success, error code otherwise
192 rte_meter_trtcm_rfc4115_config(struct rte_meter_trtcm_rfc4115 *m,
193 struct rte_meter_trtcm_rfc4115_profile *p);
196 * srTCM color blind traffic metering
199 * Handle to srTCM instance
201 * srTCM profile specified at srTCM object creation time
203 * Current CPU time stamp (measured in CPU cycles)
205 * Length of the current IP packet (measured in bytes)
207 * Color assigned to the current IP packet
209 static inline enum rte_color
210 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
211 struct rte_meter_srtcm_profile *p,
216 * srTCM color aware traffic metering
219 * Handle to srTCM instance
221 * srTCM profile specified at srTCM object creation time
223 * Current CPU time stamp (measured in CPU cycles)
225 * Length of the current IP packet (measured in bytes)
227 * Input color of the current IP packet
229 * Color assigned to the current IP packet
231 static inline enum rte_color
232 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
233 struct rte_meter_srtcm_profile *p,
236 enum rte_color pkt_color);
239 * trTCM color blind traffic metering
242 * Handle to trTCM instance
244 * trTCM profile specified at trTCM object creation time
246 * Current CPU time stamp (measured in CPU cycles)
248 * Length of the current IP packet (measured in bytes)
250 * Color assigned to the current IP packet
252 static inline enum rte_color
253 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
254 struct rte_meter_trtcm_profile *p,
259 * trTCM color aware traffic metering
262 * Handle to trTCM instance
264 * trTCM profile specified at trTCM object creation time
266 * Current CPU time stamp (measured in CPU cycles)
268 * Length of the current IP packet (measured in bytes)
270 * Input color of the current IP packet
272 * Color assigned to the current IP packet
274 static inline enum rte_color
275 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
276 struct rte_meter_trtcm_profile *p,
279 enum rte_color pkt_color);
283 * @b EXPERIMENTAL: this API may change without prior notice
285 * trTCM RFC4115 color blind traffic metering
288 * Handle to trTCM instance
290 * trTCM profile specified at trTCM object creation time
292 * Current CPU time stamp (measured in CPU cycles)
294 * Length of the current IP packet (measured in bytes)
296 * Color assigned to the current IP packet
299 static inline enum rte_color
300 rte_meter_trtcm_rfc4115_color_blind_check(
301 struct rte_meter_trtcm_rfc4115 *m,
302 struct rte_meter_trtcm_rfc4115_profile *p,
308 * @b EXPERIMENTAL: this API may change without prior notice
310 * trTCM RFC4115 color aware traffic metering
313 * Handle to trTCM instance
315 * trTCM profile specified at trTCM object creation time
317 * Current CPU time stamp (measured in CPU cycles)
319 * Length of the current IP packet (measured in bytes)
321 * Input color of the current IP packet
323 * Color assigned to the current IP packet
326 static inline enum rte_color
327 rte_meter_trtcm_rfc4115_color_aware_check(
328 struct rte_meter_trtcm_rfc4115 *m,
329 struct rte_meter_trtcm_rfc4115_profile *p,
332 enum rte_color pkt_color);
335 * Inline implementation of run-time methods
339 struct rte_meter_srtcm_profile {
341 /**< Upper limit for C token bucket */
343 /**< Upper limit for E token bucket */
345 /**< Number of CPU cycles for each update of C and E token buckets */
346 uint64_t cir_bytes_per_period;
347 /**< Number of bytes to add to C and E token buckets on each update */
350 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
351 struct rte_meter_srtcm {
352 uint64_t time; /* Time of latest update of C and E token buckets */
353 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
354 uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
357 struct rte_meter_trtcm_profile {
359 /**< Upper limit for C token bucket */
361 /**< Upper limit for P token bucket */
363 /**< Number of CPU cycles for one update of C token bucket */
364 uint64_t cir_bytes_per_period;
365 /**< Number of bytes to add to C token bucket on each update */
367 /**< Number of CPU cycles for one update of P token bucket */
368 uint64_t pir_bytes_per_period;
369 /**< Number of bytes to add to P token bucket on each update */
373 * Internal data structure storing the trTCM run-time context per metered
376 struct rte_meter_trtcm {
378 /**< Time of latest update of C token bucket */
380 /**< Time of latest update of P token bucket */
382 /**< Number of bytes currently available in committed(C) token bucket */
384 /**< Number of bytes currently available in the peak(P) token bucket */
387 struct rte_meter_trtcm_rfc4115_profile {
389 /**< Upper limit for C token bucket */
391 /**< Upper limit for E token bucket */
393 /**< Number of CPU cycles for one update of C token bucket */
394 uint64_t cir_bytes_per_period;
395 /**< Number of bytes to add to C token bucket on each update */
397 /**< Number of CPU cycles for one update of E token bucket */
398 uint64_t eir_bytes_per_period;
399 /**< Number of bytes to add to E token bucket on each update */
403 * Internal data structure storing the trTCM RFC4115 run-time context per
404 * metered traffic flow.
406 struct rte_meter_trtcm_rfc4115 {
408 /**< Time of latest update of C token bucket */
410 /**< Time of latest update of E token bucket */
412 /**< Number of bytes currently available in committed(C) token bucket */
414 /**< Number of bytes currently available in the excess(E) token bucket */
417 static inline enum rte_color
418 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
419 struct rte_meter_srtcm_profile *p,
423 uint64_t time_diff, n_periods, tc, te;
426 time_diff = time - m->time;
427 n_periods = time_diff / p->cir_period;
428 m->time += n_periods * p->cir_period;
430 /* Put the tokens overflowing from tc into te bucket */
431 tc = m->tc + n_periods * p->cir_bytes_per_period;
442 m->tc = tc - pkt_len;
444 return RTE_COLOR_GREEN;
449 m->te = te - pkt_len;
450 return RTE_COLOR_YELLOW;
455 return RTE_COLOR_RED;
458 static inline enum rte_color
459 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
460 struct rte_meter_srtcm_profile *p,
463 enum rte_color pkt_color)
465 uint64_t time_diff, n_periods, tc, te;
468 time_diff = time - m->time;
469 n_periods = time_diff / p->cir_period;
470 m->time += n_periods * p->cir_period;
472 /* Put the tokens overflowing from tc into te bucket */
473 tc = m->tc + n_periods * p->cir_bytes_per_period;
483 if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
484 m->tc = tc - pkt_len;
486 return RTE_COLOR_GREEN;
489 if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
491 m->te = te - pkt_len;
492 return RTE_COLOR_YELLOW;
497 return RTE_COLOR_RED;
500 static inline enum rte_color
501 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
502 struct rte_meter_trtcm_profile *p,
506 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
509 time_diff_tc = time - m->time_tc;
510 time_diff_tp = time - m->time_tp;
511 n_periods_tc = time_diff_tc / p->cir_period;
512 n_periods_tp = time_diff_tp / p->pir_period;
513 m->time_tc += n_periods_tc * p->cir_period;
514 m->time_tp += n_periods_tp * p->pir_period;
516 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
520 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
528 return RTE_COLOR_RED;
533 m->tp = tp - pkt_len;
534 return RTE_COLOR_YELLOW;
537 m->tc = tc - pkt_len;
538 m->tp = tp - pkt_len;
539 return RTE_COLOR_GREEN;
542 static inline enum rte_color
543 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
544 struct rte_meter_trtcm_profile *p,
547 enum rte_color pkt_color)
549 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
552 time_diff_tc = time - m->time_tc;
553 time_diff_tp = time - m->time_tp;
554 n_periods_tc = time_diff_tc / p->cir_period;
555 n_periods_tp = time_diff_tp / p->pir_period;
556 m->time_tc += n_periods_tc * p->cir_period;
557 m->time_tp += n_periods_tp * p->pir_period;
559 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
563 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
568 if ((pkt_color == RTE_COLOR_RED) || (tp < pkt_len)) {
571 return RTE_COLOR_RED;
574 if ((pkt_color == RTE_COLOR_YELLOW) || (tc < pkt_len)) {
576 m->tp = tp - pkt_len;
577 return RTE_COLOR_YELLOW;
580 m->tc = tc - pkt_len;
581 m->tp = tp - pkt_len;
582 return RTE_COLOR_GREEN;
586 static inline enum rte_color
587 rte_meter_trtcm_rfc4115_color_blind_check(
588 struct rte_meter_trtcm_rfc4115 *m,
589 struct rte_meter_trtcm_rfc4115_profile *p,
593 uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
596 time_diff_tc = time - m->time_tc;
597 time_diff_te = time - m->time_te;
598 n_periods_tc = time_diff_tc / p->cir_period;
599 n_periods_te = time_diff_te / p->eir_period;
600 m->time_tc += n_periods_tc * p->cir_period;
601 m->time_te += n_periods_te * p->eir_period;
603 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
607 te = m->te + n_periods_te * p->eir_bytes_per_period;
613 m->tc = tc - pkt_len;
615 return RTE_COLOR_GREEN;
619 m->te = te - pkt_len;
620 return RTE_COLOR_YELLOW;
623 /* If we end up here the color is RED */
626 return RTE_COLOR_RED;
630 static inline enum rte_color
631 rte_meter_trtcm_rfc4115_color_aware_check(
632 struct rte_meter_trtcm_rfc4115 *m,
633 struct rte_meter_trtcm_rfc4115_profile *p,
636 enum rte_color pkt_color)
638 uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
641 time_diff_tc = time - m->time_tc;
642 time_diff_te = time - m->time_te;
643 n_periods_tc = time_diff_tc / p->cir_period;
644 n_periods_te = time_diff_te / p->eir_period;
645 m->time_tc += n_periods_tc * p->cir_period;
646 m->time_te += n_periods_te * p->eir_period;
648 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
652 te = m->te + n_periods_te * p->eir_bytes_per_period;
657 if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
658 m->tc = tc - pkt_len;
660 return RTE_COLOR_GREEN;
663 if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
665 m->te = te - pkt_len;
666 return RTE_COLOR_YELLOW;
669 /* If we end up here the color is RED */
672 return RTE_COLOR_RED;
680 #endif /* __INCLUDE_RTE_METER_H__ */