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