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