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