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