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