summaryrefslogtreecommitdiffstats
path: root/lib/librte_sched/rte_red.c
blob: 45a452f68e87fcbdb494fed9871762fa393a5363 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2014 Intel Corporation
 */

#include <math.h>
#include "rte_red.h"
#include <rte_random.h>
#include <rte_common.h>

#ifdef __INTEL_COMPILER
#pragma warning(disable:2259) /* conversion may lose significant bits */
#endif

static int rte_red_init_done = 0;     /**< Flag to indicate that global initialisation is done */
uint32_t rte_red_rand_val = 0;        /**< Random value cache */
uint32_t rte_red_rand_seed = 0;       /**< Seed for random number generation */

/**
 * table[i] = log2(1-Wq) * Scale * -1
 *       Wq = 1/(2^i)
 */
uint16_t rte_red_log2_1_minus_Wq[RTE_RED_WQ_LOG2_NUM];

/**
 * table[i] = 2^(i/16) * Scale
 */
uint16_t rte_red_pow2_frac_inv[16];

/**
 * @brief Initialize tables used to compute average
 *        queue size when queue is empty.
 */
static void
__rte_red_init_tables(void)
{
	uint32_t i = 0;
	double scale = 0.0;
	double table_size = 0.0;

	scale = (double)(1 << RTE_RED_SCALING);
	table_size = (double)(RTE_DIM(rte_red_pow2_frac_inv));

	for (i = 0; i < RTE_DIM(rte_red_pow2_frac_inv); i++) {
		double m = (double)i;

		rte_red_pow2_frac_inv[i] = (uint16_t) round(scale / pow(2, m / table_size));
	}

	scale = 1024.0;

	RTE_ASSERT(RTE_RED_WQ_LOG2_NUM == RTE_DIM(rte_red_log2_1_minus_Wq));

	for (i = RTE_RED_WQ_LOG2_MIN; i <= RTE_RED_WQ_LOG2_MAX; i++) {
		double n = (double)i;
		double Wq = pow(2, -n);
		uint32_t index = i - RTE_RED_WQ_LOG2_MIN;

		rte_red_log2_1_minus_Wq[index] = (uint16_t) round(-1.0 * scale * log2(1.0 - Wq));
		/**
		* Table entry of zero, corresponds to a Wq of zero
		* which is not valid (avg would remain constant no
		* matter how long the queue is empty). So we have
		* to check for zero and round up to one.
		*/
		if (rte_red_log2_1_minus_Wq[index] == 0) {
			rte_red_log2_1_minus_Wq[index] = 1;
		}
	}
}

int
rte_red_rt_data_init(struct rte_red *red)
{
	if (red == NULL)
		return -1;

	red->avg = 0;
	red->count = 0;
	red->q_time = 0;
	return 0;
}

int
rte_red_config_init(struct rte_red_config *red_cfg,
	const uint16_t wq_log2,
	const uint16_t min_th,
	const uint16_t max_th,
	const uint16_t maxp_inv)
{
	if (red_cfg == NULL) {
		return -1;
	}
	if (max_th > RTE_RED_MAX_TH_MAX) {
		return -2;
	}
	if (min_th >= max_th) {
		return -3;
	}
	if (wq_log2 > RTE_RED_WQ_LOG2_MAX) {
		return -4;
	}
	if (wq_log2 < RTE_RED_WQ_LOG2_MIN) {
		return -5;
	}
	if (maxp_inv < RTE_RED_MAXP_INV_MIN) {
		return -6;
	}
	if (maxp_inv > RTE_RED_MAXP_INV_MAX) {
		return -7;
	}

	/**
	 *  Initialize the RED module if not already done
	 */
	if (!rte_red_init_done) {
		rte_red_rand_seed = rte_rand();
		rte_red_rand_val = rte_fast_rand();
		__rte_red_init_tables();
		rte_red_init_done = 1;
	}

	red_cfg->min_th = ((uint32_t) min_th) << (wq_log2 + RTE_RED_SCALING);
	red_cfg->max_th = ((uint32_t) max_th) << (wq_log2 + RTE_RED_SCALING);
	red_cfg->pa_const = (2 * (max_th - min_th) * maxp_inv) << RTE_RED_SCALING;
	red_cfg->maxp_inv = maxp_inv;
	red_cfg->wq_log2 = wq_log2;

	return 0;
}