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