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
22 struct rte_stats_bitrates
*bitrate_data
;
23 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 bit rate registration */
37 test_stats_bitrate_reg(void)
41 /* Test to register bit rate without metrics init */
42 ret
= rte_stats_bitrate_reg(bitrate_data
);
43 TEST_ASSERT(ret
< 0, "Test Failed: rte_stats_bitrate_reg succeeded "
44 "without metrics init, ret:%d", ret
);
46 /* Metrics initialization */
47 rte_metrics_init(rte_socket_id());
48 /* Test to register bit rate after metrics init */
49 ret
= rte_stats_bitrate_reg(bitrate_data
);
50 TEST_ASSERT((ret
>= 0), "Test Failed: rte_stats_bitrate_reg %d", ret
);
55 /* To test the bit rate registration with invalid pointer */
57 test_stats_bitrate_reg_invalidpointer(void)
61 ret
= rte_stats_bitrate_reg(NULL
);
62 TEST_ASSERT(ret
< 0, "Test Failed: Expected failure < 0 but "
68 /* To test bit rate calculation with invalid bit rate data pointer */
70 test_stats_bitrate_calc_invalid_bitrate_data(void)
74 ret
= rte_stats_bitrate_calc(NULL
, portid
);
75 TEST_ASSERT(ret
< 0, "Test Failed: rte_stats_bitrate_calc "
81 /* To test the bit rate calculation with invalid portid
82 * (higher than max ports)
85 test_stats_bitrate_calc_invalid_portid_1(void)
89 ret
= rte_stats_bitrate_calc(bitrate_data
, 33);
90 TEST_ASSERT(ret
== -EINVAL
, "Test Failed: Expected -%d for higher "
91 "portid rte_stats_bitrate_calc ret:%d", EINVAL
, ret
);
96 /* To test the bit rate calculation with invalid portid (lesser than 0) */
98 test_stats_bitrate_calc_invalid_portid_2(void)
102 ret
= rte_stats_bitrate_calc(bitrate_data
, -1);
103 TEST_ASSERT(ret
== -EINVAL
, "Test Failed: Expected -%d for invalid "
104 "portid rte_stats_bitrate_calc ret:%d", EINVAL
, ret
);
109 /* To test the bit rate calculation with non-existing portid */
111 test_stats_bitrate_calc_non_existing_portid(void)
115 ret
= rte_stats_bitrate_calc(bitrate_data
, 31);
116 TEST_ASSERT(ret
== -EINVAL
, "Test Failed: Expected -%d for "
117 "non-existing portid rte_stats_bitrate_calc ret:%d",
123 /* To test the bit rate calculation with valid bit rate data, valid portid */
125 test_stats_bitrate_calc(void)
129 ret
= rte_stats_bitrate_calc(bitrate_data
, portid
);
130 TEST_ASSERT(ret
>= 0, "Test Failed: Expected >=0 for valid portid "
131 "rte_stats_bitrate_calc ret:%d", ret
);
137 test_bit_packet_forward(void)
140 struct rte_mbuf
*pbuf
[BIT_NUM_PACKETS
] = { };
141 struct rte_mempool
*mp
;
142 char poolname
[] = "mbuf_pool";
143 ret
= test_get_mbuf_from_pool(&mp
, pbuf
, poolname
);
145 printf("allocate mbuf pool Failed\n");
148 ret
= test_packet_forward(pbuf
, portid
, QUEUE_ID
);
150 printf("send pkts Failed\n");
151 test_put_mbuf_to_pool(mp
, pbuf
);
157 test_bit_ring_setup(void)
159 test_ring_setup(&ring
, &portid
);
160 printf("port in ring setup : %d\n", portid
);
166 test_bit_ring_free(void)
168 test_ring_free(ring
);
169 test_vdev_uninit("net_ring_net_ringa");
170 rte_memzone_free(rte_memzone_lookup("RTE_METRICS"));
174 unit_test_suite bitratestats_testsuite
= {
175 .suite_name
= "BitRate Stats Unit Test Suite",
176 .setup
= test_bit_ring_setup
,
177 .teardown
= test_bit_ring_free
,
179 /* TEST CASE 1: Test to create bit rate data */
180 TEST_CASE(test_stats_bitrate_create
),
182 /* TEST CASE 2: Test to register bit rate metrics
183 * without metrics init and after metrics init
185 TEST_CASE(test_stats_bitrate_reg
),
187 /* TEST CASE 3: Test to register bit rate metrics
188 * with invalid bit rate data
190 TEST_CASE(test_stats_bitrate_reg_invalidpointer
),
192 /* TEST CASE 4: Test to calculate bit rate data metrics
193 * with invalid bit rate data
195 TEST_CASE(test_stats_bitrate_calc_invalid_bitrate_data
),
197 /* TEST CASE 5: Test to calculate bit rate data metrics
198 * with portid exceeding the max ports
200 TEST_CASE(test_stats_bitrate_calc_invalid_portid_1
),
202 /* TEST CASE 6: Test to calculate bit rate data metrics
203 * with portid less than 0
205 TEST_CASE(test_stats_bitrate_calc_invalid_portid_2
),
207 /* TEST CASE 7: Test to calculate bit rate data metrics
208 * with non-existing portid
210 TEST_CASE(test_stats_bitrate_calc_non_existing_portid
),
212 /* TEST CASE 8: Test to calculate bit rate data metrics
213 * with valid portid, valid bit rate data
215 TEST_CASE_ST(test_bit_packet_forward
, NULL
,
216 test_stats_bitrate_calc
),
222 test_bitratestats(void)
224 return unit_test_suite_runner(&bitratestats_testsuite
);
226 REGISTER_TEST_COMMAND(bitratestats_autotest
, test_bitratestats
);