devargs: use a comma instead of semicolon to separate key/values
[dpdk.git] / app / test / test_eal_flags.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2014 6WIND S.A.
6  *   All rights reserved.
7  * 
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  * 
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  * 
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 #include <stdio.h>
35
36 #include "test.h"
37
38 #ifndef RTE_EXEC_ENV_BAREMETAL
39 #include <string.h>
40 #include <stdarg.h>
41 #include <libgen.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <dirent.h>
47 #include <sys/wait.h>
48 #include <sys/file.h>
49 #include <limits.h>
50
51 #include <rte_debug.h>
52 #include <rte_string_fns.h>
53
54 #include "process.h"
55
56 #define mp_flag "--proc-type=secondary"
57 #define no_hpet "--no-hpet"
58 #define no_huge "--no-huge"
59 #define no_shconf "--no-shconf"
60 #define use_device "--use-device"
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 #ifdef RTE_EXEC_ENV_LINUXAPP
221 /*
222  * count the number of "node*" files in /sys/devices/system/node/
223  */
224 static int
225 get_number_of_sockets(void)
226 {
227         struct dirent *dirent = NULL;
228         const char * nodedir = "/sys/devices/system/node/";
229         DIR * dir = NULL;
230         int result = 0;
231
232         /* check if directory exists */
233         if ((dir = opendir(nodedir)) == NULL) {
234                 /* if errno==ENOENT this means we don't have NUMA support */
235                 if (errno == ENOENT) {
236                         printf("No NUMA nodes detected: assuming 1 available socket\n");
237                         return 1;
238                 }
239                 printf("Error opening %s: %s\n", nodedir, strerror(errno));
240                 return -1;
241         }
242
243         while ((dirent = readdir(dir)) != NULL)
244                 if (strncmp(dirent->d_name, "node", sizeof("node") - 1) == 0)
245                         result++;
246
247         closedir(dir);
248         return result;
249 }
250 #endif
251
252 static char*
253 get_current_prefix(char * prefix, int size)
254 {
255         char path[PATH_MAX] = {0};
256         char buf[PATH_MAX] = {0};
257
258         /* get file for config (fd is always 3) */
259         rte_snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
260
261         /* return NULL on error */
262         if (readlink(path, buf, sizeof(buf)) == -1)
263                 return NULL;
264
265         /* get the basename */
266         rte_snprintf(buf, sizeof(buf), "%s", basename(buf));
267
268         /* copy string all the way from second char up to start of _config */
269         rte_snprintf(prefix, size, "%.*s",
270                         strnlen(buf, sizeof(buf)) - sizeof("_config"), &buf[1]);
271
272         return prefix;
273 }
274
275 /*
276  * Test that the app doesn't run with invalid whitelist option.
277  * Final tests ensures it does run with valid options as sanity check (one
278  * test for with Domain+BDF, second for just with BDF)
279  */
280 static int
281 test_whitelist_flag(void)
282 {
283         unsigned i;
284 #ifdef RTE_EXEC_ENV_BSDAPP
285         /* BSD target doesn't support prefixes at this point */
286         const char * prefix = "";
287 #else
288         char prefix[PATH_MAX], tmp[PATH_MAX];
289         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
290                 printf("Error - unable to get current prefix!\n");
291                 return -1;
292         }
293         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
294 #endif
295
296         const char *wlinval[][11] = {
297                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
298                                 use_device, "error", "", ""},
299                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
300                                 use_device, "0:0:0", "", ""},
301                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
302                                 use_device, "0:error:0.1", "", ""},
303                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
304                                 use_device, "0:0:0.1error", "", ""},
305                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
306                                 use_device, "error0:0:0.1", "", ""},
307                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
308                                 use_device, "0:0:0.1.2", "", ""},
309         };
310         /* Test with valid whitelist option */
311         const char *wlval1[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
312                         use_device, "00FF:09:0B.3"};
313         const char *wlval2[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
314                         use_device, "09:0B.3", use_device, "0a:0b.1"};
315         const char *wlval3[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
316                         use_device, "09:0B.3,type=test",
317                         use_device, "08:00.1,type=normal"};
318
319         for (i = 0; i < sizeof(wlinval) / sizeof(wlinval[0]); i++) {
320                 if (launch_proc(wlinval[i]) == 0) {
321                         printf("Error - process did run ok with invalid "
322                             "whitelist parameter\n");
323                         return -1;
324                 }
325         }
326         if (launch_proc(wlval1) != 0 ) {
327                 printf("Error - process did not run ok with valid whitelist\n");
328                 return -1;
329         }
330         if (launch_proc(wlval2) != 0 ) {
331                 printf("Error - process did not run ok with valid whitelist value set\n");
332                 return -1;
333         }
334         if (launch_proc(wlval3) != 0 ) {
335                 printf("Error - process did not run ok with valid whitelist + args\n");
336                 return -1;
337         }
338
339         return 0;
340 }
341
342 /*
343  * Test that the app doesn't run with invalid blacklist option.
344  * Final test ensures it does run with valid options as sanity check
345  */
346 static int
347 test_invalid_b_flag(void)
348 {
349 #ifdef RTE_EXEC_ENV_BSDAPP
350         /* BSD target doesn't support prefixes at this point */
351         const char * prefix = "";
352 #else
353         char prefix[PATH_MAX], tmp[PATH_MAX];
354         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
355                 printf("Error - unable to get current prefix!\n");
356                 return -1;
357         }
358         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
359 #endif
360
361         const char *blinval[][9] = {
362                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "error"},
363                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:0:0"},
364                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:error:0.1"},
365                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:0:0.1error"},
366                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "error0:0:0.1"},
367                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:0:0.1.2"},
368         };
369         /* Test with valid blacklist option */
370         const char *blval[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "FF:09:0B.3"};
371
372         int i;
373
374         for (i = 0; i != sizeof (blinval) / sizeof (blinval[0]); i++) {
375                 if (launch_proc(blinval[i]) == 0) {
376                         printf("Error - process did run ok with invalid "
377                             "blacklist parameter\n");
378                         return -1;
379                 }
380         }
381         if (launch_proc(blval) != 0) {
382                 printf("Error - process did not run ok with valid blacklist value\n");
383                 return -1;
384         }
385         return 0;
386 }
387
388
389 /*
390  * Test that the app doesn't run with invalid -r option.
391  */
392 static int
393 test_invalid_r_flag(void)
394 {
395 #ifdef RTE_EXEC_ENV_BSDAPP
396         /* BSD target doesn't support prefixes at this point */
397         const char * prefix = "";
398 #else
399         char prefix[PATH_MAX], tmp[PATH_MAX];
400         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
401                 printf("Error - unable to get current prefix!\n");
402                 return -1;
403         }
404         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
405 #endif
406
407         const char *rinval[][9] = {
408                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "error"},
409                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "0"},
410                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "-1"},
411                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "17"},
412         };
413         /* Test with valid blacklist option */
414         const char *rval[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "16"};
415
416         int i;
417
418         for (i = 0; i != sizeof (rinval) / sizeof (rinval[0]); i++) {
419                 if (launch_proc(rinval[i]) == 0) {
420                         printf("Error - process did run ok with invalid "
421                             "-r (rank) parameter\n");
422                         return -1;
423                 }
424         }
425         if (launch_proc(rval) != 0) {
426                 printf("Error - process did not run ok with valid -r (rank) value\n");
427                 return -1;
428         }
429         return 0;
430 }
431
432 /*
433  * Test that the app doesn't run without the coremask flag. In all cases
434  * should give an error and fail to run
435  */
436 static int
437 test_missing_c_flag(void)
438 {
439 #ifdef RTE_EXEC_ENV_BSDAPP
440         /* BSD target doesn't support prefixes at this point */
441         const char * prefix = "";
442 #else
443         char prefix[PATH_MAX], tmp[PATH_MAX];
444         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
445                 printf("Error - unable to get current prefix!\n");
446                 return -1;
447         }
448         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
449 #endif
450
451         /* -c flag but no coremask value */
452         const char *argv1[] = { prgname, prefix, mp_flag, "-n", "3", "-c"};
453         /* No -c flag at all */
454         const char *argv2[] = { prgname, prefix, mp_flag, "-n", "3"};
455         /* bad coremask value */
456         const char *argv3[] = { prgname, prefix, mp_flag, "-n", "3", "-c", "error" };
457         /* sanity check of tests - valid coremask value */
458         const char *argv4[] = { prgname, prefix, mp_flag, "-n", "3", "-c", "1" };
459
460         if (launch_proc(argv1) == 0
461                         || launch_proc(argv2) == 0
462                         || launch_proc(argv3) == 0) {
463                 printf("Error - process ran without error when missing -c flag\n");
464                 return -1;
465         }
466         if (launch_proc(argv4) != 0) {
467                 printf("Error - process did not run ok with valid coremask value\n");
468                 return -1;
469         }
470         return 0;
471 }
472
473 /*
474  * Test that the app doesn't run without the -n flag. In all cases
475  * should give an error and fail to run.
476  * Since -n is not compulsory for MP, we instead use --no-huge and --no-shconf
477  * flags.
478  */
479 static int
480 test_missing_n_flag(void)
481 {
482 #ifdef RTE_EXEC_ENV_BSDAPP
483         /* BSD target doesn't support prefixes at this point */
484         const char * prefix = "";
485 #else
486         char prefix[PATH_MAX], tmp[PATH_MAX];
487         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
488                 printf("Error - unable to get current prefix!\n");
489                 return -1;
490         }
491         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
492 #endif
493
494         /* -n flag but no value */
495         const char *argv1[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n"};
496         /* No -n flag at all */
497         const char *argv2[] = { prgname, prefix, no_huge, no_shconf, "-c", "1"};
498         /* bad numeric value */
499         const char *argv3[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n", "e" };
500         /* out-of-range value */
501         const char *argv4[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n", "9" };
502         /* sanity test - check with good value */
503         const char *argv5[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n", "2" };
504
505         if (launch_proc(argv1) == 0
506                         || launch_proc(argv2) == 0
507                         || launch_proc(argv3) == 0
508                         || launch_proc(argv4) == 0) {
509                 printf("Error - process ran without error when missing -n flag\n");
510                 return -1;
511         }
512         if (launch_proc(argv5) != 0) {
513                 printf("Error - process did not run ok with valid num-channel value\n");
514                 return -1;
515         }
516         return 0;
517 }
518
519 /*
520  * Test that the app runs with HPET, and without HPET
521  */
522 static int
523 test_no_hpet_flag(void)
524 {
525         char prefix[PATH_MAX], tmp[PATH_MAX];
526
527 #ifdef RTE_EXEC_ENV_BSDAPP
528         return 0;
529 #endif
530         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
531                 printf("Error - unable to get current prefix!\n");
532                 return -1;
533         }
534         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
535
536         /* With --no-hpet */
537         const char *argv1[] = {prgname, prefix, mp_flag, no_hpet, "-c", "1", "-n", "2"};
538         /* Without --no-hpet */
539         const char *argv2[] = {prgname, prefix, mp_flag, "-c", "1", "-n", "2"};
540
541         if (launch_proc(argv1) != 0) {
542                 printf("Error - process did not run ok with --no-hpet flag\n");
543                 return -1;
544         }
545         if (launch_proc(argv2) != 0) {
546                 printf("Error - process did not run ok without --no-hpet flag\n");
547                 return -1;
548         }
549         return 0;
550 }
551
552 /*
553  * Test that the app runs with --no-huge and doesn't run when either
554  * -m or --socket-mem are specified with --no-huge.
555  */
556 static int
557 test_no_huge_flag(void)
558 {
559 #ifdef RTE_EXEC_ENV_BSDAPP
560         /* BSD target doesn't support prefixes at this point, and we also need to
561          * run another primary process here */
562         const char * prefix = no_shconf;
563 #else
564         const char * prefix = "--file-prefix=nohuge";
565 #endif
566
567         /* With --no-huge */
568         const char *argv1[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2"};
569         /* With --no-huge and -m */
570         const char *argv2[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2", "-m", "2"};
571
572         /* With --no-huge and --socket-mem */
573         const char *argv3[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2",
574                         "--socket-mem=2"};
575         /* With --no-huge, -m and --socket-mem */
576         const char *argv4[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2",
577                         "-m", "2", "--socket-mem=2"};
578         if (launch_proc(argv1) != 0) {
579                 printf("Error - process did not run ok with --no-huge flag\n");
580                 return -1;
581         }
582         if (launch_proc(argv2) == 0) {
583                 printf("Error - process run ok with --no-huge and -m flags\n");
584                 return -1;
585         }
586 #ifdef RTE_EXEC_ENV_BSDAPP
587         /* BSD target does not support NUMA, hence no --socket-mem tests */
588         return 0;
589 #endif
590
591         if (launch_proc(argv3) == 0) {
592                 printf("Error - process run ok with --no-huge and --socket-mem "
593                                 "flags\n");
594                 return -1;
595         }
596         if (launch_proc(argv4) == 0) {
597                 printf("Error - process run ok with --no-huge, -m and "
598                                 "--socket-mem flags\n");
599                 return -1;
600         }
601         return 0;
602 }
603
604 #ifdef RTE_LIBRTE_XEN_DOM0
605 static int
606 test_dom0_misc_flags(void)
607 {
608         char prefix[PATH_MAX], tmp[PATH_MAX];
609
610         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
611                 printf("Error - unable to get current prefix!\n");
612                 return -1;
613         }
614         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
615
616         /* check that some general flags don't prevent things from working.
617          * All cases, apart from the first, app should run.
618          * No futher testing of output done.
619          */
620         /* sanity check - failure with invalid option */
621         const char *argv0[] = {prgname, prefix, mp_flag, "-c", "1", "--invalid-opt"};
622
623         /* With --no-pci */
624         const char *argv1[] = {prgname, prefix, mp_flag, "-c", "1", "--no-pci"};
625         /* With -v */
626         const char *argv2[] = {prgname, prefix, mp_flag, "-c", "1", "-v"};
627         /* With valid --syslog */
628         const char *argv3[] = {prgname, prefix, mp_flag, "-c", "1",
629                         "--syslog", "syslog"};
630         /* With empty --syslog (should fail) */
631         const char *argv4[] = {prgname, prefix, mp_flag, "-c", "1", "--syslog"};
632         /* With invalid --syslog */
633         const char *argv5[] = {prgname, prefix, mp_flag, "-c", "1", "--syslog", "error"};
634         /* With no-sh-conf */
635         const char *argv6[] = {prgname, "-c", "1", "-n", "2", "-m", "20",
636                         "--no-shconf", "--file-prefix=noshconf" };
637
638         if (launch_proc(argv0) == 0) {
639                 printf("Error - process ran ok with invalid flag\n");
640                 return -1;
641         }
642         if (launch_proc(argv1) != 0) {
643                 printf("Error - process did not run ok with --no-pci flag\n");
644                 return -1;
645         }
646         if (launch_proc(argv2) != 0) {
647                 printf("Error - process did not run ok with -v flag\n");
648                 return -1;
649         }
650         if (launch_proc(argv3) != 0) {
651                 printf("Error - process did not run ok with --syslog flag\n");
652                 return -1;
653         }
654         if (launch_proc(argv4) == 0) {
655                 printf("Error - process run ok with empty --syslog flag\n");
656                 return -1;
657         }
658         if (launch_proc(argv5) == 0) {
659                 printf("Error - process run ok with invalid --syslog flag\n");
660                 return -1;
661         }
662         if (launch_proc(argv6) != 0) {
663                 printf("Error - process did not run ok with --no-shconf flag\n");
664                 return -1;
665         }
666         
667         return 0;
668 }
669 #else
670 static int
671 test_misc_flags(void)
672 {
673         char hugepath[PATH_MAX] = {0};
674 #ifdef RTE_EXEC_ENV_BSDAPP
675         /* BSD target doesn't support prefixes at this point */
676         const char * prefix = "";
677         const char * nosh_prefix = "";
678 #else
679         char prefix[PATH_MAX], tmp[PATH_MAX];
680         const char * nosh_prefix = "--file-prefix=noshconf";
681         FILE * hugedir_handle = NULL;
682         char line[PATH_MAX] = {0};
683         unsigned i, isempty = 1;
684         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
685                 printf("Error - unable to get current prefix!\n");
686                 return -1;
687         }
688         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
689
690         /*
691          * get first valid hugepage path
692          */
693
694         /* get hugetlbfs mountpoints from /proc/mounts */
695         hugedir_handle = fopen("/proc/mounts", "r");
696
697         if (hugedir_handle == NULL) {
698                 printf("Error opening /proc/mounts!\n");
699                 return -1;
700         }
701
702         /* read /proc/mounts */
703         while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
704
705                 /* find first valid hugepath */
706                 if (get_hugepage_path(line, sizeof(line), hugepath, sizeof(hugepath)))
707                         break;
708         }
709
710         fclose(hugedir_handle);
711
712         /* check if path is not empty */
713         for (i = 0; i < sizeof(hugepath); i++)
714                 if (hugepath[i] != '\0')
715                         isempty = 0;
716
717         if (isempty) {
718                 printf("No mounted hugepage dir found!\n");
719                 return -1;
720         }
721 #endif
722
723
724         /* check that some general flags don't prevent things from working.
725          * All cases, apart from the first, app should run.
726          * No futher testing of output done.
727          */
728         /* sanity check - failure with invalid option */
729         const char *argv0[] = {prgname, prefix, mp_flag, "-c", "1", "--invalid-opt"};
730
731         /* With --no-pci */
732         const char *argv1[] = {prgname, prefix, mp_flag, "-c", "1", "--no-pci"};
733         /* With -v */
734         const char *argv2[] = {prgname, prefix, mp_flag, "-c", "1", "-v"};
735         /* With valid --syslog */
736         const char *argv3[] = {prgname, prefix, mp_flag, "-c", "1",
737                         "--syslog", "syslog"};
738         /* With empty --syslog (should fail) */
739         const char *argv4[] = {prgname, prefix, mp_flag, "-c", "1", "--syslog"};
740         /* With invalid --syslog */
741         const char *argv5[] = {prgname, prefix, mp_flag, "-c", "1", "--syslog", "error"};
742         /* With no-sh-conf */
743         const char *argv6[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
744                         no_shconf, nosh_prefix };
745
746 #ifdef RTE_EXEC_ENV_BSDAPP
747         return 0;
748 #endif
749         /* With --huge-dir */
750         const char *argv7[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
751                         "--file-prefix=hugedir", "--huge-dir", hugepath};
752         /* With empty --huge-dir (should fail) */
753         const char *argv8[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
754                         "--file-prefix=hugedir", "--huge-dir"};
755         /* With invalid --huge-dir */
756         const char *argv9[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
757                         "--file-prefix=hugedir", "--huge-dir", "invalid"};
758         /* Secondary process with invalid --huge-dir (should run as flag has no
759          * effect on secondary processes) */
760         const char *argv10[] = {prgname, prefix, mp_flag, "-c", "1", "--huge-dir", "invalid"};
761
762         /* try running with base-virtaddr param */
763         const char *argv11[] = {prgname, "--file-prefix=virtaddr",
764                         "-c", "1", "-n", "2", "--base-virtaddr=0x12345678"};
765
766
767         if (launch_proc(argv0) == 0) {
768                 printf("Error - process ran ok with invalid flag\n");
769                 return -1;
770         }
771         if (launch_proc(argv1) != 0) {
772                 printf("Error - process did not run ok with --no-pci flag\n");
773                 return -1;
774         }
775         if (launch_proc(argv2) != 0) {
776                 printf("Error - process did not run ok with -v flag\n");
777                 return -1;
778         }
779         if (launch_proc(argv3) != 0) {
780                 printf("Error - process did not run ok with --syslog flag\n");
781                 return -1;
782         }
783         if (launch_proc(argv4) == 0) {
784                 printf("Error - process run ok with empty --syslog flag\n");
785                 return -1;
786         }
787         if (launch_proc(argv5) == 0) {
788                 printf("Error - process run ok with invalid --syslog flag\n");
789                 return -1;
790         }
791         if (launch_proc(argv6) != 0) {
792                 printf("Error - process did not run ok with --no-shconf flag\n");
793                 return -1;
794         }
795 #ifdef RTE_EXEC_ENV_BSDAPP
796         return 0;
797 #endif
798         if (launch_proc(argv7) != 0) {
799                 printf("Error - process did not run ok with --huge-dir flag\n");
800                 return -1;
801         }
802         if (launch_proc(argv8) == 0) {
803                 printf("Error - process run ok with empty --huge-dir flag\n");
804                 return -1;
805         }
806         if (launch_proc(argv9) == 0) {
807                 printf("Error - process run ok with invalid --huge-dir flag\n");
808                 return -1;
809         }
810         if (launch_proc(argv10) != 0) {
811                 printf("Error - secondary process did not run ok with invalid --huge-dir flag\n");
812                 return -1;
813         }
814         if (launch_proc(argv11) != 0) {
815                 printf("Error - process did not run ok with --base-virtaddr parameter\n");
816                 return -1;
817         }
818         return 0;
819 }
820 #endif
821
822 static int
823 test_file_prefix(void)
824 {
825         /*
826          * 1. check if current process hugefiles are locked
827          * 2. try to run secondary process without a corresponding primary process
828          * (while failing to run, it will also remove any unused hugepage files)
829          * 3. check if current process hugefiles are still in place and are locked
830          * 4. run a primary process with memtest1 prefix
831          * 5. check if memtest1 hugefiles are created
832          * 6. run a primary process with memtest2 prefix
833          * 7. check that only memtest2 hugefiles are present in the hugedir
834          */
835
836 #ifdef RTE_EXEC_ENV_BSDAPP
837         return 0;
838 #endif
839
840         /* this should fail unless the test itself is run with "memtest" prefix */
841         const char *argv0[] = {prgname, mp_flag, "-c", "1", "-n", "2", "-m", "2",
842                         "--file-prefix=" memtest };
843
844         /* primary process with memtest1 */
845         const char *argv1[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
846                                 "--file-prefix=" memtest1 };
847
848         /* primary process with memtest2 */
849         const char *argv2[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
850                                 "--file-prefix=" memtest2 };
851
852         char prefix[32];
853         if (get_current_prefix(prefix, sizeof(prefix)) == NULL) {
854                 printf("Error - unable to get current prefix!\n");
855                 return -1;
856         }
857 #ifdef RTE_LIBRTE_XEN_DOM0
858         return 0;
859 #endif
860
861         /* check if files for current prefix are present */
862         if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
863                 printf("Error - hugepage files for %s were not created!\n", prefix);
864                 return -1;
865         }
866
867         /* checks if files for current prefix are locked */
868         if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
869                 printf("Error - hugepages for current process aren't locked!\n");
870                 return -1;
871         }
872
873         /* check if files for secondary process are present */
874         if (process_hugefiles(memtest, HUGEPAGE_CHECK_EXISTS) == 1) {
875                 /* check if they are not locked */
876                 if (process_hugefiles(memtest, HUGEPAGE_CHECK_LOCKED) == 1) {
877                         printf("Error - hugepages for current process are locked!\n");
878                         return -1;
879                 }
880                 /* they aren't locked, delete them */
881                 else {
882                         if (process_hugefiles(memtest, HUGEPAGE_DELETE) != 1) {
883                                 printf("Error - deleting hugepages failed!\n");
884                                 return -1;
885                         }
886                 }
887         }
888
889         if (launch_proc(argv0) == 0) {
890                 printf("Error - secondary process ran ok without primary process\n");
891                 return -1;
892         }
893
894         /* check if files for current prefix are present */
895         if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
896                 printf("Error - hugepage files for %s were not created!\n", prefix);
897                 return -1;
898         }
899
900         /* checks if files for current prefix are locked */
901         if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
902                 printf("Error - hugepages for current process aren't locked!\n");
903                 return -1;
904         }
905
906         if (launch_proc(argv1) != 0) {
907                 printf("Error - failed to run with --file-prefix=%s\n", memtest);
908                 return -1;
909         }
910
911         /* check if memtest1_map0 is present */
912         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 1) {
913                 printf("Error - hugepage files for %s were not created!\n", memtest1);
914                 return -1;
915         }
916
917         if (launch_proc(argv2) != 0) {
918                 printf("Error - failed to run with --file-prefix=%s\n", memtest2);
919                 return -1;
920         }
921
922         /* check if hugefiles for memtest2 are present */
923         if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 1) {
924                 printf("Error - hugepage files for %s were not created!\n", memtest2);
925                 return -1;
926         }
927
928         /* check if hugefiles for memtest1 are present */
929         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
930                 printf("Error - hugepage files for %s were not deleted!\n", memtest1);
931                 return -1;
932         }
933
934         return 0;
935 }
936
937 /*
938  * Tests for correct handling of -m and --socket-mem flags
939  */
940 static int
941 test_memory_flags(void)
942 {
943         const char* mem_size = NULL;
944 #ifdef RTE_EXEC_ENV_BSDAPP
945         /* BSD target doesn't support prefixes at this point */
946         const char * prefix = "";
947 #else
948         char prefix[PATH_MAX], tmp[PATH_MAX];
949         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
950                 printf("Error - unable to get current prefix!\n");
951                 return -1;
952         }
953         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
954 #endif
955 #ifdef RTE_LIBRTE_XEN_DOM0
956         mem_size = "30";
957 #else
958         mem_size = "2";
959 #endif
960
961
962         /* valid -m flag and mp flag */
963         const char *argv0[] = {prgname, prefix, mp_flag, "-c", "10",
964                         "-n", "2", "-m", mem_size};
965
966         /* valid -m flag */
967         const char *argv1[] = {prgname, "-c", "10", "-n", "2",
968                         "--file-prefix=" memtest, "-m", mem_size};
969
970         /* invalid (zero) --socket-mem flag */
971         const char *argv2[] = {prgname, "-c", "10", "-n", "2",
972                         "--file-prefix=" memtest, "--socket-mem=0,0,0,0"};
973
974         /* invalid (incomplete) --socket-mem flag */
975         const char *argv3[] = {prgname, "-c", "10", "-n", "2",
976                         "--file-prefix=" memtest, "--socket-mem=2,2,"};
977
978         /* invalid (mixed with invalid data) --socket-mem flag */
979         const char *argv4[] = {prgname, "-c", "10", "-n", "2",
980                         "--file-prefix=" memtest, "--socket-mem=2,2,Fred"};
981
982         /* invalid (with numeric value as last character) --socket-mem flag */
983         const char *argv5[] = {prgname, "-c", "10", "-n", "2",
984                         "--file-prefix=" memtest, "--socket-mem=2,2,Fred0"};
985
986         /* invalid (with empty socket) --socket-mem flag */
987         const char *argv6[] = {prgname, "-c", "10", "-n", "2",
988                         "--file-prefix=" memtest, "--socket-mem=2,,2"};
989
990         /* invalid (null) --socket-mem flag */
991         const char *argv7[] = {prgname, "-c", "10", "-n", "2",
992                         "--file-prefix=" memtest, "--socket-mem="};
993
994         /* valid --socket-mem specified together with -m flag */
995         const char *argv8[] = {prgname, "-c", "10", "-n", "2",
996                         "--file-prefix=" memtest, "-m", "2", "--socket-mem=2,2"};
997
998         /* construct an invalid socket mask with 2 megs on each socket plus
999          * extra 2 megs on socket that doesn't exist on current system */
1000         char invalid_socket_mem[SOCKET_MEM_STRLEN];
1001         char buf[SOCKET_MEM_STRLEN];    /* to avoid copying string onto itself */
1002
1003 #ifdef RTE_EXEC_ENV_BSDAPP
1004         int i, num_sockets = 1;
1005 #else
1006         int i, num_sockets = get_number_of_sockets();
1007 #endif
1008
1009         if (num_sockets <= 0 || num_sockets > RTE_MAX_NUMA_NODES) {
1010                 printf("Error - cannot get number of sockets!\n");
1011                 return -1;
1012         }
1013
1014         rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "--socket-mem=");
1015
1016         /* add one extra socket */
1017         for (i = 0; i < num_sockets + 1; i++) {
1018                 rte_snprintf(buf, sizeof(buf), "%s2", invalid_socket_mem);
1019                 rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "%s", buf);
1020
1021                 if (num_sockets + 1 - i > 1) {
1022                         rte_snprintf(buf, sizeof(buf), "%s,", invalid_socket_mem);
1023                         rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "%s", buf);
1024                 }
1025         }
1026
1027         /* construct a valid socket mask with 2 megs on each existing socket */
1028         char valid_socket_mem[SOCKET_MEM_STRLEN];
1029
1030         rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "--socket-mem=");
1031
1032         /* add one extra socket */
1033         for (i = 0; i < num_sockets; i++) {
1034                 rte_snprintf(buf, sizeof(buf), "%s2", valid_socket_mem);
1035                 rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "%s", buf);
1036
1037                 if (num_sockets - i > 1) {
1038                         rte_snprintf(buf, sizeof(buf), "%s,", valid_socket_mem);
1039                         rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "%s", buf);
1040                 }
1041         }
1042
1043         /* invalid --socket-mem flag (with extra socket) */
1044         const char *argv9[] = {prgname, "-c", "10", "-n", "2",
1045                         "--file-prefix=" memtest, invalid_socket_mem};
1046
1047         /* valid --socket-mem flag */
1048         const char *argv10[] = {prgname, "-c", "10", "-n", "2",
1049                         "--file-prefix=" memtest, valid_socket_mem};
1050
1051         if (launch_proc(argv0) != 0) {
1052                 printf("Error - secondary process failed with valid -m flag !\n");
1053                 return -1;
1054         }
1055
1056 #ifdef RTE_EXEC_ENV_BSDAPP
1057         /* no other tests are applicable to BSD */
1058         return 0;
1059 #endif
1060
1061         if (launch_proc(argv1) != 0) {
1062                 printf("Error - process failed with valid -m flag!\n");
1063                 return -1;
1064         }
1065 #ifdef RTE_LIBRTE_XEN_DOM0
1066         return 0;
1067 #endif
1068         if (launch_proc(argv2) == 0) {
1069                 printf("Error - process run ok with invalid (zero) --socket-mem!\n");
1070                 return -1;
1071         }
1072
1073         if (launch_proc(argv3) == 0) {
1074                 printf("Error - process run ok with invalid "
1075                                 "(incomplete) --socket-mem!\n");
1076                 return -1;
1077         }
1078
1079         if (launch_proc(argv4) == 0) {
1080                 printf("Error - process run ok with invalid "
1081                                 "(mixed with invalid input) --socket-mem!\n");
1082                 return -1;
1083         }
1084
1085         if (launch_proc(argv5) == 0) {
1086                 printf("Error - process run ok with invalid "
1087                                 "(mixed with invalid input with a numeric value as "
1088                                 "last character) --socket-mem!\n");
1089                 return -1;
1090         }
1091
1092         if (launch_proc(argv6) == 0) {
1093                 printf("Error - process run ok with invalid "
1094                                 "(with empty socket) --socket-mem!\n");
1095                 return -1;
1096         }
1097
1098         if (launch_proc(argv7) == 0) {
1099                 printf("Error - process run ok with invalid (null) --socket-mem!\n");
1100                 return -1;
1101         }
1102
1103         if (launch_proc(argv8) == 0) {
1104                 printf("Error - process run ok with --socket-mem and -m specified!\n");
1105                 return -1;
1106         }
1107
1108         if (launch_proc(argv9) == 0) {
1109                 printf("Error - process run ok with extra socket in --socket-mem!\n");
1110                 return -1;
1111         }
1112
1113         if (launch_proc(argv10) != 0) {
1114                 printf("Error - process failed with valid --socket-mem!\n");
1115                 return -1;
1116         }
1117
1118         return 0;
1119 }
1120
1121 int
1122 test_eal_flags(void)
1123 {
1124         int ret = 0;
1125
1126         ret = test_missing_c_flag();
1127         if (ret < 0) {
1128                 printf("Error in test_missing_c_flag()\n");
1129                 return ret;
1130         }
1131
1132         ret = test_missing_n_flag();
1133         if (ret < 0) {
1134                 printf("Error in test_missing_n_flag()\n");
1135                 return ret;
1136         }
1137
1138         ret = test_no_hpet_flag();
1139         if (ret < 0) {
1140                 printf("Error in test_no_hpet_flag()\n");
1141                 return ret;
1142         }
1143
1144         ret = test_no_huge_flag();
1145         if (ret < 0) {
1146                 printf("Error in test_no_huge_flag()\n");
1147                 return ret;
1148         }
1149
1150         ret = test_whitelist_flag();
1151         if (ret < 0) {
1152                 printf("Error in test_invalid_whitelist_flag()\n");
1153                 return ret;
1154         }
1155
1156         ret = test_invalid_b_flag();
1157         if (ret < 0) {
1158                 printf("Error in test_invalid_b_flag()\n");
1159                 return ret;
1160         }
1161
1162         ret = test_invalid_r_flag();
1163         if (ret < 0) {
1164                 printf("Error in test_invalid_r_flag()\n");
1165                 return ret;
1166         }
1167
1168         ret = test_memory_flags();
1169         if (ret < 0) {
1170                 printf("Error in test_memory_flags()\n");
1171                 return ret;
1172         }
1173
1174         ret = test_file_prefix();
1175         if (ret < 0) {
1176                 printf("Error in test_file_prefix()\n");
1177                 return ret;
1178         }
1179
1180 #ifdef RTE_LIBRTE_XEN_DOM0
1181         ret = test_dom0_misc_flags();
1182 #else
1183         ret = test_misc_flags();
1184 #endif
1185         if (ret < 0) {
1186                 printf("Error in test_misc_flags()");
1187                 return ret;
1188         }
1189
1190         return ret;
1191 }
1192
1193 #else
1194 /* Baremetal version
1195  * Multiprocess not applicable, so just return 0 always
1196  */
1197 int
1198 test_eal_flags(void)
1199 {
1200         printf("Multi-process not possible for baremetal, cannot test EAL flags\n");
1201         return 0;
1202 }
1203
1204 #endif