xref: /dpdk/app/test/test_mp_secondary.c (revision f8dbaebbf1c9efcbb2e2354b341ed62175466a57)
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_ring.h>
32 #include <rte_debug.h>
33 #include <rte_log.h>
34 #include <rte_mempool.h>
35 
36 #ifdef RTE_LIB_HASH
37 #include <rte_hash.h>
38 #include <rte_fbk_hash.h>
39 #endif /* RTE_LIB_HASH */
40 
41 #ifdef RTE_LIB_LPM
42 #include <rte_lpm.h>
43 #endif /* RTE_LIB_LPM */
44 
45 #include <rte_string_fns.h>
46 
47 #include "process.h"
48 
49 #define launch_proc(ARGV) process_dup(ARGV, RTE_DIM(ARGV), __func__)
50 
51 /*
52  * This function is called in the primary i.e. main test, to spawn off secondary
53  * processes to run actual mp tests. Uses fork() and exec pair
54  */
55 static int
56 run_secondary_instances(void)
57 {
58 	int ret = 0;
59 	char coremask[10];
60 
61 #ifdef RTE_EXEC_ENV_LINUX
62 	char tmp[PATH_MAX] = {0};
63 	char prefix[PATH_MAX] = {0};
64 
65 	get_current_prefix(tmp, sizeof(tmp));
66 
67 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
68 #else
69 	const char *prefix = "";
70 #endif
71 
72 	/* good case, using secondary */
73 	const char *argv1[] = {
74 			prgname, "-c", coremask, "--proc-type=secondary",
75 			prefix
76 	};
77 	/* good case, using auto */
78 	const char *argv2[] = {
79 			prgname, "-c", coremask, "--proc-type=auto",
80 			prefix
81 	};
82 	/* bad case, using invalid type */
83 	const char *argv3[] = {
84 			prgname, "-c", coremask, "--proc-type=ERROR",
85 			prefix
86 	};
87 #ifdef RTE_EXEC_ENV_LINUX
88 	/* bad case, using invalid file prefix */
89 	const char *argv4[]  = {
90 			prgname, "-c", coremask, "--proc-type=secondary",
91 					"--file-prefix=ERROR"
92 	};
93 #endif
94 
95 	snprintf(coremask, sizeof(coremask), "%x", \
96 			(1 << rte_get_main_lcore()));
97 
98 	ret |= launch_proc(argv1);
99 	printf("### Testing rte_mp_disable() reject:\n");
100 	if (rte_mp_disable()) {
101 		printf("Error: rte_mp_disable() has been accepted\n");
102 		ret |= -1;
103 	} else {
104 		printf("# Checked rte_mp_disable() is refused\n");
105 	}
106 	ret |= launch_proc(argv2);
107 
108 	ret |= !(launch_proc(argv3));
109 #ifdef RTE_EXEC_ENV_LINUX
110 	ret |= !(launch_proc(argv4));
111 #endif
112 
113 	return ret;
114 }
115 
116 /*
117  * This function is run in the secondary instance to test that creation of
118  * objects fails in a secondary
119  */
120 static int
121 run_object_creation_tests(void)
122 {
123 	const unsigned flags = 0;
124 	const unsigned size = 1024;
125 	const unsigned elt_size = 64;
126 	const unsigned cache_size = 64;
127 	const unsigned priv_data_size = 32;
128 
129 	printf("### Testing object creation - expect lots of mz reserve errors!\n");
130 
131 	rte_errno = 0;
132 	if ((rte_memzone_reserve("test_mz", size, rte_socket_id(),
133 				 flags) == NULL) &&
134 	    (rte_memzone_lookup("test_mz") == NULL)) {
135 		printf("Error: unexpected return value from rte_memzone_reserve\n");
136 		return -1;
137 	}
138 	printf("# Checked rte_memzone_reserve() OK\n");
139 
140 	rte_errno = 0;
141 	if ((rte_ring_create(
142 		     "test_ring", size, rte_socket_id(), flags) == NULL) &&
143 		    (rte_ring_lookup("test_ring") == NULL)){
144 		printf("Error: unexpected return value from rte_ring_create()\n");
145 		return -1;
146 	}
147 	printf("# Checked rte_ring_create() OK\n");
148 
149 	rte_errno = 0;
150 	if ((rte_mempool_create("test_mp", size, elt_size, cache_size,
151 				priv_data_size, NULL, NULL, NULL, NULL,
152 				rte_socket_id(), flags) == NULL) &&
153 	     (rte_mempool_lookup("test_mp") == NULL)){
154 		printf("Error: unexpected return value from rte_mempool_create()\n");
155 		return -1;
156 	}
157 	printf("# Checked rte_mempool_create() OK\n");
158 
159 #ifdef RTE_LIB_HASH
160 	const struct rte_hash_parameters hash_params = { .name = "test_mp_hash" };
161 	rte_errno=0;
162 	if ((rte_hash_create(&hash_params) != NULL) &&
163 	    (rte_hash_find_existing(hash_params.name) == NULL)){
164 		printf("Error: unexpected return value from rte_hash_create()\n");
165 		return -1;
166 	}
167 	printf("# Checked rte_hash_create() OK\n");
168 
169 	const struct rte_fbk_hash_params fbk_params = { .name = "test_fbk_mp_hash" };
170 	rte_errno=0;
171 	if ((rte_fbk_hash_create(&fbk_params) != NULL) &&
172 	    (rte_fbk_hash_find_existing(fbk_params.name) == NULL)){
173 		printf("Error: unexpected return value from rte_fbk_hash_create()\n");
174 		return -1;
175 	}
176 	printf("# Checked rte_fbk_hash_create() OK\n");
177 #endif
178 
179 #ifdef RTE_LIB_LPM
180 	rte_errno=0;
181 	struct rte_lpm_config config;
182 
183 	config.max_rules = rte_socket_id();
184 	config.number_tbl8s = 256;
185 	config.flags = 0;
186 	if ((rte_lpm_create("test_lpm", size, &config) != NULL) &&
187 	    (rte_lpm_find_existing("test_lpm") == NULL)){
188 		printf("Error: unexpected return value from rte_lpm_create()\n");
189 		return -1;
190 	}
191 	printf("# Checked rte_lpm_create() OK\n");
192 #endif
193 
194 	return 0;
195 }
196 
197 /* if called in a primary process, just spawns off a secondary process to
198  * run validation tests - which brings us right back here again...
199  * if called in a secondary process, this runs a series of API tests to check
200  * how things run in a secondary instance.
201  */
202 int
203 test_mp_secondary(void)
204 {
205 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
206 		return run_secondary_instances();
207 	}
208 
209 	printf("IN SECONDARY PROCESS\n");
210 
211 	return run_object_creation_tests();
212 }
213 
214 REGISTER_TEST_COMMAND(multiprocess_autotest, test_mp_secondary);
215