xref: /dpdk/app/test/test_eal_flags.c (revision 7564d550)
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 pci_whitelist "--pci-whitelist"
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 whitelist 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_whitelist_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 				pci_whitelist, "error", "", ""},
249 		{prgname, prefix, mp_flag,
250 				pci_whitelist, "0:0:0", "", ""},
251 		{prgname, prefix, mp_flag,
252 				pci_whitelist, "0:error:0.1", "", ""},
253 		{prgname, prefix, mp_flag,
254 				pci_whitelist, "0:0:0.1error", "", ""},
255 		{prgname, prefix, mp_flag,
256 				pci_whitelist, "error0:0:0.1", "", ""},
257 		{prgname, prefix, mp_flag,
258 				pci_whitelist, "0:0:0.1.2", "", ""},
259 	};
260 	/* Test with valid whitelist option */
261 	const char *wlval1[] = {prgname, prefix, mp_flag,
262 			pci_whitelist, "00FF:09:0B.3"};
263 	const char *wlval2[] = {prgname, prefix, mp_flag,
264 			pci_whitelist, "09:0B.3", pci_whitelist, "0a:0b.1"};
265 	const char *wlval3[] = {prgname, prefix, mp_flag,
266 			pci_whitelist, "09:0B.3,type=test",
267 			pci_whitelist, "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 			    "whitelist parameter\n");
274 			return -1;
275 		}
276 	}
277 	if (launch_proc(wlval1) != 0 ) {
278 		printf("Error - process did not run ok with valid whitelist\n");
279 		return -1;
280 	}
281 	if (launch_proc(wlval2) != 0 ) {
282 		printf("Error - process did not run ok with valid whitelist value set\n");
283 		return -1;
284 	}
285 	if (launch_proc(wlval3) != 0 ) {
286 		printf("Error - process did not run ok with valid whitelist + args\n");
287 		return -1;
288 	}
289 
290 	return 0;
291 }
292 
293 /*
294  * Test that the app doesn't run with invalid blacklist 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 blacklist 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 			    "blacklist parameter\n");
330 			return -1;
331 		}
332 	}
333 	if (launch_proc(blval) != 0) {
334 		printf("Error - process did not run ok with valid blacklist 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_LIBRTE_PMD_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 blacklist 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 
531 	if (launch_proc(argv2) != 0) {
532 		printf("Error - "
533 		       "process did not run ok when missing -c flag\n");
534 		return -1;
535 	}
536 
537 	if (launch_proc(argv1) == 0
538 			|| launch_proc(argv3) == 0) {
539 		printf("Error - "
540 		       "process ran without error with invalid -c flag\n");
541 		return -1;
542 	}
543 	if (launch_proc(argv4) != 0) {
544 		printf("Error - "
545 		       "process did not run ok with valid coremask value\n");
546 		return -1;
547 	}
548 
549 	/* start -l test */
550 	if (launch_proc(argv5) == 0
551 			|| launch_proc(argv6) == 0
552 			|| launch_proc(argv7) == 0
553 			|| launch_proc(argv8) == 0
554 			|| launch_proc(argv9) == 0
555 			|| launch_proc(argv10) == 0
556 			|| launch_proc(argv11) == 0
557 			|| launch_proc(argv12) == 0
558 			|| launch_proc(argv13) == 0
559 			|| launch_proc(argv14) == 0) {
560 		printf("Error - "
561 		       "process ran without error with invalid -l flag\n");
562 		return -1;
563 	}
564 	if (rte_lcore_is_enabled(0) && rte_lcore_is_enabled(1) &&
565 	    rte_lcore_is_enabled(2) && rte_lcore_is_enabled(3) &&
566 	    launch_proc(argv15) != 0) {
567 		printf("Error - "
568 		       "process did not run ok with valid corelist value\n");
569 		return -1;
570 	}
571 
572 	/* start --lcores tests */
573 	if (launch_proc(argv16) == 0 || launch_proc(argv17) == 0 ||
574 	    launch_proc(argv18) == 0 || launch_proc(argv19) == 0 ||
575 	    launch_proc(argv20) == 0 || launch_proc(argv21) == 0 ||
576 	    launch_proc(argv22) == 0 || launch_proc(argv23) == 0 ||
577 	    launch_proc(argv24) == 0 || launch_proc(argv25) == 0 ||
578 	    launch_proc(argv26) == 0 || launch_proc(argv27) == 0 ||
579 	    launch_proc(argv28) == 0) {
580 		printf("Error - "
581 		       "process ran without error with invalid --lcore flag\n");
582 		return -1;
583 	}
584 
585 	if (rte_lcore_is_enabled(0) && rte_lcore_is_enabled(1) &&
586 	    rte_lcore_is_enabled(2) && rte_lcore_is_enabled(3) &&
587 	    rte_lcore_is_enabled(3) && rte_lcore_is_enabled(5) &&
588 	    rte_lcore_is_enabled(4) && rte_lcore_is_enabled(7) &&
589 	    launch_proc(argv29) != 0) {
590 		printf("Error - "
591 		       "process did not run ok with valid corelist value\n");
592 		return -1;
593 	}
594 
595 	return 0;
596 }
597 
598 /*
599  * Test --master-lcore option with matching coremask
600  */
601 static int
602 test_master_lcore_flag(void)
603 {
604 #ifdef RTE_EXEC_ENV_FREEBSD
605 	/* BSD target doesn't support prefixes at this point */
606 	const char *prefix = "";
607 #else
608 	char prefix[PATH_MAX], tmp[PATH_MAX];
609 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
610 		printf("Error - unable to get current prefix!\n");
611 		return -1;
612 	}
613 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
614 #endif
615 
616 	if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1))
617 		return TEST_SKIPPED;
618 
619 	/* --master-lcore flag but no value */
620 	const char *argv1[] = { prgname, prefix, mp_flag,
621 				"-c", "3", "--master-lcore"};
622 	/* --master-lcore flag with invalid value */
623 	const char *argv2[] = { prgname, prefix, mp_flag,
624 				"-c", "3", "--master-lcore", "-1"};
625 	const char *argv3[] = { prgname, prefix, mp_flag,
626 				"-c", "3", "--master-lcore", "X"};
627 	/* master lcore not in coremask */
628 	const char *argv4[] = { prgname, prefix, mp_flag,
629 				"-c", "3", "--master-lcore", "2"};
630 	/* valid value */
631 	const char *argv5[] = { prgname, prefix, mp_flag,
632 				"-c", "3", "--master-lcore", "1"};
633 	/* valid value set before coremask */
634 	const char *argv6[] = { prgname, prefix, mp_flag,
635 				"--master-lcore", "1", "-c", "3"};
636 
637 	if (launch_proc(argv1) == 0
638 			|| launch_proc(argv2) == 0
639 			|| launch_proc(argv3) == 0
640 			|| launch_proc(argv4) == 0) {
641 		printf("Error - process ran without error with wrong --master-lcore\n");
642 		return -1;
643 	}
644 	if (launch_proc(argv5) != 0
645 			|| launch_proc(argv6) != 0) {
646 		printf("Error - process did not run ok with valid --master-lcore\n");
647 		return -1;
648 	}
649 	return 0;
650 }
651 
652 /*
653  * Test that the app doesn't run with invalid -n flag option.
654  * Final test ensures it does run with valid options as sanity check
655  * Since -n is not compulsory for MP, we instead use --no-huge and --no-shconf
656  * flags.
657  */
658 static int
659 test_invalid_n_flag(void)
660 {
661 #ifdef RTE_EXEC_ENV_FREEBSD
662 	/* BSD target doesn't support prefixes at this point */
663 	const char * prefix = "";
664 #else
665 	char prefix[PATH_MAX], tmp[PATH_MAX];
666 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
667 		printf("Error - unable to get current prefix!\n");
668 		return -1;
669 	}
670 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
671 #endif
672 
673 	/* -n flag but no value */
674 	const char *argv1[] = { prgname, prefix, no_huge, no_shconf,
675 				"-n"};
676 	/* bad numeric value */
677 	const char *argv2[] = { prgname, prefix, no_huge, no_shconf,
678 				"-n", "e" };
679 	/* zero is invalid */
680 	const char *argv3[] = { prgname, prefix, no_huge, no_shconf,
681 				"-n", "0" };
682 	/* sanity test - check with good value */
683 	const char *argv4[] = { prgname, prefix, no_huge, no_shconf,
684 				"-n", "2" };
685 	/* sanity test - check with no -n flag */
686 	const char *argv5[] = { prgname, prefix, no_huge, no_shconf};
687 
688 	if (launch_proc(argv1) == 0
689 			|| launch_proc(argv2) == 0
690 			|| launch_proc(argv3) == 0) {
691 		printf("Error - process ran without error when"
692 		       "invalid -n flag\n");
693 		return -1;
694 	}
695 	if (launch_proc(argv4) != 0) {
696 		printf("Error - process did not run ok with valid num-channel value\n");
697 		return -1;
698 	}
699 	if (launch_proc(argv5) != 0) {
700 		printf("Error - process did not run ok without -n flag\n");
701 		return -1;
702 	}
703 
704 	return 0;
705 }
706 
707 /*
708  * Test that the app runs with HPET, and without HPET
709  */
710 static int
711 test_no_hpet_flag(void)
712 {
713 	char prefix[PATH_MAX] = "";
714 
715 #ifdef RTE_EXEC_ENV_FREEBSD
716 	return 0;
717 #else
718 	char tmp[PATH_MAX];
719 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
720 		printf("Error - unable to get current prefix!\n");
721 		return -1;
722 	}
723 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
724 #endif
725 
726 	/* With --no-hpet */
727 	const char *argv1[] = {prgname, prefix, mp_flag, no_hpet};
728 	/* Without --no-hpet */
729 	const char *argv2[] = {prgname, prefix, mp_flag};
730 
731 	if (launch_proc(argv1) != 0) {
732 		printf("Error - process did not run ok with --no-hpet flag\n");
733 		return -1;
734 	}
735 	if (launch_proc(argv2) != 0) {
736 		printf("Error - process did not run ok without --no-hpet flag\n");
737 		return -1;
738 	}
739 	return 0;
740 }
741 
742 /*
743  * Test that the app runs with --no-huge and doesn't run when --socket-mem are
744  * specified with --no-huge.
745  */
746 static int
747 test_no_huge_flag(void)
748 {
749 #ifdef RTE_EXEC_ENV_FREEBSD
750 	/* BSD target doesn't support prefixes at this point, and we also need to
751 	 * run another primary process here */
752 	const char * prefix = no_shconf;
753 #else
754 	const char * prefix = "--file-prefix=nohuge";
755 #endif
756 
757 	/* With --no-huge */
758 	const char *argv1[] = {prgname, prefix, no_huge};
759 	/* With --no-huge and -m */
760 	const char *argv2[] = {prgname, prefix, no_huge,
761 			"-m", DEFAULT_MEM_SIZE};
762 
763 	/* With --no-huge and --socket-mem */
764 	const char *argv3[] = {prgname, prefix, no_huge,
765 			"--socket-mem=" DEFAULT_MEM_SIZE};
766 	/* With --no-huge, -m and --socket-mem */
767 	const char *argv4[] = {prgname, prefix, no_huge,
768 			"-m", DEFAULT_MEM_SIZE, "--socket-mem=" DEFAULT_MEM_SIZE};
769 	if (launch_proc(argv1) != 0) {
770 		printf("Error - process did not run ok with --no-huge flag\n");
771 		return -1;
772 	}
773 	if (launch_proc(argv2) != 0) {
774 		printf("Error - process did not run ok with --no-huge and -m flags\n");
775 		return -1;
776 	}
777 #ifdef RTE_EXEC_ENV_FREEBSD
778 	/* BSD target does not support NUMA, hence no --socket-mem tests */
779 	return 0;
780 #endif
781 
782 	if (launch_proc(argv3) == 0) {
783 		printf("Error - process run ok with --no-huge and --socket-mem "
784 				"flags\n");
785 		return -1;
786 	}
787 	if (launch_proc(argv4) == 0) {
788 		printf("Error - process run ok with --no-huge, -m and "
789 				"--socket-mem flags\n");
790 		return -1;
791 	}
792 	return 0;
793 }
794 
795 static int
796 test_misc_flags(void)
797 {
798 	char hugepath[PATH_MAX] = {0};
799 #ifdef RTE_EXEC_ENV_FREEBSD
800 	/* BSD target doesn't support prefixes at this point */
801 	const char * prefix = "";
802 	const char * nosh_prefix = "";
803 #else
804 	char prefix[PATH_MAX], tmp[PATH_MAX];
805 	const char * nosh_prefix = "--file-prefix=noshconf";
806 	FILE * hugedir_handle = NULL;
807 	char line[PATH_MAX] = {0};
808 	unsigned i, isempty = 1;
809 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
810 		printf("Error - unable to get current prefix!\n");
811 		return -1;
812 	}
813 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
814 
815 	/*
816 	 * get first valid hugepage path
817 	 */
818 
819 	/* get hugetlbfs mountpoints from /proc/mounts */
820 	hugedir_handle = fopen("/proc/mounts", "r");
821 
822 	if (hugedir_handle == NULL) {
823 		printf("Error opening /proc/mounts!\n");
824 		return -1;
825 	}
826 
827 	/* read /proc/mounts */
828 	while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
829 
830 		/* find first valid hugepath */
831 		if (get_hugepage_path(line, sizeof(line), hugepath, sizeof(hugepath)))
832 			break;
833 	}
834 
835 	fclose(hugedir_handle);
836 
837 	/* check if path is not empty */
838 	for (i = 0; i < sizeof(hugepath); i++)
839 		if (hugepath[i] != '\0')
840 			isempty = 0;
841 
842 	if (isempty) {
843 		printf("No mounted hugepage dir found!\n");
844 		return -1;
845 	}
846 #endif
847 
848 
849 	/* check that some general flags don't prevent things from working.
850 	 * All cases, apart from the first, app should run.
851 	 * No further testing of output done.
852 	 */
853 	/* sanity check - failure with invalid option */
854 	const char *argv0[] = {prgname, prefix, mp_flag, "--invalid-opt"};
855 
856 	/* With --no-pci */
857 	const char *argv1[] = {prgname, prefix, mp_flag, "--no-pci"};
858 	/* With -v */
859 	const char *argv2[] = {prgname, prefix, mp_flag, "-v"};
860 	/* With valid --syslog */
861 	const char *argv3[] = {prgname, prefix, mp_flag,
862 			"--syslog", "syslog"};
863 	/* With empty --syslog (should fail) */
864 	const char *argv4[] = {prgname, prefix, mp_flag, "--syslog"};
865 	/* With invalid --syslog */
866 	const char *argv5[] = {prgname, prefix, mp_flag, "--syslog", "error"};
867 	/* With no-sh-conf, also use no-huge to ensure this test runs on BSD */
868 	const char *argv6[] = {prgname, "-m", DEFAULT_MEM_SIZE,
869 			no_shconf, nosh_prefix, no_huge};
870 
871 	/* With --huge-dir */
872 	const char *argv7[] = {prgname, "-m", DEFAULT_MEM_SIZE,
873 			"--file-prefix=hugedir", "--huge-dir", hugepath};
874 	/* With empty --huge-dir (should fail) */
875 	const char *argv8[] = {prgname, "-m", DEFAULT_MEM_SIZE,
876 			"--file-prefix=hugedir", "--huge-dir"};
877 	/* With invalid --huge-dir */
878 	const char *argv9[] = {prgname, "-m", DEFAULT_MEM_SIZE,
879 			"--file-prefix=hugedir", "--huge-dir", "invalid"};
880 	/* Secondary process with invalid --huge-dir (should run as flag has no
881 	 * effect on secondary processes) */
882 	const char *argv10[] = {prgname, prefix, mp_flag,
883 			"--huge-dir", "invalid"};
884 
885 	/* try running with base-virtaddr param */
886 	const char *argv11[] = {prgname, "--file-prefix=virtaddr",
887 			"--base-virtaddr=0x12345678"};
888 
889 	/* try running with --vfio-intr INTx flag */
890 	const char *argv12[] = {prgname, "--file-prefix=intr",
891 			"--vfio-intr=legacy"};
892 
893 	/* try running with --vfio-intr MSI flag */
894 	const char *argv13[] = {prgname, "--file-prefix=intr",
895 			"--vfio-intr=msi"};
896 
897 	/* try running with --vfio-intr MSI-X flag */
898 	const char *argv14[] = {prgname, "--file-prefix=intr",
899 			"--vfio-intr=msix"};
900 
901 	/* try running with --vfio-intr invalid flag */
902 	const char *argv15[] = {prgname, "--file-prefix=intr",
903 			"--vfio-intr=invalid"};
904 
905 	/* With process type as auto-detect */
906 	const char * const argv16[] = {prgname, "--file-prefix=auto",
907 			"--proc-type=auto"};
908 
909 	/* With process type as auto-detect with no-shconf */
910 	const char * const argv17[] = {prgname, "--proc-type=auto",
911 			no_shconf, nosh_prefix, no_huge};
912 
913 	/* With process type as --create-uio-dev flag */
914 	const char * const argv18[] = {prgname, "--file-prefix=uiodev",
915 			"--create-uio-dev"};
916 
917 	/* run all tests also applicable to FreeBSD first */
918 
919 	if (launch_proc(argv0) == 0) {
920 		printf("Error - process ran ok with invalid flag\n");
921 		return -1;
922 	}
923 	if (launch_proc(argv1) != 0) {
924 		printf("Error - process did not run ok with --no-pci flag\n");
925 		return -1;
926 	}
927 	if (launch_proc(argv2) != 0) {
928 		printf("Error - process did not run ok with -v flag\n");
929 		return -1;
930 	}
931 	if (launch_proc(argv6) != 0) {
932 		printf("Error - process did not run ok with --no-shconf flag\n");
933 		return -1;
934 	}
935 
936 #ifdef RTE_EXEC_ENV_FREEBSD
937 	/* no more tests to be done on FreeBSD */
938 	return 0;
939 #endif
940 
941 	if (launch_proc(argv3) != 0) {
942 		printf("Error - process did not run ok with --syslog flag\n");
943 		return -1;
944 	}
945 	if (launch_proc(argv4) == 0) {
946 		printf("Error - process run ok with empty --syslog flag\n");
947 		return -1;
948 	}
949 	if (launch_proc(argv5) == 0) {
950 		printf("Error - process run ok with invalid --syslog flag\n");
951 		return -1;
952 	}
953 	if (launch_proc(argv7) != 0) {
954 		printf("Error - process did not run ok with --huge-dir flag\n");
955 		return -1;
956 	}
957 	if (launch_proc(argv8) == 0) {
958 		printf("Error - process run ok with empty --huge-dir flag\n");
959 		return -1;
960 	}
961 	if (launch_proc(argv9) == 0) {
962 		printf("Error - process run ok with invalid --huge-dir flag\n");
963 		return -1;
964 	}
965 	if (launch_proc(argv10) != 0) {
966 		printf("Error - secondary process did not run ok with invalid --huge-dir flag\n");
967 		return -1;
968 	}
969 	if (launch_proc(argv11) != 0) {
970 		printf("Error - process did not run ok with --base-virtaddr parameter\n");
971 		return -1;
972 	}
973 	if (launch_proc(argv12) != 0) {
974 		printf("Error - process did not run ok with "
975 				"--vfio-intr INTx parameter\n");
976 		return -1;
977 	}
978 	if (launch_proc(argv13) != 0) {
979 		printf("Error - process did not run ok with "
980 				"--vfio-intr MSI parameter\n");
981 		return -1;
982 	}
983 	if (launch_proc(argv14) != 0) {
984 		printf("Error - process did not run ok with "
985 				"--vfio-intr MSI-X parameter\n");
986 		return -1;
987 	}
988 	if (launch_proc(argv15) == 0) {
989 		printf("Error - process run ok with "
990 				"--vfio-intr invalid parameter\n");
991 		return -1;
992 	}
993 	if (launch_proc(argv16) != 0) {
994 		printf("Error - process did not run ok with "
995 				"--proc-type as auto parameter\n");
996 		return -1;
997 	}
998 	if (launch_proc(argv17) != 0) {
999 		printf("Error - process did not run ok with "
1000 				"--proc-type and --no-shconf parameter\n");
1001 		return -1;
1002 	}
1003 	if (launch_proc(argv18) != 0) {
1004 		printf("Error - process did not run ok with "
1005 				"--create-uio-dev parameter\n");
1006 		return -1;
1007 	}
1008 
1009 	return 0;
1010 }
1011 
1012 static int
1013 test_file_prefix(void)
1014 {
1015 	/*
1016 	 * 1. check if current process hugefiles are locked
1017 	 * 2. try to run secondary process without a corresponding primary process
1018 	 * (while failing to run, it will also remove any unused hugepage files)
1019 	 * 3. check if current process hugefiles are still in place and are locked
1020 	 * 4. run a primary process with memtest1 prefix in default and legacy
1021 	 *    mem mode
1022 	 * 5. check if memtest1 hugefiles are created in case of legacy mem
1023 	 *    mode, and deleted in case of default mem mode
1024 	 * 6. run a primary process with memtest2 prefix in default and legacy
1025 	 *    mem modes
1026 	 * 7. check that memtest2 hugefiles are present in the hugedir after a
1027 	 *    run in legacy mode, and not present at all after run in default
1028 	 *    mem mode
1029 	 */
1030 	char prefix[PATH_MAX] = "";
1031 
1032 #ifdef RTE_EXEC_ENV_FREEBSD
1033 	return 0;
1034 #else
1035 	if (get_current_prefix(prefix, sizeof(prefix)) == NULL) {
1036 		printf("Error - unable to get current prefix!\n");
1037 		return -1;
1038 	}
1039 #endif
1040 
1041 	/* this should fail unless the test itself is run with "memtest" prefix */
1042 	const char *argv0[] = {prgname, mp_flag, "-m",
1043 			DEFAULT_MEM_SIZE, "--file-prefix=" memtest };
1044 
1045 	/* primary process with memtest1 and default mem mode */
1046 	const char *argv1[] = {prgname, "-m",
1047 			DEFAULT_MEM_SIZE, "--file-prefix=" memtest1 };
1048 
1049 	/* primary process with memtest1 and legacy mem mode */
1050 	const char *argv2[] = {prgname, "-m",
1051 			DEFAULT_MEM_SIZE, "--file-prefix=" memtest1,
1052 			"--legacy-mem" };
1053 
1054 	/* primary process with memtest2 and legacy mem mode */
1055 	const char *argv3[] = {prgname, "-m",
1056 			DEFAULT_MEM_SIZE, "--file-prefix=" memtest2,
1057 			"--legacy-mem" };
1058 
1059 	/* primary process with memtest2 and default mem mode */
1060 	const char *argv4[] = {prgname, "-m",
1061 			DEFAULT_MEM_SIZE, "--file-prefix=" memtest2 };
1062 
1063 	/* primary process with --in-memory mode */
1064 	const char * const argv5[] = {prgname, "-m",
1065 		DEFAULT_MEM_SIZE, "--in-memory" };
1066 
1067 	/* primary process with memtest1 and --in-memory mode */
1068 	const char * const argv6[] = {prgname, "-m",
1069 		DEFAULT_MEM_SIZE, "--in-memory",
1070 		"--file-prefix=" memtest1 };
1071 
1072 	/* primary process with parent file-prefix and --in-memory mode */
1073 	const char * const argv7[] = {prgname, "-m",
1074 		DEFAULT_MEM_SIZE, "--in-memory", "--file-prefix", prefix };
1075 
1076 	/* primary process with memtest1 and --single-file-segments mode */
1077 	const char * const argv8[] = {prgname, "-m",
1078 		DEFAULT_MEM_SIZE, "--single-file-segments",
1079 		"--file-prefix=" memtest1 };
1080 
1081 	/* check if files for current prefix are present */
1082 	if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
1083 		printf("Error - hugepage files for %s were not created!\n", prefix);
1084 		return -1;
1085 	}
1086 
1087 	/* checks if files for current prefix are locked */
1088 	if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
1089 		printf("Error - hugepages for current process aren't locked!\n");
1090 		return -1;
1091 	}
1092 
1093 	/* check if files for secondary process are present */
1094 	if (process_hugefiles(memtest, HUGEPAGE_CHECK_EXISTS) == 1) {
1095 		/* check if they are not locked */
1096 		if (process_hugefiles(memtest, HUGEPAGE_CHECK_LOCKED) == 1) {
1097 			printf("Error - hugepages for current process are locked!\n");
1098 			return -1;
1099 		}
1100 		/* they aren't locked, delete them */
1101 		else {
1102 			if (process_hugefiles(memtest, HUGEPAGE_DELETE) != 1) {
1103 				printf("Error - deleting hugepages failed!\n");
1104 				return -1;
1105 			}
1106 		}
1107 	}
1108 
1109 	if (launch_proc(argv0) == 0) {
1110 		printf("Error - secondary process ran ok without primary process\n");
1111 		return -1;
1112 	}
1113 
1114 	/* check if files for current prefix are present */
1115 	if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
1116 		printf("Error - hugepage files for %s were not created!\n", prefix);
1117 		return -1;
1118 	}
1119 
1120 	/* checks if files for current prefix are locked */
1121 	if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
1122 		printf("Error - hugepages for current process aren't locked!\n");
1123 		return -1;
1124 	}
1125 
1126 	/* we're running this process in default memory mode, which means it
1127 	 * should clean up after itself on exit and leave no hugepages behind.
1128 	 */
1129 	if (launch_proc(argv1) != 0) {
1130 		printf("Error - failed to run with --file-prefix=%s\n",
1131 				memtest1);
1132 		return -1;
1133 	}
1134 
1135 	/* check if memtest1_map0 is present */
1136 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1137 		printf("Error - hugepage files for %s were not deleted!\n",
1138 				memtest1);
1139 		return -1;
1140 	}
1141 
1142 	/* now, we're running a process under the same prefix, but with legacy
1143 	 * mem mode - this should leave behind hugepage files.
1144 	 */
1145 	if (launch_proc(argv2) != 0) {
1146 		printf("Error - failed to run with --file-prefix=%s\n",
1147 				memtest1);
1148 		return -1;
1149 	}
1150 
1151 	/* check if memtest1_map0 is present */
1152 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 1) {
1153 		printf("Error - hugepage files for %s were not created!\n",
1154 				memtest1);
1155 		return -1;
1156 	}
1157 
1158 	if (launch_proc(argv3) != 0) {
1159 		printf("Error - failed to run with --file-prefix=%s\n",
1160 				memtest2);
1161 		return -1;
1162 	}
1163 
1164 	/* check if hugefiles for memtest2 are present */
1165 	if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 1) {
1166 		printf("Error - hugepage files for %s were not created!\n",
1167 				memtest2);
1168 		return -1;
1169 	}
1170 
1171 	/* check if hugefiles for memtest1 are present */
1172 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1173 		printf("Error - hugepage files for %s were not deleted!\n",
1174 				memtest1);
1175 		return -1;
1176 	}
1177 
1178 	/* this process will run in default mem mode, so it should not leave any
1179 	 * hugepage files behind.
1180 	 */
1181 	if (launch_proc(argv4) != 0) {
1182 		printf("Error - failed to run with --file-prefix=%s\n",
1183 				memtest2);
1184 		return -1;
1185 	}
1186 
1187 	/* check if hugefiles for memtest2 are present */
1188 	if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 0) {
1189 		printf("Error - hugepage files for %s were not deleted!\n",
1190 				memtest2);
1191 		return -1;
1192 	}
1193 
1194 	/* check if hugefiles for memtest1 are present */
1195 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1196 		printf("Error - hugepage files for %s were not deleted!\n",
1197 				memtest1);
1198 		return -1;
1199 	}
1200 
1201 	/* this process will run in --in-memory mode, so it should not leave any
1202 	 * hugepage files behind.
1203 	 */
1204 
1205 	/* test case to check eal-options with --in-memory mode */
1206 	if (launch_proc(argv5) != 0) {
1207 		printf("Error - failed to run with --in-memory mode\n");
1208 		return -1;
1209 	}
1210 
1211 	/*test case to check eal-options with --in-memory mode with
1212 	 * custom file-prefix.
1213 	 */
1214 	if (launch_proc(argv6) != 0) {
1215 		printf("Error - failed to run with --in-memory mode\n");
1216 		return -1;
1217 	}
1218 
1219 	/* check if hugefiles for memtest1 are present */
1220 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1221 		printf("Error - hugepage files for %s were created and not deleted!\n",
1222 				memtest1);
1223 		return -1;
1224 	}
1225 
1226 	/* test case to check eal-options with --in-memory mode with
1227 	 * parent file-prefix.
1228 	 */
1229 	if (launch_proc(argv7) != 0) {
1230 		printf("Error - failed to run with --file-prefix=%s\n", prefix);
1231 		return -1;
1232 	}
1233 
1234 	/* this process will run in --single-file-segments mode,
1235 	 * so it should not leave any hugepage files behind.
1236 	 */
1237 	if (launch_proc(argv8) != 0) {
1238 		printf("Error - failed to run with --single-file-segments mode\n");
1239 		return -1;
1240 	}
1241 
1242 	/* check if hugefiles for memtest1 are present */
1243 	if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1244 		printf("Error - hugepage files for %s were not deleted!\n",
1245 				memtest1);
1246 		return -1;
1247 	}
1248 
1249 	return 0;
1250 }
1251 
1252 /* This function writes in passed buf pointer a valid --socket-mem= option
1253  * for num_sockets then concatenates the provided suffix string.
1254  *
1255  * Example for num_sockets 4, mem "2", suffix "plop"
1256  * --socket-mem=2,2,2,2plop
1257  */
1258 static void
1259 populate_socket_mem_param(int num_sockets, const char *mem,
1260 		const char *suffix, char *buf, size_t buf_size)
1261 {
1262 	unsigned int offset = 0;
1263 	int written;
1264 	int i;
1265 
1266 	written = snprintf(&buf[offset], buf_size - offset, "--socket-mem=");
1267 	if (written < 0 || written + offset >= buf_size)
1268 		return;
1269 	offset += written;
1270 
1271 	for (i = 0; i < num_sockets - 1; i++) {
1272 		written = snprintf(&buf[offset], buf_size - offset,
1273 			"%s,", mem);
1274 		if (written < 0 || written + offset >= buf_size)
1275 			return;
1276 		offset += written;
1277 	}
1278 
1279 	written = snprintf(&buf[offset], buf_size - offset, "%s%s", mem,
1280 		suffix);
1281 	if (written < 0 || written + offset >= buf_size)
1282 		return;
1283 	offset += written;
1284 }
1285 
1286 /*
1287  * Tests for correct handling of -m and --socket-mem flags
1288  */
1289 static int
1290 test_memory_flags(void)
1291 {
1292 #ifdef RTE_EXEC_ENV_FREEBSD
1293 	/* BSD target doesn't support prefixes at this point */
1294 	const char * prefix = "";
1295 #else
1296 	char prefix[PATH_MAX], tmp[PATH_MAX];
1297 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
1298 		printf("Error - unable to get current prefix!\n");
1299 		return -1;
1300 	}
1301 	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
1302 #endif
1303 
1304 	/* valid -m flag and mp flag */
1305 	const char *argv0[] = {prgname, prefix, mp_flag,
1306 			"-m", DEFAULT_MEM_SIZE};
1307 
1308 	/* valid -m flag */
1309 	const char *argv1[] = {prgname,
1310 			"--file-prefix=" memtest, "-m", DEFAULT_MEM_SIZE};
1311 
1312 	/* valid (zero) --socket-mem flag */
1313 	char arg2_socket_mem[SOCKET_MEM_STRLEN];
1314 	const char *argv2[] = {prgname,
1315 			"--file-prefix=" memtest, arg2_socket_mem};
1316 
1317 	/* invalid (incomplete) --socket-mem flag */
1318 	char arg3_socket_mem[SOCKET_MEM_STRLEN];
1319 	const char *argv3[] = {prgname,
1320 			"--file-prefix=" memtest, arg3_socket_mem};
1321 
1322 	/* invalid (mixed with invalid data) --socket-mem flag */
1323 	char arg4_socket_mem[SOCKET_MEM_STRLEN];
1324 	const char *argv4[] = {prgname,
1325 			"--file-prefix=" memtest, arg4_socket_mem};
1326 
1327 	/* invalid (with numeric value as last character) --socket-mem flag */
1328 	char arg5_socket_mem[SOCKET_MEM_STRLEN];
1329 	const char *argv5[] = {prgname,
1330 			"--file-prefix=" memtest, arg5_socket_mem};
1331 
1332 	/* invalid (with empty socket) --socket-mem flag */
1333 	char arg6_socket_mem[SOCKET_MEM_STRLEN];
1334 	const char *argv6[] = {prgname,
1335 			"--file-prefix=" memtest, arg6_socket_mem};
1336 
1337 	/* invalid (null) --socket-mem flag */
1338 	const char *argv7[] = {prgname,
1339 			"--file-prefix=" memtest, "--socket-mem="};
1340 
1341 	/* valid --socket-mem specified together with -m flag */
1342 	char arg8_socket_mem[SOCKET_MEM_STRLEN];
1343 	const char *argv8[] = {prgname,
1344 			"--file-prefix=" memtest, "-m", DEFAULT_MEM_SIZE,
1345 			arg8_socket_mem};
1346 
1347 #ifdef RTE_EXEC_ENV_FREEBSD
1348 	int num_sockets = 1;
1349 #else
1350 	int num_sockets = RTE_MIN(get_number_of_sockets(),
1351 			RTE_MAX_NUMA_NODES);
1352 #endif
1353 
1354 	if (num_sockets <= 0) {
1355 		printf("Error - cannot get number of sockets!\n");
1356 		return -1;
1357 	}
1358 
1359 	/* invalid --socket-mem flag (with extra socket) */
1360 	char invalid_socket_mem[SOCKET_MEM_STRLEN];
1361 	const char *argv9[] = {prgname,
1362 			"--file-prefix=" memtest, invalid_socket_mem};
1363 
1364 	/* valid --socket-mem flag */
1365 	char valid_socket_mem[SOCKET_MEM_STRLEN];
1366 	const char *argv10[] = {prgname,
1367 			"--file-prefix=" memtest, valid_socket_mem};
1368 
1369 	if (launch_proc(argv0) != 0) {
1370 		printf("Error - secondary process failed with valid -m flag !\n");
1371 		return -1;
1372 	}
1373 
1374 #ifdef RTE_EXEC_ENV_FREEBSD
1375 	/* no other tests are applicable to BSD */
1376 	return 0;
1377 #endif
1378 
1379 	if (launch_proc(argv1) != 0) {
1380 		printf("Error - process failed with valid -m flag!\n");
1381 		return -1;
1382 	}
1383 
1384 	populate_socket_mem_param(num_sockets, "0", "",
1385 		arg2_socket_mem, sizeof(arg2_socket_mem));
1386 	if (launch_proc(argv2) != 0) {
1387 		printf("Error - process failed with valid (zero) --socket-mem!\n");
1388 		return -1;
1389 	}
1390 
1391 	if (num_sockets > 1) {
1392 		populate_socket_mem_param(num_sockets - 1, "2", ",",
1393 			arg3_socket_mem, sizeof(arg3_socket_mem));
1394 		if (launch_proc(argv3) == 0) {
1395 			printf("Error - process run ok with invalid "
1396 				"(incomplete) --socket-mem!\n");
1397 			return -1;
1398 		}
1399 
1400 		populate_socket_mem_param(num_sockets - 1, "2", ",Fred",
1401 			arg4_socket_mem, sizeof(arg4_socket_mem));
1402 		if (launch_proc(argv4) == 0) {
1403 			printf("Error - process run ok with invalid "
1404 				"(mixed with invalid input) --socket-mem!\n");
1405 			return -1;
1406 		}
1407 
1408 		populate_socket_mem_param(num_sockets - 1, "2", ",Fred0",
1409 			arg5_socket_mem, sizeof(arg5_socket_mem));
1410 		if (launch_proc(argv5) == 0) {
1411 			printf("Error - process run ok with invalid "
1412 				"(mixed with invalid input with a numeric value as "
1413 				"last character) --socket-mem!\n");
1414 			return -1;
1415 		}
1416 	}
1417 
1418 	if (num_sockets > 2) {
1419 		populate_socket_mem_param(num_sockets - 2, "2", ",,2",
1420 			arg6_socket_mem, sizeof(arg6_socket_mem));
1421 		if (launch_proc(argv6) == 0) {
1422 			printf("Error - process run ok with invalid "
1423 				"(with empty socket) --socket-mem!\n");
1424 			return -1;
1425 		}
1426 	}
1427 
1428 	if (launch_proc(argv7) == 0) {
1429 		printf("Error - process run ok with invalid (null) --socket-mem!\n");
1430 		return -1;
1431 	}
1432 
1433 	populate_socket_mem_param(num_sockets, "2", "",
1434 		arg8_socket_mem, sizeof(arg8_socket_mem));
1435 	if (launch_proc(argv8) == 0) {
1436 		printf("Error - process run ok with --socket-mem and -m specified!\n");
1437 		return -1;
1438 	}
1439 
1440 	populate_socket_mem_param(num_sockets + 1, "2", "",
1441 		invalid_socket_mem, sizeof(invalid_socket_mem));
1442 	if (launch_proc(argv9) == 0) {
1443 		printf("Error - process run ok with extra socket in --socket-mem!\n");
1444 		return -1;
1445 	}
1446 
1447 	populate_socket_mem_param(num_sockets, "2", "",
1448 		valid_socket_mem, sizeof(valid_socket_mem));
1449 	if (launch_proc(argv10) != 0) {
1450 		printf("Error - process failed with valid --socket-mem!\n");
1451 		return -1;
1452 	}
1453 
1454 	return 0;
1455 }
1456 
1457 static int
1458 test_eal_flags(void)
1459 {
1460 	int ret = 0;
1461 
1462 	ret = test_missing_c_flag();
1463 	if (ret < 0) {
1464 		printf("Error in test_missing_c_flag()\n");
1465 		return ret;
1466 	}
1467 
1468 	ret = test_master_lcore_flag();
1469 	if (ret < 0) {
1470 		printf("Error in test_master_lcore_flag()\n");
1471 		return ret;
1472 	}
1473 
1474 	ret = test_invalid_n_flag();
1475 	if (ret < 0) {
1476 		printf("Error in test_invalid_n_flag()\n");
1477 		return ret;
1478 	}
1479 
1480 	ret = test_no_hpet_flag();
1481 	if (ret < 0) {
1482 		printf("Error in test_no_hpet_flag()\n");
1483 		return ret;
1484 	}
1485 
1486 	ret = test_no_huge_flag();
1487 	if (ret < 0) {
1488 		printf("Error in test_no_huge_flag()\n");
1489 		return ret;
1490 	}
1491 
1492 	ret = test_whitelist_flag();
1493 	if (ret < 0) {
1494 		printf("Error in test_invalid_whitelist_flag()\n");
1495 		return ret;
1496 	}
1497 
1498 	ret = test_invalid_b_flag();
1499 	if (ret < 0) {
1500 		printf("Error in test_invalid_b_flag()\n");
1501 		return ret;
1502 	}
1503 
1504 #ifdef RTE_LIBRTE_PMD_RING
1505 	ret = test_invalid_vdev_flag();
1506 	if (ret < 0) {
1507 		printf("Error in test_invalid_vdev_flag()\n");
1508 		return ret;
1509 	}
1510 #endif
1511 	ret = test_invalid_r_flag();
1512 	if (ret < 0) {
1513 		printf("Error in test_invalid_r_flag()\n");
1514 		return ret;
1515 	}
1516 
1517 	ret = test_memory_flags();
1518 	if (ret < 0) {
1519 		printf("Error in test_memory_flags()\n");
1520 		return ret;
1521 	}
1522 
1523 	ret = test_file_prefix();
1524 	if (ret < 0) {
1525 		printf("Error in test_file_prefix()\n");
1526 		return ret;
1527 	}
1528 
1529 	ret = test_misc_flags();
1530 	if (ret < 0) {
1531 		printf("Error in test_misc_flags()");
1532 		return ret;
1533 	}
1534 
1535 	return ret;
1536 }
1537 
1538 REGISTER_TEST_COMMAND(eal_flags_autotest, test_eal_flags);
1539 
1540 /* subtests used in meson for CI */
1541 REGISTER_TEST_COMMAND(eal_flags_c_opt_autotest, test_missing_c_flag);
1542 REGISTER_TEST_COMMAND(eal_flags_master_opt_autotest, test_master_lcore_flag);
1543 REGISTER_TEST_COMMAND(eal_flags_n_opt_autotest, test_invalid_n_flag);
1544 REGISTER_TEST_COMMAND(eal_flags_hpet_autotest, test_no_hpet_flag);
1545 REGISTER_TEST_COMMAND(eal_flags_no_huge_autotest, test_no_huge_flag);
1546 REGISTER_TEST_COMMAND(eal_flags_w_opt_autotest, test_whitelist_flag);
1547 REGISTER_TEST_COMMAND(eal_flags_b_opt_autotest, test_invalid_b_flag);
1548 REGISTER_TEST_COMMAND(eal_flags_vdev_opt_autotest, test_invalid_vdev_flag);
1549 REGISTER_TEST_COMMAND(eal_flags_r_opt_autotest, test_invalid_r_flag);
1550 REGISTER_TEST_COMMAND(eal_flags_mem_autotest, test_memory_flags);
1551 REGISTER_TEST_COMMAND(eal_flags_file_prefix_autotest, test_file_prefix);
1552 REGISTER_TEST_COMMAND(eal_flags_misc_autotest, test_misc_flags);
1553