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_reg test */
37 test_stats_bitrate_free(void)
41 ret = rte_metrics_deinit();
42 TEST_ASSERT(ret >= 0, "Test Failed: rte_metrics_deinit failed");
47 /* To test bit rate registration */
49 test_stats_bitrate_reg(void)
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);
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);
67 /* To test the bit rate registration with invalid pointer */
69 test_stats_bitrate_reg_invalidpointer(void)
73 ret = rte_stats_bitrate_reg(NULL);
74 TEST_ASSERT(ret < 0, "Test Failed: Expected failure < 0 but "
80 /* To test bit rate calculation with invalid bit rate data pointer */
82 test_stats_bitrate_calc_invalid_bitrate_data(void)
86 ret = rte_stats_bitrate_calc(NULL, portid);
87 TEST_ASSERT(ret < 0, "Test Failed: rte_stats_bitrate_calc "
93 /* To test the bit rate calculation with invalid portid
94 * (higher than max ports)
97 test_stats_bitrate_calc_invalid_portid_1(void)
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);
108 /* To test the bit rate calculation with invalid portid (lesser than 0) */
110 test_stats_bitrate_calc_invalid_portid_2(void)
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);
121 /* To test the bit rate calculation with non-existing portid */
123 test_stats_bitrate_calc_non_existing_portid(void)
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",
135 /* To test the bit rate calculation with valid bit rate data, valid portid */
137 test_stats_bitrate_calc(void)
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);
149 test_bit_packet_forward(void)
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);
157 printf("allocate mbuf pool Failed\n");
160 ret = test_packet_forward(pbuf, portid, QUEUE_ID);
162 printf("send pkts Failed\n");
163 test_put_mbuf_to_pool(mp, pbuf);
169 test_bit_ring_setup(void)
171 test_ring_setup(&ring, &portid);
172 printf("port in ring setup : %d\n", portid);
178 test_bit_ring_free(void)
180 test_ring_free(ring);
181 test_vdev_uninit("net_ring_net_ringa");
182 rte_memzone_free(rte_memzone_lookup("RTE_METRICS"));
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,
191 /* TEST CASE 1: Test to create bit rate data */
192 TEST_CASE(test_stats_bitrate_create),
194 /* TEST CASE 2: Test to register bit rate metrics
195 * without metrics init and after metrics init
197 TEST_CASE(test_stats_bitrate_reg),
199 /* TEST CASE 3: Test to register bit rate metrics
200 * with invalid bit rate data
202 TEST_CASE(test_stats_bitrate_reg_invalidpointer),
204 /* TEST CASE 4: Test to calculate bit rate data metrics
205 * with invalid bit rate data
207 TEST_CASE(test_stats_bitrate_calc_invalid_bitrate_data),
209 /* TEST CASE 5: Test to calculate bit rate data metrics
210 * with portid exceeding the max ports
212 TEST_CASE(test_stats_bitrate_calc_invalid_portid_1),
214 /* TEST CASE 6: Test to calculate bit rate data metrics
215 * with portid less than 0
217 TEST_CASE(test_stats_bitrate_calc_invalid_portid_2),
219 /* TEST CASE 7: Test to calculate bit rate data metrics
220 * with non-existing portid
222 TEST_CASE(test_stats_bitrate_calc_non_existing_portid),
224 /* TEST CASE 8: Test to calculate bit rate data metrics
225 * with valid portid, valid bit rate data
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),
236 test_bitratestats(void)
238 return unit_test_suite_runner(&bitratestats_testsuite);
240 REGISTER_TEST_COMMAND(bitratestats_autotest, test_bitratestats);