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