32b1b0fc0e19994789b26ddc8d9508317230be52
[dpdk.git] / app / test / test_bitratestats.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8
9 #include <errno.h>
10 #include <rte_lcore.h>
11 #include <rte_memzone.h>
12 #include <rte_metrics.h>
13 #include <rte_bitrate.h>
14
15 #include "sample_packet_forward.h"
16 #include "test.h"
17
18 #define BIT_NUM_PACKETS 10
19 #define QUEUE_ID 0
20
21 uint16_t portid;
22 struct rte_stats_bitrates *bitrate_data;
23 struct rte_ring *ring;
24
25 /* To test whether rte_stats_bitrate_create is successful */
26 static int
27 test_stats_bitrate_create(void)
28 {
29         bitrate_data = rte_stats_bitrate_create();
30         TEST_ASSERT(bitrate_data != NULL, "rte_stats_bitrate_create failed");
31
32         return TEST_SUCCESS;
33 }
34
35 /* To test bit rate registration */
36 static int
37 test_stats_bitrate_reg(void)
38 {
39         int ret = 0;
40
41         /* Test to register bit rate without metrics init */
42         ret = rte_stats_bitrate_reg(bitrate_data);
43         TEST_ASSERT(ret < 0, "Test Failed: rte_stats_bitrate_reg succeeded "
44                         "without metrics init, ret:%d", ret);
45
46         /* Metrics initialization */
47         rte_metrics_init(rte_socket_id());
48         /* Test to register bit rate after metrics init */
49         ret = rte_stats_bitrate_reg(bitrate_data);
50         TEST_ASSERT((ret >= 0), "Test Failed: rte_stats_bitrate_reg %d", ret);
51
52         return TEST_SUCCESS;
53 }
54
55 /* To test the bit rate registration with invalid pointer */
56 static int
57 test_stats_bitrate_reg_invalidpointer(void)
58 {
59         int ret = 0;
60
61         ret = rte_stats_bitrate_reg(NULL);
62         TEST_ASSERT(ret < 0, "Test Failed: Expected failure < 0 but "
63                         "got %d", ret);
64
65         return TEST_SUCCESS;
66 }
67
68 /* To test bit rate calculation with invalid bit rate data pointer */
69 static int
70 test_stats_bitrate_calc_invalid_bitrate_data(void)
71 {
72         int ret = 0;
73
74         ret = rte_stats_bitrate_calc(NULL, portid);
75         TEST_ASSERT(ret < 0, "Test Failed: rte_stats_bitrate_calc "
76                         "ret:%d", ret);
77
78         return TEST_SUCCESS;
79 }
80
81 /* To test the bit rate calculation with invalid portid
82  * (higher than max ports)
83  */
84 static int
85 test_stats_bitrate_calc_invalid_portid_1(void)
86 {
87         int ret = 0;
88
89         ret = rte_stats_bitrate_calc(bitrate_data, 33);
90         TEST_ASSERT(ret == -EINVAL, "Test Failed: Expected -%d for higher "
91                         "portid rte_stats_bitrate_calc ret:%d", EINVAL, ret);
92
93         return TEST_SUCCESS;
94 }
95
96 /* To test the bit rate calculation with invalid portid (lesser than 0) */
97 static int
98 test_stats_bitrate_calc_invalid_portid_2(void)
99 {
100         int ret = 0;
101
102         ret = rte_stats_bitrate_calc(bitrate_data, -1);
103         TEST_ASSERT(ret == -EINVAL, "Test Failed: Expected -%d for invalid "
104                         "portid rte_stats_bitrate_calc ret:%d", EINVAL, ret);
105
106         return TEST_SUCCESS;
107 }
108
109 /* To test the bit rate calculation with non-existing portid */
110 static int
111 test_stats_bitrate_calc_non_existing_portid(void)
112 {
113         int ret = 0;
114
115         ret = rte_stats_bitrate_calc(bitrate_data, 31);
116         TEST_ASSERT(ret ==  -EINVAL, "Test Failed: Expected -%d for "
117                         "non-existing portid rte_stats_bitrate_calc ret:%d",
118                         EINVAL, ret);
119
120         return TEST_SUCCESS;
121 }
122
123 /* To test the bit rate calculation with valid bit rate data, valid portid */
124 static int
125 test_stats_bitrate_calc(void)
126 {
127         int ret = 0;
128
129         ret = rte_stats_bitrate_calc(bitrate_data, portid);
130         TEST_ASSERT(ret >= 0, "Test Failed: Expected >=0 for valid portid "
131                         "rte_stats_bitrate_calc ret:%d", ret);
132
133         return TEST_SUCCESS;
134 }
135
136 static int
137 test_bit_packet_forward(void)
138 {
139         int ret;
140         struct rte_mbuf *pbuf[BIT_NUM_PACKETS] = { };
141         struct rte_mempool *mp;
142         char poolname[] = "mbuf_pool";
143         ret = test_get_mbuf_from_pool(&mp, pbuf, poolname);
144         if (ret < 0) {
145                 printf("allocate mbuf pool Failed\n");
146                 return TEST_FAILED;
147         }
148         ret = test_packet_forward(pbuf, portid, QUEUE_ID);
149         if (ret < 0)
150                 printf("send pkts Failed\n");
151         test_put_mbuf_to_pool(mp, pbuf);
152
153         return TEST_SUCCESS;
154 }
155
156 static int
157 test_bit_ring_setup(void)
158 {
159         test_ring_setup(&ring, &portid);
160         printf("port in ring setup : %d\n", portid);
161
162         return TEST_SUCCESS;
163 }
164
165 static void
166 test_bit_ring_free(void)
167 {
168         test_ring_free(ring);
169         test_vdev_uninit("net_ring_net_ringa");
170         rte_memzone_free(rte_memzone_lookup("RTE_METRICS"));
171 }
172
173 static struct
174 unit_test_suite bitratestats_testsuite  = {
175         .suite_name = "BitRate Stats Unit Test Suite",
176         .setup = test_bit_ring_setup,
177         .teardown = test_bit_ring_free,
178         .unit_test_cases = {
179                 /* TEST CASE 1: Test to create bit rate data */
180                 TEST_CASE(test_stats_bitrate_create),
181
182                 /* TEST CASE 2: Test to register bit rate metrics
183                  * without metrics init and after metrics init
184                  */
185                 TEST_CASE(test_stats_bitrate_reg),
186
187                 /* TEST CASE 3: Test to register bit rate metrics
188                  * with invalid bit rate data
189                  */
190                 TEST_CASE(test_stats_bitrate_reg_invalidpointer),
191
192                 /* TEST CASE 4: Test to calculate bit rate data metrics
193                  * with invalid bit rate data
194                  */
195                 TEST_CASE(test_stats_bitrate_calc_invalid_bitrate_data),
196
197                 /* TEST CASE 5: Test to calculate bit rate data metrics
198                  * with portid exceeding the max ports
199                  */
200                 TEST_CASE(test_stats_bitrate_calc_invalid_portid_1),
201
202                 /* TEST CASE 6: Test to calculate bit rate data metrics
203                  * with portid less than 0
204                  */
205                 TEST_CASE(test_stats_bitrate_calc_invalid_portid_2),
206
207                 /* TEST CASE 7: Test to calculate bit rate data metrics
208                  * with non-existing portid
209                  */
210                 TEST_CASE(test_stats_bitrate_calc_non_existing_portid),
211
212                 /* TEST CASE 8: Test to calculate bit rate data metrics
213                  * with valid portid, valid bit rate data
214                  */
215                 TEST_CASE_ST(test_bit_packet_forward, NULL,
216                                 test_stats_bitrate_calc),
217                 TEST_CASES_END()
218         }
219 };
220
221 static int
222 test_bitratestats(void)
223 {
224         return unit_test_suite_runner(&bitratestats_testsuite);
225 }
226 REGISTER_TEST_COMMAND(bitratestats_autotest, test_bitratestats);