vhost: fix async callbacks return type
[dpdk.git] / app / test / test_bitratestats.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8
9 #include <errno.h>
10 #include <rte_lcore.h>
11 #include <rte_memzone.h>
12 #include <rte_metrics.h>
13 #include <rte_bitrate.h>
14
15 #include "sample_packet_forward.h"
16 #include "test.h"
17
18 #define BIT_NUM_PACKETS 10
19 #define QUEUE_ID 0
20
21 static uint16_t portid;
22 static struct rte_stats_bitrates *bitrate_data;
23 static struct rte_ring *ring;
24
25 /* To test whether rte_stats_bitrate_create is successful */
26 static int
27 test_stats_bitrate_create(void)
28 {
29         bitrate_data = rte_stats_bitrate_create();
30         TEST_ASSERT(bitrate_data != NULL, "rte_stats_bitrate_create failed");
31
32         return TEST_SUCCESS;
33 }
34
35 /* To test free the resources from bitrate_create test */
36 static int
37 test_stats_bitrate_free(void)
38 {
39         int ret = 0;
40
41         rte_stats_bitrate_free(bitrate_data);
42
43         ret = rte_metrics_deinit();
44         TEST_ASSERT(ret >= 0, "Test Failed: rte_metrics_deinit failed");
45
46         return TEST_SUCCESS;
47 }
48
49 /* To test bit rate registration */
50 static int
51 test_stats_bitrate_reg(void)
52 {
53         int ret = 0;
54
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);
59
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);
65
66         return TEST_SUCCESS;
67 }
68
69 /* To test the bit rate registration with invalid pointer */
70 static int
71 test_stats_bitrate_reg_invalidpointer(void)
72 {
73         int ret = 0;
74
75         ret = rte_stats_bitrate_reg(NULL);
76         TEST_ASSERT(ret < 0, "Test Failed: Expected failure < 0 but "
77                         "got %d", ret);
78
79         return TEST_SUCCESS;
80 }
81
82 /* To test bit rate calculation with invalid bit rate data pointer */
83 static int
84 test_stats_bitrate_calc_invalid_bitrate_data(void)
85 {
86         int ret = 0;
87
88         ret = rte_stats_bitrate_calc(NULL, portid);
89         TEST_ASSERT(ret < 0, "Test Failed: rte_stats_bitrate_calc "
90                         "ret:%d", ret);
91
92         return TEST_SUCCESS;
93 }
94
95 /* To test the bit rate calculation with invalid portid
96  * (higher than max ports)
97  */
98 static int
99 test_stats_bitrate_calc_invalid_portid_1(void)
100 {
101         int ret = 0;
102
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);
106
107         return TEST_SUCCESS;
108 }
109
110 /* To test the bit rate calculation with invalid portid (lesser than 0) */
111 static int
112 test_stats_bitrate_calc_invalid_portid_2(void)
113 {
114         int ret = 0;
115
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);
119
120         return TEST_SUCCESS;
121 }
122
123 /* To test the bit rate calculation with non-existing portid */
124 static int
125 test_stats_bitrate_calc_non_existing_portid(void)
126 {
127         int ret = 0;
128
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",
132                         ENODEV, ret);
133
134         return TEST_SUCCESS;
135 }
136
137 /* To test the bit rate calculation with valid bit rate data, valid portid */
138 static int
139 test_stats_bitrate_calc(void)
140 {
141         int ret = 0;
142
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);
146
147         return TEST_SUCCESS;
148 }
149
150 static int
151 test_bit_packet_forward(void)
152 {
153         int ret;
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);
158         if (ret < 0) {
159                 printf("allocate mbuf pool Failed\n");
160                 return TEST_FAILED;
161         }
162         ret = test_packet_forward(pbuf, portid, QUEUE_ID);
163         if (ret < 0)
164                 printf("send pkts Failed\n");
165         test_put_mbuf_to_pool(mp, pbuf);
166
167         return TEST_SUCCESS;
168 }
169
170 static int
171 test_bit_ring_setup(void)
172 {
173         test_ring_setup(&ring, &portid);
174         printf("port in ring setup : %d\n", portid);
175
176         return TEST_SUCCESS;
177 }
178
179 static void
180 test_bit_ring_free(void)
181 {
182         test_ring_free(ring);
183         test_vdev_uninit("net_ring_net_ringa");
184         rte_memzone_free(rte_memzone_lookup("RTE_METRICS"));
185 }
186
187 static struct
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,
192         .unit_test_cases = {
193                 /* TEST CASE 1: Test to create bit rate data */
194                 TEST_CASE(test_stats_bitrate_create),
195
196                 /* TEST CASE 2: Test to register bit rate metrics
197                  * without metrics init and after metrics init
198                  */
199                 TEST_CASE(test_stats_bitrate_reg),
200
201                 /* TEST CASE 3: Test to register bit rate metrics
202                  * with invalid bit rate data
203                  */
204                 TEST_CASE(test_stats_bitrate_reg_invalidpointer),
205
206                 /* TEST CASE 4: Test to calculate bit rate data metrics
207                  * with invalid bit rate data
208                  */
209                 TEST_CASE(test_stats_bitrate_calc_invalid_bitrate_data),
210
211                 /* TEST CASE 5: Test to calculate bit rate data metrics
212                  * with portid exceeding the max ports
213                  */
214                 TEST_CASE(test_stats_bitrate_calc_invalid_portid_1),
215
216                 /* TEST CASE 6: Test to calculate bit rate data metrics
217                  * with portid less than 0
218                  */
219                 TEST_CASE(test_stats_bitrate_calc_invalid_portid_2),
220
221                 /* TEST CASE 7: Test to calculate bit rate data metrics
222                  * with non-existing portid
223                  */
224                 TEST_CASE(test_stats_bitrate_calc_non_existing_portid),
225
226                 /* TEST CASE 8: Test to calculate bit rate data metrics
227                  * with valid portid, valid bit rate data
228                  */
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),
233                 TEST_CASES_END()
234         }
235 };
236
237 static int
238 test_bitratestats(void)
239 {
240         return unit_test_suite_runner(&bitratestats_testsuite);
241 }
242 REGISTER_TEST_COMMAND(bitratestats_autotest, test_bitratestats);