xref: /dpdk/lib/meter/rte_meter.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson 
299a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
399a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
499a2dd95SBruce Richardson  */
599a2dd95SBruce Richardson 
699a2dd95SBruce Richardson #ifndef __INCLUDE_RTE_METER_H__
799a2dd95SBruce Richardson #define __INCLUDE_RTE_METER_H__
899a2dd95SBruce Richardson 
999a2dd95SBruce Richardson /**
1099a2dd95SBruce Richardson  * @file
1199a2dd95SBruce Richardson  * RTE Traffic Metering
1299a2dd95SBruce Richardson  *
1399a2dd95SBruce Richardson  * Traffic metering algorithms:
1499a2dd95SBruce Richardson  *    1. Single Rate Three Color Marker (srTCM): defined by IETF RFC 2697
1599a2dd95SBruce Richardson  *    2. Two Rate Three Color Marker (trTCM): defined by IETF RFC 2698
1699a2dd95SBruce Richardson  *    3. Two Rate Three Color Marker (trTCM): defined by IETF RFC 4115
173e4c5be9SThomas Monjalon  */
1899a2dd95SBruce Richardson 
1999a2dd95SBruce Richardson #include <stdint.h>
2099a2dd95SBruce Richardson 
21*719834a6SMattias Rönnblom #ifdef __cplusplus
22*719834a6SMattias Rönnblom extern "C" {
23*719834a6SMattias Rönnblom #endif
24*719834a6SMattias Rönnblom 
2599a2dd95SBruce Richardson /*
2699a2dd95SBruce Richardson  * Application Programmer's Interface (API)
273e4c5be9SThomas Monjalon  */
2899a2dd95SBruce Richardson 
2999a2dd95SBruce Richardson /**
3099a2dd95SBruce Richardson  * Color
3199a2dd95SBruce Richardson  */
3299a2dd95SBruce Richardson enum rte_color {
3399a2dd95SBruce Richardson 	RTE_COLOR_GREEN = 0, /**< Green */
3499a2dd95SBruce Richardson 	RTE_COLOR_YELLOW, /**< Yellow */
3599a2dd95SBruce Richardson 	RTE_COLOR_RED, /**< Red */
3699a2dd95SBruce Richardson 	RTE_COLORS /**< Number of colors */
3799a2dd95SBruce Richardson };
3899a2dd95SBruce Richardson 
3999a2dd95SBruce Richardson /** srTCM parameters per metered traffic flow. The CIR, CBS and EBS parameters only
4099a2dd95SBruce Richardson count bytes of IP packets and do not include link specific headers. At least one of
4199a2dd95SBruce Richardson the CBS or EBS parameters has to be greater than zero. */
4299a2dd95SBruce Richardson struct rte_meter_srtcm_params {
4399a2dd95SBruce Richardson 	uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
4499a2dd95SBruce Richardson 	uint64_t cbs; /**< Committed Burst Size (CBS).  Measured in bytes. */
4599a2dd95SBruce Richardson 	uint64_t ebs; /**< Excess Burst Size (EBS).  Measured in bytes. */
4699a2dd95SBruce Richardson };
4799a2dd95SBruce Richardson 
4899a2dd95SBruce Richardson /** trTCM parameters per metered traffic flow. The CIR, PIR, CBS and PBS parameters
4999a2dd95SBruce Richardson only count bytes of IP packets and do not include link specific headers. PIR has to
5099a2dd95SBruce Richardson be greater than or equal to CIR. Both CBS or EBS have to be greater than zero. */
5199a2dd95SBruce Richardson struct rte_meter_trtcm_params {
5299a2dd95SBruce Richardson 	uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
5399a2dd95SBruce Richardson 	uint64_t pir; /**< Peak Information Rate (PIR). Measured in bytes per second. */
5499a2dd95SBruce Richardson 	uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
5599a2dd95SBruce Richardson 	uint64_t pbs; /**< Peak Burst Size (PBS). Measured in bytes. */
5699a2dd95SBruce Richardson };
5799a2dd95SBruce Richardson 
5899a2dd95SBruce Richardson /** trTCM parameters per metered traffic flow. The CIR, EIR, CBS and EBS
5999a2dd95SBruce Richardson parameters only count bytes of IP packets and do not include link specific
6099a2dd95SBruce Richardson headers. The CBS and EBS need to be greater than zero if CIR and EIR are
6199a2dd95SBruce Richardson none-zero respectively.*/
6299a2dd95SBruce Richardson struct rte_meter_trtcm_rfc4115_params {
6399a2dd95SBruce Richardson 	uint64_t cir; /**< Committed Information Rate (CIR). Measured in bytes per second. */
6499a2dd95SBruce Richardson 	uint64_t eir; /**< Excess Information Rate (EIR). Measured in bytes per second. */
6599a2dd95SBruce Richardson 	uint64_t cbs; /**< Committed Burst Size (CBS). Measured in bytes. */
6699a2dd95SBruce Richardson 	uint64_t ebs; /**< Excess Burst Size (EBS). Measured in bytes. */
6799a2dd95SBruce Richardson };
6899a2dd95SBruce Richardson 
6999a2dd95SBruce Richardson /**
7099a2dd95SBruce Richardson  * Internal data structure storing the srTCM configuration profile. Typically
7199a2dd95SBruce Richardson  * shared by multiple srTCM objects.
7299a2dd95SBruce Richardson  */
7399a2dd95SBruce Richardson struct rte_meter_srtcm_profile;
7499a2dd95SBruce Richardson 
7599a2dd95SBruce Richardson /**
7699a2dd95SBruce Richardson  * Internal data structure storing the trTCM configuration profile. Typically
7799a2dd95SBruce Richardson  * shared by multiple trTCM objects.
7899a2dd95SBruce Richardson  */
7999a2dd95SBruce Richardson struct rte_meter_trtcm_profile;
8099a2dd95SBruce Richardson 
8199a2dd95SBruce Richardson /**
8299a2dd95SBruce Richardson  * Internal data structure storing the trTCM RFC4115 configuration profile.
8399a2dd95SBruce Richardson  * Typically shared by multiple trTCM objects.
8499a2dd95SBruce Richardson  */
8599a2dd95SBruce Richardson struct rte_meter_trtcm_rfc4115_profile;
8699a2dd95SBruce Richardson 
8799a2dd95SBruce Richardson /** Internal data structure storing the srTCM run-time context per metered traffic flow. */
8899a2dd95SBruce Richardson struct rte_meter_srtcm;
8999a2dd95SBruce Richardson 
9099a2dd95SBruce Richardson /** Internal data structure storing the trTCM run-time context per metered traffic flow. */
9199a2dd95SBruce Richardson struct rte_meter_trtcm;
9299a2dd95SBruce Richardson 
9399a2dd95SBruce Richardson /**
9499a2dd95SBruce Richardson  * Internal data structure storing the trTCM RFC4115 run-time context per
9599a2dd95SBruce Richardson  * metered traffic flow.
9699a2dd95SBruce Richardson  */
9799a2dd95SBruce Richardson struct rte_meter_trtcm_rfc4115;
9899a2dd95SBruce Richardson 
9999a2dd95SBruce Richardson /**
10099a2dd95SBruce Richardson  * srTCM profile configuration
10199a2dd95SBruce Richardson  *
10299a2dd95SBruce Richardson  * @param p
10399a2dd95SBruce Richardson  *    Pointer to pre-allocated srTCM profile data structure
10499a2dd95SBruce Richardson  * @param params
10599a2dd95SBruce Richardson  *    srTCM profile parameters
10699a2dd95SBruce Richardson  * @return
10799a2dd95SBruce Richardson  *    0 upon success, error code otherwise
10899a2dd95SBruce Richardson  */
10999a2dd95SBruce Richardson int
11099a2dd95SBruce Richardson rte_meter_srtcm_profile_config(struct rte_meter_srtcm_profile *p,
11199a2dd95SBruce Richardson 	struct rte_meter_srtcm_params *params);
11299a2dd95SBruce Richardson 
11399a2dd95SBruce Richardson /**
11499a2dd95SBruce Richardson  * trTCM profile configuration
11599a2dd95SBruce Richardson  *
11699a2dd95SBruce Richardson  * @param p
11799a2dd95SBruce Richardson  *    Pointer to pre-allocated trTCM profile data structure
11899a2dd95SBruce Richardson  * @param params
11999a2dd95SBruce Richardson  *    trTCM profile parameters
12099a2dd95SBruce Richardson  * @return
12199a2dd95SBruce Richardson  *    0 upon success, error code otherwise
12299a2dd95SBruce Richardson  */
12399a2dd95SBruce Richardson int
12499a2dd95SBruce Richardson rte_meter_trtcm_profile_config(struct rte_meter_trtcm_profile *p,
12599a2dd95SBruce Richardson 	struct rte_meter_trtcm_params *params);
12699a2dd95SBruce Richardson /**
12799a2dd95SBruce Richardson  * trTCM RFC 4115 profile configuration
12899a2dd95SBruce Richardson  *
12999a2dd95SBruce Richardson  * @param p
13099a2dd95SBruce Richardson  *    Pointer to pre-allocated trTCM profile data structure
13199a2dd95SBruce Richardson  * @param params
13299a2dd95SBruce Richardson  *    trTCM profile parameters
13399a2dd95SBruce Richardson  * @return
13499a2dd95SBruce Richardson  *    0 upon success, error code otherwise
13599a2dd95SBruce Richardson  */
13699a2dd95SBruce Richardson int
13799a2dd95SBruce Richardson rte_meter_trtcm_rfc4115_profile_config(
13899a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115_profile *p,
13999a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115_params *params);
14099a2dd95SBruce Richardson 
14199a2dd95SBruce Richardson /**
14299a2dd95SBruce Richardson  * srTCM configuration per metered traffic flow
14399a2dd95SBruce Richardson  *
14499a2dd95SBruce Richardson  * @param m
14599a2dd95SBruce Richardson  *    Pointer to pre-allocated srTCM data structure
14699a2dd95SBruce Richardson  * @param p
14799a2dd95SBruce Richardson  *    srTCM profile. Needs to be valid.
14899a2dd95SBruce Richardson  * @return
14999a2dd95SBruce Richardson  *    0 upon success, error code otherwise
15099a2dd95SBruce Richardson  */
15199a2dd95SBruce Richardson int
15299a2dd95SBruce Richardson rte_meter_srtcm_config(struct rte_meter_srtcm *m,
15399a2dd95SBruce Richardson 	struct rte_meter_srtcm_profile *p);
15499a2dd95SBruce Richardson 
15599a2dd95SBruce Richardson /**
15699a2dd95SBruce Richardson  * trTCM configuration per metered traffic flow
15799a2dd95SBruce Richardson  *
15899a2dd95SBruce Richardson  * @param m
15999a2dd95SBruce Richardson  *    Pointer to pre-allocated trTCM data structure
16099a2dd95SBruce Richardson  * @param p
16199a2dd95SBruce Richardson  *    trTCM profile. Needs to be valid.
16299a2dd95SBruce Richardson  * @return
16399a2dd95SBruce Richardson  *    0 upon success, error code otherwise
16499a2dd95SBruce Richardson  */
16599a2dd95SBruce Richardson int
16699a2dd95SBruce Richardson rte_meter_trtcm_config(struct rte_meter_trtcm *m,
16799a2dd95SBruce Richardson 	struct rte_meter_trtcm_profile *p);
16899a2dd95SBruce Richardson 
16999a2dd95SBruce Richardson /**
17099a2dd95SBruce Richardson  * trTCM RFC 4115 configuration per metered traffic flow
17199a2dd95SBruce Richardson  *
17299a2dd95SBruce Richardson  * @param m
17399a2dd95SBruce Richardson  *    Pointer to pre-allocated trTCM data structure
17499a2dd95SBruce Richardson  * @param p
17599a2dd95SBruce Richardson  *    trTCM profile. Needs to be valid.
17699a2dd95SBruce Richardson  * @return
17799a2dd95SBruce Richardson  *    0 upon success, error code otherwise
17899a2dd95SBruce Richardson  */
17999a2dd95SBruce Richardson int
18099a2dd95SBruce Richardson rte_meter_trtcm_rfc4115_config(struct rte_meter_trtcm_rfc4115 *m,
18199a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115_profile *p);
18299a2dd95SBruce Richardson 
18399a2dd95SBruce Richardson /**
18499a2dd95SBruce Richardson  * srTCM color blind traffic metering
18599a2dd95SBruce Richardson  *
18699a2dd95SBruce Richardson  * @param m
18799a2dd95SBruce Richardson  *    Handle to srTCM instance
18899a2dd95SBruce Richardson  * @param p
18999a2dd95SBruce Richardson  *    srTCM profile specified at srTCM object creation time
19099a2dd95SBruce Richardson  * @param time
19199a2dd95SBruce Richardson  *    Current CPU time stamp (measured in CPU cycles)
19299a2dd95SBruce Richardson  * @param pkt_len
19399a2dd95SBruce Richardson  *    Length of the current IP packet (measured in bytes)
19499a2dd95SBruce Richardson  * @return
19599a2dd95SBruce Richardson  *    Color assigned to the current IP packet
19699a2dd95SBruce Richardson  */
19799a2dd95SBruce Richardson static inline enum rte_color
19899a2dd95SBruce Richardson rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
19999a2dd95SBruce Richardson 	struct rte_meter_srtcm_profile *p,
20099a2dd95SBruce Richardson 	uint64_t time,
20199a2dd95SBruce Richardson 	uint32_t pkt_len);
20299a2dd95SBruce Richardson 
20399a2dd95SBruce Richardson /**
20499a2dd95SBruce Richardson  * srTCM color aware traffic metering
20599a2dd95SBruce Richardson  *
20699a2dd95SBruce Richardson  * @param m
20799a2dd95SBruce Richardson  *    Handle to srTCM instance
20899a2dd95SBruce Richardson  * @param p
20999a2dd95SBruce Richardson  *    srTCM profile specified at srTCM object creation time
21099a2dd95SBruce Richardson  * @param time
21199a2dd95SBruce Richardson  *    Current CPU time stamp (measured in CPU cycles)
21299a2dd95SBruce Richardson  * @param pkt_len
21399a2dd95SBruce Richardson  *    Length of the current IP packet (measured in bytes)
21499a2dd95SBruce Richardson  * @param pkt_color
21599a2dd95SBruce Richardson  *    Input color of the current IP packet
21699a2dd95SBruce Richardson  * @return
21799a2dd95SBruce Richardson  *    Color assigned to the current IP packet
21899a2dd95SBruce Richardson  */
21999a2dd95SBruce Richardson static inline enum rte_color
22099a2dd95SBruce Richardson rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
22199a2dd95SBruce Richardson 	struct rte_meter_srtcm_profile *p,
22299a2dd95SBruce Richardson 	uint64_t time,
22399a2dd95SBruce Richardson 	uint32_t pkt_len,
22499a2dd95SBruce Richardson 	enum rte_color pkt_color);
22599a2dd95SBruce Richardson 
22699a2dd95SBruce Richardson /**
22799a2dd95SBruce Richardson  * trTCM color blind traffic metering
22899a2dd95SBruce Richardson  *
22999a2dd95SBruce Richardson  * @param m
23099a2dd95SBruce Richardson  *    Handle to trTCM instance
23199a2dd95SBruce Richardson  * @param p
23299a2dd95SBruce Richardson  *    trTCM profile specified at trTCM object creation time
23399a2dd95SBruce Richardson  * @param time
23499a2dd95SBruce Richardson  *    Current CPU time stamp (measured in CPU cycles)
23599a2dd95SBruce Richardson  * @param pkt_len
23699a2dd95SBruce Richardson  *    Length of the current IP packet (measured in bytes)
23799a2dd95SBruce Richardson  * @return
23899a2dd95SBruce Richardson  *    Color assigned to the current IP packet
23999a2dd95SBruce Richardson  */
24099a2dd95SBruce Richardson static inline enum rte_color
24199a2dd95SBruce Richardson rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
24299a2dd95SBruce Richardson 	struct rte_meter_trtcm_profile *p,
24399a2dd95SBruce Richardson 	uint64_t time,
24499a2dd95SBruce Richardson 	uint32_t pkt_len);
24599a2dd95SBruce Richardson 
24699a2dd95SBruce Richardson /**
24799a2dd95SBruce Richardson  * trTCM color aware traffic metering
24899a2dd95SBruce Richardson  *
24999a2dd95SBruce Richardson  * @param m
25099a2dd95SBruce Richardson  *    Handle to trTCM instance
25199a2dd95SBruce Richardson  * @param p
25299a2dd95SBruce Richardson  *    trTCM profile specified at trTCM object creation time
25399a2dd95SBruce Richardson  * @param time
25499a2dd95SBruce Richardson  *    Current CPU time stamp (measured in CPU cycles)
25599a2dd95SBruce Richardson  * @param pkt_len
25699a2dd95SBruce Richardson  *    Length of the current IP packet (measured in bytes)
25799a2dd95SBruce Richardson  * @param pkt_color
25899a2dd95SBruce Richardson  *    Input color of the current IP packet
25999a2dd95SBruce Richardson  * @return
26099a2dd95SBruce Richardson  *    Color assigned to the current IP packet
26199a2dd95SBruce Richardson  */
26299a2dd95SBruce Richardson static inline enum rte_color
26399a2dd95SBruce Richardson rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
26499a2dd95SBruce Richardson 	struct rte_meter_trtcm_profile *p,
26599a2dd95SBruce Richardson 	uint64_t time,
26699a2dd95SBruce Richardson 	uint32_t pkt_len,
26799a2dd95SBruce Richardson 	enum rte_color pkt_color);
26899a2dd95SBruce Richardson 
26999a2dd95SBruce Richardson /**
27099a2dd95SBruce Richardson  * trTCM RFC4115 color blind traffic metering
27199a2dd95SBruce Richardson  *
27299a2dd95SBruce Richardson  * @param m
27399a2dd95SBruce Richardson  *    Handle to trTCM instance
27499a2dd95SBruce Richardson  * @param p
27599a2dd95SBruce Richardson  *    trTCM profile specified at trTCM object creation time
27699a2dd95SBruce Richardson  * @param time
27799a2dd95SBruce Richardson  *    Current CPU time stamp (measured in CPU cycles)
27899a2dd95SBruce Richardson  * @param pkt_len
27999a2dd95SBruce Richardson  *    Length of the current IP packet (measured in bytes)
28099a2dd95SBruce Richardson  * @return
28199a2dd95SBruce Richardson  *    Color assigned to the current IP packet
28299a2dd95SBruce Richardson  */
28399a2dd95SBruce Richardson static inline enum rte_color
28499a2dd95SBruce Richardson rte_meter_trtcm_rfc4115_color_blind_check(
28599a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115 *m,
28699a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115_profile *p,
28799a2dd95SBruce Richardson 	uint64_t time,
28899a2dd95SBruce Richardson 	uint32_t pkt_len);
28999a2dd95SBruce Richardson 
29099a2dd95SBruce Richardson /**
29199a2dd95SBruce Richardson  * trTCM RFC4115 color aware traffic metering
29299a2dd95SBruce Richardson  *
29399a2dd95SBruce Richardson  * @param m
29499a2dd95SBruce Richardson  *    Handle to trTCM instance
29599a2dd95SBruce Richardson  * @param p
29699a2dd95SBruce Richardson  *    trTCM profile specified at trTCM object creation time
29799a2dd95SBruce Richardson  * @param time
29899a2dd95SBruce Richardson  *    Current CPU time stamp (measured in CPU cycles)
29999a2dd95SBruce Richardson  * @param pkt_len
30099a2dd95SBruce Richardson  *    Length of the current IP packet (measured in bytes)
30199a2dd95SBruce Richardson  * @param pkt_color
30299a2dd95SBruce Richardson  *    Input color of the current IP packet
30399a2dd95SBruce Richardson  * @return
30499a2dd95SBruce Richardson  *    Color assigned to the current IP packet
30599a2dd95SBruce Richardson  */
30699a2dd95SBruce Richardson static inline enum rte_color
30799a2dd95SBruce Richardson rte_meter_trtcm_rfc4115_color_aware_check(
30899a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115 *m,
30999a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115_profile *p,
31099a2dd95SBruce Richardson 	uint64_t time,
31199a2dd95SBruce Richardson 	uint32_t pkt_len,
31299a2dd95SBruce Richardson 	enum rte_color pkt_color);
31399a2dd95SBruce Richardson 
31499a2dd95SBruce Richardson /*
31599a2dd95SBruce Richardson  * Inline implementation of run-time methods
3163e4c5be9SThomas Monjalon  */
31799a2dd95SBruce Richardson 
31899a2dd95SBruce Richardson struct rte_meter_srtcm_profile {
31999a2dd95SBruce Richardson 	uint64_t cbs;
32099a2dd95SBruce Richardson 	/**< Upper limit for C token bucket */
32199a2dd95SBruce Richardson 	uint64_t ebs;
32299a2dd95SBruce Richardson 	/**< Upper limit for E token bucket */
32399a2dd95SBruce Richardson 	uint64_t cir_period;
32499a2dd95SBruce Richardson 	/**< Number of CPU cycles for each update of C and E token buckets */
32599a2dd95SBruce Richardson 	uint64_t cir_bytes_per_period;
32699a2dd95SBruce Richardson 	/**< Number of bytes to add to C and E token buckets on each update */
32799a2dd95SBruce Richardson };
32899a2dd95SBruce Richardson 
32999a2dd95SBruce Richardson /* Internal data structure storing the srTCM run-time context per metered traffic flow. */
33099a2dd95SBruce Richardson struct rte_meter_srtcm {
33199a2dd95SBruce Richardson 	uint64_t time; /* Time of latest update of C and E token buckets */
33299a2dd95SBruce Richardson 	uint64_t tc;   /* Number of bytes currently available in the committed (C) token bucket */
33399a2dd95SBruce Richardson 	uint64_t te;   /* Number of bytes currently available in the excess (E) token bucket */
33499a2dd95SBruce Richardson };
33599a2dd95SBruce Richardson 
33699a2dd95SBruce Richardson struct rte_meter_trtcm_profile {
33799a2dd95SBruce Richardson 	uint64_t cbs;
33899a2dd95SBruce Richardson 	/**< Upper limit for C token bucket */
33999a2dd95SBruce Richardson 	uint64_t pbs;
34099a2dd95SBruce Richardson 	/**< Upper limit for P token bucket */
34199a2dd95SBruce Richardson 	uint64_t cir_period;
34299a2dd95SBruce Richardson 	/**< Number of CPU cycles for one update of C token bucket */
34399a2dd95SBruce Richardson 	uint64_t cir_bytes_per_period;
34499a2dd95SBruce Richardson 	/**< Number of bytes to add to C token bucket on each update */
34599a2dd95SBruce Richardson 	uint64_t pir_period;
34699a2dd95SBruce Richardson 	/**< Number of CPU cycles for one update of P token bucket */
34799a2dd95SBruce Richardson 	uint64_t pir_bytes_per_period;
34899a2dd95SBruce Richardson 	/**< Number of bytes to add to P token bucket on each update */
34999a2dd95SBruce Richardson };
35099a2dd95SBruce Richardson 
35199a2dd95SBruce Richardson /**
35299a2dd95SBruce Richardson  * Internal data structure storing the trTCM run-time context per metered
35399a2dd95SBruce Richardson  * traffic flow.
35499a2dd95SBruce Richardson  */
35599a2dd95SBruce Richardson struct rte_meter_trtcm {
35699a2dd95SBruce Richardson 	uint64_t time_tc;
35799a2dd95SBruce Richardson 	/**< Time of latest update of C token bucket */
35899a2dd95SBruce Richardson 	uint64_t time_tp;
35999a2dd95SBruce Richardson 	/**< Time of latest update of P token bucket */
36099a2dd95SBruce Richardson 	uint64_t tc;
36199a2dd95SBruce Richardson 	/**< Number of bytes currently available in committed(C) token bucket */
36299a2dd95SBruce Richardson 	uint64_t tp;
36399a2dd95SBruce Richardson 	/**< Number of bytes currently available in the peak(P) token bucket */
36499a2dd95SBruce Richardson };
36599a2dd95SBruce Richardson 
36699a2dd95SBruce Richardson struct rte_meter_trtcm_rfc4115_profile {
36799a2dd95SBruce Richardson 	uint64_t cbs;
36899a2dd95SBruce Richardson 	/**< Upper limit for C token bucket */
36999a2dd95SBruce Richardson 	uint64_t ebs;
37099a2dd95SBruce Richardson 	/**< Upper limit for E token bucket */
37199a2dd95SBruce Richardson 	uint64_t cir_period;
37299a2dd95SBruce Richardson 	/**< Number of CPU cycles for one update of C token bucket */
37399a2dd95SBruce Richardson 	uint64_t cir_bytes_per_period;
37499a2dd95SBruce Richardson 	/**< Number of bytes to add to C token bucket on each update */
37599a2dd95SBruce Richardson 	uint64_t eir_period;
37699a2dd95SBruce Richardson 	/**< Number of CPU cycles for one update of E token bucket */
37799a2dd95SBruce Richardson 	uint64_t eir_bytes_per_period;
37899a2dd95SBruce Richardson 	/**< Number of bytes to add to E token bucket on each update */
37999a2dd95SBruce Richardson };
38099a2dd95SBruce Richardson 
38199a2dd95SBruce Richardson /**
38299a2dd95SBruce Richardson  * Internal data structure storing the trTCM RFC4115 run-time context per
38399a2dd95SBruce Richardson  * metered traffic flow.
38499a2dd95SBruce Richardson  */
38599a2dd95SBruce Richardson struct rte_meter_trtcm_rfc4115 {
38699a2dd95SBruce Richardson 	uint64_t time_tc;
38799a2dd95SBruce Richardson 	/**< Time of latest update of C token bucket */
38899a2dd95SBruce Richardson 	uint64_t time_te;
38999a2dd95SBruce Richardson 	/**< Time of latest update of E token bucket */
39099a2dd95SBruce Richardson 	uint64_t tc;
39199a2dd95SBruce Richardson 	/**< Number of bytes currently available in committed(C) token bucket */
39299a2dd95SBruce Richardson 	uint64_t te;
39399a2dd95SBruce Richardson 	/**< Number of bytes currently available in the excess(E) token bucket */
39499a2dd95SBruce Richardson };
39599a2dd95SBruce Richardson 
39699a2dd95SBruce Richardson static inline enum rte_color
39799a2dd95SBruce Richardson rte_meter_srtcm_color_blind_check(struct rte_meter_srtcm *m,
39899a2dd95SBruce Richardson 	struct rte_meter_srtcm_profile *p,
39999a2dd95SBruce Richardson 	uint64_t time,
40099a2dd95SBruce Richardson 	uint32_t pkt_len)
40199a2dd95SBruce Richardson {
40299a2dd95SBruce Richardson 	uint64_t time_diff, n_periods, tc, te;
40399a2dd95SBruce Richardson 
40499a2dd95SBruce Richardson 	/* Bucket update */
40599a2dd95SBruce Richardson 	time_diff = time - m->time;
40699a2dd95SBruce Richardson 	n_periods = time_diff / p->cir_period;
40799a2dd95SBruce Richardson 	m->time += n_periods * p->cir_period;
40899a2dd95SBruce Richardson 
40999a2dd95SBruce Richardson 	/* Put the tokens overflowing from tc into te bucket */
41099a2dd95SBruce Richardson 	tc = m->tc + n_periods * p->cir_bytes_per_period;
41199a2dd95SBruce Richardson 	te = m->te;
41299a2dd95SBruce Richardson 	if (tc > p->cbs) {
41399a2dd95SBruce Richardson 		te += (tc - p->cbs);
41499a2dd95SBruce Richardson 		if (te > p->ebs)
41599a2dd95SBruce Richardson 			te = p->ebs;
41699a2dd95SBruce Richardson 		tc = p->cbs;
41799a2dd95SBruce Richardson 	}
41899a2dd95SBruce Richardson 
41999a2dd95SBruce Richardson 	/* Color logic */
42099a2dd95SBruce Richardson 	if (tc >= pkt_len) {
42199a2dd95SBruce Richardson 		m->tc = tc - pkt_len;
42299a2dd95SBruce Richardson 		m->te = te;
42399a2dd95SBruce Richardson 		return RTE_COLOR_GREEN;
42499a2dd95SBruce Richardson 	}
42599a2dd95SBruce Richardson 
42699a2dd95SBruce Richardson 	if (te >= pkt_len) {
42799a2dd95SBruce Richardson 		m->tc = tc;
42899a2dd95SBruce Richardson 		m->te = te - pkt_len;
42999a2dd95SBruce Richardson 		return RTE_COLOR_YELLOW;
43099a2dd95SBruce Richardson 	}
43199a2dd95SBruce Richardson 
43299a2dd95SBruce Richardson 	m->tc = tc;
43399a2dd95SBruce Richardson 	m->te = te;
43499a2dd95SBruce Richardson 	return RTE_COLOR_RED;
43599a2dd95SBruce Richardson }
43699a2dd95SBruce Richardson 
43799a2dd95SBruce Richardson static inline enum rte_color
43899a2dd95SBruce Richardson rte_meter_srtcm_color_aware_check(struct rte_meter_srtcm *m,
43999a2dd95SBruce Richardson 	struct rte_meter_srtcm_profile *p,
44099a2dd95SBruce Richardson 	uint64_t time,
44199a2dd95SBruce Richardson 	uint32_t pkt_len,
44299a2dd95SBruce Richardson 	enum rte_color pkt_color)
44399a2dd95SBruce Richardson {
44499a2dd95SBruce Richardson 	uint64_t time_diff, n_periods, tc, te;
44599a2dd95SBruce Richardson 
44699a2dd95SBruce Richardson 	/* Bucket update */
44799a2dd95SBruce Richardson 	time_diff = time - m->time;
44899a2dd95SBruce Richardson 	n_periods = time_diff / p->cir_period;
44999a2dd95SBruce Richardson 	m->time += n_periods * p->cir_period;
45099a2dd95SBruce Richardson 
45199a2dd95SBruce Richardson 	/* Put the tokens overflowing from tc into te bucket */
45299a2dd95SBruce Richardson 	tc = m->tc + n_periods * p->cir_bytes_per_period;
45399a2dd95SBruce Richardson 	te = m->te;
45499a2dd95SBruce Richardson 	if (tc > p->cbs) {
45599a2dd95SBruce Richardson 		te += (tc - p->cbs);
45699a2dd95SBruce Richardson 		if (te > p->ebs)
45799a2dd95SBruce Richardson 			te = p->ebs;
45899a2dd95SBruce Richardson 		tc = p->cbs;
45999a2dd95SBruce Richardson 	}
46099a2dd95SBruce Richardson 
46199a2dd95SBruce Richardson 	/* Color logic */
46299a2dd95SBruce Richardson 	if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
46399a2dd95SBruce Richardson 		m->tc = tc - pkt_len;
46499a2dd95SBruce Richardson 		m->te = te;
46599a2dd95SBruce Richardson 		return RTE_COLOR_GREEN;
46699a2dd95SBruce Richardson 	}
46799a2dd95SBruce Richardson 
46899a2dd95SBruce Richardson 	if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
46999a2dd95SBruce Richardson 		m->tc = tc;
47099a2dd95SBruce Richardson 		m->te = te - pkt_len;
47199a2dd95SBruce Richardson 		return RTE_COLOR_YELLOW;
47299a2dd95SBruce Richardson 	}
47399a2dd95SBruce Richardson 
47499a2dd95SBruce Richardson 	m->tc = tc;
47599a2dd95SBruce Richardson 	m->te = te;
47699a2dd95SBruce Richardson 	return RTE_COLOR_RED;
47799a2dd95SBruce Richardson }
47899a2dd95SBruce Richardson 
47999a2dd95SBruce Richardson static inline enum rte_color
48099a2dd95SBruce Richardson rte_meter_trtcm_color_blind_check(struct rte_meter_trtcm *m,
48199a2dd95SBruce Richardson 	struct rte_meter_trtcm_profile *p,
48299a2dd95SBruce Richardson 	uint64_t time,
48399a2dd95SBruce Richardson 	uint32_t pkt_len)
48499a2dd95SBruce Richardson {
48599a2dd95SBruce Richardson 	uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
48699a2dd95SBruce Richardson 
48799a2dd95SBruce Richardson 	/* Bucket update */
48899a2dd95SBruce Richardson 	time_diff_tc = time - m->time_tc;
48999a2dd95SBruce Richardson 	time_diff_tp = time - m->time_tp;
49099a2dd95SBruce Richardson 	n_periods_tc = time_diff_tc / p->cir_period;
49199a2dd95SBruce Richardson 	n_periods_tp = time_diff_tp / p->pir_period;
49299a2dd95SBruce Richardson 	m->time_tc += n_periods_tc * p->cir_period;
49399a2dd95SBruce Richardson 	m->time_tp += n_periods_tp * p->pir_period;
49499a2dd95SBruce Richardson 
49599a2dd95SBruce Richardson 	tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
49699a2dd95SBruce Richardson 	if (tc > p->cbs)
49799a2dd95SBruce Richardson 		tc = p->cbs;
49899a2dd95SBruce Richardson 
49999a2dd95SBruce Richardson 	tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
50099a2dd95SBruce Richardson 	if (tp > p->pbs)
50199a2dd95SBruce Richardson 		tp = p->pbs;
50299a2dd95SBruce Richardson 
50399a2dd95SBruce Richardson 	/* Color logic */
50499a2dd95SBruce Richardson 	if (tp < pkt_len) {
50599a2dd95SBruce Richardson 		m->tc = tc;
50699a2dd95SBruce Richardson 		m->tp = tp;
50799a2dd95SBruce Richardson 		return RTE_COLOR_RED;
50899a2dd95SBruce Richardson 	}
50999a2dd95SBruce Richardson 
51099a2dd95SBruce Richardson 	if (tc < pkt_len) {
51199a2dd95SBruce Richardson 		m->tc = tc;
51299a2dd95SBruce Richardson 		m->tp = tp - pkt_len;
51399a2dd95SBruce Richardson 		return RTE_COLOR_YELLOW;
51499a2dd95SBruce Richardson 	}
51599a2dd95SBruce Richardson 
51699a2dd95SBruce Richardson 	m->tc = tc - pkt_len;
51799a2dd95SBruce Richardson 	m->tp = tp - pkt_len;
51899a2dd95SBruce Richardson 	return RTE_COLOR_GREEN;
51999a2dd95SBruce Richardson }
52099a2dd95SBruce Richardson 
52199a2dd95SBruce Richardson static inline enum rte_color
52299a2dd95SBruce Richardson rte_meter_trtcm_color_aware_check(struct rte_meter_trtcm *m,
52399a2dd95SBruce Richardson 	struct rte_meter_trtcm_profile *p,
52499a2dd95SBruce Richardson 	uint64_t time,
52599a2dd95SBruce Richardson 	uint32_t pkt_len,
52699a2dd95SBruce Richardson 	enum rte_color pkt_color)
52799a2dd95SBruce Richardson {
52899a2dd95SBruce Richardson 	uint64_t time_diff_tc, time_diff_tp, n_periods_tc, n_periods_tp, tc, tp;
52999a2dd95SBruce Richardson 
53099a2dd95SBruce Richardson 	/* Bucket update */
53199a2dd95SBruce Richardson 	time_diff_tc = time - m->time_tc;
53299a2dd95SBruce Richardson 	time_diff_tp = time - m->time_tp;
53399a2dd95SBruce Richardson 	n_periods_tc = time_diff_tc / p->cir_period;
53499a2dd95SBruce Richardson 	n_periods_tp = time_diff_tp / p->pir_period;
53599a2dd95SBruce Richardson 	m->time_tc += n_periods_tc * p->cir_period;
53699a2dd95SBruce Richardson 	m->time_tp += n_periods_tp * p->pir_period;
53799a2dd95SBruce Richardson 
53899a2dd95SBruce Richardson 	tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
53999a2dd95SBruce Richardson 	if (tc > p->cbs)
54099a2dd95SBruce Richardson 		tc = p->cbs;
54199a2dd95SBruce Richardson 
54299a2dd95SBruce Richardson 	tp = m->tp + n_periods_tp * p->pir_bytes_per_period;
54399a2dd95SBruce Richardson 	if (tp > p->pbs)
54499a2dd95SBruce Richardson 		tp = p->pbs;
54599a2dd95SBruce Richardson 
54699a2dd95SBruce Richardson 	/* Color logic */
54799a2dd95SBruce Richardson 	if ((pkt_color == RTE_COLOR_RED) || (tp < pkt_len)) {
54899a2dd95SBruce Richardson 		m->tc = tc;
54999a2dd95SBruce Richardson 		m->tp = tp;
55099a2dd95SBruce Richardson 		return RTE_COLOR_RED;
55199a2dd95SBruce Richardson 	}
55299a2dd95SBruce Richardson 
55399a2dd95SBruce Richardson 	if ((pkt_color == RTE_COLOR_YELLOW) || (tc < pkt_len)) {
55499a2dd95SBruce Richardson 		m->tc = tc;
55599a2dd95SBruce Richardson 		m->tp = tp - pkt_len;
55699a2dd95SBruce Richardson 		return RTE_COLOR_YELLOW;
55799a2dd95SBruce Richardson 	}
55899a2dd95SBruce Richardson 
55999a2dd95SBruce Richardson 	m->tc = tc - pkt_len;
56099a2dd95SBruce Richardson 	m->tp = tp - pkt_len;
56199a2dd95SBruce Richardson 	return RTE_COLOR_GREEN;
56299a2dd95SBruce Richardson }
56399a2dd95SBruce Richardson 
56499a2dd95SBruce Richardson static inline enum rte_color
56599a2dd95SBruce Richardson rte_meter_trtcm_rfc4115_color_blind_check(
56699a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115 *m,
56799a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115_profile *p,
56899a2dd95SBruce Richardson 	uint64_t time,
56999a2dd95SBruce Richardson 	uint32_t pkt_len)
57099a2dd95SBruce Richardson {
57199a2dd95SBruce Richardson 	uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
57299a2dd95SBruce Richardson 
57399a2dd95SBruce Richardson 	/* Bucket update */
57499a2dd95SBruce Richardson 	time_diff_tc = time - m->time_tc;
57599a2dd95SBruce Richardson 	time_diff_te = time - m->time_te;
57699a2dd95SBruce Richardson 	n_periods_tc = time_diff_tc / p->cir_period;
57799a2dd95SBruce Richardson 	n_periods_te = time_diff_te / p->eir_period;
57899a2dd95SBruce Richardson 	m->time_tc += n_periods_tc * p->cir_period;
57999a2dd95SBruce Richardson 	m->time_te += n_periods_te * p->eir_period;
58099a2dd95SBruce Richardson 
58199a2dd95SBruce Richardson 	tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
58299a2dd95SBruce Richardson 	if (tc > p->cbs)
58399a2dd95SBruce Richardson 		tc = p->cbs;
58499a2dd95SBruce Richardson 
58599a2dd95SBruce Richardson 	te = m->te + n_periods_te * p->eir_bytes_per_period;
58699a2dd95SBruce Richardson 	if (te > p->ebs)
58799a2dd95SBruce Richardson 		te = p->ebs;
58899a2dd95SBruce Richardson 
58999a2dd95SBruce Richardson 	/* Color logic */
59099a2dd95SBruce Richardson 	if (tc >= pkt_len) {
59199a2dd95SBruce Richardson 		m->tc = tc - pkt_len;
59299a2dd95SBruce Richardson 		m->te = te;
59399a2dd95SBruce Richardson 		return RTE_COLOR_GREEN;
59499a2dd95SBruce Richardson 	}
59599a2dd95SBruce Richardson 	if (te >= pkt_len) {
59699a2dd95SBruce Richardson 		m->tc = tc;
59799a2dd95SBruce Richardson 		m->te = te - pkt_len;
59899a2dd95SBruce Richardson 		return RTE_COLOR_YELLOW;
59999a2dd95SBruce Richardson 	}
60099a2dd95SBruce Richardson 
60199a2dd95SBruce Richardson 	/* If we end up here the color is RED */
60299a2dd95SBruce Richardson 	m->tc = tc;
60399a2dd95SBruce Richardson 	m->te = te;
60499a2dd95SBruce Richardson 	return RTE_COLOR_RED;
60599a2dd95SBruce Richardson }
60699a2dd95SBruce Richardson 
60799a2dd95SBruce Richardson static inline enum rte_color
60899a2dd95SBruce Richardson rte_meter_trtcm_rfc4115_color_aware_check(
60999a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115 *m,
61099a2dd95SBruce Richardson 	struct rte_meter_trtcm_rfc4115_profile *p,
61199a2dd95SBruce Richardson 	uint64_t time,
61299a2dd95SBruce Richardson 	uint32_t pkt_len,
61399a2dd95SBruce Richardson 	enum rte_color pkt_color)
61499a2dd95SBruce Richardson {
61599a2dd95SBruce Richardson 	uint64_t time_diff_tc, time_diff_te, n_periods_tc, n_periods_te, tc, te;
61699a2dd95SBruce Richardson 
61799a2dd95SBruce Richardson 	/* Bucket update */
61899a2dd95SBruce Richardson 	time_diff_tc = time - m->time_tc;
61999a2dd95SBruce Richardson 	time_diff_te = time - m->time_te;
62099a2dd95SBruce Richardson 	n_periods_tc = time_diff_tc / p->cir_period;
62199a2dd95SBruce Richardson 	n_periods_te = time_diff_te / p->eir_period;
62299a2dd95SBruce Richardson 	m->time_tc += n_periods_tc * p->cir_period;
62399a2dd95SBruce Richardson 	m->time_te += n_periods_te * p->eir_period;
62499a2dd95SBruce Richardson 
62599a2dd95SBruce Richardson 	tc = m->tc + n_periods_tc * p->cir_bytes_per_period;
62699a2dd95SBruce Richardson 	if (tc > p->cbs)
62799a2dd95SBruce Richardson 		tc = p->cbs;
62899a2dd95SBruce Richardson 
62999a2dd95SBruce Richardson 	te = m->te + n_periods_te * p->eir_bytes_per_period;
63099a2dd95SBruce Richardson 	if (te > p->ebs)
63199a2dd95SBruce Richardson 		te = p->ebs;
63299a2dd95SBruce Richardson 
63399a2dd95SBruce Richardson 	/* Color logic */
63499a2dd95SBruce Richardson 	if ((pkt_color == RTE_COLOR_GREEN) && (tc >= pkt_len)) {
63599a2dd95SBruce Richardson 		m->tc = tc - pkt_len;
63699a2dd95SBruce Richardson 		m->te = te;
63799a2dd95SBruce Richardson 		return RTE_COLOR_GREEN;
63899a2dd95SBruce Richardson 	}
63999a2dd95SBruce Richardson 
64099a2dd95SBruce Richardson 	if ((pkt_color != RTE_COLOR_RED) && (te >= pkt_len)) {
64199a2dd95SBruce Richardson 		m->tc = tc;
64299a2dd95SBruce Richardson 		m->te = te - pkt_len;
64399a2dd95SBruce Richardson 		return RTE_COLOR_YELLOW;
64499a2dd95SBruce Richardson 	}
64599a2dd95SBruce Richardson 
64699a2dd95SBruce Richardson 	/* If we end up here the color is RED */
64799a2dd95SBruce Richardson 	m->tc = tc;
64899a2dd95SBruce Richardson 	m->te = te;
64999a2dd95SBruce Richardson 	return RTE_COLOR_RED;
65099a2dd95SBruce Richardson }
65199a2dd95SBruce Richardson 
65299a2dd95SBruce Richardson 
65399a2dd95SBruce Richardson #ifdef __cplusplus
65499a2dd95SBruce Richardson }
65599a2dd95SBruce Richardson #endif
65699a2dd95SBruce Richardson 
65799a2dd95SBruce Richardson #endif /* __INCLUDE_RTE_METER_H__ */
658