Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright(c) 2010-2014 Intel Corporation 3 : : */ 4 : : 5 : : #ifndef __RTE_RED_H_INCLUDED__ 6 : : #define __RTE_RED_H_INCLUDED__ 7 : : 8 : : /** 9 : : * @file 10 : : * RTE Random Early Detection (RED) 11 : : */ 12 : : 13 : : #include <stdint.h> 14 : : #include <limits.h> 15 : : #include <rte_debug.h> 16 : : #include <rte_cycles.h> 17 : : #include <rte_branch_prediction.h> 18 : : #include <rte_random.h> 19 : : 20 : : #ifdef __cplusplus 21 : : extern "C" { 22 : : #endif 23 : : 24 : : #define RTE_RED_SCALING 10 /**< Fraction size for fixed-point */ 25 : : #define RTE_RED_S (1 << 22) /**< Packet size multiplied by number of leaf queues */ 26 : : #define RTE_RED_MAX_TH_MAX 1023 /**< Max threshold limit in fixed point format */ 27 : : #define RTE_RED_WQ_LOG2_MIN 1 /**< Min inverse filter weight value */ 28 : : #define RTE_RED_WQ_LOG2_MAX 12 /**< Max inverse filter weight value */ 29 : : #define RTE_RED_MAXP_INV_MIN 1 /**< Min inverse mark probability value */ 30 : : #define RTE_RED_MAXP_INV_MAX 255 /**< Max inverse mark probability value */ 31 : : #define RTE_RED_2POW16 (1<<16) /**< 2 power 16 */ 32 : : #define RTE_RED_INT16_NBITS (sizeof(uint16_t) * CHAR_BIT) 33 : : #define RTE_RED_WQ_LOG2_NUM (RTE_RED_WQ_LOG2_MAX - RTE_RED_WQ_LOG2_MIN + 1) 34 : : 35 : : /** 36 : : * Externs 37 : : */ 38 : : extern uint16_t rte_red_log2_1_minus_Wq[RTE_RED_WQ_LOG2_NUM]; 39 : : extern uint16_t rte_red_pow2_frac_inv[16]; 40 : : 41 : : /** 42 : : * RED configuration parameters passed by user 43 : : */ 44 : : struct rte_red_params { 45 : : uint16_t min_th; /**< Minimum threshold for queue (max_th) */ 46 : : uint16_t max_th; /**< Maximum threshold for queue (max_th) */ 47 : : uint16_t maxp_inv; /**< Inverse of packet marking probability maximum value (maxp = 1 / maxp_inv) */ 48 : : uint16_t wq_log2; /**< Negated log2 of queue weight (wq = 1 / (2 ^ wq_log2)) */ 49 : : }; 50 : : 51 : : /** 52 : : * RED configuration parameters 53 : : */ 54 : : struct rte_red_config { 55 : : uint32_t min_th; /**< min_th scaled in fixed-point format */ 56 : : uint32_t max_th; /**< max_th scaled in fixed-point format */ 57 : : uint32_t pa_const; /**< Precomputed constant value used for pa calculation (scaled in fixed-point format) */ 58 : : uint8_t maxp_inv; /**< maxp_inv */ 59 : : uint8_t wq_log2; /**< wq_log2 */ 60 : : }; 61 : : 62 : : /** 63 : : * RED run-time data 64 : : */ 65 : : struct rte_red { 66 : : uint32_t avg; /**< Average queue size (avg), scaled in fixed-point format */ 67 : : uint32_t count; /**< Number of packets since last marked packet (count) */ 68 : : uint64_t q_time; /**< Start of the queue idle time (q_time) */ 69 : : }; 70 : : 71 : : /** 72 : : * @brief Initialises run-time data 73 : : * 74 : : * @param red [in,out] data pointer to RED runtime data 75 : : * 76 : : * @return Operation status 77 : : * @retval 0 success 78 : : * @retval !0 error 79 : : */ 80 : : int 81 : : rte_red_rt_data_init(struct rte_red *red); 82 : : 83 : : /** 84 : : * @brief Configures a single RED configuration parameter structure. 85 : : * 86 : : * @param red_cfg [in,out] config pointer to a RED configuration parameter structure 87 : : * @param wq_log2 [in] log2 of the filter weight, valid range is: 88 : : * RTE_RED_WQ_LOG2_MIN <= wq_log2 <= RTE_RED_WQ_LOG2_MAX 89 : : * @param min_th [in] queue minimum threshold in number of packets 90 : : * @param max_th [in] queue maximum threshold in number of packets 91 : : * @param maxp_inv [in] inverse maximum mark probability 92 : : * 93 : : * @return Operation status 94 : : * @retval 0 success 95 : : * @retval !0 error 96 : : */ 97 : : int 98 : : rte_red_config_init(struct rte_red_config *red_cfg, 99 : : const uint16_t wq_log2, 100 : : const uint16_t min_th, 101 : : const uint16_t max_th, 102 : : const uint16_t maxp_inv); 103 : : 104 : : /** 105 : : * @brief calculate factor to scale average queue size when queue 106 : : * becomes empty 107 : : * 108 : : * @param wq_log2 [in] where EWMA filter weight wq = 1/(2 ^ wq_log2) 109 : : * @param m [in] exponent in the computed value (1 - wq) ^ m 110 : : * 111 : : * @return computed value 112 : : * @retval ((1 - wq) ^ m) scaled in fixed-point format 113 : : */ 114 : : static inline uint16_t 115 : 0 : __rte_red_calc_qempty_factor(uint8_t wq_log2, uint16_t m) 116 : : { 117 : : uint32_t n = 0; 118 : : uint32_t f = 0; 119 : : 120 : : /** 121 : : * Basic math tells us that: 122 : : * a^b = 2^(b * log2(a) ) 123 : : * 124 : : * in our case: 125 : : * a = (1-Wq) 126 : : * b = m 127 : : * Wq = 1/ (2^log2n) 128 : : * 129 : : * So we are computing this equation: 130 : : * factor = 2 ^ ( m * log2(1-Wq)) 131 : : * 132 : : * First we are computing: 133 : : * n = m * log2(1-Wq) 134 : : * 135 : : * To avoid dealing with signed numbers log2 values are positive 136 : : * but they should be negative because (1-Wq) is always < 1. 137 : : * Contents of log2 table values are also scaled for precision. 138 : : */ 139 : : 140 : 0 : n = m * rte_red_log2_1_minus_Wq[wq_log2 - RTE_RED_WQ_LOG2_MIN]; 141 : : 142 : : /** 143 : : * The tricky part is computing 2^n, for this I split n into 144 : : * integer part and fraction part. 145 : : * f - is fraction part of n 146 : : * n - is integer part of original n 147 : : * 148 : : * Now using basic math we compute 2^n: 149 : : * 2^(f+n) = 2^f * 2^n 150 : : * 2^f - we use lookup table 151 : : * 2^n - can be replaced with bit shift right operations 152 : : */ 153 : : 154 : 0 : f = (n >> 6) & 0xf; 155 : 0 : n >>= 10; 156 : : 157 [ # # ]: 0 : if (n < RTE_RED_SCALING) { 158 : : /* When n == 0, no rounding or shifting needed. 159 : : * For n > 0, add 2^(n-1) for rounding before right shift. 160 : : * This avoids UB from (1 << -1) when n == 0. 161 : : */ 162 [ # # ]: 0 : if (n == 0) 163 : 0 : return (uint16_t) rte_red_pow2_frac_inv[f]; 164 : 0 : return (uint16_t) ((rte_red_pow2_frac_inv[f] + (1 << (n - 1))) >> n); 165 : : } 166 : : 167 : : return 0; 168 : : } 169 : : 170 : : /** 171 : : * @brief Updates queue average in condition when queue is empty 172 : : * 173 : : * Note: packet is never dropped in this particular case. 174 : : * 175 : : * @param red_cfg [in] config pointer to a RED configuration parameter structure 176 : : * @param red [in,out] data pointer to RED runtime data 177 : : * @param time [in] current time stamp 178 : : * 179 : : * @return Operation status 180 : : * @retval 0 enqueue the packet 181 : : * @retval 1 drop the packet based on max threshold criterion 182 : : * @retval 2 drop the packet based on mark probability criterion 183 : : */ 184 : : static inline int 185 : 0 : rte_red_enqueue_empty(const struct rte_red_config *red_cfg, 186 : : struct rte_red *red, 187 : : const uint64_t time) 188 : : { 189 : : uint64_t time_diff = 0, m = 0; 190 : : 191 : : RTE_ASSERT(red_cfg != NULL); 192 : : RTE_ASSERT(red != NULL); 193 : : 194 : 0 : red->count ++; 195 : : 196 : : /** 197 : : * We compute avg but we don't compare avg against 198 : : * min_th or max_th, nor calculate drop probability 199 : : */ 200 : 0 : time_diff = time - red->q_time; 201 : : 202 : : /** 203 : : * m is the number of packets that might have arrived while the queue was empty. 204 : : * In this case we have time stamps provided by scheduler in byte units (bytes 205 : : * transmitted on network port). Such time stamp translates into time units as 206 : : * port speed is fixed but such approach simplifies the code. 207 : : */ 208 : 0 : m = time_diff / RTE_RED_S; 209 : : 210 : : /** 211 : : * Check that m will fit into 16-bit unsigned integer 212 : : */ 213 [ # # ]: 0 : if (m >= RTE_RED_2POW16) { 214 : 0 : red->avg = 0; 215 : : } else { 216 : 0 : red->avg = (red->avg >> RTE_RED_SCALING) * __rte_red_calc_qempty_factor(red_cfg->wq_log2, (uint16_t) m); 217 : : } 218 : : 219 : 0 : return 0; 220 : : } 221 : : 222 : : /** 223 : : * Drop probability (Sally Floyd and Van Jacobson): 224 : : * 225 : : * pb = (1 / maxp_inv) * (avg - min_th) / (max_th - min_th) 226 : : * pa = pb / (2 - count * pb) 227 : : * 228 : : * 229 : : * (1 / maxp_inv) * (avg - min_th) 230 : : * --------------------------------- 231 : : * max_th - min_th 232 : : * pa = ----------------------------------------------- 233 : : * count * (1 / maxp_inv) * (avg - min_th) 234 : : * 2 - ----------------------------------------- 235 : : * max_th - min_th 236 : : * 237 : : * 238 : : * avg - min_th 239 : : * pa = ----------------------------------------------------------- 240 : : * 2 * (max_th - min_th) * maxp_inv - count * (avg - min_th) 241 : : * 242 : : * 243 : : * We define pa_const as: pa_const = 2 * (max_th - min_th) * maxp_inv. Then: 244 : : * 245 : : * 246 : : * avg - min_th 247 : : * pa = ----------------------------------- 248 : : * pa_const - count * (avg - min_th) 249 : : */ 250 : : 251 : : /** 252 : : * @brief make a decision to drop or enqueue a packet based on mark probability 253 : : * criteria 254 : : * 255 : : * @param red_cfg [in] config pointer to structure defining RED parameters 256 : : * @param red [in,out] data pointer to RED runtime data 257 : : * 258 : : * @return operation status 259 : : * @retval 0 enqueue the packet 260 : : * @retval 1 drop the packet 261 : : */ 262 : : static inline int 263 : 0 : __rte_red_drop(const struct rte_red_config *red_cfg, struct rte_red *red) 264 : : { 265 : : uint32_t pa_num = 0; /* numerator of drop-probability */ 266 : : uint32_t pa_den = 0; /* denominator of drop-probability */ 267 : : uint32_t pa_num_count = 0; 268 : : 269 : 0 : pa_num = (red->avg - red_cfg->min_th) >> (red_cfg->wq_log2); 270 : : 271 : 0 : pa_num_count = red->count * pa_num; 272 : : 273 [ # # ]: 0 : if (red_cfg->pa_const <= pa_num_count) 274 : : return 1; 275 : : 276 : 0 : pa_den = red_cfg->pa_const - pa_num_count; 277 : : 278 [ # # ]: 0 : if (unlikely((rte_rand() % pa_den) < pa_num)) 279 : 0 : return 1; 280 : : 281 : : /* No drop */ 282 : : return 0; 283 : : } 284 : : 285 : : /** 286 : : * @brief Decides if new packet should be enqueued or dropped in queue non-empty case 287 : : * 288 : : * @param red_cfg [in] config pointer to a RED configuration parameter structure 289 : : * @param red [in,out] data pointer to RED runtime data 290 : : * @param q [in] current queue size (measured in packets) 291 : : * 292 : : * @return Operation status 293 : : * @retval 0 enqueue the packet 294 : : * @retval 1 drop the packet based on max threshold criterion 295 : : * @retval 2 drop the packet based on mark probability criterion 296 : : */ 297 : : static inline int 298 : 0 : rte_red_enqueue_nonempty(const struct rte_red_config *red_cfg, 299 : : struct rte_red *red, 300 : : const unsigned q) 301 : : { 302 : : RTE_ASSERT(red_cfg != NULL); 303 : : RTE_ASSERT(red != NULL); 304 : : 305 : : /** 306 : : * EWMA filter (Sally Floyd and Van Jacobson): 307 : : * avg = (1 - wq) * avg + wq * q 308 : : * avg = avg + q * wq - avg * wq 309 : : * 310 : : * We select: wq = 2^(-n). Let scaled version of avg be: avg_s = avg * 2^(N+n). We get: 311 : : * avg_s = avg_s + q * 2^N - avg_s * 2^(-n) 312 : : * 313 : : * By using shift left/right operations, we get: 314 : : * avg_s = avg_s + (q << N) - (avg_s >> n) 315 : : * avg_s += (q << N) - (avg_s >> n) 316 : : */ 317 : : 318 : : /* avg update */ 319 : 0 : red->avg += (q << RTE_RED_SCALING) - (red->avg >> red_cfg->wq_log2); 320 : : 321 : : /* avg < min_th: do not mark the packet */ 322 [ # # ]: 0 : if (red->avg < red_cfg->min_th) { 323 : 0 : red->count ++; 324 : 0 : return 0; 325 : : } 326 : : 327 : : /* min_th <= avg < max_th: mark the packet with pa probability */ 328 [ # # ]: 0 : if (red->avg < red_cfg->max_th) { 329 [ # # ]: 0 : if (!__rte_red_drop(red_cfg, red)) { 330 : 0 : red->count ++; 331 : 0 : return 0; 332 : : } 333 : : 334 : 0 : red->count = 0; 335 : 0 : return 2; 336 : : } 337 : : 338 : : /* max_th <= avg: always mark the packet */ 339 : 0 : red->count = 0; 340 : 0 : return 1; 341 : : } 342 : : 343 : : /** 344 : : * @brief Decides if new packet should be enqueued or dropped 345 : : * Updates run time data based on new queue size value. 346 : : * Based on new queue average and RED configuration parameters 347 : : * gives verdict whether to enqueue or drop the packet. 348 : : * 349 : : * @param red_cfg [in] config pointer to a RED configuration parameter structure 350 : : * @param red [in,out] data pointer to RED runtime data 351 : : * @param q [in] updated queue size in packets 352 : : * @param time [in] current time stamp 353 : : * 354 : : * @return Operation status 355 : : * @retval 0 enqueue the packet 356 : : * @retval 1 drop the packet based on max threshold criteria 357 : : * @retval 2 drop the packet based on mark probability criteria 358 : : */ 359 : : static inline int 360 : 0 : rte_red_enqueue(const struct rte_red_config *red_cfg, 361 : : struct rte_red *red, 362 : : const unsigned q, 363 : : const uint64_t time) 364 : : { 365 : : RTE_ASSERT(red_cfg != NULL); 366 : : RTE_ASSERT(red != NULL); 367 : : 368 [ # # ]: 0 : if (q != 0) { 369 : 0 : return rte_red_enqueue_nonempty(red_cfg, red, q); 370 : : } else { 371 : 0 : return rte_red_enqueue_empty(red_cfg, red, time); 372 : : } 373 : : } 374 : : 375 : : /** 376 : : * @brief Callback to records time that queue became empty 377 : : * 378 : : * @param red [in,out] data pointer to RED runtime data 379 : : * @param time [in] current time stamp 380 : : */ 381 : : static inline void 382 : : rte_red_mark_queue_empty(struct rte_red *red, const uint64_t time) 383 : : { 384 : 0 : red->q_time = time; 385 : 0 : } 386 : : 387 : : #ifdef __cplusplus 388 : : } 389 : : #endif 390 : : 391 : : #endif /* __RTE_RED_H_INCLUDED__ */