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