4d32c6f544bef4599f37322aaecf66a2d2d534e2
[dpdk.git] / app / test / test_tailq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <stdarg.h>
37 #include <errno.h>
38 #include <sys/queue.h>
39
40 #include <rte_eal.h>
41 #include <rte_eal_memconfig.h>
42 #include <rte_string_fns.h>
43 #include <rte_tailq.h>
44
45 #include "test.h"
46
47 #define do_return(...) do { \
48         printf("Error at %s, line %d: ", __func__, __LINE__); \
49         printf(__VA_ARGS__); \
50         return 1; \
51 } while (0)
52
53 #define DEFAULT_TAILQ (RTE_TAILQ_NUM)
54
55 static struct rte_tailq_entry d_elem;
56
57 static int
58 test_tailq_create(void)
59 {
60         struct rte_tailq_entry_head *d_head;
61         unsigned i;
62
63         /* create a first tailq and check its non-null */
64         d_head = RTE_TAILQ_RESERVE_BY_IDX(DEFAULT_TAILQ, rte_tailq_entry_head);
65         if (d_head == NULL)
66                 do_return("Error allocating dummy_q0\n");
67
68         /* check we can add an item to it
69          */
70         TAILQ_INSERT_TAIL(d_head, &d_elem, next);
71
72         /* try allocating dummy_q0 again, and check for failure */
73         if (RTE_TAILQ_RESERVE_BY_IDX(DEFAULT_TAILQ, rte_tailq_entry_head) == NULL)
74                 do_return("Error, non-null result returned when attemption to "
75                                 "re-allocate a tailq\n");
76
77         /* now fill up the tailq slots available and check we get an error */
78         for (i = RTE_TAILQ_NUM; i < RTE_MAX_TAILQ; i++){
79                 if ((d_head = RTE_TAILQ_RESERVE_BY_IDX(i,
80                                 rte_tailq_entry_head)) == NULL)
81                         break;
82         }
83
84         /* check that we had an error return before RTE_MAX_TAILQ */
85         if (i != RTE_MAX_TAILQ)
86                 do_return("Error, we did not have a reservation as expected\n");
87
88         return 0;
89 }
90
91 static int
92 test_tailq_lookup(void)
93 {
94         /* run successful  test - check result is found */
95         struct rte_tailq_entry_head *d_head;
96         struct rte_tailq_entry *d_ptr;
97
98         d_head = RTE_TAILQ_LOOKUP_BY_IDX(DEFAULT_TAILQ, rte_tailq_entry_head);
99         if (d_head == NULL)
100                 do_return("Error with tailq lookup\n");
101
102         TAILQ_FOREACH(d_ptr, d_head, next)
103                 if (d_ptr != &d_elem)
104                         do_return("Error with tailq returned from lookup - "
105                                         "expected element not found\n");
106
107         /* now try a bad/error lookup */
108         d_head = RTE_TAILQ_LOOKUP_BY_IDX(RTE_MAX_TAILQ, rte_tailq_entry_head);
109         if (d_head != NULL)
110                 do_return("Error, lookup does not return NULL for bad tailq name\n");
111
112         return 0;
113 }
114
115 /* test for deprecated functions - mainly for coverage */
116 static int
117 test_tailq_deprecated(void)
118 {
119         struct rte_tailq_entry_head *d_head;
120
121         /* since TAILQ_RESERVE is not able to create new tailqs,
122          * we should find an existing one (IOW, RTE_TAILQ_RESERVE behaves identical
123          * to RTE_TAILQ_LOOKUP).
124          *
125          * PCI_RESOURCE_LIST tailq is guaranteed to
126          * be present in any DPDK app. */
127         d_head = RTE_TAILQ_RESERVE("PCI_RESOURCE_LIST", rte_tailq_entry_head);
128         if (d_head == NULL)
129                 do_return("Error finding PCI_RESOURCE_LIST\n");
130
131         d_head = RTE_TAILQ_LOOKUP("PCI_RESOURCE_LIST", rte_tailq_entry_head);
132         if (d_head == NULL)
133                 do_return("Error finding PCI_RESOURCE_LIST\n");
134
135         /* try doing that with non-existent names */
136         d_head = RTE_TAILQ_RESERVE("random name", rte_tailq_entry_head);
137         if (d_head != NULL)
138                 do_return("Non-existent tailq found!\n");
139
140         d_head = RTE_TAILQ_LOOKUP("random name", rte_tailq_entry_head);
141         if (d_head != NULL)
142                 do_return("Non-existent tailq found!\n");
143
144         /* try doing the same with NULL names */
145         d_head = RTE_TAILQ_RESERVE(NULL, rte_tailq_entry_head);
146         if (d_head != NULL)
147                 do_return("NULL tailq found!\n");
148
149         d_head = RTE_TAILQ_LOOKUP(NULL, rte_tailq_entry_head);
150         if (d_head != NULL)
151                 do_return("NULL tailq found!\n");
152
153         return 0;
154 }
155
156 static int
157 test_tailq(void)
158 {
159         int ret = 0;
160         ret |= test_tailq_create();
161         ret |= test_tailq_lookup();
162         ret |= test_tailq_deprecated();
163         return ret;
164 }
165
166 static struct test_command tailq_cmd = {
167         .command = "tailq_autotest",
168         .callback = test_tailq,
169 };
170 REGISTER_TEST_COMMAND(tailq_cmd);