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