563f9359fd608567e840713f8902e27f18669aca
[dpdk.git] / app / test / test_eal_flags.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34 #include <stdio.h>
35
36 #include <cmdline_parse.h>
37
38 #include "test.h"
39
40 #ifndef RTE_EXEC_ENV_BAREMETAL
41 #include <string.h>
42 #include <stdarg.h>
43 #include <libgen.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <dirent.h>
49 #include <sys/wait.h>
50 #include <sys/file.h>
51
52 #include <rte_debug.h>
53 #include <rte_string_fns.h>
54
55 #include "process.h"
56
57 #define mp_flag "--proc-type=secondary"
58 #define no_hpet "--no-hpet"
59 #define no_huge "--no-huge"
60 #define no_shconf "--no-shconf"
61 #define memtest "memtest"
62 #define memtest1 "memtest1"
63 #define memtest2 "memtest2"
64 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
65 #define launch_proc(ARGV) process_dup(ARGV, \
66                 sizeof(ARGV)/(sizeof(ARGV[0])), __func__)
67
68 enum hugepage_action {
69         HUGEPAGE_CHECK_EXISTS = 0,
70         HUGEPAGE_CHECK_LOCKED,
71         HUGEPAGE_DELETE,
72         HUGEPAGE_INVALID
73 };
74
75 /* if string contains a hugepage path */
76 static int
77 get_hugepage_path(char * src, int src_len, char * dst, int dst_len)
78 {
79 #define NUM_TOKENS 4
80         char *tokens[NUM_TOKENS];
81
82         /* if we couldn't properly split the string */
83         if (rte_strsplit(src, src_len, tokens, NUM_TOKENS, ' ') < NUM_TOKENS)
84                 return 0;
85
86         if (strncmp(tokens[2], "hugetlbfs", sizeof("hugetlbfs")) == 0) {
87                 rte_snprintf(dst, dst_len, "%s", tokens[1]);
88                 return 1;
89         }
90         return 0;
91 }
92
93 /*
94  * Cycles through hugepage directories and looks for hugepage
95  * files associated with a given prefix. Depending on value of
96  * action, the hugepages are checked if they exist, checked if
97  * they can be locked, or are simply deleted.
98  *
99  * Returns 1 if it finds at least one hugepage matching the action
100  * Returns 0 if no matching hugepages were found
101  * Returns -1 if it encounters an error
102  */
103 static int
104 process_hugefiles(const char * prefix, enum hugepage_action action)
105 {
106         FILE * hugedir_handle = NULL;
107         DIR * hugepage_dir = NULL;
108         struct dirent *dirent = NULL;
109
110         char hugefile_prefix[PATH_MAX] = {0};
111         char hugedir[PATH_MAX] = {0};
112         char line[PATH_MAX] = {0};
113
114         int fd, lck_result, result = 0;
115
116         const int prefix_len = rte_snprintf(hugefile_prefix,
117                         sizeof(hugefile_prefix), "%smap_", prefix);
118         if (prefix_len <= 0 || prefix_len >= (int)sizeof(hugefile_prefix)
119                         || prefix_len >= (int)sizeof(dirent->d_name)) {
120                 printf("Error creating hugefile filename prefix\n");
121                 return -1;
122         }
123
124         /* get hugetlbfs mountpoints from /proc/mounts */
125         hugedir_handle = fopen("/proc/mounts", "r");
126
127         if (hugedir_handle == NULL) {
128                 printf("Error parsing /proc/mounts!\n");
129                 return -1;
130         }
131
132         /* read and parse script output */
133         while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
134
135                 /* check if we have a hugepage filesystem path */
136                 if (!get_hugepage_path(line, sizeof(line), hugedir, sizeof(hugedir)))
137                         continue;
138
139                 /* check if directory exists */
140                 if ((hugepage_dir = opendir(hugedir)) == NULL) {
141                         fclose(hugedir_handle);
142                         printf("Error reading %s: %s\n", hugedir, strerror(errno));
143                         return -1;
144                 }
145
146                 while ((dirent = readdir(hugepage_dir)) != NULL) {
147                         if (memcmp(dirent->d_name, hugefile_prefix, prefix_len) != 0)
148                                 continue;
149
150                         switch (action) {
151                         case HUGEPAGE_CHECK_EXISTS:
152                                 {
153                                         /* file exists, return */
154                                         result = 1;
155                                         goto end;
156                                 }
157                                 break;
158                         case HUGEPAGE_DELETE:
159                                 {
160                                         char file_path[PATH_MAX] = {0};
161
162                                         rte_snprintf(file_path, sizeof(file_path),
163                                                 "%s/%s", hugedir, dirent->d_name);
164                                         
165                                         /* remove file */
166                                         if (remove(file_path) < 0) {
167                                                 printf("Error deleting %s - %s!\n",
168                                                                 dirent->d_name, strerror(errno));
169                                                 closedir(hugepage_dir);
170                                                 result = -1;
171                                                 goto end;
172                                         }
173                                         result = 1;
174                                 }
175                                 break;
176                         case HUGEPAGE_CHECK_LOCKED:
177                                 {
178                                         /* try and lock the file */
179                                         fd = openat(dirfd(hugepage_dir), dirent->d_name, O_RDONLY);
180
181                                         /* this shouldn't happen */
182                                         if (fd == -1) {
183                                                 printf("Error opening %s - %s!\n",
184                                                                 dirent->d_name, strerror(errno));
185                                                 closedir(hugepage_dir);
186                                                 result = -1;
187                                                 goto end;
188                                         }
189
190                                         /* non-blocking lock */
191                                         lck_result = flock(fd, LOCK_EX | LOCK_NB);
192
193                                         /* if lock succeeds, there's something wrong */
194                                         if (lck_result != -1) {
195                                                 result = 0;
196
197                                                 /* unlock the resulting lock */
198                                                 flock(fd, LOCK_UN);
199                                                 close(fd);
200                                                 closedir(hugepage_dir);
201                                                 goto end;
202                                         }
203                                         result = 1;
204                                         close(fd);
205                                 }
206                                 break;
207                                 /* shouldn't happen */
208                         default:
209                                 goto end;
210                         } /* switch */
211
212                 } /* read hugepage directory */
213                 closedir(hugepage_dir);
214         } /* read /proc/mounts */
215 end:
216         fclose(hugedir_handle);
217         return result;
218 }
219
220 /*
221  * count the number of "node*" files in /sys/devices/system/node/
222  */
223 static int
224 get_number_of_sockets(void)
225 {
226         struct dirent *dirent = NULL;
227         const char * nodedir = "/sys/devices/system/node/";
228         DIR * dir = NULL;
229         int result = 0;
230
231         /* check if directory exists */
232         if ((dir = opendir(nodedir)) == NULL) {
233                 /* if errno==ENOENT this means we don't have NUMA support */
234                 if (errno == ENOENT) {
235                         printf("No NUMA nodes detected: assuming 1 available socket\n");
236                         return 1;
237                 }
238                 printf("Error opening %s: %s\n", nodedir, strerror(errno));
239                 return -1;
240         }
241
242         while ((dirent = readdir(dir)) != NULL)
243                 if (strncmp(dirent->d_name, "node", sizeof("node") - 1) == 0)
244                         result++;
245
246         closedir(dir);
247         return result;
248 }
249
250 static char*
251 get_current_prefix(char * prefix, int size)
252 {
253         char path[PATH_MAX] = {0};
254         char buf[PATH_MAX] = {0};
255
256         /* get file for config (fd is always 3) */
257         rte_snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
258
259         /* return NULL on error */
260         if (readlink(path, buf, sizeof(buf)) == -1)
261                 return NULL;
262
263         /* get the basename */
264         rte_snprintf(buf, sizeof(buf), "%s", basename(buf));
265
266         /* copy string all the way from second char up to start of _config */
267         rte_snprintf(prefix, size, "%.*s",
268                         strnlen(buf, sizeof(buf)) - sizeof("_config"), &buf[1]);
269
270         return prefix;
271 }
272
273 /*
274  * Test that the app doesn't run without invalid blacklist option.
275  * Final test ensures it does run with valid options as sanity check
276  */
277 static int
278 test_invalid_b_flag(void)
279 {
280         char prefix[PATH_MAX], tmp[PATH_MAX];
281         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
282                 printf("Error - unable to get current prefix!\n");
283                 return -1;
284         }
285         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
286
287         const char *blinval[][9] = {
288                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "error"},
289                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:0:0"},
290                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:error:0.1"},
291                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:0:0.1error"},
292                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "error0:0:0.1"},
293                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:0:0.1.2"},
294         };
295         /* Test with valid blacklist option */
296         const char *blval[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "FF:09:0B.3"};
297
298         int i;
299
300         for (i = 0; i != sizeof (blinval) / sizeof (blinval[0]); i++) {
301                 if (launch_proc(blinval[i]) == 0) {
302                         printf("Error - process did run ok with invalid "
303                             "blacklist parameter\n");
304                         return -1;
305                 }
306         }
307         if (launch_proc(blval) != 0) {
308                 printf("Error - process did not run ok with valid blacklist value\n");
309                 return -1;
310         }
311         return 0;
312 }
313
314
315 /*
316  * Test that the app doesn't run with invalid -r option.
317  */
318 static int
319 test_invalid_r_flag(void)
320 {
321         char prefix[PATH_MAX], tmp[PATH_MAX];
322         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
323                 printf("Error - unable to get current prefix!\n");
324                 return -1;
325         }
326         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
327
328         const char *rinval[][9] = {
329                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "error"},
330                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "0"},
331                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "-1"},
332                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "17"},
333         };
334         /* Test with valid blacklist option */
335         const char *rval[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "16"};
336
337         int i;
338
339         for (i = 0; i != sizeof (rinval) / sizeof (rinval[0]); i++) {
340                 if (launch_proc(rinval[i]) == 0) {
341                         printf("Error - process did run ok with invalid "
342                             "-r (rank) parameter\n");
343                         return -1;
344                 }
345         }
346         if (launch_proc(rval) != 0) {
347                 printf("Error - process did not run ok with valid -r (rank) value\n");
348                 return -1;
349         }
350         return 0;
351 }
352
353 /*
354  * Test that the app doesn't run without the coremask flag. In all cases
355  * should give an error and fail to run
356  */
357 static int
358 test_missing_c_flag(void)
359 {
360         char prefix[PATH_MAX], tmp[PATH_MAX];
361         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
362                 printf("Error - unable to get current prefix!\n");
363                 return -1;
364         }
365         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
366
367         /* -c flag but no coremask value */
368         const char *argv1[] = { prgname, prefix, mp_flag, "-n", "3", "-c"};
369         /* No -c flag at all */
370         const char *argv2[] = { prgname, prefix, mp_flag, "-n", "3"};
371         /* bad coremask value */
372         const char *argv3[] = { prgname, prefix, mp_flag, "-n", "3", "-c", "error" };
373         /* sanity check of tests - valid coremask value */
374         const char *argv4[] = { prgname, prefix, mp_flag, "-n", "3", "-c", "1" };
375
376         if (launch_proc(argv1) == 0
377                         || launch_proc(argv2) == 0
378                         || launch_proc(argv3) == 0) {
379                 printf("Error - process ran without error when missing -c flag\n");
380                 return -1;
381         }
382         if (launch_proc(argv4) != 0) {
383                 printf("Error - process did not run ok with valid coremask value\n");
384                 return -1;
385         }
386         return 0;
387 }
388
389 /*
390  * Test that the app doesn't run without the -n flag. In all cases
391  * should give an error and fail to run.
392  * Since -n is not compulsory for MP, we instead use --no-huge and --no-shconf
393  * flags.
394  */
395 static int
396 test_missing_n_flag(void)
397 {
398         char prefix[PATH_MAX], tmp[PATH_MAX];
399         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
400                 printf("Error - unable to get current prefix!\n");
401                 return -1;
402         }
403         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
404
405         /* -n flag but no value */
406         const char *argv1[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n"};
407         /* No -n flag at all */
408         const char *argv2[] = { prgname, prefix, no_huge, no_shconf, "-c", "1"};
409         /* bad numeric value */
410         const char *argv3[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n", "e" };
411         /* out-of-range value */
412         const char *argv4[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n", "9" };
413         /* sanity test - check with good value */
414         const char *argv5[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n", "2" };
415
416         if (launch_proc(argv1) == 0
417                         || launch_proc(argv2) == 0
418                         || launch_proc(argv3) == 0
419                         || launch_proc(argv4) == 0) {
420                 printf("Error - process ran without error when missing -n flag\n");
421                 return -1;
422         }
423         if (launch_proc(argv5) != 0) {
424                 printf("Error - process did not run ok with valid num-channel value\n");
425                 return -1;
426         }
427         return 0;
428 }
429
430 /*
431  * Test that the app runs with HPET, and without HPET
432  */
433 static int
434 test_no_hpet_flag(void)
435 {
436         char prefix[PATH_MAX], tmp[PATH_MAX];
437         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
438                 printf("Error - unable to get current prefix!\n");
439                 return -1;
440         }
441         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
442
443         /* With --no-hpet */
444         const char *argv1[] = {prgname, prefix, mp_flag, no_hpet, "-c", "1", "-n", "2"};
445         /* Without --no-hpet */
446         const char *argv2[] = {prgname, prefix, mp_flag, "-c", "1", "-n", "2"};
447
448         if (launch_proc(argv1) != 0) {
449                 printf("Error - process did not run ok with --no-hpet flag\n");
450                 return -1;
451         }
452         if (launch_proc(argv2) != 0) {
453                 printf("Error - process did not run ok without --no-hpet flag\n");
454                 return -1;
455         }
456         return 0;
457 }
458
459 /*
460  * Test that the app runs with --no-huge and doesn't run when either
461  * -m or --socket-mem are specified with --no-huge.
462  */
463 static int
464 test_no_huge_flag(void)
465 {
466         char prefix[PATH_MAX], tmp[PATH_MAX];
467         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
468                 printf("Error - unable to get current prefix!\n");
469                 return -1;
470         }
471         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
472
473         /* With --no-huge */
474         const char *argv1[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2",
475                         "--file-prefix=nohuge"};
476         /* With --no-huge and -m */
477         const char *argv2[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2", "-m", "2",
478                         "--file-prefix=nohuge"};
479         /* With --no-huge and --socket-mem */
480         const char *argv3[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2",
481                         "--socket-mem=2", "--file-prefix=nohuge"};
482         /* With --no-huge, -m and --socket-mem */
483         const char *argv4[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2",
484                         "-m", "2", "--socket-mem=2", "--file-prefix=nohuge"};
485
486         if (launch_proc(argv1) != 0) {
487                 printf("Error - process did not run ok with --no-huge flag\n");
488                 return -1;
489         }
490         if (launch_proc(argv2) == 0) {
491                 printf("Error - process run ok with --no-huge and -m flags\n");
492                 return -1;
493         }
494         if (launch_proc(argv3) == 0) {
495                 printf("Error - process run ok with --no-huge and --socket-mem "
496                                 "flags\n");
497                 return -1;
498         }
499         if (launch_proc(argv4) == 0) {
500                 printf("Error - process run ok with --no-huge, -m and "
501                                 "--socket-mem flags\n");
502                 return -1;
503         }
504         return 0;
505 }
506
507 static int
508 test_misc_flags(void)
509 {
510         FILE * hugedir_handle = NULL;
511         char line[PATH_MAX] = {0};
512         char hugepath[PATH_MAX] = {0};
513         char prefix[PATH_MAX], tmp[PATH_MAX];
514         unsigned i, isempty = 1;
515
516         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
517                 printf("Error - unable to get current prefix!\n");
518                 return -1;
519         }
520         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
521
522         /*
523          * get first valid hugepage path
524          */
525
526         /* get hugetlbfs mountpoints from /proc/mounts */
527         hugedir_handle = fopen("/proc/mounts", "r");
528
529         if (hugedir_handle == NULL) {
530                 printf("Error opening /proc/mounts!\n");
531                 return -1;
532         }
533
534         /* read /proc/mounts */
535         while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
536
537                 /* find first valid hugepath */
538                 if (get_hugepage_path(line, sizeof(line), hugepath, sizeof(hugepath)))
539                         break;
540         }
541
542         fclose(hugedir_handle);
543
544         /* check if path is not empty */
545         for (i = 0; i < sizeof(hugepath); i++)
546                 if (hugepath[i] != '\0')
547                         isempty = 0;
548
549         if (isempty) {
550                 printf("No mounted hugepage dir found!\n");
551                 return -1;
552         }
553
554
555         /* check that some general flags don't prevent things from working.
556          * All cases, apart from the first, app should run.
557          * No futher testing of output done.
558          */
559         /* sanity check - failure with invalid option */
560         const char *argv0[] = {prgname, prefix, mp_flag, "-c", "1", "--invalid-opt"};
561
562         /* With --no-pci */
563         const char *argv1[] = {prgname, prefix, mp_flag, "-c", "1", "--no-pci"};
564         /* With -v */
565         const char *argv2[] = {prgname, prefix, mp_flag, "-c", "1", "-v"};
566         /* With valid --syslog */
567         const char *argv3[] = {prgname, prefix, mp_flag, "-c", "1",
568                         "--syslog", "syslog"};
569         /* With empty --syslog (should fail) */
570         const char *argv4[] = {prgname, prefix, mp_flag, "-c", "1", "--syslog"};
571         /* With invalid --syslog */
572         const char *argv5[] = {prgname, prefix, mp_flag, "-c", "1", "--syslog", "error"};
573         /* With no-sh-conf */
574         const char *argv6[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
575                         "--no-shconf", "--file-prefix=noshconf" };
576         /* With --huge-dir */
577         const char *argv7[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
578                         "--file-prefix=hugedir", "--huge-dir", hugepath};
579         /* With empty --huge-dir (should fail) */
580         const char *argv8[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
581                         "--file-prefix=hugedir", "--huge-dir"};
582         /* With invalid --huge-dir */
583         const char *argv9[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
584                         "--file-prefix=hugedir", "--huge-dir", "invalid"};
585         /* Secondary process with invalid --huge-dir (should run as flag has no
586          * effect on secondary processes) */
587         const char *argv10[] = {prgname, prefix, mp_flag, "-c", "1", "--huge-dir", "invalid"};
588
589
590         if (launch_proc(argv0) == 0) {
591                 printf("Error - process ran ok with invalid flag\n");
592                 return -1;
593         }
594         if (launch_proc(argv1) != 0) {
595                 printf("Error - process did not run ok with --no-pci flag\n");
596                 return -1;
597         }
598         if (launch_proc(argv2) != 0) {
599                 printf("Error - process did not run ok with -v flag\n");
600                 return -1;
601         }
602         if (launch_proc(argv3) != 0) {
603                 printf("Error - process did not run ok with --syslog flag\n");
604                 return -1;
605         }
606         if (launch_proc(argv4) == 0) {
607                 printf("Error - process run ok with empty --syslog flag\n");
608                 return -1;
609         }
610         if (launch_proc(argv5) == 0) {
611                 printf("Error - process run ok with invalid --syslog flag\n");
612                 return -1;
613         }
614         if (launch_proc(argv6) != 0) {
615                 printf("Error - process did not run ok with --no-shconf flag\n");
616                 return -1;
617         }
618         if (launch_proc(argv7) != 0) {
619                 printf("Error - process did not run ok with --huge-dir flag\n");
620                 return -1;
621         }
622         if (launch_proc(argv8) == 0) {
623                 printf("Error - process run ok with empty --huge-dir flag\n");
624                 return -1;
625         }
626         if (launch_proc(argv9) == 0) {
627                 printf("Error - process run ok with invalid --huge-dir flag\n");
628                 return -1;
629         }
630         if (launch_proc(argv10) != 0) {
631                 printf("Error - secondary process did not run ok with invalid --huge-dir flag\n");
632                 return -1;
633         }
634         return 0;
635 }
636
637 static int
638 test_file_prefix(void)
639 {
640         /*
641          * 1. check if current process hugefiles are locked
642          * 2. try to run secondary process without a corresponding primary process
643          * (while failing to run, it will also remove any unused hugepage files)
644          * 3. check if current process hugefiles are still in place and are locked
645          * 4. run a primary process with memtest1 prefix
646          * 5. check if memtest1 hugefiles are created
647          * 6. run a primary process with memtest2 prefix
648          * 7. check that only memtest2 hugefiles are present in the hugedir
649          */
650
651         /* this should fail unless the test itself is run with "memtest" prefix */
652         const char *argv0[] = {prgname, mp_flag, "-c", "1", "-n", "2", "-m", "2",
653                         "--file-prefix=" memtest };
654
655         /* primary process with memtest1 */
656         const char *argv1[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
657                                 "--file-prefix=" memtest1 };
658
659         /* primary process with memtest2 */
660         const char *argv2[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
661                                 "--file-prefix=" memtest2 };
662
663         char prefix[32];
664         if (get_current_prefix(prefix, sizeof(prefix)) == NULL) {
665                 printf("Error - unable to get current prefix!\n");
666                 return -1;
667         }
668
669         /* check if files for current prefix are present */
670         if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
671                 printf("Error - hugepage files for %s were not created!\n", prefix);
672                 return -1;
673         }
674
675         /* checks if files for current prefix are locked */
676         if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
677                 printf("Error - hugepages for current process aren't locked!\n");
678                 return -1;
679         }
680
681         /* check if files for secondary process are present */
682         if (process_hugefiles(memtest, HUGEPAGE_CHECK_EXISTS) == 1) {
683                 /* check if they are not locked */
684                 if (process_hugefiles(memtest, HUGEPAGE_CHECK_LOCKED) == 1) {
685                         printf("Error - hugepages for current process are locked!\n");
686                         return -1;
687                 }
688                 /* they aren't locked, delete them */
689                 else {
690                         if (process_hugefiles(memtest, HUGEPAGE_DELETE) != 1) {
691                                 printf("Error - deleting hugepages failed!\n");
692                                 return -1;
693                         }
694                 }
695         }
696
697         if (launch_proc(argv0) == 0) {
698                 printf("Error - secondary process ran ok without primary process\n");
699                 return -1;
700         }
701
702         /* check if files for current prefix are present */
703         if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
704                 printf("Error - hugepage files for %s were not created!\n", prefix);
705                 return -1;
706         }
707
708         /* checks if files for current prefix are locked */
709         if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
710                 printf("Error - hugepages for current process aren't locked!\n");
711                 return -1;
712         }
713
714         if (launch_proc(argv1) != 0) {
715                 printf("Error - failed to run with --file-prefix=%s\n", memtest);
716                 return -1;
717         }
718
719         /* check if memtest1_map0 is present */
720         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 1) {
721                 printf("Error - hugepage files for %s were not created!\n", memtest1);
722                 return -1;
723         }
724
725         if (launch_proc(argv2) != 0) {
726                 printf("Error - failed to run with --file-prefix=%s\n", memtest2);
727                 return -1;
728         }
729
730         /* check if hugefiles for memtest2 are present */
731         if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 1) {
732                 printf("Error - hugepage files for %s were not created!\n", memtest2);
733                 return -1;
734         }
735
736         /* check if hugefiles for memtest1 are present */
737         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
738                 printf("Error - hugepage files for %s were not deleted!\n", memtest1);
739                 return -1;
740         }
741
742         return 0;
743 }
744
745 /*
746  * Tests for correct handling of -m and --socket-mem flags
747  */
748 static int
749 test_memory_flags(void)
750 {
751         char prefix[PATH_MAX], tmp[PATH_MAX];
752         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
753                 printf("Error - unable to get current prefix!\n");
754                 return -1;
755         }
756         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
757
758         /* valid -m flag */
759         const char *argv0[] = {prgname, "-c", "10", "-n", "2",
760                         "--file-prefix=" memtest, "-m", "2"};
761
762         /* valid -m flag and mp flag */
763         const char *argv1[] = {prgname, prefix, mp_flag, "-c", "10",
764                         "-n", "2", "-m", "2"};
765
766         /* invalid (zero) --socket-mem flag */
767         const char *argv2[] = {prgname, "-c", "10", "-n", "2",
768                         "--file-prefix=" memtest, "--socket-mem=0,0,0,0"};
769
770         /* invalid (incomplete) --socket-mem flag */
771         const char *argv3[] = {prgname, "-c", "10", "-n", "2",
772                         "--file-prefix=" memtest, "--socket-mem=2,2,"};
773
774         /* invalid (mixed with invalid data) --socket-mem flag */
775         const char *argv4[] = {prgname, "-c", "10", "-n", "2",
776                         "--file-prefix=" memtest, "--socket-mem=2,2,Fred"};
777
778         /* invalid (with numeric value as last character) --socket-mem flag */
779         const char *argv5[] = {prgname, "-c", "10", "-n", "2",
780                         "--file-prefix=" memtest, "--socket-mem=2,2,Fred0"};
781
782         /* invalid (with empty socket) --socket-mem flag */
783         const char *argv6[] = {prgname, "-c", "10", "-n", "2",
784                         "--file-prefix=" memtest, "--socket-mem=2,,2"};
785
786         /* invalid (null) --socket-mem flag */
787         const char *argv7[] = {prgname, "-c", "10", "-n", "2",
788                         "--file-prefix=" memtest, "--socket-mem="};
789
790         /* valid --socket-mem specified together with -m flag */
791         const char *argv8[] = {prgname, "-c", "10", "-n", "2",
792                         "--file-prefix=" memtest, "-m", "2", "--socket-mem=2,2"};
793
794         /* construct an invalid socket mask with 2 megs on each socket plus
795          * extra 2 megs on socket that doesn't exist on current system */
796         char invalid_socket_mem[SOCKET_MEM_STRLEN];
797         char buf[SOCKET_MEM_STRLEN];    /* to avoid copying string onto itself */
798         int i, num_sockets = get_number_of_sockets();
799
800         if (num_sockets <= 0 || num_sockets > RTE_MAX_NUMA_NODES) {
801                 printf("Error - cannot get number of sockets!\n");
802                 return -1;
803         }
804
805         rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "--socket-mem=");
806
807         /* add one extra socket */
808         for (i = 0; i < num_sockets + 1; i++) {
809                 rte_snprintf(buf, sizeof(buf), "%s2", invalid_socket_mem);
810                 rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "%s", buf);
811
812                 if (num_sockets + 1 - i > 1) {
813                         rte_snprintf(buf, sizeof(buf), "%s,", invalid_socket_mem);
814                         rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "%s", buf);
815                 }
816         }
817
818         /* construct a valid socket mask with 2 megs on each existing socket */
819         char valid_socket_mem[SOCKET_MEM_STRLEN];
820
821         rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "--socket-mem=");
822
823         /* add one extra socket */
824         for (i = 0; i < num_sockets; i++) {
825                 rte_snprintf(buf, sizeof(buf), "%s2", valid_socket_mem);
826                 rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "%s", buf);
827
828                 if (num_sockets - i > 1) {
829                         rte_snprintf(buf, sizeof(buf), "%s,", valid_socket_mem);
830                         rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "%s", buf);
831                 }
832         }
833
834         /* invalid --socket-mem flag (with extra socket) */
835         const char *argv9[] = {prgname, "-c", "10", "-n", "2",
836                         "--file-prefix=" memtest, invalid_socket_mem};
837
838         /* valid --socket-mem flag */
839         const char *argv10[] = {prgname, "-c", "10", "-n", "2",
840                         "--file-prefix=" memtest, valid_socket_mem};
841
842         if (launch_proc(argv0) != 0) {
843                 printf("Error - process failed with valid -m flag!\n");
844                 return -1;
845         }
846
847         if (launch_proc(argv1) != 0) {
848                 printf("Error - secondary process failed with valid -m flag !\n");
849                 return -1;
850         }
851
852         if (launch_proc(argv2) == 0) {
853                 printf("Error - process run ok with invalid (zero) --socket-mem!\n");
854                 return -1;
855         }
856
857         if (launch_proc(argv3) == 0) {
858                 printf("Error - process run ok with invalid "
859                                 "(incomplete) --socket-mem!\n");
860                 return -1;
861         }
862
863         if (launch_proc(argv4) == 0) {
864                 printf("Error - process run ok with invalid "
865                                 "(mixed with invalid input) --socket-mem!\n");
866                 return -1;
867         }
868
869         if (launch_proc(argv5) == 0) {
870                 printf("Error - process run ok with invalid "
871                                 "(mixed with invalid input with a numeric value as "
872                                 "last character) --socket-mem!\n");
873                 return -1;
874         }
875
876         if (launch_proc(argv6) == 0) {
877                 printf("Error - process run ok with invalid "
878                                 "(with empty socket) --socket-mem!\n");
879                 return -1;
880         }
881
882         if (launch_proc(argv7) == 0) {
883                 printf("Error - process run ok with invalid (null) --socket-mem!\n");
884                 return -1;
885         }
886
887         if (launch_proc(argv8) == 0) {
888                 printf("Error - process run ok with --socket-mem and -m specified!\n");
889                 return -1;
890         }
891
892         if (launch_proc(argv9) == 0) {
893                 printf("Error - process run ok with extra socket in --socket-mem!\n");
894                 return -1;
895         }
896
897         if (launch_proc(argv10) != 0) {
898                 printf("Error - process failed with valid --socket-mem!\n");
899                 return -1;
900         }
901
902         return 0;
903 }
904
905 int
906 test_eal_flags(void)
907 {
908         int ret = 0;
909
910         ret = test_missing_c_flag();
911         if (ret < 0) {
912                 printf("Error in test_missing_c_flag()\n");
913                 return ret;
914         }
915
916         ret = test_missing_n_flag();
917         if (ret < 0) {
918                 printf("Error in test_missing_n_flag()\n");
919                 return ret;
920         }
921
922         ret = test_no_hpet_flag();
923         if (ret < 0) {
924                 printf("Error in test_no_hpet_flag()\n");
925                 return ret;
926         }
927
928         ret = test_no_huge_flag();
929         if (ret < 0) {
930                 printf("Error in test_no_huge_flag()\n");
931                 return ret;
932         }
933
934         ret = test_invalid_b_flag();
935         if (ret < 0) {
936                 printf("Error in test_invalid_b_flag()\n");
937                 return ret;
938         }
939
940         ret = test_invalid_r_flag();
941         if (ret < 0) {
942                 printf("Error in test_invalid_r_flag()\n");
943                 return ret;
944         }
945
946         ret = test_memory_flags();
947         if (ret < 0) {
948                 printf("Error in test_memory_flags()\n");
949                 return ret;
950         }
951
952         ret = test_file_prefix();
953         if (ret < 0) {
954                 printf("Error in test_file_prefix()\n");
955                 return ret;
956         }
957
958         ret = test_misc_flags();
959         if (ret < 0) {
960                 printf("Error in test_misc_flags()");
961                 return ret;
962         }
963
964         return ret;
965 }
966
967 #else
968 /* Baremetal version
969  * Multiprocess not applicable, so just return 0 always
970  */
971 int
972 test_eal_flags(void)
973 {
974         printf("Multi-process not possible for baremetal, cannot test EAL flags\n");
975         return 0;
976 }
977
978 #endif