xref: /dpdk/app/test/test_tailq.c (revision 7f12dfdc)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <stdarg.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <sys/queue.h>
11 
12 #include <rte_eal.h>
13 #include <rte_eal_memconfig.h>
14 #include <rte_string_fns.h>
15 #include <rte_tailq.h>
16 
17 #include "test.h"
18 
19 #define do_return(...) do { \
20 	printf("Error at %s, line %d: ", __func__, __LINE__); \
21 	printf(__VA_ARGS__); \
22 	return 1; \
23 } while (0)
24 
25 static struct rte_tailq_elem rte_dummy_tailq = {
26 	.name = "dummy",
27 };
28 EAL_REGISTER_TAILQ(rte_dummy_tailq)
29 
30 static struct rte_tailq_elem rte_dummy_dyn_tailq = {
31 	.name = "dummy_dyn",
32 };
33 static struct rte_tailq_elem rte_dummy_dyn2_tailq = {
34 	.name = "dummy_dyn",
35 };
36 
37 static struct rte_tailq_entry d_elem;
38 static struct rte_tailq_entry d_dyn_elem;
39 
40 static int
41 test_tailq_early(void)
42 {
43 	struct rte_tailq_entry_head *d_head;
44 
45 	d_head = RTE_TAILQ_CAST(rte_dummy_tailq.head, rte_tailq_entry_head);
46 	if (d_head == NULL)
47 		do_return("Error %s has not been initialised\n",
48 			  rte_dummy_tailq.name);
49 
50 	/* check we can add an item to it */
51 	TAILQ_INSERT_TAIL(d_head, &d_elem, next);
52 
53 	return 0;
54 }
55 
56 static int
57 test_tailq_create(void)
58 {
59 	struct rte_tailq_entry_head *d_head;
60 
61 	/* create a tailq and check its non-null (since we are post-eal init) */
62 	if ((rte_eal_tailq_register(&rte_dummy_dyn_tailq) < 0) ||
63 	    (rte_dummy_dyn_tailq.head == NULL))
64 		do_return("Error allocating %s\n", rte_dummy_dyn_tailq.name);
65 
66 	d_head = RTE_TAILQ_CAST(rte_dummy_dyn_tailq.head, rte_tailq_entry_head);
67 
68 	/* check we can add an item to it */
69 	TAILQ_INSERT_TAIL(d_head, &d_dyn_elem, next);
70 
71 	if (strcmp(rte_dummy_dyn2_tailq.name, rte_dummy_dyn_tailq.name))
72 		do_return("Error, something is wrong in the tailq test\n");
73 
74 	/* try allocating again, and check for failure */
75 	if (!rte_eal_tailq_register(&rte_dummy_dyn2_tailq))
76 		do_return("Error, registering the same tailq %s did not fail\n",
77 			  rte_dummy_dyn2_tailq.name);
78 
79 	return 0;
80 }
81 
82 static int
83 test_tailq_lookup(void)
84 {
85 	/* run successful  test - check result is found */
86 	struct rte_tailq_entry_head *d_head;
87 	struct rte_tailq_entry *d_ptr;
88 
89 	d_head = RTE_TAILQ_LOOKUP(rte_dummy_tailq.name, rte_tailq_entry_head);
90 	/* rte_dummy_tailq has been registered by EAL_REGISTER_TAILQ */
91 	if (d_head == NULL ||
92 	    d_head != RTE_TAILQ_CAST(rte_dummy_tailq.head, rte_tailq_entry_head))
93 		do_return("Error with tailq lookup\n");
94 
95 	TAILQ_FOREACH(d_ptr, d_head, next)
96 		if (d_ptr != &d_elem)
97 			do_return("Error with tailq returned from lookup - "
98 					"expected element not found\n");
99 
100 	d_head = RTE_TAILQ_LOOKUP(rte_dummy_dyn_tailq.name, rte_tailq_entry_head);
101 	/* rte_dummy_dyn_tailq has been registered by test_tailq_create */
102 	if (d_head == NULL ||
103 	    d_head != RTE_TAILQ_CAST(rte_dummy_dyn_tailq.head, rte_tailq_entry_head))
104 		do_return("Error with tailq lookup\n");
105 
106 	TAILQ_FOREACH(d_ptr, d_head, next)
107 		if (d_ptr != &d_dyn_elem)
108 			do_return("Error with tailq returned from lookup - "
109 					"expected element not found\n");
110 
111 	/* now try a bad/error lookup */
112 	d_head = RTE_TAILQ_LOOKUP("coucou", rte_tailq_entry_head);
113 	if (d_head != NULL)
114 		do_return("Error, lookup does not return NULL for bad tailq name\n");
115 
116 	return 0;
117 }
118 
119 static int
120 test_tailq(void)
121 {
122 	int ret = 0;
123 	ret |= test_tailq_early();
124 	ret |= test_tailq_create();
125 	ret |= test_tailq_lookup();
126 	return ret;
127 }
128 
129 REGISTER_TEST_COMMAND(tailq_autotest, test_tailq);
130