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