xref: /dpdk/app/test/test_eal_flags.c (revision 68a03efeed657e6e05f281479b33b51102797e15)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  */
5 
6 #include <stdio.h>
7 
8 #include "test.h"
9 
10 #include <string.h>
11 #include <stdarg.h>
12 #include <libgen.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <dirent.h>
17 #include <sys/wait.h>
18 #include <sys/file.h>
19 #include <limits.h>
20 #include <fcntl.h>
21 
22 #include <rte_lcore.h>
23 #include <rte_debug.h>
24 #include <rte_string_fns.h>
25 
26 #include "process.h"
27 
28 #define DEFAULT_MEM_SIZE "18"
29 #define mp_flag "--proc-type=secondary"
30 #define no_hpet "--no-hpet"
31 #define no_huge "--no-huge"
32 #define no_shconf "--no-shconf"
33 #define allow "--allow"
34 #define vdev "--vdev"
35 #define memtest "memtest"
36 #define memtest1 "memtest1"
37 #define memtest2 "memtest2"
38 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 20)
39 #define launch_proc(ARGV) process_dup(ARGV, RTE_DIM(ARGV), __func__)
40 
41 enum hugepage_action {
42 	HUGEPAGE_CHECK_EXISTS = 0,
43 	HUGEPAGE_CHECK_LOCKED,
44 	HUGEPAGE_DELETE,
45 	HUGEPAGE_INVALID
46 };
47 
48 /* if string contains a hugepage path */
49 static int
50 get_hugepage_path(char * src, int src_len, char * dst, int dst_len)
51 {
52 #define NUM_TOKENS 4
53 	char *tokens[NUM_TOKENS];
54 
55 	/* if we couldn't properly split the string */
56 	if (rte_strsplit(src, src_len, tokens, NUM_TOKENS, ' ') < NUM_TOKENS)
57 		return 0;
58 
59 	if (strncmp(tokens[2], "hugetlbfs", sizeof("hugetlbfs")) == 0) {
60 		strlcpy(dst, tokens[1], dst_len);
61 		return 1;
62 	}
63 	return 0;
64 }
65 
66 /*
67  * Cycles through hugepage directories and looks for hugepage
68  * files associated with a given prefix. Depending on value of
69  * action, the hugepages are checked if they exist, checked if
70  * they can be locked, or are simply deleted.
71  *
72  * Returns 1 if it finds at least one hugepage matching the action
73  * Returns 0 if no matching hugepages were found
74  * Returns -1 if it encounters an error
75  */
76 static int
77 process_hugefiles(const char * prefix, enum hugepage_action action)
78 {
79 	FILE * hugedir_handle = NULL;
80 	DIR * hugepage_dir = NULL;
81 	struct dirent *dirent = NULL;
82 
83 	char hugefile_prefix[PATH_MAX] = {0};
84 	char hugedir[PATH_MAX] = {0};
85 	char line[PATH_MAX] = {0};
86 
87 	int fd, lck_result, result = 0;
88 
89 	const int prefix_len = snprintf(hugefile_prefix,
90 			sizeof(hugefile_prefix), "%smap_", prefix);
91 	if (prefix_len <= 0 || prefix_len >= (int)sizeof(hugefile_prefix)
92 			|| prefix_len >= (int)sizeof(dirent->d_name)) {
93 		printf("Error creating hugefile filename prefix\n");
94 		return -1;
95 	}
96 
97 	/* get hugetlbfs mountpoints from /proc/mounts */
98 	hugedir_handle = fopen("/proc/mounts", "r");
99 
100 	if (hugedir_handle == NULL) {
101 		printf("Error parsing /proc/mounts!\n");
102 		return -1;
103 	}
104 
105 	/* read and parse script output */
106 	while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
107 
108 		/* check if we have a hugepage filesystem path */
109 		if (!get_hugepage_path(line, sizeof(line), hugedir, sizeof(hugedir)))
110 			continue;
111 
112 		/* check if directory exists */
113 		if ((hugepage_dir = opendir(hugedir)) == NULL) {
114 			fclose(hugedir_handle);
115 			printf("Error reading %s: %s\n", hugedir, strerror(errno));
116 			return -1;
117 		}
118 
119 		while ((dirent = readdir(hugepage_dir)) != NULL) {
120 			if (memcmp(dirent->d_name, hugefile_prefix, prefix_len) != 0)
121 				continue;
122 
123 			switch (action) {
124 			case HUGEPAGE_CHECK_EXISTS:
125 				{
126 					/* file exists, return */
127 					result = 1;
128 					goto end;
129 				}
130 				break;
131 			case HUGEPAGE_DELETE:
132 				{
133 					char file_path[PATH_MAX] = {0};
134 
135 					snprintf(file_path, sizeof(file_path),
136 						"%s/%s", hugedir, dirent->d_name);
137 
138 					/* remove file */
139 					if (remove(file_path) < 0) {
140 						printf("Error deleting %s - %s!\n",
141 								dirent->d_name, strerror(errno));
142 						closedir(hugepage_dir);
143 						result = -1;
144 						goto end;
145 					}
146 					result = 1;
147 				}
148 				break;
149 			case HUGEPAGE_CHECK_LOCKED:
150 				{
151 					/* try and lock the file */
152 					fd = openat(dirfd(hugepage_dir), dirent->d_name, O_RDONLY);
153 
154 					/* this shouldn't happen */
155 					if (fd == -1) {
156 						printf("Error opening %s - %s!\n",
157 								dirent->d_name, strerror(errno));
158 						closedir(hugepage_dir);
159 						result = -1;
160 						goto end;
161 					}
162 
163 					/* non-blocking lock */
164 					lck_result = flock(fd, LOCK_EX | LOCK_NB);
165 
166 					/* if lock succeeds, there's something wrong */
167 					if (lck_result != -1) {
168 						result = 0;
169 
170 						/* unlock the resulting lock */
171 						flock(fd, LOCK_UN);
172 						close(fd);
173 						closedir(hugepage_dir);
174 						goto end;
175 					}
176 					result = 1;
177 					close(fd);
178 				}
179 				break;
180 				/* shouldn't happen */
181 			default:
182 				goto end;
183 			} /* switch */
184 
185 		} /* read hugepage directory */
186 		closedir(hugepage_dir);
187 	} /* read /proc/mounts */
188 end:
189 	fclose(hugedir_handle);
190 	return result;
191 }
192 
193 #ifdef RTE_EXEC_ENV_LINUX
194 /*
195  * count the number of "node*" files in /sys/devices/system/node/
196  */
197 static int
198 get_number_of_sockets(void)
199 {
200 	struct dirent *dirent = NULL;
201 	const char * nodedir = "/sys/devices/system/node/";
202 	DIR * dir = NULL;
203 	int result = 0;
204 
205 	/* check if directory exists */
206 	if ((dir = opendir(nodedir)) == NULL) {
207 		/* if errno==ENOENT this means we don't have NUMA support */
208 		if (errno == ENOENT) {
209 			printf("No NUMA nodes detected: assuming 1 available socket\n");
210 			return 1;
211 		}
212 		printf("Error opening %s: %s\n", nodedir, strerror(errno));
213 		return -1;
214 	}
215 
216 	while ((dirent = readdir(dir)) != NULL)
217 		if (strncmp(dirent->d_name, "node", sizeof("node") - 1) == 0)
218 			result++;
219 
220 	closedir(dir);
221 	return result;
222 }
223 #endif
224 
225 /*
226  * Test that the app doesn't run with invalid allow option.
227  * Final tests ensures it does run with valid options as sanity check (one
228  * test for with Domain+BDF, second for just with BDF)
229  */
230 static int
231 test_allow_flag(void)
232 {
233 	unsigned i;
234 #ifdef RTE_EXEC_ENV_FREEBSD
235 	/* BSD target doesn't support prefixes at this point */
236 	const char * prefix = "";
237 #else
238 	char prefix[PATH_MAX], tmp[PATH_MAX];
239 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
240 		printf("Error - unable to get current prefix!\n");
241 		return -1;
242 	}
243 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
244 #endif
245 
246 	const char *wlinval[][7] = {
247 		{prgname, prefix, mp_flag,
248 				allow, "error", "", ""},
249 		{prgname, prefix, mp_flag,
250 				allow, "0:0:0", "", ""},
251 		{prgname, prefix, mp_flag,
252 				allow, "0:error:0.1", "", ""},
253 		{prgname, prefix, mp_flag,
254 				allow, "0:0:0.1error", "", ""},
255 		{prgname, prefix, mp_flag,
256 				allow, "error0:0:0.1", "", ""},
257 		{prgname, prefix, mp_flag,
258 				allow, "0:0:0.1.2", "", ""},
259 	};
260 	/* Test with valid allow option */
261 	const char *wlval1[] = {prgname, prefix, mp_flag,
262 			allow, "00FF:09:0B.3"};
263 	const char *wlval2[] = {prgname, prefix, mp_flag,
264 			allow, "09:0B.3", allow, "0a:0b.1"};
265 	const char *wlval3[] = {prgname, prefix, mp_flag,
266 			allow, "09:0B.3,type=test",
267 			allow, "08:00.1,type=normal",
268 	};
269 
270 	for (i = 0; i < RTE_DIM(wlinval); i++) {
271 		if (launch_proc(wlinval[i]) == 0) {
272 			printf("Error - process did run ok with invalid "
273 			    "allow parameter\n");
274 			return -1;
275 		}
276 	}
277 	if (launch_proc(wlval1) != 0 ) {
278 		printf("Error - process did not run ok with valid allow\n");
279 		return -1;
280 	}
281 	if (launch_proc(wlval2) != 0 ) {
282 		printf("Error - process did not run ok with valid allow value set\n");
283 		return -1;
284 	}
285 	if (launch_proc(wlval3) != 0 ) {
286 		printf("Error - process did not run ok with valid allow + args\n");
287 		return -1;
288 	}
289 
290 	return 0;
291 }
292 
293 /*
294  * Test that the app doesn't run with invalid blocklist option.
295  * Final test ensures it does run with valid options as sanity check
296  */
297 static int
298 test_invalid_b_flag(void)
299 {
300 #ifdef RTE_EXEC_ENV_FREEBSD
301 	/* BSD target doesn't support prefixes at this point */
302 	const char * prefix = "";
303 #else
304 	char prefix[PATH_MAX], tmp[PATH_MAX];
305 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
306 		printf("Error - unable to get current prefix!\n");
307 		return -1;
308 	}
309 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
310 #endif
311 
312 	const char *blinval[][5] = {
313 		{prgname, prefix, mp_flag, "-b", "error"},
314 		{prgname, prefix, mp_flag, "-b", "0:0:0"},
315 		{prgname, prefix, mp_flag, "-b", "0:error:0.1"},
316 		{prgname, prefix, mp_flag, "-b", "0:0:0.1error"},
317 		{prgname, prefix, mp_flag, "-b", "error0:0:0.1"},
318 		{prgname, prefix, mp_flag, "-b", "0:0:0.1.2"},
319 	};
320 	/* Test with valid blocklist option */
321 	const char *blval[] = {prgname, prefix, mp_flag,
322 			       "-b", "FF:09:0B.3"};
323 
324 	int i;
325 
326 	for (i = 0; i != RTE_DIM(blinval); i++) {
327 		if (launch_proc(blinval[i]) == 0) {
328 			printf("Error - process did run ok with invalid "
329 			    "blocklist parameter\n");
330 			return -1;
331 		}
332 	}
333 	if (launch_proc(blval) != 0) {
334 		printf("Error - process did not run ok with valid blocklist value\n");
335 		return -1;
336 	}
337 	return 0;
338 }
339 
340 /*
341  *  Test that the app doesn't run with invalid vdev option.
342  *  Final test ensures it does run with valid options as sanity check
343  */
344 static int
345 test_invalid_vdev_flag(void)
346 {
347 #ifdef RTE_NET_RING
348 #ifdef RTE_EXEC_ENV_FREEBSD
349 	/* BSD target doesn't support prefixes at this point, and we also need to
350 	 * run another primary process here */
351 	const char * prefix = no_shconf;
352 #else
353 	const char * prefix = "--file-prefix=vdev";
354 #endif
355 
356 	/* Test with invalid vdev option */
357 	const char *vdevinval[] = {prgname, prefix, no_huge,
358 				vdev, "eth_dummy"};
359 
360 	/* Test with valid vdev option */
361 	const char *vdevval1[] = {prgname, prefix, no_huge,
362 	vdev, "net_ring0"};
363 
364 	const char *vdevval2[] = {prgname, prefix, no_huge,
365 	vdev, "net_ring0,args=test"};
366 
367 	const char *vdevval3[] = {prgname, prefix, no_huge,
368 	vdev, "net_ring0,nodeaction=r1:0:CREATE"};
369 
370 	if (launch_proc(vdevinval) == 0) {
371 		printf("Error - process did run ok with invalid "
372 			"vdev parameter\n");
373 		return -1;
374 	}
375 
376 	if (launch_proc(vdevval1) != 0) {
377 		printf("Error - process did not run ok with valid vdev value\n");
378 		return -1;
379 	}
380 
381 	if (launch_proc(vdevval2) != 0) {
382 		printf("Error - process did not run ok with valid vdev value,"
383 			"with dummy args\n");
384 		return -1;
385 	}
386 
387 	if (launch_proc(vdevval3) != 0) {
388 		printf("Error - process did not run ok with valid vdev value,"
389 			"with valid args\n");
390 		return -1;
391 	}
392 	return 0;
393 #else
394 	return TEST_SKIPPED;
395 #endif
396 }
397 
398 /*
399  * Test that the app doesn't run with invalid -r option.
400  */
401 static int
402 test_invalid_r_flag(void)
403 {
404 #ifdef RTE_EXEC_ENV_FREEBSD
405 	/* BSD target doesn't support prefixes at this point */
406 	const char * prefix = "";
407 #else
408 	char prefix[PATH_MAX], tmp[PATH_MAX];
409 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
410 		printf("Error - unable to get current prefix!\n");
411 		return -1;
412 	}
413 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
414 #endif
415 
416 	const char *rinval[][5] = {
417 			{prgname, prefix, mp_flag, "-r", "error"},
418 			{prgname, prefix, mp_flag, "-r", "0"},
419 			{prgname, prefix, mp_flag, "-r", "-1"},
420 			{prgname, prefix, mp_flag, "-r", "17"},
421 	};
422 	/* Test with valid blocklist option */
423 	const char *rval[] = {prgname, prefix, mp_flag, "-r", "16"};
424 
425 	int i;
426 
427 	for (i = 0; i != RTE_DIM(rinval); i++) {
428 		if (launch_proc(rinval[i]) == 0) {
429 			printf("Error - process did run ok with invalid "
430 			    "-r (rank) parameter\n");
431 			return -1;
432 		}
433 	}
434 	if (launch_proc(rval) != 0) {
435 		printf("Error - process did not run ok with valid -r (rank) value\n");
436 		return -1;
437 	}
438 	return 0;
439 }
440 
441 /*
442  * Test that the app doesn't run without the coremask/corelist flags. In all cases
443  * should give an error and fail to run
444  */
445 static int
446 test_missing_c_flag(void)
447 {
448 #ifdef RTE_EXEC_ENV_FREEBSD
449 	/* BSD target doesn't support prefixes at this point */
450 	const char * prefix = "";
451 #else
452 	char prefix[PATH_MAX], tmp[PATH_MAX];
453 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
454 		printf("Error - unable to get current prefix!\n");
455 		return -1;
456 	}
457 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
458 #endif
459 
460 	/* -c flag but no coremask value */
461 	const char *argv1[] = { prgname, prefix, mp_flag, "-c"};
462 	/* No -c, -l or --lcores flag at all */
463 	const char *argv2[] = { prgname, prefix, mp_flag};
464 	/* bad coremask value */
465 	const char *argv3[] = { prgname, prefix, mp_flag,
466 				"-c", "error" };
467 	/* sanity check of tests - valid coremask value */
468 	const char *argv4[] = { prgname, prefix, mp_flag,
469 				"-c", "1" };
470 	/* -l flag but no corelist value */
471 	const char *argv5[] = { prgname, prefix, mp_flag,
472 				"-l"};
473 	const char *argv6[] = { prgname, prefix, mp_flag,
474 				"-l", " " };
475 	/* bad corelist values */
476 	const char *argv7[] = { prgname, prefix, mp_flag,
477 				"-l", "error" };
478 	const char *argv8[] = { prgname, prefix, mp_flag,
479 				"-l", "1-" };
480 	const char *argv9[] = { prgname, prefix, mp_flag,
481 				"-l", "1," };
482 	const char *argv10[] = { prgname, prefix, mp_flag,
483 				 "-l", "1#2" };
484 	/* core number is negative value */
485 	const char * const argv11[] = { prgname, prefix, mp_flag,
486 				"-l", "-5" };
487 	const char * const argv12[] = { prgname, prefix, mp_flag,
488 				"-l", "-5-7" };
489 	/* core number is maximum value */
490 	const char * const argv13[] = { prgname, prefix, mp_flag,
491 				"-l", RTE_STR(RTE_MAX_LCORE) };
492 	const char * const argv14[] = { prgname, prefix, mp_flag,
493 				"-l", "1-"RTE_STR(RTE_MAX_LCORE) };
494 	/* sanity check test - valid corelist value */
495 	const char * const argv15[] = { prgname, prefix, mp_flag,
496 				 "-l", "1-2,3" };
497 
498 	/* --lcores flag but no lcores value */
499 	const char * const argv16[] = { prgname, prefix, mp_flag,
500 				 "--lcores" };
501 	const char * const argv17[] = { prgname, prefix, mp_flag,
502 				 "--lcores", " " };
503 	/* bad lcores value */
504 	const char * const argv18[] = { prgname, prefix, mp_flag,
505 				 "--lcores", "1-3-5" };
506 	const char * const argv19[] = { prgname, prefix, mp_flag,
507 				 "--lcores", "0-1,,2" };
508 	const char * const argv20[] = { prgname, prefix, mp_flag,
509 				 "--lcores", "0-,1" };
510 	const char * const argv21[] = { prgname, prefix, mp_flag,
511 				 "--lcores", "(0-,2-4)" };
512 	const char * const argv22[] = { prgname, prefix, mp_flag,
513 				 "--lcores", "(-1,2)" };
514 	const char * const argv23[] = { prgname, prefix, mp_flag,
515 				 "--lcores", "(2-4)@(2-4-6)" };
516 	const char * const argv24[] = { prgname, prefix, mp_flag,
517 				 "--lcores", "(a,2)" };
518 	const char * const argv25[] = { prgname, prefix, mp_flag,
519 				 "--lcores", "1-3@(1,3)" };
520 	const char * const argv26[] = { prgname, prefix, mp_flag,
521 				 "--lcores", "3@((1,3)" };
522 	const char * const argv27[] = { prgname, prefix, mp_flag,
523 				 "--lcores", "(4-7)=(1,3)" };
524 	const char * const argv28[] = { prgname, prefix, mp_flag,
525 				 "--lcores", "[4-7]@(1,3)" };
526 	/* sanity check of tests - valid lcores value */
527 	const char * const argv29[] = { prgname, prefix, mp_flag,
528 				 "--lcores",
529 				 "0-1,2@(5-7),(3-5)@(0,2),(0,6),7"};
530 	/* check an invalid cpu value >= CPU_SETSIZE */
531 	const char * const argv30[] = { prgname, prefix, mp_flag,
532 				 "--lcores", "3@" RTE_STR(CPU_SETSIZE) };
533 
534 	if (launch_proc(argv2) != 0) {
535 		printf("Error - "
536 		       "process did not run ok when missing -c flag\n");
537 		return -1;
538 	}
539 
540 	if (launch_proc(argv1) == 0
541 			|| launch_proc(argv3) == 0) {
542 		printf("Error - "
543 		       "process ran without error with invalid -c flag\n");
544 		return -1;
545 	}
546 	if (launch_proc(argv4) != 0) {
547 		printf("Error - "
548 		       "process did not run ok with valid coremask value\n");
549 		return -1;
550 	}
551 
552 	/* start -l test */
553 	if (launch_proc(argv5) == 0
554 			|| launch_proc(argv6) == 0
555 			|| launch_proc(argv7) == 0
556 			|| launch_proc(argv8) == 0
557 			|| launch_proc(argv9) == 0
558 			|| launch_proc(argv10) == 0
559 			|| launch_proc(argv11) == 0
560 			|| launch_proc(argv12) == 0
561 			|| launch_proc(argv13) == 0
562 			|| launch_proc(argv14) == 0) {
563 		printf("Error - "
564 		       "process ran without error with invalid -l flag\n");
565 		return -1;
566 	}
567 	if (rte_lcore_is_enabled(0) && rte_lcore_is_enabled(1) &&
568 	    rte_lcore_is_enabled(2) && rte_lcore_is_enabled(3) &&
569 	    launch_proc(argv15) != 0) {
570 		printf("Error - "
571 		       "process did not run ok with valid corelist value\n");
572 		return -1;
573 	}
574 
575 	/* start --lcores tests */
576 	if (launch_proc(argv16) == 0 || launch_proc(argv17) == 0 ||
577 	    launch_proc(argv18) == 0 || launch_proc(argv19) == 0 ||
578 	    launch_proc(argv20) == 0 || launch_proc(argv21) == 0 ||
579 	    launch_proc(argv22) == 0 || launch_proc(argv23) == 0 ||
580 	    launch_proc(argv24) == 0 || launch_proc(argv25) == 0 ||
581 	    launch_proc(argv26) == 0 || launch_proc(argv27) == 0 ||
582 	    launch_proc(argv28) == 0 || launch_proc(argv30) == 0) {
583 		printf("Error - "
584 		       "process ran without error with invalid --lcore flag\n");
585 		return -1;
586 	}
587 
588 	if (rte_lcore_is_enabled(0) && rte_lcore_is_enabled(1) &&
589 	    rte_lcore_is_enabled(2) && rte_lcore_is_enabled(3) &&
590 	    rte_lcore_is_enabled(3) && rte_lcore_is_enabled(5) &&
591 	    rte_lcore_is_enabled(4) && rte_lcore_is_enabled(7) &&
592 	    launch_proc(argv29) != 0) {
593 		printf("Error - "
594 		       "process did not run ok with valid corelist value\n");
595 		return -1;
596 	}
597 
598 	return 0;
599 }
600 
601 /*
602  * Test --main-lcore option with matching coremask
603  */
604 static int
605 test_main_lcore_flag(void)
606 {
607 #ifdef RTE_EXEC_ENV_FREEBSD
608 	/* BSD target doesn't support prefixes at this point */
609 	const char *prefix = "";
610 #else
611 	char prefix[PATH_MAX], tmp[PATH_MAX];
612 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
613 		printf("Error - unable to get current prefix!\n");
614 		return -1;
615 	}
616 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
617 #endif
618 
619 	if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1))
620 		return TEST_SKIPPED;
621 
622 	/* --main-lcore flag but no value */
623 	const char *argv1[] = { prgname, prefix, mp_flag,
624 				"-c", "3", "--main-lcore"};
625 	/* --main-lcore flag with invalid value */
626 	const char *argv2[] = { prgname, prefix, mp_flag,
627 				"-c", "3", "--main-lcore", "-1"};
628 	const char *argv3[] = { prgname, prefix, mp_flag,
629 				"-c", "3", "--main-lcore", "X"};
630 	/* main lcore not in coremask */
631 	const char *argv4[] = { prgname, prefix, mp_flag,
632 				"-c", "3", "--main-lcore", "2"};
633 	/* valid value */
634 	const char *argv5[] = { prgname, prefix, mp_flag,
635 				"-c", "3", "--main-lcore", "1"};
636 	/* valid value set before coremask */
637 	const char *argv6[] = { prgname, prefix, mp_flag,
638 				"--main-lcore", "1", "-c", "3"};
639 
640 	if (launch_proc(argv1) == 0
641 			|| launch_proc(argv2) == 0
642 			|| launch_proc(argv3) == 0
643 			|| launch_proc(argv4) == 0) {
644 		printf("Error - process ran without error with wrong --main-lcore\n");
645 		return -1;
646 	}
647 	if (launch_proc(argv5) != 0
648 			|| launch_proc(argv6) != 0) {
649 		printf("Error - process did not run ok with valid --main-lcore\n");
650 		return -1;
651 	}
652 	return 0;
653 }
654 
655 /*
656  * Test that the app doesn't run with invalid -n flag option.
657  * Final test ensures it does run with valid options as sanity check
658  * Since -n is not compulsory for MP, we instead use --no-huge and --no-shconf
659  * flags.
660  */
661 static int
662 test_invalid_n_flag(void)
663 {
664 #ifdef RTE_EXEC_ENV_FREEBSD
665 	/* BSD target doesn't support prefixes at this point */
666 	const char * prefix = "";
667 #else
668 	char prefix[PATH_MAX], tmp[PATH_MAX];
669 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
670 		printf("Error - unable to get current prefix!\n");
671 		return -1;
672 	}
673 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
674 #endif
675 
676 	/* -n flag but no value */
677 	const char *argv1[] = { prgname, prefix, no_huge, no_shconf,
678 				"-n"};
679 	/* bad numeric value */
680 	const char *argv2[] = { prgname, prefix, no_huge, no_shconf,
681 				"-n", "e" };
682 	/* zero is invalid */
683 	const char *argv3[] = { prgname, prefix, no_huge, no_shconf,
684 				"-n", "0" };
685 	/* sanity test - check with good value */
686 	const char *argv4[] = { prgname, prefix, no_huge, no_shconf,
687 				"-n", "2" };
688 	/* sanity test - check with no -n flag */
689 	const char *argv5[] = { prgname, prefix, no_huge, no_shconf};
690 
691 	if (launch_proc(argv1) == 0
692 			|| launch_proc(argv2) == 0
693 			|| launch_proc(argv3) == 0) {
694 		printf("Error - process ran without error when"
695 		       "invalid -n flag\n");
696 		return -1;
697 	}
698 	if (launch_proc(argv4) != 0) {
699 		printf("Error - process did not run ok with valid num-channel value\n");
700 		return -1;
701 	}
702 	if (launch_proc(argv5) != 0) {
703 		printf("Error - process did not run ok without -n flag\n");
704 		return -1;
705 	}
706 
707 	return 0;
708 }
709 
710 /*
711  * Test that the app runs with HPET, and without HPET
712  */
713 static int
714 test_no_hpet_flag(void)
715 {
716 	char prefix[PATH_MAX] = "";
717 
718 #ifdef RTE_EXEC_ENV_FREEBSD
719 	return 0;
720 #else
721 	char tmp[PATH_MAX];
722 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
723 		printf("Error - unable to get current prefix!\n");
724 		return -1;
725 	}
726 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
727 #endif
728 
729 	/* With --no-hpet */
730 	const char *argv1[] = {prgname, prefix, mp_flag, no_hpet};
731 	/* Without --no-hpet */
732 	const char *argv2[] = {prgname, prefix, mp_flag};
733 
734 	if (launch_proc(argv1) != 0) {
735 		printf("Error - process did not run ok with --no-hpet flag\n");
736 		return -1;
737 	}
738 	if (launch_proc(argv2) != 0) {
739 		printf("Error - process did not run ok without --no-hpet flag\n");
740 		return -1;
741 	}
742 	return 0;
743 }
744 
745 /*
746  * Test that the app runs with --no-huge and doesn't run when --socket-mem are
747  * specified with --no-huge.
748  */
749 static int
750 test_no_huge_flag(void)
751 {
752 #ifdef RTE_EXEC_ENV_FREEBSD
753 	/* BSD target doesn't support prefixes at this point, and we also need to
754 	 * run another primary process here */
755 	const char * prefix = no_shconf;
756 #else
757 	const char * prefix = "--file-prefix=nohuge";
758 #endif
759 
760 	/* With --no-huge */
761 	const char *argv1[] = {prgname, prefix, no_huge};
762 	/* With --no-huge and -m */
763 	const char *argv2[] = {prgname, prefix, no_huge,
764 			"-m", DEFAULT_MEM_SIZE};
765 
766 	/* With --no-huge and --socket-mem */
767 	const char *argv3[] = {prgname, prefix, no_huge,
768 			"--socket-mem=" DEFAULT_MEM_SIZE};
769 	/* With --no-huge, -m and --socket-mem */
770 	const char *argv4[] = {prgname, prefix, no_huge,
771 			"-m", DEFAULT_MEM_SIZE, "--socket-mem=" DEFAULT_MEM_SIZE};
772 	if (launch_proc(argv1) != 0) {
773 		printf("Error - process did not run ok with --no-huge flag\n");
774 		return -1;
775 	}
776 	if (launch_proc(argv2) != 0) {
777 		printf("Error - process did not run ok with --no-huge and -m flags\n");
778 		return -1;
779 	}
780 #ifdef RTE_EXEC_ENV_FREEBSD
781 	/* BSD target does not support NUMA, hence no --socket-mem tests */
782 	return 0;
783 #endif
784 
785 	if (launch_proc(argv3) == 0) {
786 		printf("Error - process run ok with --no-huge and --socket-mem "
787 				"flags\n");
788 		return -1;
789 	}
790 	if (launch_proc(argv4) == 0) {
791 		printf("Error - process run ok with --no-huge, -m and "
792 				"--socket-mem flags\n");
793 		return -1;
794 	}
795 	return 0;
796 }
797 
798 static int
799 test_misc_flags(void)
800 {
801 	char hugepath[PATH_MAX] = {0};
802 #ifdef RTE_EXEC_ENV_FREEBSD
803 	/* BSD target doesn't support prefixes at this point */
804 	const char * prefix = "";
805 	const char * nosh_prefix = "";
806 #else
807 	char prefix[PATH_MAX], tmp[PATH_MAX];
808 	const char * nosh_prefix = "--file-prefix=noshconf";
809 	FILE * hugedir_handle = NULL;
810 	char line[PATH_MAX] = {0};
811 	unsigned i, isempty = 1;
812 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
813 		printf("Error - unable to get current prefix!\n");
814 		return -1;
815 	}
816 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
817 
818 	/*
819 	 * get first valid hugepage path
820 	 */
821 
822 	/* get hugetlbfs mountpoints from /proc/mounts */
823 	hugedir_handle = fopen("/proc/mounts", "r");
824 
825 	if (hugedir_handle == NULL) {
826 		printf("Error opening /proc/mounts!\n");
827 		return -1;
828 	}
829 
830 	/* read /proc/mounts */
831 	while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
832 
833 		/* find first valid hugepath */
834 		if (get_hugepage_path(line, sizeof(line), hugepath, sizeof(hugepath)))
835 			break;
836 	}
837 
838 	fclose(hugedir_handle);
839 
840 	/* check if path is not empty */
841 	for (i = 0; i < sizeof(hugepath); i++)
842 		if (hugepath[i] != '\0')
843 			isempty = 0;
844 
845 	if (isempty) {
846 		printf("No mounted hugepage dir found!\n");
847 		return -1;
848 	}
849 #endif
850 
851 
852 	/* check that some general flags don't prevent things from working.
853 	 * All cases, apart from the first, app should run.
854 	 * No further testing of output done.
855 	 */
856 	/* sanity check - failure with invalid option */
857 	const char *argv0[] = {prgname, prefix, mp_flag, "--invalid-opt"};
858 
859 	/* With --no-pci */
860 	const char *argv1[] = {prgname, prefix, mp_flag, "--no-pci"};
861 	/* With -v */
862 	const char *argv2[] = {prgname, prefix, mp_flag, "-v"};
863 	/* With valid --syslog */
864 	const char *argv3[] = {prgname, prefix, mp_flag,
865 			"--syslog", "syslog"};
866 	/* With empty --syslog (should fail) */
867 	const char *argv4[] = {prgname, prefix, mp_flag, "--syslog"};
868 	/* With invalid --syslog */
869 	const char *argv5[] = {prgname, prefix, mp_flag, "--syslog", "error"};
870 	/* With no-sh-conf, also use no-huge to ensure this test runs on BSD */
871 	const char *argv6[] = {prgname, "-m", DEFAULT_MEM_SIZE,
872 			no_shconf, nosh_prefix, no_huge};
873 
874 	/* With --huge-dir */
875 	const char *argv7[] = {prgname, "-m", DEFAULT_MEM_SIZE,
876 			"--file-prefix=hugedir", "--huge-dir", hugepath};
877 	/* With empty --huge-dir (should fail) */
878 	const char *argv8[] = {prgname, "-m", DEFAULT_MEM_SIZE,
879 			"--file-prefix=hugedir", "--huge-dir"};
880 	/* With invalid --huge-dir */
881 	const char *argv9[] = {prgname, "-m", DEFAULT_MEM_SIZE,
882 			"--file-prefix=hugedir", "--huge-dir", "invalid"};
883 	/* Secondary process with invalid --huge-dir (should run as flag has no
884 	 * effect on secondary processes) */
885 	const char *argv10[] = {prgname, prefix, mp_flag,
886 			"--huge-dir", "invalid"};
887 
888 	/* try running with base-virtaddr param */
889 	const char *argv11[] = {prgname, "--file-prefix=virtaddr",
890 			"--base-virtaddr=0x12345678"};
891 
892 	/* try running with --vfio-intr INTx flag */
893 	const char *argv12[] = {prgname, "--file-prefix=intr",
894 			"--vfio-intr=legacy"};
895 
896 	/* try running with --vfio-intr MSI flag */
897 	const char *argv13[] = {prgname, "--file-prefix=intr",
898 			"--vfio-intr=msi"};
899 
900 	/* try running with --vfio-intr MSI-X flag */
901 	const char *argv14[] = {prgname, "--file-prefix=intr",
902 			"--vfio-intr=msix"};
903 
904 	/* try running with --vfio-intr invalid flag */
905 	const char *argv15[] = {prgname, "--file-prefix=intr",
906 			"--vfio-intr=invalid"};
907 
908 	/* With process type as auto-detect */
909 	const char * const argv16[] = {prgname, "--file-prefix=auto",
910 			"--proc-type=auto"};
911 
912 	/* With process type as auto-detect with no-shconf */
913 	const char * const argv17[] = {prgname, "--proc-type=auto",
914 			no_shconf, nosh_prefix, no_huge};
915 
916 	/* With process type as --create-uio-dev flag */
917 	const char * const argv18[] = {prgname, "--file-prefix=uiodev",
918 			"--create-uio-dev"};
919 
920 	/* run all tests also applicable to FreeBSD first */
921 
922 	if (launch_proc(argv0) == 0) {
923 		printf("Error - process ran ok with invalid flag\n");
924 		return -1;
925 	}
926 	if (launch_proc(argv1) != 0) {
927 		printf("Error - process did not run ok with --no-pci flag\n");
928 		return -1;
929 	}
930 	if (launch_proc(argv2) != 0) {
931 		printf("Error - process did not run ok with -v flag\n");
932 		return -1;
933 	}
934 	if (launch_proc(argv6) != 0) {
935 		printf("Error - process did not run ok with --no-shconf flag\n");
936 		return -1;
937 	}
938 
939 #ifdef RTE_EXEC_ENV_FREEBSD
940 	/* no more tests to be done on FreeBSD */
941 	return 0;
942 #endif
943 
944 	if (launch_proc(argv3) != 0) {
945 		printf("Error - process did not run ok with --syslog flag\n");
946 		return -1;
947 	}
948 	if (launch_proc(argv4) == 0) {
949 		printf("Error - process run ok with empty --syslog flag\n");
950 		return -1;
951 	}
952 	if (launch_proc(argv5) == 0) {
953 		printf("Error - process run ok with invalid --syslog flag\n");
954 		return -1;
955 	}
956 	if (launch_proc(argv7) != 0) {
957 		printf("Error - process did not run ok with --huge-dir flag\n");
958 		return -1;
959 	}
960 	if (launch_proc(argv8) == 0) {
961 		printf("Error - process run ok with empty --huge-dir flag\n");
962 		return -1;
963 	}
964 	if (launch_proc(argv9) == 0) {
965 		printf("Error - process run ok with invalid --huge-dir flag\n");
966 		return -1;
967 	}
968 	if (launch_proc(argv10) != 0) {
969 		printf("Error - secondary process did not run ok with invalid --huge-dir flag\n");
970 		return -1;
971 	}
972 	if (launch_proc(argv11) != 0) {
973 		printf("Error - process did not run ok with --base-virtaddr parameter\n");
974 		return -1;
975 	}
976 	if (launch_proc(argv12) != 0) {
977 		printf("Error - process did not run ok with "
978 				"--vfio-intr INTx parameter\n");
979 		return -1;
980 	}
981 	if (launch_proc(argv13) != 0) {
982 		printf("Error - process did not run ok with "
983 				"--vfio-intr MSI parameter\n");
984 		return -1;
985 	}
986 	if (launch_proc(argv14) != 0) {
987 		printf("Error - process did not run ok with "
988 				"--vfio-intr MSI-X parameter\n");
989 		return -1;
990 	}
991 	if (launch_proc(argv15) == 0) {
992 		printf("Error - process run ok with "
993 				"--vfio-intr invalid parameter\n");
994 		return -1;
995 	}
996 	if (launch_proc(argv16) != 0) {
997 		printf("Error - process did not run ok with "
998 				"--proc-type as auto parameter\n");
999 		return -1;
1000 	}
1001 	if (launch_proc(argv17) != 0) {
1002 		printf("Error - process did not run ok with "
1003 				"--proc-type and --no-shconf parameter\n");
1004 		return -1;
1005 	}
1006 	if (launch_proc(argv18) != 0) {
1007 		printf("Error - process did not run ok with "
1008 				"--create-uio-dev parameter\n");
1009 		return -1;
1010 	}
1011 
1012 	return 0;
1013 }
1014 
1015 static int
1016 test_file_prefix(void)
1017 {
1018 	/*
1019 	 * 1. check if current process hugefiles are locked
1020 	 * 2. try to run secondary process without a corresponding primary process
1021 	 * (while failing to run, it will also remove any unused hugepage files)
1022 	 * 3. check if current process hugefiles are still in place and are locked
1023 	 * 4. run a primary process with memtest1 prefix in default and legacy
1024 	 *    mem mode
1025 	 * 5. check if memtest1 hugefiles are created in case of legacy mem
1026 	 *    mode, and deleted in case of default mem mode
1027 	 * 6. run a primary process with memtest2 prefix in default and legacy
1028 	 *    mem modes
1029 	 * 7. check that memtest2 hugefiles are present in the hugedir after a
1030 	 *    run in legacy mode, and not present at all after run in default
1031 	 *    mem mode
1032 	 */
1033 	char prefix[PATH_MAX] = "";
1034 
1035 #ifdef RTE_EXEC_ENV_FREEBSD
1036 	return 0;
1037 #else
1038 	if (get_current_prefix(prefix, sizeof(prefix)) == NULL) {
1039 		printf("Error - unable to get current prefix!\n");
1040 		return -1;
1041 	}
1042 #endif
1043 
1044 	/* this should fail unless the test itself is run with "memtest" prefix */
1045 	const char *argv0[] = {prgname, mp_flag, "-m",
1046 			DEFAULT_MEM_SIZE, "--file-prefix=" memtest };
1047 
1048 	/* primary process with memtest1 and default mem mode */
1049 	const char *argv1[] = {prgname, "-m",
1050 			DEFAULT_MEM_SIZE, "--file-prefix=" memtest1 };
1051 
1052 	/* primary process with memtest1 and legacy mem mode */
1053 	const char *argv2[] = {prgname, "-m",
1054 			DEFAULT_MEM_SIZE, "--file-prefix=" memtest1,
1055 			"--legacy-mem" };
1056 
1057 	/* primary process with memtest2 and legacy mem mode */
1058 	const char *argv3[] = {prgname, "-m",
1059 			DEFAULT_MEM_SIZE, "--file-prefix=" memtest2,
1060 			"--legacy-mem" };
1061 
1062 	/* primary process with memtest2 and default mem mode */
1063 	const char *argv4[] = {prgname, "-m",
1064 			DEFAULT_MEM_SIZE, "--file-prefix=" memtest2 };
1065 
1066 	/* primary process with --in-memory mode */
1067 	const char * const argv5[] = {prgname, "-m",
1068 		DEFAULT_MEM_SIZE, "--in-memory" };
1069 
1070 	/* primary process with memtest1 and --in-memory mode */
1071 	const char * const argv6[] = {prgname, "-m",
1072 		DEFAULT_MEM_SIZE, "--in-memory",
1073 		"--file-prefix=" memtest1 };
1074 
1075 	/* primary process with parent file-prefix and --in-memory mode */
1076 	const char * const argv7[] = {prgname, "-m",
1077 		DEFAULT_MEM_SIZE, "--in-memory", "--file-prefix", prefix };
1078 
1079 	/* primary process with memtest1 and --single-file-segments mode */
1080 	const char * const argv8[] = {prgname, "-m",
1081 		DEFAULT_MEM_SIZE, "--single-file-segments",
1082 		"--file-prefix=" memtest1 };
1083 
1084 	/* check if files for current prefix are present */
1085 	if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
1086 		printf("Error - hugepage files for %s were not created!\n", prefix);
1087 		return -1;
1088 	}
1089 
1090 	/* checks if files for current prefix are locked */
1091 	if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
1092 		printf("Error - hugepages for current process aren't locked!\n");
1093 		return -1;
1094 	}
1095 
1096 	/* check if files for secondary process are present */
1097 	if (process_hugefiles(memtest, HUGEPAGE_CHECK_EXISTS) == 1) {
1098 		/* check if they are not locked */
1099 		if (process_hugefiles(memtest, HUGEPAGE_CHECK_LOCKED) == 1) {
1100 			printf("Error - hugepages for current process are locked!\n");
1101 			return -1;
1102 		}
1103 		/* they aren't locked, delete them */
1104 		else {
1105 			if (process_hugefiles(memtest, HUGEPAGE_DELETE) != 1) {
1106 				printf("Error - deleting hugepages failed!\n");
1107 				return -1;
1108 			}
1109 		}
1110 	}
1111 
1112 	if (launch_proc(argv0) == 0) {
1113 		printf("Error - secondary process ran ok without primary process\n");
1114 		return -1;
1115 	}
1116 
1117 	/* check if files for current prefix are present */
1118 	if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
1119 		printf("Error - hugepage files for %s were not created!\n", prefix);
1120 		return -1;
1121 	}
1122 
1123 	/* checks if files for current prefix are locked */
1124 	if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
1125 		printf("Error - hugepages for current process aren't locked!\n");
1126 		return -1;
1127 	}
1128 
1129 	/* we're running this process in default memory mode, which means it
1130 	 * should clean up after itself on exit and leave no hugepages behind.
1131 	 */
1132 	if (launch_proc(argv1) != 0) {
1133 		printf("Error - failed to run with --file-prefix=%s\n",
1134 				memtest1);
1135 		return -1;
1136 	}
1137 
1138 	/* check if memtest1_map0 is present */
1139 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1140 		printf("Error - hugepage files for %s were not deleted!\n",
1141 				memtest1);
1142 		return -1;
1143 	}
1144 
1145 	/* now, we're running a process under the same prefix, but with legacy
1146 	 * mem mode - this should leave behind hugepage files.
1147 	 */
1148 	if (launch_proc(argv2) != 0) {
1149 		printf("Error - failed to run with --file-prefix=%s\n",
1150 				memtest1);
1151 		return -1;
1152 	}
1153 
1154 	/* check if memtest1_map0 is present */
1155 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 1) {
1156 		printf("Error - hugepage files for %s were not created!\n",
1157 				memtest1);
1158 		return -1;
1159 	}
1160 
1161 	if (launch_proc(argv3) != 0) {
1162 		printf("Error - failed to run with --file-prefix=%s\n",
1163 				memtest2);
1164 		return -1;
1165 	}
1166 
1167 	/* check if hugefiles for memtest2 are present */
1168 	if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 1) {
1169 		printf("Error - hugepage files for %s were not created!\n",
1170 				memtest2);
1171 		return -1;
1172 	}
1173 
1174 	/* check if hugefiles for memtest1 are present */
1175 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1176 		printf("Error - hugepage files for %s were not deleted!\n",
1177 				memtest1);
1178 		return -1;
1179 	}
1180 
1181 	/* this process will run in default mem mode, so it should not leave any
1182 	 * hugepage files behind.
1183 	 */
1184 	if (launch_proc(argv4) != 0) {
1185 		printf("Error - failed to run with --file-prefix=%s\n",
1186 				memtest2);
1187 		return -1;
1188 	}
1189 
1190 	/* check if hugefiles for memtest2 are present */
1191 	if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 0) {
1192 		printf("Error - hugepage files for %s were not deleted!\n",
1193 				memtest2);
1194 		return -1;
1195 	}
1196 
1197 	/* check if hugefiles for memtest1 are present */
1198 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1199 		printf("Error - hugepage files for %s were not deleted!\n",
1200 				memtest1);
1201 		return -1;
1202 	}
1203 
1204 	/* this process will run in --in-memory mode, so it should not leave any
1205 	 * hugepage files behind.
1206 	 */
1207 
1208 	/* test case to check eal-options with --in-memory mode */
1209 	if (launch_proc(argv5) != 0) {
1210 		printf("Error - failed to run with --in-memory mode\n");
1211 		return -1;
1212 	}
1213 
1214 	/*test case to check eal-options with --in-memory mode with
1215 	 * custom file-prefix.
1216 	 */
1217 	if (launch_proc(argv6) != 0) {
1218 		printf("Error - failed to run with --in-memory mode\n");
1219 		return -1;
1220 	}
1221 
1222 	/* check if hugefiles for memtest1 are present */
1223 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1224 		printf("Error - hugepage files for %s were created and not deleted!\n",
1225 				memtest1);
1226 		return -1;
1227 	}
1228 
1229 	/* test case to check eal-options with --in-memory mode with
1230 	 * parent file-prefix.
1231 	 */
1232 	if (launch_proc(argv7) != 0) {
1233 		printf("Error - failed to run with --file-prefix=%s\n", prefix);
1234 		return -1;
1235 	}
1236 
1237 	/* this process will run in --single-file-segments mode,
1238 	 * so it should not leave any hugepage files behind.
1239 	 */
1240 	if (launch_proc(argv8) != 0) {
1241 		printf("Error - failed to run with --single-file-segments mode\n");
1242 		return -1;
1243 	}
1244 
1245 	/* check if hugefiles for memtest1 are present */
1246 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1247 		printf("Error - hugepage files for %s were not deleted!\n",
1248 				memtest1);
1249 		return -1;
1250 	}
1251 
1252 	return 0;
1253 }
1254 
1255 /* This function writes in passed buf pointer a valid --socket-mem= option
1256  * for num_sockets then concatenates the provided suffix string.
1257  *
1258  * Example for num_sockets 4, mem "2", suffix "plop"
1259  * --socket-mem=2,2,2,2plop
1260  */
1261 static void
1262 populate_socket_mem_param(int num_sockets, const char *mem,
1263 		const char *suffix, char *buf, size_t buf_size)
1264 {
1265 	unsigned int offset = 0;
1266 	int written;
1267 	int i;
1268 
1269 	written = snprintf(&buf[offset], buf_size - offset, "--socket-mem=");
1270 	if (written < 0 || written + offset >= buf_size)
1271 		return;
1272 	offset += written;
1273 
1274 	for (i = 0; i < num_sockets - 1; i++) {
1275 		written = snprintf(&buf[offset], buf_size - offset,
1276 			"%s,", mem);
1277 		if (written < 0 || written + offset >= buf_size)
1278 			return;
1279 		offset += written;
1280 	}
1281 
1282 	written = snprintf(&buf[offset], buf_size - offset, "%s%s", mem,
1283 		suffix);
1284 	if (written < 0 || written + offset >= buf_size)
1285 		return;
1286 	offset += written;
1287 }
1288 
1289 /*
1290  * Tests for correct handling of -m and --socket-mem flags
1291  */
1292 static int
1293 test_memory_flags(void)
1294 {
1295 #ifdef RTE_EXEC_ENV_FREEBSD
1296 	/* BSD target doesn't support prefixes at this point */
1297 	const char * prefix = "";
1298 #else
1299 	char prefix[PATH_MAX], tmp[PATH_MAX];
1300 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
1301 		printf("Error - unable to get current prefix!\n");
1302 		return -1;
1303 	}
1304 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
1305 #endif
1306 
1307 	/* valid -m flag and mp flag */
1308 	const char *argv0[] = {prgname, prefix, mp_flag,
1309 			"-m", DEFAULT_MEM_SIZE};
1310 
1311 	/* valid -m flag */
1312 	const char *argv1[] = {prgname,
1313 			"--file-prefix=" memtest, "-m", DEFAULT_MEM_SIZE};
1314 
1315 	/* valid (zero) --socket-mem flag */
1316 	char arg2_socket_mem[SOCKET_MEM_STRLEN];
1317 	const char *argv2[] = {prgname,
1318 			"--file-prefix=" memtest, arg2_socket_mem};
1319 
1320 	/* invalid (incomplete) --socket-mem flag */
1321 	char arg3_socket_mem[SOCKET_MEM_STRLEN];
1322 	const char *argv3[] = {prgname,
1323 			"--file-prefix=" memtest, arg3_socket_mem};
1324 
1325 	/* invalid (mixed with invalid data) --socket-mem flag */
1326 	char arg4_socket_mem[SOCKET_MEM_STRLEN];
1327 	const char *argv4[] = {prgname,
1328 			"--file-prefix=" memtest, arg4_socket_mem};
1329 
1330 	/* invalid (with numeric value as last character) --socket-mem flag */
1331 	char arg5_socket_mem[SOCKET_MEM_STRLEN];
1332 	const char *argv5[] = {prgname,
1333 			"--file-prefix=" memtest, arg5_socket_mem};
1334 
1335 	/* invalid (with empty socket) --socket-mem flag */
1336 	char arg6_socket_mem[SOCKET_MEM_STRLEN];
1337 	const char *argv6[] = {prgname,
1338 			"--file-prefix=" memtest, arg6_socket_mem};
1339 
1340 	/* invalid (null) --socket-mem flag */
1341 	const char *argv7[] = {prgname,
1342 			"--file-prefix=" memtest, "--socket-mem="};
1343 
1344 	/* valid --socket-mem specified together with -m flag */
1345 	char arg8_socket_mem[SOCKET_MEM_STRLEN];
1346 	const char *argv8[] = {prgname,
1347 			"--file-prefix=" memtest, "-m", DEFAULT_MEM_SIZE,
1348 			arg8_socket_mem};
1349 
1350 #ifdef RTE_EXEC_ENV_FREEBSD
1351 	int num_sockets = 1;
1352 #else
1353 	int num_sockets = RTE_MIN(get_number_of_sockets(),
1354 			RTE_MAX_NUMA_NODES);
1355 #endif
1356 
1357 	if (num_sockets <= 0) {
1358 		printf("Error - cannot get number of sockets!\n");
1359 		return -1;
1360 	}
1361 
1362 	/* invalid --socket-mem flag (with extra socket) */
1363 	char invalid_socket_mem[SOCKET_MEM_STRLEN];
1364 	const char *argv9[] = {prgname,
1365 			"--file-prefix=" memtest, invalid_socket_mem};
1366 
1367 	/* valid --socket-mem flag */
1368 	char valid_socket_mem[SOCKET_MEM_STRLEN];
1369 	const char *argv10[] = {prgname,
1370 			"--file-prefix=" memtest, valid_socket_mem};
1371 
1372 	if (launch_proc(argv0) != 0) {
1373 		printf("Error - secondary process failed with valid -m flag !\n");
1374 		return -1;
1375 	}
1376 
1377 #ifdef RTE_EXEC_ENV_FREEBSD
1378 	/* no other tests are applicable to BSD */
1379 	return 0;
1380 #endif
1381 
1382 	if (launch_proc(argv1) != 0) {
1383 		printf("Error - process failed with valid -m flag!\n");
1384 		return -1;
1385 	}
1386 
1387 	populate_socket_mem_param(num_sockets, "0", "",
1388 		arg2_socket_mem, sizeof(arg2_socket_mem));
1389 	if (launch_proc(argv2) != 0) {
1390 		printf("Error - process failed with valid (zero) --socket-mem!\n");
1391 		return -1;
1392 	}
1393 
1394 	if (num_sockets > 1) {
1395 		populate_socket_mem_param(num_sockets - 1, "2", ",",
1396 			arg3_socket_mem, sizeof(arg3_socket_mem));
1397 		if (launch_proc(argv3) == 0) {
1398 			printf("Error - process run ok with invalid "
1399 				"(incomplete) --socket-mem!\n");
1400 			return -1;
1401 		}
1402 
1403 		populate_socket_mem_param(num_sockets - 1, "2", ",Fred",
1404 			arg4_socket_mem, sizeof(arg4_socket_mem));
1405 		if (launch_proc(argv4) == 0) {
1406 			printf("Error - process run ok with invalid "
1407 				"(mixed with invalid input) --socket-mem!\n");
1408 			return -1;
1409 		}
1410 
1411 		populate_socket_mem_param(num_sockets - 1, "2", ",Fred0",
1412 			arg5_socket_mem, sizeof(arg5_socket_mem));
1413 		if (launch_proc(argv5) == 0) {
1414 			printf("Error - process run ok with invalid "
1415 				"(mixed with invalid input with a numeric value as "
1416 				"last character) --socket-mem!\n");
1417 			return -1;
1418 		}
1419 	}
1420 
1421 	if (num_sockets > 2) {
1422 		populate_socket_mem_param(num_sockets - 2, "2", ",,2",
1423 			arg6_socket_mem, sizeof(arg6_socket_mem));
1424 		if (launch_proc(argv6) == 0) {
1425 			printf("Error - process run ok with invalid "
1426 				"(with empty socket) --socket-mem!\n");
1427 			return -1;
1428 		}
1429 	}
1430 
1431 	if (launch_proc(argv7) == 0) {
1432 		printf("Error - process run ok with invalid (null) --socket-mem!\n");
1433 		return -1;
1434 	}
1435 
1436 	populate_socket_mem_param(num_sockets, "2", "",
1437 		arg8_socket_mem, sizeof(arg8_socket_mem));
1438 	if (launch_proc(argv8) == 0) {
1439 		printf("Error - process run ok with --socket-mem and -m specified!\n");
1440 		return -1;
1441 	}
1442 
1443 	populate_socket_mem_param(num_sockets + 1, "2", "",
1444 		invalid_socket_mem, sizeof(invalid_socket_mem));
1445 	if (launch_proc(argv9) == 0) {
1446 		printf("Error - process run ok with extra socket in --socket-mem!\n");
1447 		return -1;
1448 	}
1449 
1450 	populate_socket_mem_param(num_sockets, "2", "",
1451 		valid_socket_mem, sizeof(valid_socket_mem));
1452 	if (launch_proc(argv10) != 0) {
1453 		printf("Error - process failed with valid --socket-mem!\n");
1454 		return -1;
1455 	}
1456 
1457 	return 0;
1458 }
1459 
1460 static int
1461 test_eal_flags(void)
1462 {
1463 	int ret = 0;
1464 
1465 	ret = test_missing_c_flag();
1466 	if (ret < 0) {
1467 		printf("Error in test_missing_c_flag()\n");
1468 		return ret;
1469 	}
1470 
1471 	ret = test_main_lcore_flag();
1472 	if (ret < 0) {
1473 		printf("Error in test_main_lcore_flag()\n");
1474 		return ret;
1475 	}
1476 
1477 	ret = test_invalid_n_flag();
1478 	if (ret < 0) {
1479 		printf("Error in test_invalid_n_flag()\n");
1480 		return ret;
1481 	}
1482 
1483 	ret = test_no_hpet_flag();
1484 	if (ret < 0) {
1485 		printf("Error in test_no_hpet_flag()\n");
1486 		return ret;
1487 	}
1488 
1489 	ret = test_no_huge_flag();
1490 	if (ret < 0) {
1491 		printf("Error in test_no_huge_flag()\n");
1492 		return ret;
1493 	}
1494 
1495 	ret = test_allow_flag();
1496 	if (ret < 0) {
1497 		printf("Error in test_allow_flag()\n");
1498 		return ret;
1499 	}
1500 
1501 	ret = test_invalid_b_flag();
1502 	if (ret < 0) {
1503 		printf("Error in test_invalid_b_flag()\n");
1504 		return ret;
1505 	}
1506 
1507 #ifdef RTE_NET_RING
1508 	ret = test_invalid_vdev_flag();
1509 	if (ret < 0) {
1510 		printf("Error in test_invalid_vdev_flag()\n");
1511 		return ret;
1512 	}
1513 #endif
1514 	ret = test_invalid_r_flag();
1515 	if (ret < 0) {
1516 		printf("Error in test_invalid_r_flag()\n");
1517 		return ret;
1518 	}
1519 
1520 	ret = test_memory_flags();
1521 	if (ret < 0) {
1522 		printf("Error in test_memory_flags()\n");
1523 		return ret;
1524 	}
1525 
1526 	ret = test_file_prefix();
1527 	if (ret < 0) {
1528 		printf("Error in test_file_prefix()\n");
1529 		return ret;
1530 	}
1531 
1532 	ret = test_misc_flags();
1533 	if (ret < 0) {
1534 		printf("Error in test_misc_flags()");
1535 		return ret;
1536 	}
1537 
1538 	return ret;
1539 }
1540 
1541 REGISTER_TEST_COMMAND(eal_flags_autotest, test_eal_flags);
1542 
1543 /* subtests used in meson for CI */
1544 REGISTER_TEST_COMMAND(eal_flags_c_opt_autotest, test_missing_c_flag);
1545 REGISTER_TEST_COMMAND(eal_flags_main_opt_autotest, test_main_lcore_flag);
1546 REGISTER_TEST_COMMAND(eal_flags_n_opt_autotest, test_invalid_n_flag);
1547 REGISTER_TEST_COMMAND(eal_flags_hpet_autotest, test_no_hpet_flag);
1548 REGISTER_TEST_COMMAND(eal_flags_no_huge_autotest, test_no_huge_flag);
1549 REGISTER_TEST_COMMAND(eal_flags_a_opt_autotest, test_allow_flag);
1550 REGISTER_TEST_COMMAND(eal_flags_b_opt_autotest, test_invalid_b_flag);
1551 REGISTER_TEST_COMMAND(eal_flags_vdev_opt_autotest, test_invalid_vdev_flag);
1552 REGISTER_TEST_COMMAND(eal_flags_r_opt_autotest, test_invalid_r_flag);
1553 REGISTER_TEST_COMMAND(eal_flags_mem_autotest, test_memory_flags);
1554 REGISTER_TEST_COMMAND(eal_flags_file_prefix_autotest, test_file_prefix);
1555 REGISTER_TEST_COMMAND(eal_flags_misc_autotest, test_misc_flags);
1556