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 /* New rte_color is defined and used to deprecate rte_meter_color soon. */
44 #define rte_meter_color rte_color
45 #define e_RTE_METER_GREEN RTE_COLOR_GREEN
46 #define e_RTE_METER_YELLOW RTE_COLOR_YELLOW
47 #define e_RTE_METER_RED RTE_COLOR_RED
48 #define e_RTE_METER_COLORS RTE_COLORS
50 /** srTCM parameters per metered traffic flow. The CIR, CBS and EBS parameters only
51 count bytes of IP packets and do not include link specific headers. At least one of
52 the CBS or EBS parameters has to be greater than zero. */
53 struct rte_meter_srtcm_params {
54 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
55 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
56 uint64_t ebs; /**< Excess Burst Size (EBS). Measured in bytes. */
59 /** trTCM parameters per metered traffic flow. The CIR, PIR, CBS and PBS parameters
60 only count bytes of IP packets and do not include link specific headers. PIR has to
61 be greater than or equal to CIR. Both CBS or EBS have to be greater than zero. */
62 struct rte_meter_trtcm_params {
63 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
64 uint64_t pir; /**< Peak Information Rate (PIR). Measured in bytes per second. */
65 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
66 uint64_t pbs; /**< Peak Burst Size (PBS). Measured in bytes. */
69 /** trTCM parameters per metered traffic flow. The CIR, EIR, CBS and EBS
70 parameters only count bytes of IP packets and do not include link specific
71 headers. The CBS and EBS need to be greater than zero if CIR and EIR are
72 none-zero respectively.*/
73 struct rte_meter_trtcm_rfc4115_params {
74 uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
75 uint64_t eir; /**< Excess Information Rate (EIR). Measured in bytes per second. */
76 uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
77 uint64_t ebs; /**< Excess Burst Size (EBS). Measured in bytes. */
81 * Internal data structure storing the srTCM configuration profile. Typically
82 * shared by multiple srTCM objects.
84 struct rte_meter_srtcm_profile;
87 * Internal data structure storing the trTCM configuration profile. Typically
88 * shared by multiple trTCM objects.
90 struct rte_meter_trtcm_profile;
93 * Internal data structure storing the trTCM RFC4115 configuration profile.
94 * Typically shared by multiple trTCM objects.
96 struct rte_meter_trtcm_rfc4115_profile;
98 /** Internal data structure storing the srTCM run-time context per metered traffic flow. */
99 struct rte_meter_srtcm;
101 /** Internal data structure storing the trTCM run-time context per metered traffic flow. */
102 struct rte_meter_trtcm;
105 * Internal data structure storing the trTCM RFC4115 run-time context per
106 * metered traffic flow.
108 struct rte_meter_trtcm_rfc4115;
111 * srTCM profile configuration
114 * Pointer to pre-allocated srTCM profile data structure
116 * srTCM profile parameters
118 * 0 upon success, error code otherwise
121 rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
122 struct rte_meter_srtcm_params *params);
125 * trTCM profile configuration
128 * Pointer to pre-allocated trTCM profile data structure
130 * trTCM profile parameters
132 * 0 upon success, error code otherwise
135 rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
136 struct rte_meter_trtcm_params *params);
139 * @b EXPERIMENTAL: this API may change without prior notice
141 * trTCM RFC 4115 profile configuration
144 * Pointer to pre-allocated trTCM profile data structure
146 * trTCM profile parameters
148 * 0 upon success, error code otherwise
150 int __rte_experimental
151 rte_meter_trtcm_rfc4115_profile_config(
152 struct rte_meter_trtcm_rfc4115_profile *p,
153 struct rte_meter_trtcm_rfc4115_params *params);
156 * srTCM configuration per metered traffic flow
159 * Pointer to pre-allocated srTCM data structure
161 * srTCM profile. Needs to be valid.
163 * 0 upon success, error code otherwise
166 rte_meter_srtcm_config(struct rte_meter_srtcm *m,
167 struct rte_meter_srtcm_profile *p);
170 * trTCM configuration per metered traffic flow
173 * Pointer to pre-allocated trTCM data structure
175 * trTCM profile. Needs to be valid.
177 * 0 upon success, error code otherwise
180 rte_meter_trtcm_config(struct rte_meter_trtcm *m,
181 struct rte_meter_trtcm_profile *p);
185 * @b EXPERIMENTAL: this API may change without prior notice
187 * trTCM RFC 4115 configuration per metered traffic flow
190 * Pointer to pre-allocated trTCM data structure
192 * trTCM profile. Needs to be valid.
194 * 0 upon success, error code otherwise
196 int __rte_experimental
197 rte_meter_trtcm_rfc4115_config(struct rte_meter_trtcm_rfc4115 *m,
198 struct rte_meter_trtcm_rfc4115_profile *p);
201 * srTCM color blind traffic metering
204 * Handle to srTCM instance
206 * srTCM profile specified at srTCM object creation time
208 * Current CPU time stamp (measured in CPU cycles)
210 * Length of the current IP packet (measured in bytes)
212 * Color assigned to the current IP packet
214 static inline enum rte_meter_color
215 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
216 struct rte_meter_srtcm_profile *p,
221 * srTCM color aware traffic metering
224 * Handle to srTCM instance
226 * srTCM profile specified at srTCM object creation time
228 * Current CPU time stamp (measured in CPU cycles)
230 * Length of the current IP packet (measured in bytes)
232 * Input color of the current IP packet
234 * Color assigned to the current IP packet
236 static inline enum rte_meter_color
237 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
238 struct rte_meter_srtcm_profile *p,
241 enum rte_meter_color pkt_color);
244 * trTCM color blind traffic metering
247 * Handle to trTCM instance
249 * trTCM profile specified at trTCM object creation time
251 * Current CPU time stamp (measured in CPU cycles)
253 * Length of the current IP packet (measured in bytes)
255 * Color assigned to the current IP packet
257 static inline enum rte_meter_color
258 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
259 struct rte_meter_trtcm_profile *p,
264 * trTCM color aware traffic metering
267 * Handle to trTCM instance
269 * trTCM profile specified at trTCM object creation time
271 * Current CPU time stamp (measured in CPU cycles)
273 * Length of the current IP packet (measured in bytes)
275 * Input color of the current IP packet
277 * Color assigned to the current IP packet
279 static inline enum rte_meter_color
280 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
281 struct rte_meter_trtcm_profile *p,
284 enum rte_meter_color pkt_color);
288 * @b EXPERIMENTAL: this API may change without prior notice
290 * trTCM RFC4115 color blind traffic metering
293 * Handle to trTCM instance
295 * trTCM profile specified at trTCM object creation time
297 * Current CPU time stamp (measured in CPU cycles)
299 * Length of the current IP packet (measured in bytes)
301 * Color assigned to the current IP packet
303 static inline enum rte_meter_color __rte_experimental
304 rte_meter_trtcm_rfc4115_color_blind_check(
305 struct rte_meter_trtcm_rfc4115 *m,
306 struct rte_meter_trtcm_rfc4115_profile *p,
312 * @b EXPERIMENTAL: this API may change without prior notice
314 * trTCM RFC4115 color aware traffic metering
317 * Handle to trTCM instance
319 * trTCM profile specified at trTCM object creation time
321 * Current CPU time stamp (measured in CPU cycles)
323 * Length of the current IP packet (measured in bytes)
325 * Input color of the current IP packet
327 * Color assigned to the current IP packet
329 static inline enum rte_meter_color __rte_experimental
330 rte_meter_trtcm_rfc4115_color_aware_check(
331 struct rte_meter_trtcm_rfc4115 *m,
332 struct rte_meter_trtcm_rfc4115_profile *p,
335 enum rte_meter_color pkt_color);
338 * Inline implementation of run-time methods
342 struct rte_meter_srtcm_profile {
344 /**< Upper limit for C token bucket */
346 /**< Upper limit for E token bucket */
348 /**< Number of CPU cycles for each update of C and E token buckets */
349 uint64_t cir_bytes_per_period;
350 /**< Number of bytes to add to C and E token buckets on each update */
353 /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
354 struct rte_meter_srtcm {
355 uint64_t time; /* Time of latest update of C and E token buckets */
356 uint64_t tc; /* Number of bytes currently available in the committed (C) token bucket */
357 uint64_t te; /* Number of bytes currently available in the excess (E) token bucket */
360 struct rte_meter_trtcm_profile {
362 /**< Upper limit for C token bucket */
364 /**< Upper limit for P token bucket */
366 /**< Number of CPU cycles for one update of C token bucket */
367 uint64_t cir_bytes_per_period;
368 /**< Number of bytes to add to C token bucket on each update */
370 /**< Number of CPU cycles for one update of P token bucket */
371 uint64_t pir_bytes_per_period;
372 /**< Number of bytes to add to P token bucket on each update */
376 * Internal data structure storing the trTCM run-time context per metered
379 struct rte_meter_trtcm {
381 /**< Time of latest update of C token bucket */
383 /**< Time of latest update of P token bucket */
385 /**< Number of bytes currently available in committed(C) token bucket */
387 /**< Number of bytes currently available in the peak(P) token bucket */
390 struct rte_meter_trtcm_rfc4115_profile {
392 /**< Upper limit for C token bucket */
394 /**< Upper limit for E token bucket */
396 /**< Number of CPU cycles for one update of C token bucket */
397 uint64_t cir_bytes_per_period;
398 /**< Number of bytes to add to C token bucket on each update */
400 /**< Number of CPU cycles for one update of E token bucket */
401 uint64_t eir_bytes_per_period;
402 /**< Number of bytes to add to E token bucket on each update */
406 * Internal data structure storing the trTCM RFC4115 run-time context per
407 * metered traffic flow.
409 struct rte_meter_trtcm_rfc4115 {
411 /**< Time of latest update of C token bucket */
413 /**< Time of latest update of E token bucket */
415 /**< Number of bytes currently available in committed(C) token bucket */
417 /**< Number of bytes currently available in the excess(E) token bucket */
420 static inline enum rte_meter_color
421 rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
422 struct rte_meter_srtcm_profile *p,
426 uint64_t time_diff, n_periods, tc, te;
429 time_diff = time - m->time;
430 n_periods = time_diff / p->cir_period;
431 m->time += n_periods * p->cir_period;
433 /* Put the tokens overflowing from tc into te bucket */
434 tc = m->tc + n_periods * p->cir_bytes_per_period;
445 m->tc = tc - pkt_len;
447 return e_RTE_METER_GREEN;
452 m->te = te - pkt_len;
453 return e_RTE_METER_YELLOW;
458 return e_RTE_METER_RED;
461 static inline enum rte_meter_color
462 rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
463 struct rte_meter_srtcm_profile *p,
466 enum rte_meter_color pkt_color)
468 uint64_t time_diff, n_periods, tc, te;
471 time_diff = time - m->time;
472 n_periods = time_diff / p->cir_period;
473 m->time += n_periods * p->cir_period;
475 /* Put the tokens overflowing from tc into te bucket */
476 tc = m->tc + n_periods * p->cir_bytes_per_period;
486 if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
487 m->tc = tc - pkt_len;
489 return e_RTE_METER_GREEN;
492 if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
494 m->te = te - pkt_len;
495 return e_RTE_METER_YELLOW;
500 return e_RTE_METER_RED;
503 static inline enum rte_meter_color
504 rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
505 struct rte_meter_trtcm_profile *p,
509 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
512 time_diff_tc = time - m->time_tc;
513 time_diff_tp = time - m->time_tp;
514 n_periods_tc = time_diff_tc / p->cir_period;
515 n_periods_tp = time_diff_tp / p->pir_period;
516 m->time_tc += n_periods_tc * p->cir_period;
517 m->time_tp += n_periods_tp * p->pir_period;
519 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
523 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
531 return e_RTE_METER_RED;
536 m->tp = tp - pkt_len;
537 return e_RTE_METER_YELLOW;
540 m->tc = tc - pkt_len;
541 m->tp = tp - pkt_len;
542 return e_RTE_METER_GREEN;
545 static inline enum rte_meter_color
546 rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
547 struct rte_meter_trtcm_profile *p,
550 enum rte_meter_color pkt_color)
552 uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
555 time_diff_tc = time - m->time_tc;
556 time_diff_tp = time - m->time_tp;
557 n_periods_tc = time_diff_tc / p->cir_period;
558 n_periods_tp = time_diff_tp / p->pir_period;
559 m->time_tc += n_periods_tc * p->cir_period;
560 m->time_tp += n_periods_tp * p->pir_period;
562 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
566 tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
571 if ((pkt_color == e_RTE_METER_RED) || (tp < pkt_len)) {
574 return e_RTE_METER_RED;
577 if ((pkt_color == e_RTE_METER_YELLOW) || (tc < pkt_len)) {
579 m->tp = tp - pkt_len;
580 return e_RTE_METER_YELLOW;
583 m->tc = tc - pkt_len;
584 m->tp = tp - pkt_len;
585 return e_RTE_METER_GREEN;
588 static inline enum rte_meter_color __rte_experimental
589 rte_meter_trtcm_rfc4115_color_blind_check(
590 struct rte_meter_trtcm_rfc4115 *m,
591 struct rte_meter_trtcm_rfc4115_profile *p,
595 uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
598 time_diff_tc = time - m->time_tc;
599 time_diff_te = time - m->time_te;
600 n_periods_tc = time_diff_tc / p->cir_period;
601 n_periods_te = time_diff_te / p->eir_period;
602 m->time_tc += n_periods_tc * p->cir_period;
603 m->time_te += n_periods_te * p->eir_period;
605 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
609 te = m->te + n_periods_te * p->eir_bytes_per_period;
615 m->tc = tc - pkt_len;
617 return e_RTE_METER_GREEN;
621 m->te = te - pkt_len;
622 return e_RTE_METER_YELLOW;
625 /* If we end up here the color is RED */
628 return e_RTE_METER_RED;
631 static inline enum rte_meter_color __rte_experimental
632 rte_meter_trtcm_rfc4115_color_aware_check(
633 struct rte_meter_trtcm_rfc4115 *m,
634 struct rte_meter_trtcm_rfc4115_profile *p,
637 enum rte_meter_color pkt_color)
639 uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
642 time_diff_tc = time - m->time_tc;
643 time_diff_te = time - m->time_te;
644 n_periods_tc = time_diff_tc / p->cir_period;
645 n_periods_te = time_diff_te / p->eir_period;
646 m->time_tc += n_periods_tc * p->cir_period;
647 m->time_te += n_periods_te * p->eir_period;
649 tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
653 te = m->te + n_periods_te * p->eir_bytes_per_period;
658 if ((pkt_color == e_RTE_METER_GREEN) && (tc >= pkt_len)) {
659 m->tc = tc - pkt_len;
661 return e_RTE_METER_GREEN;
664 if ((pkt_color != e_RTE_METER_RED) && (te >= pkt_len)) {
666 m->te = te - pkt_len;
667 return e_RTE_METER_YELLOW;
670 /* If we end up here the color is RED */
673 return e_RTE_METER_RED;
681 #endif /* __INCLUDE_RTE_METER_H__ */