test: move unit tests to separate directory
[dpdk.git] / test / test / test_mp_secondary.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
36 #include "test.h"
37
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <inttypes.h>
42 #include <sys/queue.h>
43 #include <errno.h>
44 #include <stdarg.h>
45 #include <inttypes.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <unistd.h>
50 #include <sys/wait.h>
51 #include <libgen.h>
52 #include <dirent.h>
53 #include <limits.h>
54
55 #include <rte_common.h>
56 #include <rte_memory.h>
57 #include <rte_memzone.h>
58 #include <rte_eal.h>
59 #include <rte_launch.h>
60 #include <rte_per_lcore.h>
61 #include <rte_lcore.h>
62 #include <rte_errno.h>
63 #include <rte_branch_prediction.h>
64 #include <rte_atomic.h>
65 #include <rte_ring.h>
66 #include <rte_debug.h>
67 #include <rte_log.h>
68 #include <rte_mempool.h>
69
70 #ifdef RTE_LIBRTE_HASH
71 #include <rte_hash.h>
72 #include <rte_fbk_hash.h>
73 #endif /* RTE_LIBRTE_HASH */
74
75 #ifdef RTE_LIBRTE_LPM
76 #include <rte_lpm.h>
77 #endif /* RTE_LIBRTE_LPM */
78
79 #include <rte_string_fns.h>
80
81 #include "process.h"
82
83 #define launch_proc(ARGV) process_dup(ARGV, \
84                 sizeof(ARGV)/(sizeof(ARGV[0])), __func__)
85
86 #ifdef RTE_EXEC_ENV_LINUXAPP
87 static char*
88 get_current_prefix(char * prefix, int size)
89 {
90         char path[PATH_MAX] = {0};
91         char buf[PATH_MAX] = {0};
92
93         /* get file for config (fd is always 3) */
94         snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
95
96         /* return NULL on error */
97         if (readlink(path, buf, sizeof(buf)) == -1)
98                 return NULL;
99
100         /* get the basename */
101         snprintf(buf, sizeof(buf), "%s", basename(buf));
102
103         /* copy string all the way from second char up to start of _config */
104         snprintf(prefix, size, "%.*s",
105                         (int)(strnlen(buf, sizeof(buf)) - sizeof("_config")),
106                         &buf[1]);
107
108         return prefix;
109 }
110 #endif
111
112 /*
113  * This function is called in the primary i.e. main test, to spawn off secondary
114  * processes to run actual mp tests. Uses fork() and exec pair
115  */
116 static int
117 run_secondary_instances(void)
118 {
119         int ret = 0;
120         char coremask[10];
121
122 #ifdef RTE_EXEC_ENV_LINUXAPP
123         char tmp[PATH_MAX] = {0};
124         char prefix[PATH_MAX] = {0};
125
126         get_current_prefix(tmp, sizeof(tmp));
127
128         snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
129 #else
130         const char *prefix = "";
131 #endif
132
133         /* good case, using secondary */
134         const char *argv1[] = {
135                         prgname, "-c", coremask, "--proc-type=secondary",
136                         prefix
137         };
138         /* good case, using auto */
139         const char *argv2[] = {
140                         prgname, "-c", coremask, "--proc-type=auto",
141                         prefix
142         };
143         /* bad case, using invalid type */
144         const char *argv3[] = {
145                         prgname, "-c", coremask, "--proc-type=ERROR",
146                         prefix
147         };
148 #ifdef RTE_EXEC_ENV_LINUXAPP
149         /* bad case, using invalid file prefix */
150         const char *argv4[]  = {
151                         prgname, "-c", coremask, "--proc-type=secondary",
152                                         "--file-prefix=ERROR"
153         };
154 #endif
155
156         snprintf(coremask, sizeof(coremask), "%x", \
157                         (1 << rte_get_master_lcore()));
158
159         ret |= launch_proc(argv1);
160         ret |= launch_proc(argv2);
161
162         ret |= !(launch_proc(argv3));
163 #ifdef RTE_EXEC_ENV_LINUXAPP
164         ret |= !(launch_proc(argv4));
165 #endif
166
167         return ret;
168 }
169
170 /*
171  * This function is run in the secondary instance to test that creation of
172  * objects fails in a secondary
173  */
174 static int
175 run_object_creation_tests(void)
176 {
177         const unsigned flags = 0;
178         const unsigned size = 1024;
179         const unsigned elt_size = 64;
180         const unsigned cache_size = 64;
181         const unsigned priv_data_size = 32;
182
183         printf("### Testing object creation - expect lots of mz reserve errors!\n");
184
185         rte_errno = 0;
186         if ((rte_memzone_reserve("test_mz", size, rte_socket_id(),
187                                  flags) == NULL) &&
188             (rte_memzone_lookup("test_mz") == NULL)) {
189                 printf("Error: unexpected return value from rte_memzone_reserve\n");
190                 return -1;
191         }
192         printf("# Checked rte_memzone_reserve() OK\n");
193
194         rte_errno = 0;
195         if ((rte_ring_create(
196                      "test_ring", size, rte_socket_id(), flags) == NULL) &&
197                     (rte_ring_lookup("test_ring") == NULL)){
198                 printf("Error: unexpected return value from rte_ring_create()\n");
199                 return -1;
200         }
201         printf("# Checked rte_ring_create() OK\n");
202
203         rte_errno = 0;
204         if ((rte_mempool_create("test_mp", size, elt_size, cache_size,
205                                 priv_data_size, NULL, NULL, NULL, NULL,
206                                 rte_socket_id(), flags) == NULL) &&
207              (rte_mempool_lookup("test_mp") == NULL)){
208                 printf("Error: unexpected return value from rte_mempool_create()\n");
209                 return -1;
210         }
211         printf("# Checked rte_mempool_create() OK\n");
212
213 #ifdef RTE_LIBRTE_HASH
214         const struct rte_hash_parameters hash_params = { .name = "test_mp_hash" };
215         rte_errno=0;
216         if ((rte_hash_create(&hash_params) != NULL) &&
217             (rte_hash_find_existing(hash_params.name) == NULL)){
218                 printf("Error: unexpected return value from rte_hash_create()\n");
219                 return -1;
220         }
221         printf("# Checked rte_hash_create() OK\n");
222
223         const struct rte_fbk_hash_params fbk_params = { .name = "test_fbk_mp_hash" };
224         rte_errno=0;
225         if ((rte_fbk_hash_create(&fbk_params) != NULL) &&
226             (rte_fbk_hash_find_existing(fbk_params.name) == NULL)){
227                 printf("Error: unexpected return value from rte_fbk_hash_create()\n");
228                 return -1;
229         }
230         printf("# Checked rte_fbk_hash_create() OK\n");
231 #endif
232
233 #ifdef RTE_LIBRTE_LPM
234         rte_errno=0;
235         struct rte_lpm_config config;
236
237         config.max_rules = rte_socket_id();
238         config.number_tbl8s = 256;
239         config.flags = 0;
240         if ((rte_lpm_create("test_lpm", size, &config) != NULL) &&
241             (rte_lpm_find_existing("test_lpm") == NULL)){
242                 printf("Error: unexpected return value from rte_lpm_create()\n");
243                 return -1;
244         }
245         printf("# Checked rte_lpm_create() OK\n");
246 #endif
247
248 #ifdef RTE_APP_TEST_RESOURCE_TAR
249         /* Run a test_pci call */
250         if (test_pci() != 0) {
251                 printf("PCI scan failed in secondary\n");
252                 if (getuid() == 0) /* pci scans can fail as non-root */
253                         return -1;
254         } else
255                 printf("PCI scan succeeded in secondary\n");
256 #endif
257
258         return 0;
259 }
260
261 /* if called in a primary process, just spawns off a secondary process to
262  * run validation tests - which brings us right back here again...
263  * if called in a secondary process, this runs a series of API tests to check
264  * how things run in a secondary instance.
265  */
266 int
267 test_mp_secondary(void)
268 {
269         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
270                 if (!test_pci_run) {
271 #ifdef RTE_APP_TEST_RESOURCE_TAR
272                         printf("=== Running pre-requisite test of test_pci\n");
273                         test_pci();
274                         printf("=== Requisite test done\n");
275 #endif
276                 }
277                 return run_secondary_instances();
278         }
279
280         printf("IN SECONDARY PROCESS\n");
281
282         return run_object_creation_tests();
283 }
284
285 REGISTER_TEST_COMMAND(multiprocess_autotest, test_mp_secondary);