1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
10 #include <rte_lcore.h>
11 #include <rte_memzone.h>
12 #include <rte_metrics.h>
13 #include <rte_bitrate.h>
15 #include "sample_packet_forward.h"
18 #define BIT_NUM_PACKETS 10
21 static uint16_t portid;
22 static struct rte_stats_bitrates *bitrate_data;
23 static struct rte_ring *ring;
25 /* To test whether rte_stats_bitrate_create is successful */
27 test_stats_bitrate_create(void)
29 bitrate_data = rte_stats_bitrate_create();
30 TEST_ASSERT(bitrate_data != NULL, "rte_stats_bitrate_create failed");
35 /* To test free the resources from bitrate_create test */
37 test_stats_bitrate_free(void)
41 rte_stats_bitrate_free(bitrate_data);
43 ret = rte_metrics_deinit();
44 TEST_ASSERT(ret >= 0, "Test Failed: rte_metrics_deinit failed");
49 /* To test bit rate registration */
51 test_stats_bitrate_reg(void)
55 /* Test to register bit rate without metrics init */
56 ret = rte_stats_bitrate_reg(bitrate_data);
57 TEST_ASSERT(ret < 0, "Test Failed: rte_stats_bitrate_reg succeeded "
58 "without metrics init, ret:%d", ret);
60 /* Metrics initialization */
61 rte_metrics_init(rte_socket_id());
62 /* Test to register bit rate after metrics init */
63 ret = rte_stats_bitrate_reg(bitrate_data);
64 TEST_ASSERT((ret >= 0), "Test Failed: rte_stats_bitrate_reg %d", ret);
69 /* To test the bit rate registration with invalid pointer */
71 test_stats_bitrate_reg_invalidpointer(void)
75 ret = rte_stats_bitrate_reg(NULL);
76 TEST_ASSERT(ret < 0, "Test Failed: Expected failure < 0 but "
82 /* To test bit rate calculation with invalid bit rate data pointer */
84 test_stats_bitrate_calc_invalid_bitrate_data(void)
88 ret = rte_stats_bitrate_calc(NULL, portid);
89 TEST_ASSERT(ret < 0, "Test Failed: rte_stats_bitrate_calc "
95 /* To test the bit rate calculation with invalid portid
96 * (higher than max ports)
99 test_stats_bitrate_calc_invalid_portid_1(void)
103 ret = rte_stats_bitrate_calc(bitrate_data, 33);
104 TEST_ASSERT(ret == -ENODEV, "Test Failed: Expected -%d for higher "
105 "portid rte_stats_bitrate_calc ret:%d", ENODEV, ret);
110 /* To test the bit rate calculation with invalid portid (lesser than 0) */
112 test_stats_bitrate_calc_invalid_portid_2(void)
116 ret = rte_stats_bitrate_calc(bitrate_data, -1);
117 TEST_ASSERT(ret == -ENODEV, "Test Failed: Expected -%d for invalid "
118 "portid rte_stats_bitrate_calc ret:%d", ENODEV, ret);
123 /* To test the bit rate calculation with non-existing portid */
125 test_stats_bitrate_calc_non_existing_portid(void)
129 ret = rte_stats_bitrate_calc(bitrate_data, 31);
130 TEST_ASSERT(ret == -ENODEV, "Test Failed: Expected -%d for "
131 "non-existing portid rte_stats_bitrate_calc ret:%d",
137 /* To test the bit rate calculation with valid bit rate data, valid portid */
139 test_stats_bitrate_calc(void)
143 ret = rte_stats_bitrate_calc(bitrate_data, portid);
144 TEST_ASSERT(ret >= 0, "Test Failed: Expected >=0 for valid portid "
145 "rte_stats_bitrate_calc ret:%d", ret);
151 test_bit_packet_forward(void)
154 struct rte_mbuf *pbuf[BIT_NUM_PACKETS] = { };
155 struct rte_mempool *mp;
156 char poolname[] = "mbuf_pool";
157 ret = test_get_mbuf_from_pool(&mp, pbuf, poolname);
159 printf("allocate mbuf pool Failed\n");
162 ret = test_packet_forward(pbuf, portid, QUEUE_ID);
164 printf("send pkts Failed\n");
165 test_put_mbuf_to_pool(mp, pbuf);
171 test_bit_ring_setup(void)
173 test_ring_setup(&ring, &portid);
174 printf("port in ring setup : %d\n", portid);
180 test_bit_ring_free(void)
182 test_ring_free(ring);
183 test_vdev_uninit("net_ring_net_ringa");
184 rte_memzone_free(rte_memzone_lookup("RTE_METRICS"));
188 unit_test_suite bitratestats_testsuite = {
189 .suite_name = "BitRate Stats Unit Test Suite",
190 .setup = test_bit_ring_setup,
191 .teardown = test_bit_ring_free,
193 /* TEST CASE 1: Test to create bit rate data */
194 TEST_CASE(test_stats_bitrate_create),
196 /* TEST CASE 2: Test to register bit rate metrics
197 * without metrics init and after metrics init
199 TEST_CASE(test_stats_bitrate_reg),
201 /* TEST CASE 3: Test to register bit rate metrics
202 * with invalid bit rate data
204 TEST_CASE(test_stats_bitrate_reg_invalidpointer),
206 /* TEST CASE 4: Test to calculate bit rate data metrics
207 * with invalid bit rate data
209 TEST_CASE(test_stats_bitrate_calc_invalid_bitrate_data),
211 /* TEST CASE 5: Test to calculate bit rate data metrics
212 * with portid exceeding the max ports
214 TEST_CASE(test_stats_bitrate_calc_invalid_portid_1),
216 /* TEST CASE 6: Test to calculate bit rate data metrics
217 * with portid less than 0
219 TEST_CASE(test_stats_bitrate_calc_invalid_portid_2),
221 /* TEST CASE 7: Test to calculate bit rate data metrics
222 * with non-existing portid
224 TEST_CASE(test_stats_bitrate_calc_non_existing_portid),
226 /* TEST CASE 8: Test to calculate bit rate data metrics
227 * with valid portid, valid bit rate data
229 TEST_CASE_ST(test_bit_packet_forward, NULL,
230 test_stats_bitrate_calc),
231 /* TEST CASE 9: Test to do the cleanup w.r.t create */
232 TEST_CASE(test_stats_bitrate_free),
238 test_bitratestats(void)
240 return unit_test_suite_runner(&bitratestats_testsuite);
242 REGISTER_TEST_COMMAND(bitratestats_autotest, test_bitratestats);