1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright(c) 2014 6WIND S.A.
22 #include <rte_lcore.h>
23 #include <rte_debug.h>
24 #include <rte_string_fns.h>
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"
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__)
41 enum hugepage_action {
42 HUGEPAGE_CHECK_EXISTS = 0,
43 HUGEPAGE_CHECK_LOCKED,
48 /* if string contains a hugepage path */
50 get_hugepage_path(char * src, int src_len, char * dst, int dst_len)
53 char *tokens[NUM_TOKENS];
55 /* if we couldn't properly split the string */
56 if (rte_strsplit(src, src_len, tokens, NUM_TOKENS, ' ') < NUM_TOKENS)
59 if (strncmp(tokens[2], "hugetlbfs", sizeof("hugetlbfs")) == 0) {
60 strlcpy(dst, tokens[1], dst_len);
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.
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
77 process_hugefiles(const char * prefix, enum hugepage_action action)
79 FILE * hugedir_handle = NULL;
80 DIR * hugepage_dir = NULL;
81 struct dirent *dirent = NULL;
83 char hugefile_prefix[PATH_MAX] = {0};
84 char hugedir[PATH_MAX] = {0};
85 char line[PATH_MAX] = {0};
87 int fd, lck_result, result = 0;
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");
97 /* get hugetlbfs mountpoints from /proc/mounts */
98 hugedir_handle = fopen("/proc/mounts", "r");
100 if (hugedir_handle == NULL) {
101 printf("Error parsing /proc/mounts!\n");
105 /* read and parse script output */
106 while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
108 /* check if we have a hugepage filesystem path */
109 if (!get_hugepage_path(line, sizeof(line), hugedir, sizeof(hugedir)))
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));
119 while ((dirent = readdir(hugepage_dir)) != NULL) {
120 if (memcmp(dirent->d_name, hugefile_prefix, prefix_len) != 0)
124 case HUGEPAGE_CHECK_EXISTS:
126 /* file exists, return */
131 case HUGEPAGE_DELETE:
133 char file_path[PATH_MAX] = {0};
135 snprintf(file_path, sizeof(file_path),
136 "%s/%s", hugedir, dirent->d_name);
139 if (remove(file_path) < 0) {
140 printf("Error deleting %s - %s!\n",
141 dirent->d_name, strerror(errno));
142 closedir(hugepage_dir);
149 case HUGEPAGE_CHECK_LOCKED:
151 /* try and lock the file */
152 fd = openat(dirfd(hugepage_dir), dirent->d_name, O_RDONLY);
154 /* this shouldn't happen */
156 printf("Error opening %s - %s!\n",
157 dirent->d_name, strerror(errno));
158 closedir(hugepage_dir);
163 /* non-blocking lock */
164 lck_result = flock(fd, LOCK_EX | LOCK_NB);
166 /* if lock succeeds, there's something wrong */
167 if (lck_result != -1) {
170 /* unlock the resulting lock */
173 closedir(hugepage_dir);
180 /* shouldn't happen */
185 } /* read hugepage directory */
186 closedir(hugepage_dir);
187 } /* read /proc/mounts */
189 fclose(hugedir_handle);
193 #ifdef RTE_EXEC_ENV_LINUX
195 * count the number of "node*" files in /sys/devices/system/node/
198 get_number_of_sockets(void)
200 struct dirent *dirent = NULL;
201 const char * nodedir = "/sys/devices/system/node/";
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");
212 printf("Error opening %s: %s\n", nodedir, strerror(errno));
216 while ((dirent = readdir(dir)) != NULL)
217 if (strncmp(dirent->d_name, "node", sizeof("node") - 1) == 0)
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)
231 test_whitelist_flag(void)
234 #ifdef RTE_EXEC_ENV_FREEBSD
235 /* BSD target doesn't support prefixes at this point */
236 const char * prefix = "";
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");
243 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
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", "", ""},
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",
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");
277 if (launch_proc(wlval1) != 0 ) {
278 printf("Error - process did not run ok with valid whitelist\n");
281 if (launch_proc(wlval2) != 0 ) {
282 printf("Error - process did not run ok with valid whitelist value set\n");
285 if (launch_proc(wlval3) != 0 ) {
286 printf("Error - process did not run ok with valid whitelist + args\n");
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
298 test_invalid_b_flag(void)
300 #ifdef RTE_EXEC_ENV_FREEBSD
301 /* BSD target doesn't support prefixes at this point */
302 const char * prefix = "";
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");
309 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
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"},
320 /* Test with valid blacklist option */
321 const char *blval[] = {prgname, prefix, mp_flag,
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");
333 if (launch_proc(blval) != 0) {
334 printf("Error - process did not run ok with valid blacklist value\n");
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
345 test_invalid_vdev_flag(void)
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;
353 const char * prefix = "--file-prefix=vdev";
356 /* Test with invalid vdev option */
357 const char *vdevinval[] = {prgname, prefix, no_huge,
360 /* Test with valid vdev option */
361 const char *vdevval1[] = {prgname, prefix, no_huge,
364 const char *vdevval2[] = {prgname, prefix, no_huge,
365 vdev, "net_ring0,args=test"};
367 const char *vdevval3[] = {prgname, prefix, no_huge,
368 vdev, "net_ring0,nodeaction=r1:0:CREATE"};
370 if (launch_proc(vdevinval) == 0) {
371 printf("Error - process did run ok with invalid "
376 if (launch_proc(vdevval1) != 0) {
377 printf("Error - process did not run ok with valid vdev value\n");
381 if (launch_proc(vdevval2) != 0) {
382 printf("Error - process did not run ok with valid vdev value,"
383 "with dummy args\n");
387 if (launch_proc(vdevval3) != 0) {
388 printf("Error - process did not run ok with valid vdev value,"
389 "with valid args\n");
399 * Test that the app doesn't run with invalid -r option.
402 test_invalid_r_flag(void)
404 #ifdef RTE_EXEC_ENV_FREEBSD
405 /* BSD target doesn't support prefixes at this point */
406 const char * prefix = "";
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");
413 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
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"},
422 /* Test with valid blacklist option */
423 const char *rval[] = {prgname, prefix, mp_flag, "-r", "16"};
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");
434 if (launch_proc(rval) != 0) {
435 printf("Error - process did not run ok with valid -r (rank) value\n");
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
446 test_missing_c_flag(void)
448 #ifdef RTE_EXEC_ENV_FREEBSD
449 /* BSD target doesn't support prefixes at this point */
450 const char * prefix = "";
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");
457 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
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,
467 /* sanity check of tests - valid coremask value */
468 const char *argv4[] = { prgname, prefix, mp_flag,
470 /* -l flag but no corelist value */
471 const char *argv5[] = { prgname, prefix, mp_flag,
473 const char *argv6[] = { prgname, prefix, mp_flag,
475 /* bad corelist values */
476 const char *argv7[] = { prgname, prefix, mp_flag,
478 const char *argv8[] = { prgname, prefix, mp_flag,
480 const char *argv9[] = { prgname, prefix, mp_flag,
482 const char *argv10[] = { prgname, prefix, mp_flag,
484 /* core number is negative value */
485 const char * const argv11[] = { prgname, prefix, mp_flag,
487 const char * const argv12[] = { prgname, prefix, mp_flag,
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,
498 /* --lcores flag but no lcores value */
499 const char * const argv16[] = { prgname, prefix, mp_flag,
501 const char * const argv17[] = { prgname, prefix, mp_flag,
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,
529 "0-1,2@(5-7),(3-5)@(0,2),(0,6),7"};
530 /* check an invalid cpu value >= CPU_SETSIZE */
531 const char * const argv30[] = { prgname, prefix, mp_flag,
532 "--lcores", "3@" RTE_STR(CPU_SETSIZE) };
534 if (launch_proc(argv2) != 0) {
536 "process did not run ok when missing -c flag\n");
540 if (launch_proc(argv1) == 0
541 || launch_proc(argv3) == 0) {
543 "process ran without error with invalid -c flag\n");
546 if (launch_proc(argv4) != 0) {
548 "process did not run ok with valid coremask value\n");
553 if (launch_proc(argv5) == 0
554 || launch_proc(argv6) == 0
555 || launch_proc(argv7) == 0
556 || launch_proc(argv8) == 0
557 || launch_proc(argv9) == 0
558 || launch_proc(argv10) == 0
559 || launch_proc(argv11) == 0
560 || launch_proc(argv12) == 0
561 || launch_proc(argv13) == 0
562 || launch_proc(argv14) == 0) {
564 "process ran without error with invalid -l flag\n");
567 if (rte_lcore_is_enabled(0) && rte_lcore_is_enabled(1) &&
568 rte_lcore_is_enabled(2) && rte_lcore_is_enabled(3) &&
569 launch_proc(argv15) != 0) {
571 "process did not run ok with valid corelist value\n");
575 /* start --lcores tests */
576 if (launch_proc(argv16) == 0 || launch_proc(argv17) == 0 ||
577 launch_proc(argv18) == 0 || launch_proc(argv19) == 0 ||
578 launch_proc(argv20) == 0 || launch_proc(argv21) == 0 ||
579 launch_proc(argv22) == 0 || launch_proc(argv23) == 0 ||
580 launch_proc(argv24) == 0 || launch_proc(argv25) == 0 ||
581 launch_proc(argv26) == 0 || launch_proc(argv27) == 0 ||
582 launch_proc(argv28) == 0 || launch_proc(argv30) == 0) {
584 "process ran without error with invalid --lcore flag\n");
588 if (rte_lcore_is_enabled(0) && rte_lcore_is_enabled(1) &&
589 rte_lcore_is_enabled(2) && rte_lcore_is_enabled(3) &&
590 rte_lcore_is_enabled(3) && rte_lcore_is_enabled(5) &&
591 rte_lcore_is_enabled(4) && rte_lcore_is_enabled(7) &&
592 launch_proc(argv29) != 0) {
594 "process did not run ok with valid corelist value\n");
602 * Test --master-lcore option with matching coremask
605 test_master_lcore_flag(void)
607 #ifdef RTE_EXEC_ENV_FREEBSD
608 /* BSD target doesn't support prefixes at this point */
609 const char *prefix = "";
611 char prefix[PATH_MAX], tmp[PATH_MAX];
612 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
613 printf("Error - unable to get current prefix!\n");
616 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
619 if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1))
622 /* --master-lcore flag but no value */
623 const char *argv1[] = { prgname, prefix, mp_flag,
624 "-c", "3", "--master-lcore"};
625 /* --master-lcore flag with invalid value */
626 const char *argv2[] = { prgname, prefix, mp_flag,
627 "-c", "3", "--master-lcore", "-1"};
628 const char *argv3[] = { prgname, prefix, mp_flag,
629 "-c", "3", "--master-lcore", "X"};
630 /* master lcore not in coremask */
631 const char *argv4[] = { prgname, prefix, mp_flag,
632 "-c", "3", "--master-lcore", "2"};
634 const char *argv5[] = { prgname, prefix, mp_flag,
635 "-c", "3", "--master-lcore", "1"};
636 /* valid value set before coremask */
637 const char *argv6[] = { prgname, prefix, mp_flag,
638 "--master-lcore", "1", "-c", "3"};
640 if (launch_proc(argv1) == 0
641 || launch_proc(argv2) == 0
642 || launch_proc(argv3) == 0
643 || launch_proc(argv4) == 0) {
644 printf("Error - process ran without error with wrong --master-lcore\n");
647 if (launch_proc(argv5) != 0
648 || launch_proc(argv6) != 0) {
649 printf("Error - process did not run ok with valid --master-lcore\n");
656 * Test that the app doesn't run with invalid -n flag option.
657 * Final test ensures it does run with valid options as sanity check
658 * Since -n is not compulsory for MP, we instead use --no-huge and --no-shconf
662 test_invalid_n_flag(void)
664 #ifdef RTE_EXEC_ENV_FREEBSD
665 /* BSD target doesn't support prefixes at this point */
666 const char * prefix = "";
668 char prefix[PATH_MAX], tmp[PATH_MAX];
669 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
670 printf("Error - unable to get current prefix!\n");
673 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
676 /* -n flag but no value */
677 const char *argv1[] = { prgname, prefix, no_huge, no_shconf,
679 /* bad numeric value */
680 const char *argv2[] = { prgname, prefix, no_huge, no_shconf,
682 /* zero is invalid */
683 const char *argv3[] = { prgname, prefix, no_huge, no_shconf,
685 /* sanity test - check with good value */
686 const char *argv4[] = { prgname, prefix, no_huge, no_shconf,
688 /* sanity test - check with no -n flag */
689 const char *argv5[] = { prgname, prefix, no_huge, no_shconf};
691 if (launch_proc(argv1) == 0
692 || launch_proc(argv2) == 0
693 || launch_proc(argv3) == 0) {
694 printf("Error - process ran without error when"
695 "invalid -n flag\n");
698 if (launch_proc(argv4) != 0) {
699 printf("Error - process did not run ok with valid num-channel value\n");
702 if (launch_proc(argv5) != 0) {
703 printf("Error - process did not run ok without -n flag\n");
711 * Test that the app runs with HPET, and without HPET
714 test_no_hpet_flag(void)
716 char prefix[PATH_MAX] = "";
718 #ifdef RTE_EXEC_ENV_FREEBSD
722 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
723 printf("Error - unable to get current prefix!\n");
726 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
730 const char *argv1[] = {prgname, prefix, mp_flag, no_hpet};
731 /* Without --no-hpet */
732 const char *argv2[] = {prgname, prefix, mp_flag};
734 if (launch_proc(argv1) != 0) {
735 printf("Error - process did not run ok with --no-hpet flag\n");
738 if (launch_proc(argv2) != 0) {
739 printf("Error - process did not run ok without --no-hpet flag\n");
746 * Test that the app runs with --no-huge and doesn't run when --socket-mem are
747 * specified with --no-huge.
750 test_no_huge_flag(void)
752 #ifdef RTE_EXEC_ENV_FREEBSD
753 /* BSD target doesn't support prefixes at this point, and we also need to
754 * run another primary process here */
755 const char * prefix = no_shconf;
757 const char * prefix = "--file-prefix=nohuge";
761 const char *argv1[] = {prgname, prefix, no_huge};
762 /* With --no-huge and -m */
763 const char *argv2[] = {prgname, prefix, no_huge,
764 "-m", DEFAULT_MEM_SIZE};
766 /* With --no-huge and --socket-mem */
767 const char *argv3[] = {prgname, prefix, no_huge,
768 "--socket-mem=" DEFAULT_MEM_SIZE};
769 /* With --no-huge, -m and --socket-mem */
770 const char *argv4[] = {prgname, prefix, no_huge,
771 "-m", DEFAULT_MEM_SIZE, "--socket-mem=" DEFAULT_MEM_SIZE};
772 if (launch_proc(argv1) != 0) {
773 printf("Error - process did not run ok with --no-huge flag\n");
776 if (launch_proc(argv2) != 0) {
777 printf("Error - process did not run ok with --no-huge and -m flags\n");
780 #ifdef RTE_EXEC_ENV_FREEBSD
781 /* BSD target does not support NUMA, hence no --socket-mem tests */
785 if (launch_proc(argv3) == 0) {
786 printf("Error - process run ok with --no-huge and --socket-mem "
790 if (launch_proc(argv4) == 0) {
791 printf("Error - process run ok with --no-huge, -m and "
792 "--socket-mem flags\n");
799 test_misc_flags(void)
801 char hugepath[PATH_MAX] = {0};
802 #ifdef RTE_EXEC_ENV_FREEBSD
803 /* BSD target doesn't support prefixes at this point */
804 const char * prefix = "";
805 const char * nosh_prefix = "";
807 char prefix[PATH_MAX], tmp[PATH_MAX];
808 const char * nosh_prefix = "--file-prefix=noshconf";
809 FILE * hugedir_handle = NULL;
810 char line[PATH_MAX] = {0};
811 unsigned i, isempty = 1;
812 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
813 printf("Error - unable to get current prefix!\n");
816 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
819 * get first valid hugepage path
822 /* get hugetlbfs mountpoints from /proc/mounts */
823 hugedir_handle = fopen("/proc/mounts", "r");
825 if (hugedir_handle == NULL) {
826 printf("Error opening /proc/mounts!\n");
830 /* read /proc/mounts */
831 while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
833 /* find first valid hugepath */
834 if (get_hugepage_path(line, sizeof(line), hugepath, sizeof(hugepath)))
838 fclose(hugedir_handle);
840 /* check if path is not empty */
841 for (i = 0; i < sizeof(hugepath); i++)
842 if (hugepath[i] != '\0')
846 printf("No mounted hugepage dir found!\n");
852 /* check that some general flags don't prevent things from working.
853 * All cases, apart from the first, app should run.
854 * No further testing of output done.
856 /* sanity check - failure with invalid option */
857 const char *argv0[] = {prgname, prefix, mp_flag, "--invalid-opt"};
860 const char *argv1[] = {prgname, prefix, mp_flag, "--no-pci"};
862 const char *argv2[] = {prgname, prefix, mp_flag, "-v"};
863 /* With valid --syslog */
864 const char *argv3[] = {prgname, prefix, mp_flag,
865 "--syslog", "syslog"};
866 /* With empty --syslog (should fail) */
867 const char *argv4[] = {prgname, prefix, mp_flag, "--syslog"};
868 /* With invalid --syslog */
869 const char *argv5[] = {prgname, prefix, mp_flag, "--syslog", "error"};
870 /* With no-sh-conf, also use no-huge to ensure this test runs on BSD */
871 const char *argv6[] = {prgname, "-m", DEFAULT_MEM_SIZE,
872 no_shconf, nosh_prefix, no_huge};
874 /* With --huge-dir */
875 const char *argv7[] = {prgname, "-m", DEFAULT_MEM_SIZE,
876 "--file-prefix=hugedir", "--huge-dir", hugepath};
877 /* With empty --huge-dir (should fail) */
878 const char *argv8[] = {prgname, "-m", DEFAULT_MEM_SIZE,
879 "--file-prefix=hugedir", "--huge-dir"};
880 /* With invalid --huge-dir */
881 const char *argv9[] = {prgname, "-m", DEFAULT_MEM_SIZE,
882 "--file-prefix=hugedir", "--huge-dir", "invalid"};
883 /* Secondary process with invalid --huge-dir (should run as flag has no
884 * effect on secondary processes) */
885 const char *argv10[] = {prgname, prefix, mp_flag,
886 "--huge-dir", "invalid"};
888 /* try running with base-virtaddr param */
889 const char *argv11[] = {prgname, "--file-prefix=virtaddr",
890 "--base-virtaddr=0x12345678"};
892 /* try running with --vfio-intr INTx flag */
893 const char *argv12[] = {prgname, "--file-prefix=intr",
894 "--vfio-intr=legacy"};
896 /* try running with --vfio-intr MSI flag */
897 const char *argv13[] = {prgname, "--file-prefix=intr",
900 /* try running with --vfio-intr MSI-X flag */
901 const char *argv14[] = {prgname, "--file-prefix=intr",
904 /* try running with --vfio-intr invalid flag */
905 const char *argv15[] = {prgname, "--file-prefix=intr",
906 "--vfio-intr=invalid"};
908 /* With process type as auto-detect */
909 const char * const argv16[] = {prgname, "--file-prefix=auto",
912 /* With process type as auto-detect with no-shconf */
913 const char * const argv17[] = {prgname, "--proc-type=auto",
914 no_shconf, nosh_prefix, no_huge};
916 /* With process type as --create-uio-dev flag */
917 const char * const argv18[] = {prgname, "--file-prefix=uiodev",
920 /* run all tests also applicable to FreeBSD first */
922 if (launch_proc(argv0) == 0) {
923 printf("Error - process ran ok with invalid flag\n");
926 if (launch_proc(argv1) != 0) {
927 printf("Error - process did not run ok with --no-pci flag\n");
930 if (launch_proc(argv2) != 0) {
931 printf("Error - process did not run ok with -v flag\n");
934 if (launch_proc(argv6) != 0) {
935 printf("Error - process did not run ok with --no-shconf flag\n");
939 #ifdef RTE_EXEC_ENV_FREEBSD
940 /* no more tests to be done on FreeBSD */
944 if (launch_proc(argv3) != 0) {
945 printf("Error - process did not run ok with --syslog flag\n");
948 if (launch_proc(argv4) == 0) {
949 printf("Error - process run ok with empty --syslog flag\n");
952 if (launch_proc(argv5) == 0) {
953 printf("Error - process run ok with invalid --syslog flag\n");
956 if (launch_proc(argv7) != 0) {
957 printf("Error - process did not run ok with --huge-dir flag\n");
960 if (launch_proc(argv8) == 0) {
961 printf("Error - process run ok with empty --huge-dir flag\n");
964 if (launch_proc(argv9) == 0) {
965 printf("Error - process run ok with invalid --huge-dir flag\n");
968 if (launch_proc(argv10) != 0) {
969 printf("Error - secondary process did not run ok with invalid --huge-dir flag\n");
972 if (launch_proc(argv11) != 0) {
973 printf("Error - process did not run ok with --base-virtaddr parameter\n");
976 if (launch_proc(argv12) != 0) {
977 printf("Error - process did not run ok with "
978 "--vfio-intr INTx parameter\n");
981 if (launch_proc(argv13) != 0) {
982 printf("Error - process did not run ok with "
983 "--vfio-intr MSI parameter\n");
986 if (launch_proc(argv14) != 0) {
987 printf("Error - process did not run ok with "
988 "--vfio-intr MSI-X parameter\n");
991 if (launch_proc(argv15) == 0) {
992 printf("Error - process run ok with "
993 "--vfio-intr invalid parameter\n");
996 if (launch_proc(argv16) != 0) {
997 printf("Error - process did not run ok with "
998 "--proc-type as auto parameter\n");
1001 if (launch_proc(argv17) != 0) {
1002 printf("Error - process did not run ok with "
1003 "--proc-type and --no-shconf parameter\n");
1006 if (launch_proc(argv18) != 0) {
1007 printf("Error - process did not run ok with "
1008 "--create-uio-dev parameter\n");
1016 test_file_prefix(void)
1019 * 1. check if current process hugefiles are locked
1020 * 2. try to run secondary process without a corresponding primary process
1021 * (while failing to run, it will also remove any unused hugepage files)
1022 * 3. check if current process hugefiles are still in place and are locked
1023 * 4. run a primary process with memtest1 prefix in default and legacy
1025 * 5. check if memtest1 hugefiles are created in case of legacy mem
1026 * mode, and deleted in case of default mem mode
1027 * 6. run a primary process with memtest2 prefix in default and legacy
1029 * 7. check that memtest2 hugefiles are present in the hugedir after a
1030 * run in legacy mode, and not present at all after run in default
1033 char prefix[PATH_MAX] = "";
1035 #ifdef RTE_EXEC_ENV_FREEBSD
1038 if (get_current_prefix(prefix, sizeof(prefix)) == NULL) {
1039 printf("Error - unable to get current prefix!\n");
1044 /* this should fail unless the test itself is run with "memtest" prefix */
1045 const char *argv0[] = {prgname, mp_flag, "-m",
1046 DEFAULT_MEM_SIZE, "--file-prefix=" memtest };
1048 /* primary process with memtest1 and default mem mode */
1049 const char *argv1[] = {prgname, "-m",
1050 DEFAULT_MEM_SIZE, "--file-prefix=" memtest1 };
1052 /* primary process with memtest1 and legacy mem mode */
1053 const char *argv2[] = {prgname, "-m",
1054 DEFAULT_MEM_SIZE, "--file-prefix=" memtest1,
1057 /* primary process with memtest2 and legacy mem mode */
1058 const char *argv3[] = {prgname, "-m",
1059 DEFAULT_MEM_SIZE, "--file-prefix=" memtest2,
1062 /* primary process with memtest2 and default mem mode */
1063 const char *argv4[] = {prgname, "-m",
1064 DEFAULT_MEM_SIZE, "--file-prefix=" memtest2 };
1066 /* primary process with --in-memory mode */
1067 const char * const argv5[] = {prgname, "-m",
1068 DEFAULT_MEM_SIZE, "--in-memory" };
1070 /* primary process with memtest1 and --in-memory mode */
1071 const char * const argv6[] = {prgname, "-m",
1072 DEFAULT_MEM_SIZE, "--in-memory",
1073 "--file-prefix=" memtest1 };
1075 /* primary process with parent file-prefix and --in-memory mode */
1076 const char * const argv7[] = {prgname, "-m",
1077 DEFAULT_MEM_SIZE, "--in-memory", "--file-prefix", prefix };
1079 /* primary process with memtest1 and --single-file-segments mode */
1080 const char * const argv8[] = {prgname, "-m",
1081 DEFAULT_MEM_SIZE, "--single-file-segments",
1082 "--file-prefix=" memtest1 };
1084 /* check if files for current prefix are present */
1085 if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
1086 printf("Error - hugepage files for %s were not created!\n", prefix);
1090 /* checks if files for current prefix are locked */
1091 if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
1092 printf("Error - hugepages for current process aren't locked!\n");
1096 /* check if files for secondary process are present */
1097 if (process_hugefiles(memtest, HUGEPAGE_CHECK_EXISTS) == 1) {
1098 /* check if they are not locked */
1099 if (process_hugefiles(memtest, HUGEPAGE_CHECK_LOCKED) == 1) {
1100 printf("Error - hugepages for current process are locked!\n");
1103 /* they aren't locked, delete them */
1105 if (process_hugefiles(memtest, HUGEPAGE_DELETE) != 1) {
1106 printf("Error - deleting hugepages failed!\n");
1112 if (launch_proc(argv0) == 0) {
1113 printf("Error - secondary process ran ok without primary process\n");
1117 /* check if files for current prefix are present */
1118 if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
1119 printf("Error - hugepage files for %s were not created!\n", prefix);
1123 /* checks if files for current prefix are locked */
1124 if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
1125 printf("Error - hugepages for current process aren't locked!\n");
1129 /* we're running this process in default memory mode, which means it
1130 * should clean up after itself on exit and leave no hugepages behind.
1132 if (launch_proc(argv1) != 0) {
1133 printf("Error - failed to run with --file-prefix=%s\n",
1138 /* check if memtest1_map0 is present */
1139 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1140 printf("Error - hugepage files for %s were not deleted!\n",
1145 /* now, we're running a process under the same prefix, but with legacy
1146 * mem mode - this should leave behind hugepage files.
1148 if (launch_proc(argv2) != 0) {
1149 printf("Error - failed to run with --file-prefix=%s\n",
1154 /* check if memtest1_map0 is present */
1155 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 1) {
1156 printf("Error - hugepage files for %s were not created!\n",
1161 if (launch_proc(argv3) != 0) {
1162 printf("Error - failed to run with --file-prefix=%s\n",
1167 /* check if hugefiles for memtest2 are present */
1168 if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 1) {
1169 printf("Error - hugepage files for %s were not created!\n",
1174 /* check if hugefiles for memtest1 are present */
1175 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1176 printf("Error - hugepage files for %s were not deleted!\n",
1181 /* this process will run in default mem mode, so it should not leave any
1182 * hugepage files behind.
1184 if (launch_proc(argv4) != 0) {
1185 printf("Error - failed to run with --file-prefix=%s\n",
1190 /* check if hugefiles for memtest2 are present */
1191 if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 0) {
1192 printf("Error - hugepage files for %s were not deleted!\n",
1197 /* check if hugefiles for memtest1 are present */
1198 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1199 printf("Error - hugepage files for %s were not deleted!\n",
1204 /* this process will run in --in-memory mode, so it should not leave any
1205 * hugepage files behind.
1208 /* test case to check eal-options with --in-memory mode */
1209 if (launch_proc(argv5) != 0) {
1210 printf("Error - failed to run with --in-memory mode\n");
1214 /*test case to check eal-options with --in-memory mode with
1215 * custom file-prefix.
1217 if (launch_proc(argv6) != 0) {
1218 printf("Error - failed to run with --in-memory mode\n");
1222 /* check if hugefiles for memtest1 are present */
1223 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1224 printf("Error - hugepage files for %s were created and not deleted!\n",
1229 /* test case to check eal-options with --in-memory mode with
1230 * parent file-prefix.
1232 if (launch_proc(argv7) != 0) {
1233 printf("Error - failed to run with --file-prefix=%s\n", prefix);
1237 /* this process will run in --single-file-segments mode,
1238 * so it should not leave any hugepage files behind.
1240 if (launch_proc(argv8) != 0) {
1241 printf("Error - failed to run with --single-file-segments mode\n");
1245 /* check if hugefiles for memtest1 are present */
1246 if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1247 printf("Error - hugepage files for %s were not deleted!\n",
1255 /* This function writes in passed buf pointer a valid --socket-mem= option
1256 * for num_sockets then concatenates the provided suffix string.
1258 * Example for num_sockets 4, mem "2", suffix "plop"
1259 * --socket-mem=2,2,2,2plop
1262 populate_socket_mem_param(int num_sockets, const char *mem,
1263 const char *suffix, char *buf, size_t buf_size)
1265 unsigned int offset = 0;
1269 written = snprintf(&buf[offset], buf_size - offset, "--socket-mem=");
1270 if (written < 0 || written + offset >= buf_size)
1274 for (i = 0; i < num_sockets - 1; i++) {
1275 written = snprintf(&buf[offset], buf_size - offset,
1277 if (written < 0 || written + offset >= buf_size)
1282 written = snprintf(&buf[offset], buf_size - offset, "%s%s", mem,
1284 if (written < 0 || written + offset >= buf_size)
1290 * Tests for correct handling of -m and --socket-mem flags
1293 test_memory_flags(void)
1295 #ifdef RTE_EXEC_ENV_FREEBSD
1296 /* BSD target doesn't support prefixes at this point */
1297 const char * prefix = "";
1299 char prefix[PATH_MAX], tmp[PATH_MAX];
1300 if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
1301 printf("Error - unable to get current prefix!\n");
1304 snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
1307 /* valid -m flag and mp flag */
1308 const char *argv0[] = {prgname, prefix, mp_flag,
1309 "-m", DEFAULT_MEM_SIZE};
1312 const char *argv1[] = {prgname,
1313 "--file-prefix=" memtest, "-m", DEFAULT_MEM_SIZE};
1315 /* valid (zero) --socket-mem flag */
1316 char arg2_socket_mem[SOCKET_MEM_STRLEN];
1317 const char *argv2[] = {prgname,
1318 "--file-prefix=" memtest, arg2_socket_mem};
1320 /* invalid (incomplete) --socket-mem flag */
1321 char arg3_socket_mem[SOCKET_MEM_STRLEN];
1322 const char *argv3[] = {prgname,
1323 "--file-prefix=" memtest, arg3_socket_mem};
1325 /* invalid (mixed with invalid data) --socket-mem flag */
1326 char arg4_socket_mem[SOCKET_MEM_STRLEN];
1327 const char *argv4[] = {prgname,
1328 "--file-prefix=" memtest, arg4_socket_mem};
1330 /* invalid (with numeric value as last character) --socket-mem flag */
1331 char arg5_socket_mem[SOCKET_MEM_STRLEN];
1332 const char *argv5[] = {prgname,
1333 "--file-prefix=" memtest, arg5_socket_mem};
1335 /* invalid (with empty socket) --socket-mem flag */
1336 char arg6_socket_mem[SOCKET_MEM_STRLEN];
1337 const char *argv6[] = {prgname,
1338 "--file-prefix=" memtest, arg6_socket_mem};
1340 /* invalid (null) --socket-mem flag */
1341 const char *argv7[] = {prgname,
1342 "--file-prefix=" memtest, "--socket-mem="};
1344 /* valid --socket-mem specified together with -m flag */
1345 char arg8_socket_mem[SOCKET_MEM_STRLEN];
1346 const char *argv8[] = {prgname,
1347 "--file-prefix=" memtest, "-m", DEFAULT_MEM_SIZE,
1350 #ifdef RTE_EXEC_ENV_FREEBSD
1351 int num_sockets = 1;
1353 int num_sockets = RTE_MIN(get_number_of_sockets(),
1354 RTE_MAX_NUMA_NODES);
1357 if (num_sockets <= 0) {
1358 printf("Error - cannot get number of sockets!\n");
1362 /* invalid --socket-mem flag (with extra socket) */
1363 char invalid_socket_mem[SOCKET_MEM_STRLEN];
1364 const char *argv9[] = {prgname,
1365 "--file-prefix=" memtest, invalid_socket_mem};
1367 /* valid --socket-mem flag */
1368 char valid_socket_mem[SOCKET_MEM_STRLEN];
1369 const char *argv10[] = {prgname,
1370 "--file-prefix=" memtest, valid_socket_mem};
1372 if (launch_proc(argv0) != 0) {
1373 printf("Error - secondary process failed with valid -m flag !\n");
1377 #ifdef RTE_EXEC_ENV_FREEBSD
1378 /* no other tests are applicable to BSD */
1382 if (launch_proc(argv1) != 0) {
1383 printf("Error - process failed with valid -m flag!\n");
1387 populate_socket_mem_param(num_sockets, "0", "",
1388 arg2_socket_mem, sizeof(arg2_socket_mem));
1389 if (launch_proc(argv2) != 0) {
1390 printf("Error - process failed with valid (zero) --socket-mem!\n");
1394 if (num_sockets > 1) {
1395 populate_socket_mem_param(num_sockets - 1, "2", ",",
1396 arg3_socket_mem, sizeof(arg3_socket_mem));
1397 if (launch_proc(argv3) == 0) {
1398 printf("Error - process run ok with invalid "
1399 "(incomplete) --socket-mem!\n");
1403 populate_socket_mem_param(num_sockets - 1, "2", ",Fred",
1404 arg4_socket_mem, sizeof(arg4_socket_mem));
1405 if (launch_proc(argv4) == 0) {
1406 printf("Error - process run ok with invalid "
1407 "(mixed with invalid input) --socket-mem!\n");
1411 populate_socket_mem_param(num_sockets - 1, "2", ",Fred0",
1412 arg5_socket_mem, sizeof(arg5_socket_mem));
1413 if (launch_proc(argv5) == 0) {
1414 printf("Error - process run ok with invalid "
1415 "(mixed with invalid input with a numeric value as "
1416 "last character) --socket-mem!\n");
1421 if (num_sockets > 2) {
1422 populate_socket_mem_param(num_sockets - 2, "2", ",,2",
1423 arg6_socket_mem, sizeof(arg6_socket_mem));
1424 if (launch_proc(argv6) == 0) {
1425 printf("Error - process run ok with invalid "
1426 "(with empty socket) --socket-mem!\n");
1431 if (launch_proc(argv7) == 0) {
1432 printf("Error - process run ok with invalid (null) --socket-mem!\n");
1436 populate_socket_mem_param(num_sockets, "2", "",
1437 arg8_socket_mem, sizeof(arg8_socket_mem));
1438 if (launch_proc(argv8) == 0) {
1439 printf("Error - process run ok with --socket-mem and -m specified!\n");
1443 populate_socket_mem_param(num_sockets + 1, "2", "",
1444 invalid_socket_mem, sizeof(invalid_socket_mem));
1445 if (launch_proc(argv9) == 0) {
1446 printf("Error - process run ok with extra socket in --socket-mem!\n");
1450 populate_socket_mem_param(num_sockets, "2", "",
1451 valid_socket_mem, sizeof(valid_socket_mem));
1452 if (launch_proc(argv10) != 0) {
1453 printf("Error - process failed with valid --socket-mem!\n");
1461 test_eal_flags(void)
1465 ret = test_missing_c_flag();
1467 printf("Error in test_missing_c_flag()\n");
1471 ret = test_master_lcore_flag();
1473 printf("Error in test_master_lcore_flag()\n");
1477 ret = test_invalid_n_flag();
1479 printf("Error in test_invalid_n_flag()\n");
1483 ret = test_no_hpet_flag();
1485 printf("Error in test_no_hpet_flag()\n");
1489 ret = test_no_huge_flag();
1491 printf("Error in test_no_huge_flag()\n");
1495 ret = test_whitelist_flag();
1497 printf("Error in test_invalid_whitelist_flag()\n");
1501 ret = test_invalid_b_flag();
1503 printf("Error in test_invalid_b_flag()\n");
1507 #ifdef RTE_LIBRTE_PMD_RING
1508 ret = test_invalid_vdev_flag();
1510 printf("Error in test_invalid_vdev_flag()\n");
1514 ret = test_invalid_r_flag();
1516 printf("Error in test_invalid_r_flag()\n");
1520 ret = test_memory_flags();
1522 printf("Error in test_memory_flags()\n");
1526 ret = test_file_prefix();
1528 printf("Error in test_file_prefix()\n");
1532 ret = test_misc_flags();
1534 printf("Error in test_misc_flags()");
1541 REGISTER_TEST_COMMAND(eal_flags_autotest, test_eal_flags);
1543 /* subtests used in meson for CI */
1544 REGISTER_TEST_COMMAND(eal_flags_c_opt_autotest, test_missing_c_flag);
1545 REGISTER_TEST_COMMAND(eal_flags_master_opt_autotest, test_master_lcore_flag);
1546 REGISTER_TEST_COMMAND(eal_flags_n_opt_autotest, test_invalid_n_flag);
1547 REGISTER_TEST_COMMAND(eal_flags_hpet_autotest, test_no_hpet_flag);
1548 REGISTER_TEST_COMMAND(eal_flags_no_huge_autotest, test_no_huge_flag);
1549 REGISTER_TEST_COMMAND(eal_flags_w_opt_autotest, test_whitelist_flag);
1550 REGISTER_TEST_COMMAND(eal_flags_b_opt_autotest, test_invalid_b_flag);
1551 REGISTER_TEST_COMMAND(eal_flags_vdev_opt_autotest, test_invalid_vdev_flag);
1552 REGISTER_TEST_COMMAND(eal_flags_r_opt_autotest, test_invalid_r_flag);
1553 REGISTER_TEST_COMMAND(eal_flags_mem_autotest, test_memory_flags);
1554 REGISTER_TEST_COMMAND(eal_flags_file_prefix_autotest, test_file_prefix);
1555 REGISTER_TEST_COMMAND(eal_flags_misc_autotest, test_misc_flags);