1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7 #include <rte_random.h>
8 #include <rte_common.h>
10 #ifdef __INTEL_COMPILER
11 #pragma warning(disable:2259) /* conversion may lose significant bits */
14 static int rte_red_init_done = 0; /**< Flag to indicate that global initialisation is done */
15 uint32_t rte_red_rand_val = 0; /**< Random value cache */
16 uint32_t rte_red_rand_seed = 0; /**< Seed for random number generation */
19 * table[i] = log2(1-Wq) * Scale * -1
22 uint16_t rte_red_log2_1_minus_Wq[RTE_RED_WQ_LOG2_NUM];
25 * table[i] = 2^(i/16) * Scale
27 uint16_t rte_red_pow2_frac_inv[16];
30 * @brief Initialize tables used to compute average
31 * queue size when queue is empty.
34 __rte_red_init_tables(void)
38 double table_size = 0.0;
40 scale = (double)(1 << RTE_RED_SCALING);
41 table_size = (double)(RTE_DIM(rte_red_pow2_frac_inv));
43 for (i = 0; i < RTE_DIM(rte_red_pow2_frac_inv); i++) {
46 rte_red_pow2_frac_inv[i] = (uint16_t) round(scale / pow(2, m / table_size));
51 RTE_ASSERT(RTE_RED_WQ_LOG2_NUM == RTE_DIM(rte_red_log2_1_minus_Wq));
53 for (i = RTE_RED_WQ_LOG2_MIN; i <= RTE_RED_WQ_LOG2_MAX; i++) {
55 double Wq = pow(2, -n);
56 uint32_t index = i - RTE_RED_WQ_LOG2_MIN;
58 rte_red_log2_1_minus_Wq[index] = (uint16_t) round(-1.0 * scale * log2(1.0 - Wq));
60 * Table entry of zero, corresponds to a Wq of zero
61 * which is not valid (avg would remain constant no
62 * matter how long the queue is empty). So we have
63 * to check for zero and round up to one.
65 if (rte_red_log2_1_minus_Wq[index] == 0) {
66 rte_red_log2_1_minus_Wq[index] = 1;
72 rte_red_rt_data_init(struct rte_red *red)
84 rte_red_config_init(struct rte_red_config *red_cfg,
85 const uint16_t wq_log2,
86 const uint16_t min_th,
87 const uint16_t max_th,
88 const uint16_t maxp_inv)
90 if (red_cfg == NULL) {
93 if (max_th > RTE_RED_MAX_TH_MAX) {
96 if (min_th >= max_th) {
99 if (wq_log2 > RTE_RED_WQ_LOG2_MAX) {
102 if (wq_log2 < RTE_RED_WQ_LOG2_MIN) {
105 if (maxp_inv < RTE_RED_MAXP_INV_MIN) {
108 if (maxp_inv > RTE_RED_MAXP_INV_MAX) {
113 * Initialize the RED module if not already done
115 if (!rte_red_init_done) {
116 rte_red_rand_seed = rte_rand();
117 rte_red_rand_val = rte_fast_rand();
118 __rte_red_init_tables();
119 rte_red_init_done = 1;
122 red_cfg->min_th = ((uint32_t) min_th) << (wq_log2 + RTE_RED_SCALING);
123 red_cfg->max_th = ((uint32_t) max_th) << (wq_log2 + RTE_RED_SCALING);
124 red_cfg->pa_const = (2 * (max_th - min_th) * maxp_inv) << RTE_RED_SCALING;
125 red_cfg->maxp_inv = maxp_inv;
126 red_cfg->wq_log2 = wq_log2;