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