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>
14 #include <rte_ethdev.h>
16 #include "sample_packet_forward.h"
19 #define BIT_NUM_PACKETS 10
22 static uint16_t portid;
23 static struct rte_stats_bitrates *bitrate_data;
24 static struct rte_ring *ring;
26 /* To test whether rte_stats_bitrate_create is successful */
28 test_stats_bitrate_create(void)
30 bitrate_data = rte_stats_bitrate_create();
31 TEST_ASSERT(bitrate_data != NULL, "rte_stats_bitrate_create failed");
36 /* To test free the resources from bitrate_create test */
38 test_stats_bitrate_free(void)
42 rte_stats_bitrate_free(bitrate_data);
44 ret = rte_metrics_deinit();
45 TEST_ASSERT(ret >= 0, "Test Failed: rte_metrics_deinit failed");
50 /* To test bit rate registration */
52 test_stats_bitrate_reg(void)
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);
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);
70 /* To test the bit rate registration with invalid pointer */
72 test_stats_bitrate_reg_invalidpointer(void)
76 ret = rte_stats_bitrate_reg(NULL);
77 TEST_ASSERT(ret < 0, "Test Failed: Expected failure < 0 but "
83 /* To test bit rate calculation with invalid bit rate data pointer */
85 test_stats_bitrate_calc_invalid_bitrate_data(void)
89 ret = rte_stats_bitrate_calc(NULL, portid);
90 TEST_ASSERT(ret < 0, "Test Failed: rte_stats_bitrate_calc "
96 /* To test the bit rate calculation with invalid portid
97 * (higher than max ports)
100 test_stats_bitrate_calc_invalid_portid_1(void)
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);
111 /* To test the bit rate calculation with invalid portid (lesser than 0) */
113 test_stats_bitrate_calc_invalid_portid_2(void)
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);
124 /* To test the bit rate calculation with non-existing portid */
126 test_stats_bitrate_calc_non_existing_portid(void)
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",
138 /* To test the bit rate calculation with valid bit rate data, valid portid */
140 test_stats_bitrate_calc(void)
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);
152 test_bit_packet_forward(void)
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);
160 printf("allocate mbuf pool Failed\n");
163 ret = test_dev_start(portid, mp);
165 printf("test_dev_start(%hu, %p) failed, error code: %d\n",
170 ret = test_packet_forward(pbuf, portid, QUEUE_ID);
172 printf("send pkts Failed\n");
174 rte_eth_dev_stop(portid);
175 test_put_mbuf_to_pool(mp, pbuf);
177 return (ret >= 0) ? TEST_SUCCESS : TEST_FAILED;
181 test_bit_ring_setup(void)
183 test_ring_setup(&ring, &portid);
184 printf("port in ring setup : %d\n", portid);
190 test_bit_ring_free(void)
192 test_ring_free(ring);
193 test_vdev_uninit("net_ring_net_ringa");
194 rte_memzone_free(rte_memzone_lookup("RTE_METRICS"));
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,
203 /* TEST CASE 1: Test to create bit rate data */
204 TEST_CASE(test_stats_bitrate_create),
206 /* TEST CASE 2: Test to register bit rate metrics
207 * without metrics init and after metrics init
209 TEST_CASE(test_stats_bitrate_reg),
211 /* TEST CASE 3: Test to register bit rate metrics
212 * with invalid bit rate data
214 TEST_CASE(test_stats_bitrate_reg_invalidpointer),
216 /* TEST CASE 4: Test to calculate bit rate data metrics
217 * with invalid bit rate data
219 TEST_CASE(test_stats_bitrate_calc_invalid_bitrate_data),
221 /* TEST CASE 5: Test to calculate bit rate data metrics
222 * with portid exceeding the max ports
224 TEST_CASE(test_stats_bitrate_calc_invalid_portid_1),
226 /* TEST CASE 6: Test to calculate bit rate data metrics
227 * with portid less than 0
229 TEST_CASE(test_stats_bitrate_calc_invalid_portid_2),
231 /* TEST CASE 7: Test to calculate bit rate data metrics
232 * with non-existing portid
234 TEST_CASE(test_stats_bitrate_calc_non_existing_portid),
236 /* TEST CASE 8: Test to calculate bit rate data metrics
237 * with valid portid, valid bit rate data
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),
248 test_bitratestats(void)
250 return unit_test_suite_runner(&bitratestats_testsuite);
252 REGISTER_TEST_COMMAND(bitratestats_autotest, test_bitratestats);