New upstream version 18.02
[deb_dpdk.git] / test / test / test_mp_secondary.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6
7 #include "test.h"
8
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <inttypes.h>
13 #include <sys/queue.h>
14 #include <errno.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/wait.h>
18 #include <libgen.h>
19 #include <dirent.h>
20 #include <limits.h>
21
22 #include <rte_common.h>
23 #include <rte_memory.h>
24 #include <rte_memzone.h>
25 #include <rte_eal.h>
26 #include <rte_launch.h>
27 #include <rte_per_lcore.h>
28 #include <rte_lcore.h>
29 #include <rte_errno.h>
30 #include <rte_branch_prediction.h>
31 #include <rte_atomic.h>
32 #include <rte_ring.h>
33 #include <rte_debug.h>
34 #include <rte_log.h>
35 #include <rte_mempool.h>
36
37 #ifdef RTE_LIBRTE_HASH
38 #include <rte_hash.h>
39 #include <rte_fbk_hash.h>
40 #endif /* RTE_LIBRTE_HASH */
41
42 #ifdef RTE_LIBRTE_LPM
43 #include <rte_lpm.h>
44 #endif /* RTE_LIBRTE_LPM */
45
46 #include <rte_string_fns.h>
47
48 #include "process.h"
49
50 #define launch_proc(ARGV) process_dup(ARGV, \
51                 sizeof(ARGV)/(sizeof(ARGV[0])), __func__)
52
53 #ifdef RTE_EXEC_ENV_LINUXAPP
54 static char*
55 get_current_prefix(char * prefix, int size)
56 {
57         char path[PATH_MAX] = {0};
58         char buf[PATH_MAX] = {0};
59
60         /* get file for config (fd is always 3) */
61         snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
62
63         /* return NULL on error */
64         if (readlink(path, buf, sizeof(buf)) == -1)
65                 return NULL;
66
67         /* get the basename */
68         snprintf(buf, sizeof(buf), "%s", basename(buf));
69
70         /* copy string all the way from second char up to start of _config */
71         snprintf(prefix, size, "%.*s",
72                         (int)(strnlen(buf, sizeof(buf)) - sizeof("_config")),
73                         &buf[1]);
74
75         return prefix;
76 }
77 #endif
78
79 /*
80  * This function is called in the primary i.e. main test, to spawn off secondary
81  * processes to run actual mp tests. Uses fork() and exec pair
82  */
83 static int
84 run_secondary_instances(void)
85 {
86         int ret = 0;
87         char coremask[10];
88
89 #ifdef RTE_EXEC_ENV_LINUXAPP
90         char tmp[PATH_MAX] = {0};
91         char prefix[PATH_MAX] = {0};
92
93         get_current_prefix(tmp, sizeof(tmp));
94
95         snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
96 #else
97         const char *prefix = "";
98 #endif
99
100         /* good case, using secondary */
101         const char *argv1[] = {
102                         prgname, "-c", coremask, "--proc-type=secondary",
103                         prefix
104         };
105         /* good case, using auto */
106         const char *argv2[] = {
107                         prgname, "-c", coremask, "--proc-type=auto",
108                         prefix
109         };
110         /* bad case, using invalid type */
111         const char *argv3[] = {
112                         prgname, "-c", coremask, "--proc-type=ERROR",
113                         prefix
114         };
115 #ifdef RTE_EXEC_ENV_LINUXAPP
116         /* bad case, using invalid file prefix */
117         const char *argv4[]  = {
118                         prgname, "-c", coremask, "--proc-type=secondary",
119                                         "--file-prefix=ERROR"
120         };
121 #endif
122
123         snprintf(coremask, sizeof(coremask), "%x", \
124                         (1 << rte_get_master_lcore()));
125
126         ret |= launch_proc(argv1);
127         ret |= launch_proc(argv2);
128
129         ret |= !(launch_proc(argv3));
130 #ifdef RTE_EXEC_ENV_LINUXAPP
131         ret |= !(launch_proc(argv4));
132 #endif
133
134         return ret;
135 }
136
137 /*
138  * This function is run in the secondary instance to test that creation of
139  * objects fails in a secondary
140  */
141 static int
142 run_object_creation_tests(void)
143 {
144         const unsigned flags = 0;
145         const unsigned size = 1024;
146         const unsigned elt_size = 64;
147         const unsigned cache_size = 64;
148         const unsigned priv_data_size = 32;
149
150         printf("### Testing object creation - expect lots of mz reserve errors!\n");
151
152         rte_errno = 0;
153         if ((rte_memzone_reserve("test_mz", size, rte_socket_id(),
154                                  flags) == NULL) &&
155             (rte_memzone_lookup("test_mz") == NULL)) {
156                 printf("Error: unexpected return value from rte_memzone_reserve\n");
157                 return -1;
158         }
159         printf("# Checked rte_memzone_reserve() OK\n");
160
161         rte_errno = 0;
162         if ((rte_ring_create(
163                      "test_ring", size, rte_socket_id(), flags) == NULL) &&
164                     (rte_ring_lookup("test_ring") == NULL)){
165                 printf("Error: unexpected return value from rte_ring_create()\n");
166                 return -1;
167         }
168         printf("# Checked rte_ring_create() OK\n");
169
170         rte_errno = 0;
171         if ((rte_mempool_create("test_mp", size, elt_size, cache_size,
172                                 priv_data_size, NULL, NULL, NULL, NULL,
173                                 rte_socket_id(), flags) == NULL) &&
174              (rte_mempool_lookup("test_mp") == NULL)){
175                 printf("Error: unexpected return value from rte_mempool_create()\n");
176                 return -1;
177         }
178         printf("# Checked rte_mempool_create() OK\n");
179
180 #ifdef RTE_LIBRTE_HASH
181         const struct rte_hash_parameters hash_params = { .name = "test_mp_hash" };
182         rte_errno=0;
183         if ((rte_hash_create(&hash_params) != NULL) &&
184             (rte_hash_find_existing(hash_params.name) == NULL)){
185                 printf("Error: unexpected return value from rte_hash_create()\n");
186                 return -1;
187         }
188         printf("# Checked rte_hash_create() OK\n");
189
190         const struct rte_fbk_hash_params fbk_params = { .name = "test_fbk_mp_hash" };
191         rte_errno=0;
192         if ((rte_fbk_hash_create(&fbk_params) != NULL) &&
193             (rte_fbk_hash_find_existing(fbk_params.name) == NULL)){
194                 printf("Error: unexpected return value from rte_fbk_hash_create()\n");
195                 return -1;
196         }
197         printf("# Checked rte_fbk_hash_create() OK\n");
198 #endif
199
200 #ifdef RTE_LIBRTE_LPM
201         rte_errno=0;
202         struct rte_lpm_config config;
203
204         config.max_rules = rte_socket_id();
205         config.number_tbl8s = 256;
206         config.flags = 0;
207         if ((rte_lpm_create("test_lpm", size, &config) != NULL) &&
208             (rte_lpm_find_existing("test_lpm") == NULL)){
209                 printf("Error: unexpected return value from rte_lpm_create()\n");
210                 return -1;
211         }
212         printf("# Checked rte_lpm_create() OK\n");
213 #endif
214
215         return 0;
216 }
217
218 /* if called in a primary process, just spawns off a secondary process to
219  * run validation tests - which brings us right back here again...
220  * if called in a secondary process, this runs a series of API tests to check
221  * how things run in a secondary instance.
222  */
223 int
224 test_mp_secondary(void)
225 {
226         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
227                 return run_secondary_instances();
228         }
229
230         printf("IN SECONDARY PROCESS\n");
231
232         return run_object_creation_tests();
233 }
234
235 REGISTER_TEST_COMMAND(multiprocess_autotest, test_mp_secondary);