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
144 rte_meter_trtcm_rfc4115_profile_config(
145 struct rte_meter_trtcm_rfc4115_profile *p,
146 struct rte_meter_trtcm_rfc4115_params *params);
149 * srTCM configuration per metered traffic flow
152 * Pointer to pre-allocated srTCM data structure
154 * srTCM profile. Needs to be valid.
156 * 0 upon success, error code otherwise
159 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
160 struct rte_meter_srtcm_profile *p);
163 * trTCM configuration per metered traffic flow
166 * Pointer to pre-allocated trTCM data structure
168 * trTCM profile. Needs to be valid.
170 * 0 upon success, error code otherwise
173 rte_meter_trtcm_config(struct rte_meter_trtcm *m,
174 struct rte_meter_trtcm_profile *p);
178 * @b EXPERIMENTAL: this API may change without prior notice
180 * trTCM RFC 4115 configuration per metered traffic flow
183 * Pointer to pre-allocated trTCM data structure
185 * trTCM profile. Needs to be valid.
187 * 0 upon success, error code otherwise
190 rte_meter_trtcm_rfc4115_config(struct rte_meter_trtcm_rfc4115 *m,
191 struct rte_meter_trtcm_rfc4115_profile *p);
194 * srTCM color blind traffic metering
197 * Handle to srTCM instance
199 * srTCM profile specified at srTCM object creation time
201 * Current CPU time stamp (measured in CPU cycles)
203 * Length of the current IP packet (measured in bytes)
205 * Color assigned to the current IP packet
207 static inline enum rte_color
208 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
209 struct rte_meter_srtcm_profile *p,
214 * srTCM color aware traffic metering
217 * Handle to srTCM instance
219 * srTCM profile specified at srTCM object creation time
221 * Current CPU time stamp (measured in CPU cycles)
223 * Length of the current IP packet (measured in bytes)
225 * Input color of the current IP packet
227 * Color assigned to the current IP packet
229 static inline enum rte_color
230 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
231 struct rte_meter_srtcm_profile *p,
234 enum rte_color pkt_color);
237 * trTCM color blind traffic metering
240 * Handle to trTCM instance
242 * trTCM profile specified at trTCM object creation time
244 * Current CPU time stamp (measured in CPU cycles)
246 * Length of the current IP packet (measured in bytes)
248 * Color assigned to the current IP packet
250 static inline enum rte_color
251 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
252 struct rte_meter_trtcm_profile *p,
257 * trTCM color aware traffic metering
260 * Handle to trTCM instance
262 * trTCM profile specified at trTCM object creation time
264 * Current CPU time stamp (measured in CPU cycles)
266 * Length of the current IP packet (measured in bytes)
268 * Input color of the current IP packet
270 * Color assigned to the current IP packet
272 static inline enum rte_color
273 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
274 struct rte_meter_trtcm_profile *p,
277 enum rte_color pkt_color);
281 * @b EXPERIMENTAL: this API may change without prior notice
283 * trTCM RFC4115 color blind traffic metering
286 * Handle to trTCM instance
288 * trTCM profile specified at trTCM object creation time
290 * Current CPU time stamp (measured in CPU cycles)
292 * Length of the current IP packet (measured in bytes)
294 * Color assigned to the current IP packet
296 static inline enum rte_color
297 rte_meter_trtcm_rfc4115_color_blind_check(
298 struct rte_meter_trtcm_rfc4115 *m,
299 struct rte_meter_trtcm_rfc4115_profile *p,
305 * @b EXPERIMENTAL: this API may change without prior notice
307 * trTCM RFC4115 color aware traffic metering
310 * Handle to trTCM instance
312 * trTCM profile specified at trTCM object creation time
314 * Current CPU time stamp (measured in CPU cycles)
316 * Length of the current IP packet (measured in bytes)
318 * Input color of the current IP packet
320 * Color assigned to the current IP packet
322 static inline enum rte_color
323 rte_meter_trtcm_rfc4115_color_aware_check(
324 struct rte_meter_trtcm_rfc4115 *m,
325 struct rte_meter_trtcm_rfc4115_profile *p,
328 enum rte_color pkt_color);
331 * Inline implementation of run-time methods
335 struct rte_meter_srtcm_profile {
337 /**< Upper limit for C token bucket */
339 /**< Upper limit for E token bucket */
341 /**< Number of CPU cycles for each update of C and E token buckets */
342 uint64_t cir_bytes_per_period;
343 /**< Number of bytes to add to C and E token buckets on each update */
346 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
347 struct rte_meter_srtcm {
348 uint64_t time; /* Time of latest update of C and E token buckets */
349 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
350 uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
353 struct rte_meter_trtcm_profile {
355 /**< Upper limit for C token bucket */
357 /**< Upper limit for P token bucket */
359 /**< Number of CPU cycles for one update of C token bucket */
360 uint64_t cir_bytes_per_period;
361 /**< Number of bytes to add to C token bucket on each update */
363 /**< Number of CPU cycles for one update of P token bucket */
364 uint64_t pir_bytes_per_period;
365 /**< Number of bytes to add to P token bucket on each update */
369 * Internal data structure storing the trTCM run-time context per metered
372 struct rte_meter_trtcm {
374 /**< Time of latest update of C token bucket */
376 /**< Time of latest update of P token bucket */
378 /**< Number of bytes currently available in committed(C) token bucket */
380 /**< Number of bytes currently available in the peak(P) token bucket */
383 struct rte_meter_trtcm_rfc4115_profile {
385 /**< Upper limit for C token bucket */
387 /**< Upper limit for E token bucket */
389 /**< Number of CPU cycles for one update of C token bucket */
390 uint64_t cir_bytes_per_period;
391 /**< Number of bytes to add to C token bucket on each update */
393 /**< Number of CPU cycles for one update of E token bucket */
394 uint64_t eir_bytes_per_period;
395 /**< Number of bytes to add to E token bucket on each update */
399 * Internal data structure storing the trTCM RFC4115 run-time context per
400 * metered traffic flow.
402 struct rte_meter_trtcm_rfc4115 {
404 /**< Time of latest update of C token bucket */
406 /**< Time of latest update of E token bucket */
408 /**< Number of bytes currently available in committed(C) token bucket */
410 /**< Number of bytes currently available in the excess(E) token bucket */
413 static inline enum rte_color
414 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
415 struct rte_meter_srtcm_profile *p,
419 uint64_t time_diff, n_periods, tc, te;
422 time_diff = time - m->time;
423 n_periods = time_diff / p->cir_period;
424 m->time += n_periods * p->cir_period;
426 /* Put the tokens overflowing from tc into te bucket */
427 tc = m->tc + n_periods * p->cir_bytes_per_period;
438 m->tc = tc - pkt_len;
440 return RTE_COLOR_GREEN;
445 m->te = te - pkt_len;
446 return RTE_COLOR_YELLOW;
451 return RTE_COLOR_RED;
454 static inline enum rte_color
455 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
456 struct rte_meter_srtcm_profile *p,
459 enum rte_color pkt_color)
461 uint64_t time_diff, n_periods, tc, te;
464 time_diff = time - m->time;
465 n_periods = time_diff / p->cir_period;
466 m->time += n_periods * p->cir_period;
468 /* Put the tokens overflowing from tc into te bucket */
469 tc = m->tc + n_periods * p->cir_bytes_per_period;
479 if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
480 m->tc = tc - pkt_len;
482 return RTE_COLOR_GREEN;
485 if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
487 m->te = te - pkt_len;
488 return RTE_COLOR_YELLOW;
493 return RTE_COLOR_RED;
496 static inline enum rte_color
497 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
498 struct rte_meter_trtcm_profile *p,
502 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
505 time_diff_tc = time - m->time_tc;
506 time_diff_tp = time - m->time_tp;
507 n_periods_tc = time_diff_tc / p->cir_period;
508 n_periods_tp = time_diff_tp / p->pir_period;
509 m->time_tc += n_periods_tc * p->cir_period;
510 m->time_tp += n_periods_tp * p->pir_period;
512 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
516 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
524 return RTE_COLOR_RED;
529 m->tp = tp - pkt_len;
530 return RTE_COLOR_YELLOW;
533 m->tc = tc - pkt_len;
534 m->tp = tp - pkt_len;
535 return RTE_COLOR_GREEN;
538 static inline enum rte_color
539 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
540 struct rte_meter_trtcm_profile *p,
543 enum rte_color pkt_color)
545 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
548 time_diff_tc = time - m->time_tc;
549 time_diff_tp = time - m->time_tp;
550 n_periods_tc = time_diff_tc / p->cir_period;
551 n_periods_tp = time_diff_tp / p->pir_period;
552 m->time_tc += n_periods_tc * p->cir_period;
553 m->time_tp += n_periods_tp * p->pir_period;
555 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
559 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
564 if ((pkt_color == RTE_COLOR_RED) || (tp < pkt_len)) {
567 return RTE_COLOR_RED;
570 if ((pkt_color == RTE_COLOR_YELLOW) || (tc < pkt_len)) {
572 m->tp = tp - pkt_len;
573 return RTE_COLOR_YELLOW;
576 m->tc = tc - pkt_len;
577 m->tp = tp - pkt_len;
578 return RTE_COLOR_GREEN;
581 static inline enum rte_color
582 rte_meter_trtcm_rfc4115_color_blind_check(
583 struct rte_meter_trtcm_rfc4115 *m,
584 struct rte_meter_trtcm_rfc4115_profile *p,
588 uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
591 time_diff_tc = time - m->time_tc;
592 time_diff_te = time - m->time_te;
593 n_periods_tc = time_diff_tc / p->cir_period;
594 n_periods_te = time_diff_te / p->eir_period;
595 m->time_tc += n_periods_tc * p->cir_period;
596 m->time_te += n_periods_te * p->eir_period;
598 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
602 te = m->te + n_periods_te * p->eir_bytes_per_period;
608 m->tc = tc - pkt_len;
610 return RTE_COLOR_GREEN;
614 m->te = te - pkt_len;
615 return RTE_COLOR_YELLOW;
618 /* If we end up here the color is RED */
621 return RTE_COLOR_RED;
624 static inline enum rte_color
625 rte_meter_trtcm_rfc4115_color_aware_check(
626 struct rte_meter_trtcm_rfc4115 *m,
627 struct rte_meter_trtcm_rfc4115_profile *p,
630 enum rte_color pkt_color)
632 uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
635 time_diff_tc = time - m->time_tc;
636 time_diff_te = time - m->time_te;
637 n_periods_tc = time_diff_tc / p->cir_period;
638 n_periods_te = time_diff_te / p->eir_period;
639 m->time_tc += n_periods_tc * p->cir_period;
640 m->time_te += n_periods_te * p->eir_period;
642 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
646 te = m->te + n_periods_te * p->eir_bytes_per_period;
651 if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
652 m->tc = tc - pkt_len;
654 return RTE_COLOR_GREEN;
657 if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
659 m->te = te - pkt_len;
660 return RTE_COLOR_YELLOW;
663 /* If we end up here the color is RED */
666 return RTE_COLOR_RED;
674 #endif /* __INCLUDE_RTE_METER_H__ */