doc: whitespace changes in licenses
[dpdk.git] / lib / librte_sched / rte_red.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <math.h>
35 #include "rte_red.h"
36 #include <rte_random.h>
37
38 #ifdef __INTEL_COMPILER
39 #pragma warning(disable:2259) /* conversion may lose significant bits */
40 #endif
41
42 #define DIM(x) (sizeof(x)/sizeof(x[0]))
43
44 static int rte_red_init_done = 0;     /**< Flag to indicate that global initialisation is done */
45 uint32_t rte_red_rand_val = 0;        /**< Random value cache */
46 uint32_t rte_red_rand_seed = 0;       /**< Seed for random number generation */
47
48 /**
49  * table[i] = log2(1-Wq) * Scale * -1
50  *       Wq = 1/(2^i)
51  */
52 uint16_t rte_red_log2_1_minus_Wq[RTE_RED_WQ_LOG2_NUM];
53
54 /**
55  * table[i] = 2^(i/16) * Scale
56  */
57 uint16_t rte_red_pow2_frac_inv[16];
58
59 /**
60  * @brief Initialize tables used to compute average
61  *        queue size when queue is empty.
62  */
63 static void
64 __rte_red_init_tables(void)
65 {
66         uint32_t i = 0;
67         double scale = 0.0;
68         double table_size = 0.0;
69
70         scale = (double)(1 << RTE_RED_SCALING);
71         table_size = (double)(DIM(rte_red_pow2_frac_inv));
72
73         for (i = 0; i < DIM(rte_red_pow2_frac_inv); i++) {
74                 double m = (double)i;
75                 
76                 rte_red_pow2_frac_inv[i] = (uint16_t) round(scale / pow(2, m / table_size));
77         }
78         
79         scale = 1024.0;
80
81         RTE_RED_ASSERT(RTE_RED_WQ_LOG2_NUM == DIM(rte_red_log2_1_minus_Wq));
82
83         for (i = RTE_RED_WQ_LOG2_MIN; i <= RTE_RED_WQ_LOG2_MAX; i++) {
84                 double n = (double)i;
85                 double Wq = pow(2, -n);
86                 uint32_t index = i - RTE_RED_WQ_LOG2_MIN;
87                 
88                 rte_red_log2_1_minus_Wq[index] = (uint16_t) round(-1.0 * scale * log2(1.0 - Wq));
89                 /**
90                 * Table entry of zero, corresponds to a Wq of zero
91                 * which is not valid (avg would remain constant no
92                 * matter how long the queue is empty). So we have
93                 * to check for zero and round up to one.
94                 */
95                 if (rte_red_log2_1_minus_Wq[index] == 0) {
96                         rte_red_log2_1_minus_Wq[index] = 1;
97                 }
98         }
99 }
100
101 int
102 rte_red_rt_data_init(struct rte_red *red)
103 {
104         if (red == NULL)
105                 return -1;
106
107         red->avg = 0;
108         red->count = 0;
109         red->q_time = 0;
110         return 0;
111 }
112
113 int
114 rte_red_config_init(struct rte_red_config *red_cfg,
115         const uint16_t wq_log2,
116         const uint16_t min_th,
117         const uint16_t max_th,
118         const uint16_t maxp_inv)
119 {
120         if (red_cfg == NULL) {
121                 return -1;
122         }
123         if (max_th > RTE_RED_MAX_TH_MAX) {
124                 return -2;
125         }
126         if (min_th >= max_th) {
127                 return -3;
128         }
129         if (wq_log2 > RTE_RED_WQ_LOG2_MAX) {
130                 return -4;
131         }
132         if (wq_log2 < RTE_RED_WQ_LOG2_MIN) {
133                 return -5;
134         }
135         if (maxp_inv < RTE_RED_MAXP_INV_MIN) {
136                 return -6;
137         }
138         if (maxp_inv > RTE_RED_MAXP_INV_MAX) {
139                 return -7;
140         }
141         
142         /**
143          *  Initialize the RED module if not already done
144          */
145         if (!rte_red_init_done) {
146                 rte_red_rand_seed = rte_rand();
147                 rte_red_rand_val = rte_fast_rand();
148                 __rte_red_init_tables();
149                 rte_red_init_done = 1;
150         }
151
152         red_cfg->min_th = ((uint32_t) min_th) << (wq_log2 + RTE_RED_SCALING);
153         red_cfg->max_th = ((uint32_t) max_th) << (wq_log2 + RTE_RED_SCALING);
154         red_cfg->pa_const = (2 * (max_th - min_th) * maxp_inv) << RTE_RED_SCALING;
155         red_cfg->maxp_inv = maxp_inv;
156         red_cfg->wq_log2 = wq_log2;
157
158         return 0;
159 }