examples/fips_validation: fix buffer overflow
[dpdk.git] / drivers / raw / skeleton / skeleton_rawdev_test.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 NXP
3  */
4
5 #include <rte_common.h>
6 #include <rte_mbuf.h>
7 #include <rte_malloc.h>
8 #include <rte_memcpy.h>
9 #include <rte_dev.h>
10 #include <rte_rawdev.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_test.h>
13
14 /* Using relative path as skeleton_rawdev is not part of exported headers */
15 #include "skeleton_rawdev.h"
16
17 #define TEST_DEV_NAME "rawdev_skeleton"
18
19 #define SKELDEV_LOGS(level, fmt, args...) \
20         rte_log(RTE_LOG_ ## level, skeleton_pmd_logtype, fmt "\n", \
21                 ##args)
22
23 #define SKELDEV_TEST_INFO(fmt, args...) \
24         SKELDEV_LOGS(INFO, fmt, ## args)
25 #define SKELDEV_TEST_DEBUG(fmt, args...) \
26         SKELDEV_LOGS(DEBUG, fmt, ## args)
27
28 #define SKELDEV_TEST_RUN(setup, teardown, test) \
29         skeldev_test_run(setup, teardown, test, #test)
30
31 #define TEST_SUCCESS 0
32 #define TEST_FAILED  -1
33
34 static int total;
35 static int passed;
36 static int failed;
37 static int unsupported;
38
39 static uint16_t test_dev_id;
40
41 static int
42 testsuite_setup(void)
43 {
44         uint8_t count;
45         count = rte_rawdev_count();
46         if (!count) {
47                 SKELDEV_TEST_INFO("\tNo existing rawdev; "
48                                   "Creating 'skeldev_rawdev'");
49                 return rte_vdev_init(TEST_DEV_NAME, NULL);
50         }
51
52         return TEST_SUCCESS;
53 }
54
55 static void local_teardown(void);
56
57 static void
58 testsuite_teardown(void)
59 {
60         local_teardown();
61 }
62
63 static void
64 local_teardown(void)
65 {
66         rte_vdev_uninit(TEST_DEV_NAME);
67 }
68
69 static int
70 test_rawdev_count(void)
71 {
72         uint8_t count;
73         count = rte_rawdev_count();
74         RTE_TEST_ASSERT(count > 0, "Invalid rawdev count %" PRIu8, count);
75         return TEST_SUCCESS;
76 }
77
78 static int
79 test_rawdev_get_dev_id(void)
80 {
81         int ret;
82         ret = rte_rawdev_get_dev_id("invalid_rawdev_device");
83         RTE_TEST_ASSERT_FAIL(ret, "Expected <0 for invalid dev name ret=%d",
84                              ret);
85         return TEST_SUCCESS;
86 }
87
88 static int
89 test_rawdev_socket_id(void)
90 {
91         int socket_id;
92         socket_id = rte_rawdev_socket_id(test_dev_id);
93         RTE_TEST_ASSERT(socket_id != -EINVAL,
94                         "Failed to get socket_id %d", socket_id);
95         socket_id = rte_rawdev_socket_id(RTE_RAWDEV_MAX_DEVS);
96         RTE_TEST_ASSERT(socket_id == -EINVAL,
97                         "Expected -EINVAL %d", socket_id);
98
99         return TEST_SUCCESS;
100 }
101
102 static int
103 test_rawdev_info_get(void)
104 {
105         int ret;
106         struct rte_rawdev_info rdev_info = {0};
107         struct skeleton_rawdev_conf skel_conf = {0};
108
109         ret = rte_rawdev_info_get(test_dev_id, NULL);
110         RTE_TEST_ASSERT(ret == -EINVAL, "Expected -EINVAL, %d", ret);
111
112         rdev_info.dev_private = &skel_conf;
113
114         ret = rte_rawdev_info_get(test_dev_id, &rdev_info);
115         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to get raw dev info");
116
117         return TEST_SUCCESS;
118 }
119
120 static int
121 test_rawdev_configure(void)
122 {
123         int ret;
124         struct rte_rawdev_info rdev_info = {0};
125         struct skeleton_rawdev_conf rdev_conf_set = {0};
126         struct skeleton_rawdev_conf rdev_conf_get = {0};
127
128         /* Check invalid configuration */
129         ret = rte_rawdev_configure(test_dev_id, NULL);
130         RTE_TEST_ASSERT(ret == -EINVAL,
131                         "Null configure; Expected -EINVAL, got %d", ret);
132
133         /* Valid configuration test */
134         rdev_conf_set.num_queues = 1;
135         rdev_conf_set.capabilities = SKELETON_CAPA_FW_LOAD |
136                                      SKELETON_CAPA_FW_RESET;
137
138         rdev_info.dev_private = &rdev_conf_set;
139         ret = rte_rawdev_configure(test_dev_id,
140                                    (rte_rawdev_obj_t)&rdev_info);
141         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to configure rawdev (%d)", ret);
142
143         rdev_info.dev_private = &rdev_conf_get;
144         ret = rte_rawdev_info_get(test_dev_id,
145                                   (rte_rawdev_obj_t)&rdev_info);
146         RTE_TEST_ASSERT_SUCCESS(ret,
147                                 "Failed to obtain rawdev configuration (%d)",
148                                 ret);
149
150         RTE_TEST_ASSERT_EQUAL(rdev_conf_set.num_queues,
151                               rdev_conf_get.num_queues,
152                               "Configuration test failed; num_queues (%d)(%d)",
153                               rdev_conf_set.num_queues,
154                               rdev_conf_get.num_queues);
155         RTE_TEST_ASSERT_EQUAL(rdev_conf_set.capabilities,
156                           rdev_conf_get.capabilities,
157                           "Configuration test failed; capabilities");
158
159         return TEST_SUCCESS;
160 }
161
162 static int
163 test_rawdev_queue_default_conf_get(void)
164 {
165         int ret, i;
166         struct rte_rawdev_info rdev_info = {0};
167         struct skeleton_rawdev_conf rdev_conf_get = {0};
168         struct skeleton_rawdev_queue q = {0};
169
170         /* Get the current configuration */
171         rdev_info.dev_private = &rdev_conf_get;
172         ret = rte_rawdev_info_get(test_dev_id,
173                                   (rte_rawdev_obj_t)&rdev_info);
174         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to obtain rawdev configuration (%d)",
175                                 ret);
176
177         /* call to test_rawdev_configure would have set the num_queues = 1 */
178         RTE_TEST_ASSERT_SUCCESS(!(rdev_conf_get.num_queues > 0),
179                                 "Invalid number of queues (%d). Expected 1",
180                                 rdev_conf_get.num_queues);
181         /* All queues by default should have state = DETACH and
182          * depth = DEF_DEPTH
183          */
184         for (i = 0; i < rdev_conf_get.num_queues; i++) {
185                 rte_rawdev_queue_conf_get(test_dev_id, i, &q);
186                 RTE_TEST_ASSERT_EQUAL(q.depth, SKELETON_QUEUE_DEF_DEPTH,
187                                       "Invalid default depth of queue (%d)",
188                                       q.depth);
189                 RTE_TEST_ASSERT_EQUAL(q.state, SKELETON_QUEUE_DETACH,
190                                       "Invalid default state of queue (%d)",
191                                       q.state);
192         }
193
194         return TEST_SUCCESS;
195 }
196
197 static int
198 test_rawdev_queue_count(void)
199 {
200         unsigned int q_count;
201
202         /* Get the current configuration */
203         q_count = rte_rawdev_queue_count(test_dev_id);
204         RTE_TEST_ASSERT_EQUAL(q_count, 1, "Invalid queue count (%d)", q_count);
205
206         return TEST_SUCCESS;
207 }
208
209 static int
210 test_rawdev_queue_setup(void)
211 {
212         int ret;
213         struct rte_rawdev_info rdev_info = {0};
214         struct skeleton_rawdev_conf rdev_conf_get = {0};
215         struct skeleton_rawdev_queue qset = {0};
216         struct skeleton_rawdev_queue qget = {0};
217
218         /* Get the current configuration */
219         rdev_info.dev_private = &rdev_conf_get;
220         ret = rte_rawdev_info_get(test_dev_id,
221                                   (rte_rawdev_obj_t)&rdev_info);
222         RTE_TEST_ASSERT_SUCCESS(ret,
223                                 "Failed to obtain rawdev configuration (%d)",
224                                 ret);
225
226         /* call to test_rawdev_configure would have set the num_queues = 1 */
227         RTE_TEST_ASSERT_SUCCESS(!(rdev_conf_get.num_queues > 0),
228                                 "Invalid number of queues (%d). Expected 1",
229                                 rdev_conf_get.num_queues);
230
231         /* Modify the queue depth for Queue 0 and attach it */
232         qset.depth = 15;
233         qset.state = SKELETON_QUEUE_ATTACH;
234         ret = rte_rawdev_queue_setup(test_dev_id, 0, &qset);
235         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to setup queue (%d)", ret);
236
237         /* Now, fetching the queue 0 should show depth as 15 */
238         ret = rte_rawdev_queue_conf_get(test_dev_id, 0, &qget);
239         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to get queue config (%d)", ret);
240
241         RTE_TEST_ASSERT_EQUAL(qset.depth, qget.depth,
242                               "Failed to set queue depth: Need(%d), has(%d)",
243                               qset.depth, qget.depth);
244
245         return TEST_SUCCESS;
246 }
247
248 /* After executing test_rawdev_queue_setup, queue_id=0 would have depth as 15.
249  * Releasing should set it back to default. state would set to DETACH
250  */
251 static int
252 test_rawdev_queue_release(void)
253 {
254         int ret;
255         struct skeleton_rawdev_queue qget = {0};
256
257         /* Now, fetching the queue 0 should show depth as 100 */
258         ret = rte_rawdev_queue_release(test_dev_id, 0);
259         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to release queue 0; (%d)", ret);
260
261         /* Now, fetching the queue 0 should show depth as default */
262         ret = rte_rawdev_queue_conf_get(test_dev_id, 0, &qget);
263         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to get queue config (%d)", ret);
264
265         RTE_TEST_ASSERT_EQUAL(qget.depth, SKELETON_QUEUE_DEF_DEPTH,
266                               "Release of Queue 0 failed; (depth)");
267
268         RTE_TEST_ASSERT_EQUAL(qget.state, SKELETON_QUEUE_DETACH,
269                               "Release of Queue 0 failed; (state)");
270
271         return TEST_SUCCESS;
272 }
273
274 static int
275 test_rawdev_attr_set_get(void)
276 {
277         int ret;
278         int *dummy_value, set_value;
279         uint64_t ret_value;
280
281         /* Set an attribute and fetch it */
282         ret = rte_rawdev_set_attr(test_dev_id, "Test1", 100);
283         RTE_TEST_ASSERT(!ret, "Unable to set an attribute (Test1)");
284
285         dummy_value = &set_value;
286         *dummy_value = 200;
287         ret = rte_rawdev_set_attr(test_dev_id, "Test2", (uintptr_t)dummy_value);
288
289         /* Check if attributes have been set */
290         ret = rte_rawdev_get_attr(test_dev_id, "Test1", &ret_value);
291         RTE_TEST_ASSERT_EQUAL(ret_value, 100,
292                               "Attribute (Test1) not set correctly (%" PRIu64 ")",
293                               ret_value);
294
295         ret_value = 0;
296         ret = rte_rawdev_get_attr(test_dev_id, "Test2", &ret_value);
297         RTE_TEST_ASSERT_EQUAL(*((int *)(uintptr_t)ret_value), set_value,
298                               "Attribute (Test2) not set correctly (%" PRIu64 ")",
299                               ret_value);
300
301         return TEST_SUCCESS;
302 }
303
304 static int
305 test_rawdev_start_stop(void)
306 {
307         int ret;
308         struct rte_rawdev_info rdev_info = {0};
309         struct skeleton_rawdev_conf rdev_conf_get = {0};
310         char *dummy_firmware = NULL;
311
312         /* Get the current configuration */
313         rdev_info.dev_private = &rdev_conf_get;
314
315         /* Load a firmware using a dummy address area */
316         dummy_firmware = rte_zmalloc("RAWDEV SKELETON", sizeof(int) * 10, 0);
317         RTE_TEST_ASSERT(dummy_firmware != NULL,
318                         "Failed to create firmware memory backing");
319
320         ret = rte_rawdev_firmware_load(test_dev_id, dummy_firmware);
321         RTE_TEST_ASSERT_SUCCESS(ret, "Firmware loading failed (%d)", ret);
322
323         /* Skeleton doesn't do anything with the firmware area - that is dummy
324          * and can be removed.
325          */
326         rte_free(dummy_firmware);
327         dummy_firmware = NULL;
328
329         rte_rawdev_start(test_dev_id);
330         ret = rte_rawdev_info_get(test_dev_id, (rte_rawdev_obj_t)&rdev_info);
331         RTE_TEST_ASSERT_SUCCESS(ret,
332                                 "Failed to obtain rawdev configuration (%d)",
333                                 ret);
334         RTE_TEST_ASSERT_EQUAL(rdev_conf_get.device_state, SKELETON_DEV_RUNNING,
335                               "Device start failed. State is (%d)",
336                               rdev_conf_get.device_state);
337
338         rte_rawdev_stop(test_dev_id);
339         ret = rte_rawdev_info_get(test_dev_id, (rte_rawdev_obj_t)&rdev_info);
340         RTE_TEST_ASSERT_SUCCESS(ret,
341                                 "Failed to obtain rawdev configuration (%d)",
342                                 ret);
343         RTE_TEST_ASSERT_EQUAL(rdev_conf_get.device_state, SKELETON_DEV_STOPPED,
344                               "Device stop failed. State is (%d)",
345                               rdev_conf_get.device_state);
346
347         /* Unloading the firmware once device is stopped */
348         ret = rte_rawdev_firmware_unload(test_dev_id);
349         RTE_TEST_ASSERT_SUCCESS(ret, "Failed to unload firmware (%d)", ret);
350
351         return TEST_SUCCESS;
352 }
353
354 static int
355 test_rawdev_enqdeq(void)
356 {
357         int ret;
358         unsigned int count = 1;
359         uint16_t queue_id = 0;
360         struct rte_rawdev_buf buffers[1];
361         struct rte_rawdev_buf *deq_buffers = NULL;
362
363         buffers[0].buf_addr = malloc(strlen(TEST_DEV_NAME) + 3);
364         if (!buffers[0].buf_addr)
365                 goto cleanup;
366         snprintf(buffers[0].buf_addr, strlen(TEST_DEV_NAME) + 2, "%s%d",
367                  TEST_DEV_NAME, 0);
368
369         ret = rte_rawdev_enqueue_buffers(test_dev_id,
370                                          (struct rte_rawdev_buf **)&buffers,
371                                          count, &queue_id);
372         RTE_TEST_ASSERT_EQUAL((unsigned int)ret, count,
373                               "Unable to enqueue buffers");
374
375         deq_buffers = malloc(sizeof(struct rte_rawdev_buf) * count);
376         if (!deq_buffers)
377                 goto cleanup;
378
379         ret = rte_rawdev_dequeue_buffers(test_dev_id,
380                                         (struct rte_rawdev_buf **)&deq_buffers,
381                                         count, &queue_id);
382         RTE_TEST_ASSERT_EQUAL((unsigned int)ret, count,
383                               "Unable to dequeue buffers");
384
385         if (deq_buffers)
386                 free(deq_buffers);
387
388         return TEST_SUCCESS;
389 cleanup:
390         if (buffers[0].buf_addr)
391                 free(buffers[0].buf_addr);
392
393         return TEST_FAILED;
394 }
395
396 static void skeldev_test_run(int (*setup)(void),
397                              void (*teardown)(void),
398                              int (*test)(void),
399                              const char *name)
400 {
401         int ret = 0;
402
403         if (setup) {
404                 ret = setup();
405                 if (ret < 0) {
406                         SKELDEV_TEST_INFO("Error setting up test %s", name);
407                         unsupported++;
408                 }
409         }
410
411         if (test) {
412                 ret = test();
413                 if (ret < 0) {
414                         failed++;
415                         SKELDEV_TEST_INFO("%s Failed", name);
416                 } else {
417                         passed++;
418                         SKELDEV_TEST_DEBUG("%s Passed", name);
419                 }
420         }
421
422         if (teardown)
423                 teardown();
424
425         total++;
426 }
427
428 int
429 test_rawdev_skeldev(uint16_t dev_id)
430 {
431         test_dev_id = dev_id;
432         testsuite_setup();
433
434         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_count);
435         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_get_dev_id);
436         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_socket_id);
437         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_info_get);
438         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_configure);
439         SKELDEV_TEST_RUN(test_rawdev_configure, NULL,
440                          test_rawdev_queue_default_conf_get);
441         SKELDEV_TEST_RUN(test_rawdev_configure, NULL, test_rawdev_queue_setup);
442         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_queue_count);
443         SKELDEV_TEST_RUN(test_rawdev_queue_setup, NULL,
444                          test_rawdev_queue_release);
445         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_attr_set_get);
446         SKELDEV_TEST_RUN(NULL, NULL, test_rawdev_start_stop);
447         SKELDEV_TEST_RUN(test_rawdev_queue_setup, NULL, test_rawdev_enqdeq);
448
449         testsuite_teardown();
450
451         SKELDEV_TEST_INFO("Total tests   : %d", total);
452         SKELDEV_TEST_INFO("Passed        : %d", passed);
453         SKELDEV_TEST_INFO("Failed        : %d", failed);
454         SKELDEV_TEST_INFO("Not supported : %d", unsupported);
455
456         if (failed)
457                 return -1;
458
459         return 0;
460 };