New upstream version 17.08
[deb_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 <string.h>
45 #include <unistd.h>
46 #include <sys/wait.h>
47 #include <libgen.h>
48 #include <dirent.h>
49 #include <limits.h>
50
51 #include <rte_common.h>
52 #include <rte_memory.h>
53 #include <rte_memzone.h>
54 #include <rte_eal.h>
55 #include <rte_launch.h>
56 #include <rte_per_lcore.h>
57 #include <rte_lcore.h>
58 #include <rte_errno.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_atomic.h>
61 #include <rte_ring.h>
62 #include <rte_debug.h>
63 #include <rte_log.h>
64 #include <rte_mempool.h>
65
66 #ifdef RTE_LIBRTE_HASH
67 #include <rte_hash.h>
68 #include <rte_fbk_hash.h>
69 #endif /* RTE_LIBRTE_HASH */
70
71 #ifdef RTE_LIBRTE_LPM
72 #include <rte_lpm.h>
73 #endif /* RTE_LIBRTE_LPM */
74
75 #include <rte_string_fns.h>
76
77 #include "process.h"
78
79 #define launch_proc(ARGV) process_dup(ARGV, \
80                 sizeof(ARGV)/(sizeof(ARGV[0])), __func__)
81
82 #ifdef RTE_EXEC_ENV_LINUXAPP
83 static char*
84 get_current_prefix(char * prefix, int size)
85 {
86         char path[PATH_MAX] = {0};
87         char buf[PATH_MAX] = {0};
88
89         /* get file for config (fd is always 3) */
90         snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
91
92         /* return NULL on error */
93         if (readlink(path, buf, sizeof(buf)) == -1)
94                 return NULL;
95
96         /* get the basename */
97         snprintf(buf, sizeof(buf), "%s", basename(buf));
98
99         /* copy string all the way from second char up to start of _config */
100         snprintf(prefix, size, "%.*s",
101                         (int)(strnlen(buf, sizeof(buf)) - sizeof("_config")),
102                         &buf[1]);
103
104         return prefix;
105 }
106 #endif
107
108 /*
109  * This function is called in the primary i.e. main test, to spawn off secondary
110  * processes to run actual mp tests. Uses fork() and exec pair
111  */
112 static int
113 run_secondary_instances(void)
114 {
115         int ret = 0;
116         char coremask[10];
117
118 #ifdef RTE_EXEC_ENV_LINUXAPP
119         char tmp[PATH_MAX] = {0};
120         char prefix[PATH_MAX] = {0};
121
122         get_current_prefix(tmp, sizeof(tmp));
123
124         snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
125 #else
126         const char *prefix = "";
127 #endif
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 #ifdef RTE_EXEC_ENV_LINUXAPP
145         /* bad case, using invalid file prefix */
146         const char *argv4[]  = {
147                         prgname, "-c", coremask, "--proc-type=secondary",
148                                         "--file-prefix=ERROR"
149         };
150 #endif
151
152         snprintf(coremask, sizeof(coremask), "%x", \
153                         (1 << rte_get_master_lcore()));
154
155         ret |= launch_proc(argv1);
156         ret |= launch_proc(argv2);
157
158         ret |= !(launch_proc(argv3));
159 #ifdef RTE_EXEC_ENV_LINUXAPP
160         ret |= !(launch_proc(argv4));
161 #endif
162
163         return ret;
164 }
165
166 /*
167  * This function is run in the secondary instance to test that creation of
168  * objects fails in a secondary
169  */
170 static int
171 run_object_creation_tests(void)
172 {
173         const unsigned flags = 0;
174         const unsigned size = 1024;
175         const unsigned elt_size = 64;
176         const unsigned cache_size = 64;
177         const unsigned priv_data_size = 32;
178
179         printf("### Testing object creation - expect lots of mz reserve errors!\n");
180
181         rte_errno = 0;
182         if ((rte_memzone_reserve("test_mz", size, rte_socket_id(),
183                                  flags) == NULL) &&
184             (rte_memzone_lookup("test_mz") == NULL)) {
185                 printf("Error: unexpected return value from rte_memzone_reserve\n");
186                 return -1;
187         }
188         printf("# Checked rte_memzone_reserve() OK\n");
189
190         rte_errno = 0;
191         if ((rte_ring_create(
192                      "test_ring", size, rte_socket_id(), flags) == NULL) &&
193                     (rte_ring_lookup("test_ring") == NULL)){
194                 printf("Error: unexpected return value from rte_ring_create()\n");
195                 return -1;
196         }
197         printf("# Checked rte_ring_create() OK\n");
198
199         rte_errno = 0;
200         if ((rte_mempool_create("test_mp", size, elt_size, cache_size,
201                                 priv_data_size, NULL, NULL, NULL, NULL,
202                                 rte_socket_id(), flags) == NULL) &&
203              (rte_mempool_lookup("test_mp") == NULL)){
204                 printf("Error: unexpected return value from rte_mempool_create()\n");
205                 return -1;
206         }
207         printf("# Checked rte_mempool_create() OK\n");
208
209 #ifdef RTE_LIBRTE_HASH
210         const struct rte_hash_parameters hash_params = { .name = "test_mp_hash" };
211         rte_errno=0;
212         if ((rte_hash_create(&hash_params) != NULL) &&
213             (rte_hash_find_existing(hash_params.name) == NULL)){
214                 printf("Error: unexpected return value from rte_hash_create()\n");
215                 return -1;
216         }
217         printf("# Checked rte_hash_create() OK\n");
218
219         const struct rte_fbk_hash_params fbk_params = { .name = "test_fbk_mp_hash" };
220         rte_errno=0;
221         if ((rte_fbk_hash_create(&fbk_params) != NULL) &&
222             (rte_fbk_hash_find_existing(fbk_params.name) == NULL)){
223                 printf("Error: unexpected return value from rte_fbk_hash_create()\n");
224                 return -1;
225         }
226         printf("# Checked rte_fbk_hash_create() OK\n");
227 #endif
228
229 #ifdef RTE_LIBRTE_LPM
230         rte_errno=0;
231         struct rte_lpm_config config;
232
233         config.max_rules = rte_socket_id();
234         config.number_tbl8s = 256;
235         config.flags = 0;
236         if ((rte_lpm_create("test_lpm", size, &config) != 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         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                 return run_secondary_instances();
257         }
258
259         printf("IN SECONDARY PROCESS\n");
260
261         return run_object_creation_tests();
262 }
263
264 REGISTER_TEST_COMMAND(multiprocess_autotest, test_mp_secondary);