first public release
[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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <stdio.h>
37
38 #include <cmdline_parse.h>
39
40 #include "test.h"
41
42 #ifndef RTE_EXEC_ENV_BAREMETAL
43 #include <stdint.h>
44 #include <stdlib.h>
45 #include <stdarg.h>
46 #include <inttypes.h>
47 #include <sys/queue.h>
48 #include <errno.h>
49 #include <stdarg.h>
50 #include <inttypes.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <stdarg.h>
54 #include <unistd.h>
55 #include <sys/wait.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 <stdarg.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 /*
84  * This function is called in the primary i.e. main test, to spawn off secondary
85  * processes to run actual mp tests. Uses fork() and exec pair
86  */
87 static int
88 run_secondary_instances(void)
89 {
90         int ret = 0;
91         char coremask[10];
92
93         /* good case, using secondary */
94         const char *argv1[] = {
95                         prgname, "-c", coremask, "--proc-type=secondary"
96         };
97         /* good case, using auto */
98         const char *argv2[] = {
99                         prgname, "-c", coremask, "--proc-type=auto"
100         };
101         /* bad case, using invalid type */
102         const char *argv3[] = {
103                         prgname, "-c", coremask, "--proc-type=ERROR"
104         };
105         /* bad case, using invalid file prefix */
106         const char *argv4[]  = {
107                         prgname, "-c", coremask, "--proc-type=secondary",
108                                         "--file-prefix=ERROR"
109         };
110
111         rte_snprintf(coremask, sizeof(coremask), "%x", \
112                         (1 << rte_get_master_lcore()));
113
114         ret |= launch_proc(argv1);
115         ret |= launch_proc(argv2);
116
117         ret |= !(launch_proc(argv3));
118         ret |= !(launch_proc(argv4));
119
120         return ret;
121 }
122
123 /*
124  * This function is run in the secondary instance to test that creation of
125  * objects fails in a secondary
126  */
127 static int
128 run_object_creation_tests(void)
129 {
130         const unsigned flags = 0;
131         const unsigned size = 1024;
132         const unsigned elt_size = 64;
133         const unsigned cache_size = 64;
134         const unsigned priv_data_size = 32;
135
136         printf("### Testing object creation - expect lots of mz reserve errors!\n");
137
138         rte_errno = 0;
139         if (rte_memzone_reserve("test_mz", size, rte_socket_id(), flags) != NULL
140                         || rte_errno != E_RTE_SECONDARY){
141                 printf("Error: unexpected return value from rte_memzone_reserve\n");
142                 return -1;
143         }
144         printf("# Checked rte_memzone_reserve() OK\n");
145
146         rte_errno = 0;
147         if (rte_ring_create("test_rng", size, rte_socket_id(), flags) != NULL
148                         || rte_errno != E_RTE_SECONDARY){
149                 printf("Error: unexpected return value from rte_ring_create()\n");
150                 return -1;
151         }
152         printf("# Checked rte_ring_create() OK\n");
153
154
155         rte_errno = 0;
156         if (rte_mempool_create("test_mp", size, elt_size, cache_size,
157                         priv_data_size, NULL, NULL, NULL, NULL,
158                         rte_socket_id(), flags) != NULL
159                         || rte_errno != E_RTE_SECONDARY){
160                 printf("Error: unexpected return value from rte_ring_create()\n");
161                 return -1;
162         }
163         printf("# Checked rte_mempool_create() OK\n");
164
165         const struct rte_hash_parameters hash_params = { .name = "test_mp_hash" };
166         rte_errno=0;
167         if (rte_hash_create(&hash_params) != NULL
168                         || rte_errno != E_RTE_SECONDARY){
169                 printf("Error: unexpected return value from rte_ring_create()\n");
170                 return -1;
171         }
172         printf("# Checked rte_hash_create() OK\n");
173
174         const struct rte_fbk_hash_params fbk_params = { .name = "test_mp_hash" };
175         rte_errno=0;
176         if (rte_fbk_hash_create(&fbk_params) != NULL
177                         || rte_errno != E_RTE_SECONDARY){
178                 printf("Error: unexpected return value from rte_ring_create()\n");
179                 return -1;
180         }
181         printf("# Checked rte_fbk_hash_create() OK\n");
182
183         rte_errno=0;
184         if (rte_lpm_create("test_lpm", size, rte_socket_id(), RTE_LPM_HEAP) != NULL
185                         || rte_errno != E_RTE_SECONDARY){
186                 printf("Error: unexpected return value from rte_ring_create()\n");
187                 return -1;
188         }
189         printf("# Checked rte_lpm_create() OK\n");
190
191         /* Run a test_pci call */
192         if (test_pci() != 0) {
193                 printf("PCI scan failed in secondary\n");
194                 if (getuid() == 0) /* pci scans can fail as non-root */
195                         return -1;
196         } else
197                 printf("PCI scan succeeded in secondary\n");
198
199         return 0;
200 }
201
202 /* if called in a primary process, just spawns off a secondary process to
203  * run validation tests - which brings us right back here again...
204  * if called in a secondary process, this runs a series of API tests to check
205  * how things run in a secondary instance.
206  */
207 int
208 test_mp_secondary(void)
209 {
210         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
211                 if (!test_pci_run) {
212                         printf("=== Running pre-requisite test of test_pci\n");
213                         test_pci();
214                         printf("=== Requisite test done\n");
215                 }
216                 return run_secondary_instances();
217         }
218
219         printf("IN SECONDARY PROCESS\n");
220
221         return run_object_creation_tests();
222 }
223
224 #else
225
226 /* Baremetal version
227  * Multiprocess not applicable, so just return 0 always
228  */
229 int
230 test_mp_secondary(void)
231 {
232         printf("Multi-process not applicable for baremetal\n");
233         return 0;
234 }
235
236 #endif