eal/windows: remove useless C++ include guard
[dpdk.git] / app / test / test_eal_flags.c
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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         /* check if files for current prefix are present */
1214         if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
1215                 printf("Error - hugepage files for %s were not created!\n", prefix);
1216                 return -1;
1217         }
1218
1219         /* checks if files for current prefix are locked */
1220         if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
1221                 printf("Error - hugepages for current process aren't locked!\n");
1222                 return -1;
1223         }
1224
1225         /* check if files for secondary process are present */
1226         if (process_hugefiles(memtest, HUGEPAGE_CHECK_EXISTS) == 1) {
1227                 /* check if they are not locked */
1228                 if (process_hugefiles(memtest, HUGEPAGE_CHECK_LOCKED) == 1) {
1229                         printf("Error - hugepages for current process are locked!\n");
1230                         return -1;
1231                 }
1232                 /* they aren't locked, delete them */
1233                 else {
1234                         if (process_hugefiles(memtest, HUGEPAGE_DELETE) != 1) {
1235                                 printf("Error - deleting hugepages failed!\n");
1236                                 return -1;
1237                         }
1238                 }
1239         }
1240
1241         if (launch_proc(argv0) == 0) {
1242                 printf("Error - secondary process ran ok without primary process\n");
1243                 return -1;
1244         }
1245
1246         /* check if files for current prefix are present */
1247         if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
1248                 printf("Error - hugepage files for %s were not created!\n", prefix);
1249                 return -1;
1250         }
1251
1252         /* checks if files for current prefix are locked */
1253         if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
1254                 printf("Error - hugepages for current process aren't locked!\n");
1255                 return -1;
1256         }
1257
1258         /* we're running this process in default memory mode, which means it
1259          * should clean up after itself on exit and leave no hugepages behind.
1260          */
1261         if (launch_proc(argv1) != 0) {
1262                 printf("Error - failed to run with --file-prefix=%s\n",
1263                                 memtest1);
1264                 return -1;
1265         }
1266
1267         /* check if memtest1_map0 is present */
1268         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1269                 printf("Error - hugepage files for %s were not deleted!\n",
1270                                 memtest1);
1271                 return -1;
1272         }
1273
1274         /* now, we're running a process under the same prefix, but with legacy
1275          * mem mode - this should leave behind hugepage files.
1276          */
1277         if (launch_proc(argv2) != 0) {
1278                 printf("Error - failed to run with --file-prefix=%s\n",
1279                                 memtest1);
1280                 return -1;
1281         }
1282
1283         /* check if memtest1_map0 is present */
1284         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 1) {
1285                 printf("Error - hugepage files for %s were not created!\n",
1286                                 memtest1);
1287                 return -1;
1288         }
1289
1290         if (launch_proc(argv3) != 0) {
1291                 printf("Error - failed to run with --file-prefix=%s\n",
1292                                 memtest2);
1293                 return -1;
1294         }
1295
1296         /* check if hugefiles for memtest2 are present */
1297         if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 1) {
1298                 printf("Error - hugepage files for %s were not created!\n",
1299                                 memtest2);
1300                 return -1;
1301         }
1302
1303         /* check if hugefiles for memtest1 are present */
1304         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1305                 printf("Error - hugepage files for %s were not deleted!\n",
1306                                 memtest1);
1307                 return -1;
1308         }
1309
1310         /* this process will run in default mem mode, so it should not leave any
1311          * hugepage files behind.
1312          */
1313         if (launch_proc(argv4) != 0) {
1314                 printf("Error - failed to run with --file-prefix=%s\n",
1315                                 memtest2);
1316                 return -1;
1317         }
1318
1319         /* check if hugefiles for memtest2 are present */
1320         if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 0) {
1321                 printf("Error - hugepage files for %s were not deleted!\n",
1322                                 memtest2);
1323                 return -1;
1324         }
1325
1326         /* check if hugefiles for memtest1 are present */
1327         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1328                 printf("Error - hugepage files for %s were not deleted!\n",
1329                                 memtest1);
1330                 return -1;
1331         }
1332
1333         /* this process will run in --in-memory mode, so it should not leave any
1334          * hugepage files behind.
1335          */
1336
1337         /* test case to check eal-options with --in-memory mode */
1338         if (launch_proc(argv5) != 0) {
1339                 printf("Error - failed to run with --in-memory mode\n");
1340                 return -1;
1341         }
1342
1343         /*test case to check eal-options with --in-memory mode with
1344          * custom file-prefix.
1345          */
1346         if (launch_proc(argv6) != 0) {
1347                 printf("Error - failed to run with --in-memory mode\n");
1348                 return -1;
1349         }
1350
1351         /* check if hugefiles for memtest1 are present */
1352         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1353                 printf("Error - hugepage files for %s were created and not deleted!\n",
1354                                 memtest1);
1355                 return -1;
1356         }
1357
1358         /* test case to check eal-options with --in-memory mode with
1359          * parent file-prefix.
1360          */
1361         if (launch_proc(argv7) != 0) {
1362                 printf("Error - failed to run with --file-prefix=%s\n", prefix);
1363                 return -1;
1364         }
1365
1366         /* this process will run in --single-file-segments mode,
1367          * so it should not leave any hugepage files behind.
1368          */
1369         if (launch_proc(argv8) != 0) {
1370                 printf("Error - failed to run with --single-file-segments mode\n");
1371                 return -1;
1372         }
1373
1374         /* check if hugefiles for memtest1 are present */
1375         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
1376                 printf("Error - hugepage files for %s were not deleted!\n",
1377                                 memtest1);
1378                 return -1;
1379         }
1380
1381         return 0;
1382 }
1383
1384 /* This function writes in passed buf pointer a valid --socket-mem= option
1385  * for num_sockets then concatenates the provided suffix string.
1386  *
1387  * Example for num_sockets 4, mem "2", suffix "plop"
1388  * --socket-mem=2,2,2,2plop
1389  */
1390 static void
1391 populate_socket_mem_param(int num_sockets, const char *mem,
1392                 const char *suffix, char *buf, size_t buf_size)
1393 {
1394         unsigned int offset = 0;
1395         int written;
1396         int i;
1397
1398         written = snprintf(&buf[offset], buf_size - offset, "--socket-mem=");
1399         if (written < 0 || written + offset >= buf_size)
1400                 return;
1401         offset += written;
1402
1403         for (i = 0; i < num_sockets - 1; i++) {
1404                 written = snprintf(&buf[offset], buf_size - offset,
1405                         "%s,", mem);
1406                 if (written < 0 || written + offset >= buf_size)
1407                         return;
1408                 offset += written;
1409         }
1410
1411         written = snprintf(&buf[offset], buf_size - offset, "%s%s", mem,
1412                 suffix);
1413         if (written < 0 || written + offset >= buf_size)
1414                 return;
1415         offset += written;
1416 }
1417
1418 /*
1419  * Tests for correct handling of -m and --socket-mem flags
1420  */
1421 static int
1422 test_memory_flags(void)
1423 {
1424 #ifdef RTE_EXEC_ENV_FREEBSD
1425         /* BSD target doesn't support prefixes at this point */
1426         const char * prefix = "";
1427 #else
1428         char prefix[PATH_MAX], tmp[PATH_MAX];
1429         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
1430                 printf("Error - unable to get current prefix!\n");
1431                 return -1;
1432         }
1433         snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
1434 #endif
1435
1436         /* valid -m flag and mp flag */
1437         const char *argv0[] = {prgname, prefix, mp_flag,
1438                         "-m", DEFAULT_MEM_SIZE};
1439
1440         /* valid -m flag */
1441         const char *argv1[] = {prgname,
1442                         "--file-prefix=" memtest, "-m", DEFAULT_MEM_SIZE};
1443
1444         /* valid (zero) --socket-mem flag */
1445         char arg2_socket_mem[SOCKET_MEM_STRLEN];
1446         const char *argv2[] = {prgname,
1447                         "--file-prefix=" memtest, arg2_socket_mem};
1448
1449         /* invalid (incomplete) --socket-mem flag */
1450         char arg3_socket_mem[SOCKET_MEM_STRLEN];
1451         const char *argv3[] = {prgname,
1452                         "--file-prefix=" memtest, arg3_socket_mem};
1453
1454         /* invalid (mixed with invalid data) --socket-mem flag */
1455         char arg4_socket_mem[SOCKET_MEM_STRLEN];
1456         const char *argv4[] = {prgname,
1457                         "--file-prefix=" memtest, arg4_socket_mem};
1458
1459         /* invalid (with numeric value as last character) --socket-mem flag */
1460         char arg5_socket_mem[SOCKET_MEM_STRLEN];
1461         const char *argv5[] = {prgname,
1462                         "--file-prefix=" memtest, arg5_socket_mem};
1463
1464         /* invalid (with empty socket) --socket-mem flag */
1465         char arg6_socket_mem[SOCKET_MEM_STRLEN];
1466         const char *argv6[] = {prgname,
1467                         "--file-prefix=" memtest, arg6_socket_mem};
1468
1469         /* invalid (null) --socket-mem flag */
1470         const char *argv7[] = {prgname,
1471                         "--file-prefix=" memtest, "--socket-mem="};
1472
1473         /* valid --socket-mem specified together with -m flag */
1474         char arg8_socket_mem[SOCKET_MEM_STRLEN];
1475         const char *argv8[] = {prgname,
1476                         "--file-prefix=" memtest, "-m", DEFAULT_MEM_SIZE,
1477                         arg8_socket_mem};
1478
1479 #ifdef RTE_EXEC_ENV_FREEBSD
1480         int num_sockets = 1;
1481 #else
1482         int num_sockets = RTE_MIN(get_number_of_sockets(),
1483                         RTE_MAX_NUMA_NODES);
1484 #endif
1485
1486         if (num_sockets <= 0) {
1487                 printf("Error - cannot get number of sockets!\n");
1488                 return -1;
1489         }
1490
1491         /* invalid --socket-mem flag (with extra socket) */
1492         char invalid_socket_mem[SOCKET_MEM_STRLEN];
1493         const char *argv9[] = {prgname,
1494                         "--file-prefix=" memtest, invalid_socket_mem};
1495
1496         /* valid --socket-mem flag */
1497         char valid_socket_mem[SOCKET_MEM_STRLEN];
1498         const char *argv10[] = {prgname,
1499                         "--file-prefix=" memtest, valid_socket_mem};
1500
1501         if (launch_proc(argv0) != 0) {
1502                 printf("Error - secondary process failed with valid -m flag !\n");
1503                 return -1;
1504         }
1505
1506 #ifdef RTE_EXEC_ENV_FREEBSD
1507         /* no other tests are applicable to BSD */
1508         return 0;
1509 #endif
1510
1511         if (launch_proc(argv1) != 0) {
1512                 printf("Error - process failed with valid -m flag!\n");
1513                 return -1;
1514         }
1515
1516         populate_socket_mem_param(num_sockets, "0", "",
1517                 arg2_socket_mem, sizeof(arg2_socket_mem));
1518         if (launch_proc(argv2) != 0) {
1519                 printf("Error - process failed with valid (zero) --socket-mem!\n");
1520                 return -1;
1521         }
1522
1523         if (num_sockets > 1) {
1524                 populate_socket_mem_param(num_sockets - 1, "2", ",",
1525                         arg3_socket_mem, sizeof(arg3_socket_mem));
1526                 if (launch_proc(argv3) == 0) {
1527                         printf("Error - process run ok with invalid "
1528                                 "(incomplete) --socket-mem!\n");
1529                         return -1;
1530                 }
1531
1532                 populate_socket_mem_param(num_sockets - 1, "2", ",Fred",
1533                         arg4_socket_mem, sizeof(arg4_socket_mem));
1534                 if (launch_proc(argv4) == 0) {
1535                         printf("Error - process run ok with invalid "
1536                                 "(mixed with invalid input) --socket-mem!\n");
1537                         return -1;
1538                 }
1539
1540                 populate_socket_mem_param(num_sockets - 1, "2", ",Fred0",
1541                         arg5_socket_mem, sizeof(arg5_socket_mem));
1542                 if (launch_proc(argv5) == 0) {
1543                         printf("Error - process run ok with invalid "
1544                                 "(mixed with invalid input with a numeric value as "
1545                                 "last character) --socket-mem!\n");
1546                         return -1;
1547                 }
1548         }
1549
1550         if (num_sockets > 2) {
1551                 populate_socket_mem_param(num_sockets - 2, "2", ",,2",
1552                         arg6_socket_mem, sizeof(arg6_socket_mem));
1553                 if (launch_proc(argv6) == 0) {
1554                         printf("Error - process run ok with invalid "
1555                                 "(with empty socket) --socket-mem!\n");
1556                         return -1;
1557                 }
1558         }
1559
1560         if (launch_proc(argv7) == 0) {
1561                 printf("Error - process run ok with invalid (null) --socket-mem!\n");
1562                 return -1;
1563         }
1564
1565         populate_socket_mem_param(num_sockets, "2", "",
1566                 arg8_socket_mem, sizeof(arg8_socket_mem));
1567         if (launch_proc(argv8) == 0) {
1568                 printf("Error - process run ok with --socket-mem and -m specified!\n");
1569                 return -1;
1570         }
1571
1572         populate_socket_mem_param(num_sockets + 1, "2", "",
1573                 invalid_socket_mem, sizeof(invalid_socket_mem));
1574         if (launch_proc(argv9) == 0) {
1575                 printf("Error - process run ok with extra socket in --socket-mem!\n");
1576                 return -1;
1577         }
1578
1579         populate_socket_mem_param(num_sockets, "2", "",
1580                 valid_socket_mem, sizeof(valid_socket_mem));
1581         if (launch_proc(argv10) != 0) {
1582                 printf("Error - process failed with valid --socket-mem!\n");
1583                 return -1;
1584         }
1585
1586         return 0;
1587 }
1588
1589 #endif /* !RTE_EXEC_ENV_WINDOWS */
1590
1591 REGISTER_TEST_COMMAND(eal_flags_c_opt_autotest, test_missing_c_flag);
1592 REGISTER_TEST_COMMAND(eal_flags_main_opt_autotest, test_main_lcore_flag);
1593 REGISTER_TEST_COMMAND(eal_flags_n_opt_autotest, test_invalid_n_flag);
1594 REGISTER_TEST_COMMAND(eal_flags_hpet_autotest, test_no_hpet_flag);
1595 REGISTER_TEST_COMMAND(eal_flags_no_huge_autotest, test_no_huge_flag);
1596 REGISTER_TEST_COMMAND(eal_flags_a_opt_autotest, test_allow_flag);
1597 REGISTER_TEST_COMMAND(eal_flags_b_opt_autotest, test_invalid_b_flag);
1598 REGISTER_TEST_COMMAND(eal_flags_vdev_opt_autotest, test_invalid_vdev_flag);
1599 REGISTER_TEST_COMMAND(eal_flags_r_opt_autotest, test_invalid_r_flag);
1600 REGISTER_TEST_COMMAND(eal_flags_mem_autotest, test_memory_flags);
1601 REGISTER_TEST_COMMAND(eal_flags_file_prefix_autotest, test_file_prefix);
1602 REGISTER_TEST_COMMAND(eal_flags_misc_autotest, test_misc_flags);