eal: add command line option to select vfio interrupt type
[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 pci_whitelist "--pci-whitelist"
61 #define vdev "--vdev"
62 #define memtest "memtest"
63 #define memtest1 "memtest1"
64 #define memtest2 "memtest2"
65 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
66 #define launch_proc(ARGV) process_dup(ARGV, \
67                 sizeof(ARGV)/(sizeof(ARGV[0])), __func__)
68
69 enum hugepage_action {
70         HUGEPAGE_CHECK_EXISTS = 0,
71         HUGEPAGE_CHECK_LOCKED,
72         HUGEPAGE_DELETE,
73         HUGEPAGE_INVALID
74 };
75
76 /* if string contains a hugepage path */
77 static int
78 get_hugepage_path(char * src, int src_len, char * dst, int dst_len)
79 {
80 #define NUM_TOKENS 4
81         char *tokens[NUM_TOKENS];
82
83         /* if we couldn't properly split the string */
84         if (rte_strsplit(src, src_len, tokens, NUM_TOKENS, ' ') < NUM_TOKENS)
85                 return 0;
86
87         if (strncmp(tokens[2], "hugetlbfs", sizeof("hugetlbfs")) == 0) {
88                 rte_snprintf(dst, dst_len, "%s", tokens[1]);
89                 return 1;
90         }
91         return 0;
92 }
93
94 /*
95  * Cycles through hugepage directories and looks for hugepage
96  * files associated with a given prefix. Depending on value of
97  * action, the hugepages are checked if they exist, checked if
98  * they can be locked, or are simply deleted.
99  *
100  * Returns 1 if it finds at least one hugepage matching the action
101  * Returns 0 if no matching hugepages were found
102  * Returns -1 if it encounters an error
103  */
104 static int
105 process_hugefiles(const char * prefix, enum hugepage_action action)
106 {
107         FILE * hugedir_handle = NULL;
108         DIR * hugepage_dir = NULL;
109         struct dirent *dirent = NULL;
110
111         char hugefile_prefix[PATH_MAX] = {0};
112         char hugedir[PATH_MAX] = {0};
113         char line[PATH_MAX] = {0};
114
115         int fd, lck_result, result = 0;
116
117         const int prefix_len = rte_snprintf(hugefile_prefix,
118                         sizeof(hugefile_prefix), "%smap_", prefix);
119         if (prefix_len <= 0 || prefix_len >= (int)sizeof(hugefile_prefix)
120                         || prefix_len >= (int)sizeof(dirent->d_name)) {
121                 printf("Error creating hugefile filename prefix\n");
122                 return -1;
123         }
124
125         /* get hugetlbfs mountpoints from /proc/mounts */
126         hugedir_handle = fopen("/proc/mounts", "r");
127
128         if (hugedir_handle == NULL) {
129                 printf("Error parsing /proc/mounts!\n");
130                 return -1;
131         }
132
133         /* read and parse script output */
134         while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
135
136                 /* check if we have a hugepage filesystem path */
137                 if (!get_hugepage_path(line, sizeof(line), hugedir, sizeof(hugedir)))
138                         continue;
139
140                 /* check if directory exists */
141                 if ((hugepage_dir = opendir(hugedir)) == NULL) {
142                         fclose(hugedir_handle);
143                         printf("Error reading %s: %s\n", hugedir, strerror(errno));
144                         return -1;
145                 }
146
147                 while ((dirent = readdir(hugepage_dir)) != NULL) {
148                         if (memcmp(dirent->d_name, hugefile_prefix, prefix_len) != 0)
149                                 continue;
150
151                         switch (action) {
152                         case HUGEPAGE_CHECK_EXISTS:
153                                 {
154                                         /* file exists, return */
155                                         result = 1;
156                                         goto end;
157                                 }
158                                 break;
159                         case HUGEPAGE_DELETE:
160                                 {
161                                         char file_path[PATH_MAX] = {0};
162
163                                         rte_snprintf(file_path, sizeof(file_path),
164                                                 "%s/%s", hugedir, dirent->d_name);
165
166                                         /* remove file */
167                                         if (remove(file_path) < 0) {
168                                                 printf("Error deleting %s - %s!\n",
169                                                                 dirent->d_name, strerror(errno));
170                                                 closedir(hugepage_dir);
171                                                 result = -1;
172                                                 goto end;
173                                         }
174                                         result = 1;
175                                 }
176                                 break;
177                         case HUGEPAGE_CHECK_LOCKED:
178                                 {
179                                         /* try and lock the file */
180                                         fd = openat(dirfd(hugepage_dir), dirent->d_name, O_RDONLY);
181
182                                         /* this shouldn't happen */
183                                         if (fd == -1) {
184                                                 printf("Error opening %s - %s!\n",
185                                                                 dirent->d_name, strerror(errno));
186                                                 closedir(hugepage_dir);
187                                                 result = -1;
188                                                 goto end;
189                                         }
190
191                                         /* non-blocking lock */
192                                         lck_result = flock(fd, LOCK_EX | LOCK_NB);
193
194                                         /* if lock succeeds, there's something wrong */
195                                         if (lck_result != -1) {
196                                                 result = 0;
197
198                                                 /* unlock the resulting lock */
199                                                 flock(fd, LOCK_UN);
200                                                 close(fd);
201                                                 closedir(hugepage_dir);
202                                                 goto end;
203                                         }
204                                         result = 1;
205                                         close(fd);
206                                 }
207                                 break;
208                                 /* shouldn't happen */
209                         default:
210                                 goto end;
211                         } /* switch */
212
213                 } /* read hugepage directory */
214                 closedir(hugepage_dir);
215         } /* read /proc/mounts */
216 end:
217         fclose(hugedir_handle);
218         return result;
219 }
220
221 #ifdef RTE_EXEC_ENV_LINUXAPP
222 /*
223  * count the number of "node*" files in /sys/devices/system/node/
224  */
225 static int
226 get_number_of_sockets(void)
227 {
228         struct dirent *dirent = NULL;
229         const char * nodedir = "/sys/devices/system/node/";
230         DIR * dir = NULL;
231         int result = 0;
232
233         /* check if directory exists */
234         if ((dir = opendir(nodedir)) == NULL) {
235                 /* if errno==ENOENT this means we don't have NUMA support */
236                 if (errno == ENOENT) {
237                         printf("No NUMA nodes detected: assuming 1 available socket\n");
238                         return 1;
239                 }
240                 printf("Error opening %s: %s\n", nodedir, strerror(errno));
241                 return -1;
242         }
243
244         while ((dirent = readdir(dir)) != NULL)
245                 if (strncmp(dirent->d_name, "node", sizeof("node") - 1) == 0)
246                         result++;
247
248         closedir(dir);
249         return result;
250 }
251 #endif
252
253 static char*
254 get_current_prefix(char * prefix, int size)
255 {
256         char path[PATH_MAX] = {0};
257         char buf[PATH_MAX] = {0};
258
259         /* get file for config (fd is always 3) */
260         rte_snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
261
262         /* return NULL on error */
263         if (readlink(path, buf, sizeof(buf)) == -1)
264                 return NULL;
265
266         /* get the basename */
267         rte_snprintf(buf, sizeof(buf), "%s", basename(buf));
268
269         /* copy string all the way from second char up to start of _config */
270         rte_snprintf(prefix, size, "%.*s",
271                         strnlen(buf, sizeof(buf)) - sizeof("_config"), &buf[1]);
272
273         return prefix;
274 }
275
276 /*
277  * Test that the app doesn't run with invalid whitelist option.
278  * Final tests ensures it does run with valid options as sanity check (one
279  * test for with Domain+BDF, second for just with BDF)
280  */
281 static int
282 test_whitelist_flag(void)
283 {
284         unsigned i;
285 #ifdef RTE_EXEC_ENV_BSDAPP
286         /* BSD target doesn't support prefixes at this point */
287         const char * prefix = "";
288 #else
289         char prefix[PATH_MAX], tmp[PATH_MAX];
290         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
291                 printf("Error - unable to get current prefix!\n");
292                 return -1;
293         }
294         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
295 #endif
296
297         const char *wlinval[][11] = {
298                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
299                                 pci_whitelist, "error", "", ""},
300                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
301                                 pci_whitelist, "0:0:0", "", ""},
302                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
303                                 pci_whitelist, "0:error:0.1", "", ""},
304                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
305                                 pci_whitelist, "0:0:0.1error", "", ""},
306                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
307                                 pci_whitelist, "error0:0:0.1", "", ""},
308                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
309                                 pci_whitelist, "0:0:0.1.2", "", ""},
310         };
311         /* Test with valid whitelist option */
312         const char *wlval1[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
313                         pci_whitelist, "00FF:09:0B.3"};
314         const char *wlval2[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
315                         pci_whitelist, "09:0B.3", pci_whitelist, "0a:0b.1"};
316         const char *wlval3[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1",
317                         pci_whitelist, "09:0B.3,type=test",
318                         pci_whitelist, "08:00.1,type=normal",
319 #ifdef CONFIG_RTE_LIBRTE_PMD_RING
320                         vdev, "eth_ring,arg=test",
321 #endif
322         };
323
324         for (i = 0; i < sizeof(wlinval) / sizeof(wlinval[0]); i++) {
325                 if (launch_proc(wlinval[i]) == 0) {
326                         printf("Error - process did run ok with invalid "
327                             "whitelist parameter\n");
328                         return -1;
329                 }
330         }
331         if (launch_proc(wlval1) != 0 ) {
332                 printf("Error - process did not run ok with valid whitelist\n");
333                 return -1;
334         }
335         if (launch_proc(wlval2) != 0 ) {
336                 printf("Error - process did not run ok with valid whitelist value set\n");
337                 return -1;
338         }
339         if (launch_proc(wlval3) != 0 ) {
340                 printf("Error - process did not run ok with valid whitelist + args\n");
341                 return -1;
342         }
343
344         return 0;
345 }
346
347 /*
348  * Test that the app doesn't run with invalid blacklist option.
349  * Final test ensures it does run with valid options as sanity check
350  */
351 static int
352 test_invalid_b_flag(void)
353 {
354 #ifdef RTE_EXEC_ENV_BSDAPP
355         /* BSD target doesn't support prefixes at this point */
356         const char * prefix = "";
357 #else
358         char prefix[PATH_MAX], tmp[PATH_MAX];
359         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
360                 printf("Error - unable to get current prefix!\n");
361                 return -1;
362         }
363         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
364 #endif
365
366         const char *blinval[][9] = {
367                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "error"},
368                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:0:0"},
369                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:error:0.1"},
370                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:0:0.1error"},
371                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "error0:0:0.1"},
372                 {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "0:0:0.1.2"},
373         };
374         /* Test with valid blacklist option */
375         const char *blval[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-b", "FF:09:0B.3"};
376
377         int i;
378
379         for (i = 0; i != sizeof (blinval) / sizeof (blinval[0]); i++) {
380                 if (launch_proc(blinval[i]) == 0) {
381                         printf("Error - process did run ok with invalid "
382                             "blacklist parameter\n");
383                         return -1;
384                 }
385         }
386         if (launch_proc(blval) != 0) {
387                 printf("Error - process did not run ok with valid blacklist value\n");
388                 return -1;
389         }
390         return 0;
391 }
392
393
394 /*
395  * Test that the app doesn't run with invalid -r option.
396  */
397 static int
398 test_invalid_r_flag(void)
399 {
400 #ifdef RTE_EXEC_ENV_BSDAPP
401         /* BSD target doesn't support prefixes at this point */
402         const char * prefix = "";
403 #else
404         char prefix[PATH_MAX], tmp[PATH_MAX];
405         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
406                 printf("Error - unable to get current prefix!\n");
407                 return -1;
408         }
409         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
410 #endif
411
412         const char *rinval[][9] = {
413                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "error"},
414                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "0"},
415                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "-1"},
416                         {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "17"},
417         };
418         /* Test with valid blacklist option */
419         const char *rval[] = {prgname, prefix, mp_flag, "-n", "1", "-c", "1", "-r", "16"};
420
421         int i;
422
423         for (i = 0; i != sizeof (rinval) / sizeof (rinval[0]); i++) {
424                 if (launch_proc(rinval[i]) == 0) {
425                         printf("Error - process did run ok with invalid "
426                             "-r (rank) parameter\n");
427                         return -1;
428                 }
429         }
430         if (launch_proc(rval) != 0) {
431                 printf("Error - process did not run ok with valid -r (rank) value\n");
432                 return -1;
433         }
434         return 0;
435 }
436
437 /*
438  * Test that the app doesn't run without the coremask flag. In all cases
439  * should give an error and fail to run
440  */
441 static int
442 test_missing_c_flag(void)
443 {
444 #ifdef RTE_EXEC_ENV_BSDAPP
445         /* BSD target doesn't support prefixes at this point */
446         const char * prefix = "";
447 #else
448         char prefix[PATH_MAX], tmp[PATH_MAX];
449         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
450                 printf("Error - unable to get current prefix!\n");
451                 return -1;
452         }
453         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
454 #endif
455
456         /* -c flag but no coremask value */
457         const char *argv1[] = { prgname, prefix, mp_flag, "-n", "3", "-c"};
458         /* No -c flag at all */
459         const char *argv2[] = { prgname, prefix, mp_flag, "-n", "3"};
460         /* bad coremask value */
461         const char *argv3[] = { prgname, prefix, mp_flag, "-n", "3", "-c", "error" };
462         /* sanity check of tests - valid coremask value */
463         const char *argv4[] = { prgname, prefix, mp_flag, "-n", "3", "-c", "1" };
464
465         if (launch_proc(argv1) == 0
466                         || launch_proc(argv2) == 0
467                         || launch_proc(argv3) == 0) {
468                 printf("Error - process ran without error when missing -c flag\n");
469                 return -1;
470         }
471         if (launch_proc(argv4) != 0) {
472                 printf("Error - process did not run ok with valid coremask value\n");
473                 return -1;
474         }
475         return 0;
476 }
477
478 /*
479  * Test that the app doesn't run without the -n flag. In all cases
480  * should give an error and fail to run.
481  * Since -n is not compulsory for MP, we instead use --no-huge and --no-shconf
482  * flags.
483  */
484 static int
485 test_missing_n_flag(void)
486 {
487 #ifdef RTE_EXEC_ENV_BSDAPP
488         /* BSD target doesn't support prefixes at this point */
489         const char * prefix = "";
490 #else
491         char prefix[PATH_MAX], tmp[PATH_MAX];
492         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
493                 printf("Error - unable to get current prefix!\n");
494                 return -1;
495         }
496         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
497 #endif
498
499         /* -n flag but no value */
500         const char *argv1[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n"};
501         /* No -n flag at all */
502         const char *argv2[] = { prgname, prefix, no_huge, no_shconf, "-c", "1"};
503         /* bad numeric value */
504         const char *argv3[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n", "e" };
505         /* out-of-range value */
506         const char *argv4[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n", "9" };
507         /* sanity test - check with good value */
508         const char *argv5[] = { prgname, prefix, no_huge, no_shconf, "-c", "1", "-n", "2" };
509
510         if (launch_proc(argv1) == 0
511                         || launch_proc(argv2) == 0
512                         || launch_proc(argv3) == 0
513                         || launch_proc(argv4) == 0) {
514                 printf("Error - process ran without error when missing -n flag\n");
515                 return -1;
516         }
517         if (launch_proc(argv5) != 0) {
518                 printf("Error - process did not run ok with valid num-channel value\n");
519                 return -1;
520         }
521         return 0;
522 }
523
524 /*
525  * Test that the app runs with HPET, and without HPET
526  */
527 static int
528 test_no_hpet_flag(void)
529 {
530         char prefix[PATH_MAX], tmp[PATH_MAX];
531
532 #ifdef RTE_EXEC_ENV_BSDAPP
533         return 0;
534 #endif
535         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
536                 printf("Error - unable to get current prefix!\n");
537                 return -1;
538         }
539         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
540
541         /* With --no-hpet */
542         const char *argv1[] = {prgname, prefix, mp_flag, no_hpet, "-c", "1", "-n", "2"};
543         /* Without --no-hpet */
544         const char *argv2[] = {prgname, prefix, mp_flag, "-c", "1", "-n", "2"};
545
546         if (launch_proc(argv1) != 0) {
547                 printf("Error - process did not run ok with --no-hpet flag\n");
548                 return -1;
549         }
550         if (launch_proc(argv2) != 0) {
551                 printf("Error - process did not run ok without --no-hpet flag\n");
552                 return -1;
553         }
554         return 0;
555 }
556
557 /*
558  * Test that the app runs with --no-huge and doesn't run when either
559  * -m or --socket-mem are specified with --no-huge.
560  */
561 static int
562 test_no_huge_flag(void)
563 {
564 #ifdef RTE_EXEC_ENV_BSDAPP
565         /* BSD target doesn't support prefixes at this point, and we also need to
566          * run another primary process here */
567         const char * prefix = no_shconf;
568 #else
569         const char * prefix = "--file-prefix=nohuge";
570 #endif
571
572         /* With --no-huge */
573         const char *argv1[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2"};
574         /* With --no-huge and -m */
575         const char *argv2[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2", "-m", "2"};
576
577         /* With --no-huge and --socket-mem */
578         const char *argv3[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2",
579                         "--socket-mem=2"};
580         /* With --no-huge, -m and --socket-mem */
581         const char *argv4[] = {prgname, prefix, no_huge, "-c", "1", "-n", "2",
582                         "-m", "2", "--socket-mem=2"};
583         if (launch_proc(argv1) != 0) {
584                 printf("Error - process did not run ok with --no-huge flag\n");
585                 return -1;
586         }
587         if (launch_proc(argv2) == 0) {
588                 printf("Error - process run ok with --no-huge and -m flags\n");
589                 return -1;
590         }
591 #ifdef RTE_EXEC_ENV_BSDAPP
592         /* BSD target does not support NUMA, hence no --socket-mem tests */
593         return 0;
594 #endif
595
596         if (launch_proc(argv3) == 0) {
597                 printf("Error - process run ok with --no-huge and --socket-mem "
598                                 "flags\n");
599                 return -1;
600         }
601         if (launch_proc(argv4) == 0) {
602                 printf("Error - process run ok with --no-huge, -m and "
603                                 "--socket-mem flags\n");
604                 return -1;
605         }
606         return 0;
607 }
608
609 #ifdef RTE_LIBRTE_XEN_DOM0
610 static int
611 test_dom0_misc_flags(void)
612 {
613         char prefix[PATH_MAX], tmp[PATH_MAX];
614
615         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
616                 printf("Error - unable to get current prefix!\n");
617                 return -1;
618         }
619         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
620
621         /* check that some general flags don't prevent things from working.
622          * All cases, apart from the first, app should run.
623          * No futher testing of output done.
624          */
625         /* sanity check - failure with invalid option */
626         const char *argv0[] = {prgname, prefix, mp_flag, "-c", "1", "--invalid-opt"};
627
628         /* With --no-pci */
629         const char *argv1[] = {prgname, prefix, mp_flag, "-c", "1", "--no-pci"};
630         /* With -v */
631         const char *argv2[] = {prgname, prefix, mp_flag, "-c", "1", "-v"};
632         /* With valid --syslog */
633         const char *argv3[] = {prgname, prefix, mp_flag, "-c", "1",
634                         "--syslog", "syslog"};
635         /* With empty --syslog (should fail) */
636         const char *argv4[] = {prgname, prefix, mp_flag, "-c", "1", "--syslog"};
637         /* With invalid --syslog */
638         const char *argv5[] = {prgname, prefix, mp_flag, "-c", "1", "--syslog", "error"};
639         /* With no-sh-conf */
640         const char *argv6[] = {prgname, "-c", "1", "-n", "2", "-m", "20",
641                         "--no-shconf", "--file-prefix=noshconf" };
642
643         if (launch_proc(argv0) == 0) {
644                 printf("Error - process ran ok with invalid flag\n");
645                 return -1;
646         }
647         if (launch_proc(argv1) != 0) {
648                 printf("Error - process did not run ok with --no-pci flag\n");
649                 return -1;
650         }
651         if (launch_proc(argv2) != 0) {
652                 printf("Error - process did not run ok with -v flag\n");
653                 return -1;
654         }
655         if (launch_proc(argv3) != 0) {
656                 printf("Error - process did not run ok with --syslog flag\n");
657                 return -1;
658         }
659         if (launch_proc(argv4) == 0) {
660                 printf("Error - process run ok with empty --syslog flag\n");
661                 return -1;
662         }
663         if (launch_proc(argv5) == 0) {
664                 printf("Error - process run ok with invalid --syslog flag\n");
665                 return -1;
666         }
667         if (launch_proc(argv6) != 0) {
668                 printf("Error - process did not run ok with --no-shconf flag\n");
669                 return -1;
670         }
671
672         return 0;
673 }
674 #else
675 static int
676 test_misc_flags(void)
677 {
678         char hugepath[PATH_MAX] = {0};
679 #ifdef RTE_EXEC_ENV_BSDAPP
680         /* BSD target doesn't support prefixes at this point */
681         const char * prefix = "";
682         const char * nosh_prefix = "";
683 #else
684         char prefix[PATH_MAX], tmp[PATH_MAX];
685         const char * nosh_prefix = "--file-prefix=noshconf";
686         FILE * hugedir_handle = NULL;
687         char line[PATH_MAX] = {0};
688         unsigned i, isempty = 1;
689         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
690                 printf("Error - unable to get current prefix!\n");
691                 return -1;
692         }
693         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
694
695         /*
696          * get first valid hugepage path
697          */
698
699         /* get hugetlbfs mountpoints from /proc/mounts */
700         hugedir_handle = fopen("/proc/mounts", "r");
701
702         if (hugedir_handle == NULL) {
703                 printf("Error opening /proc/mounts!\n");
704                 return -1;
705         }
706
707         /* read /proc/mounts */
708         while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
709
710                 /* find first valid hugepath */
711                 if (get_hugepage_path(line, sizeof(line), hugepath, sizeof(hugepath)))
712                         break;
713         }
714
715         fclose(hugedir_handle);
716
717         /* check if path is not empty */
718         for (i = 0; i < sizeof(hugepath); i++)
719                 if (hugepath[i] != '\0')
720                         isempty = 0;
721
722         if (isempty) {
723                 printf("No mounted hugepage dir found!\n");
724                 return -1;
725         }
726 #endif
727
728
729         /* check that some general flags don't prevent things from working.
730          * All cases, apart from the first, app should run.
731          * No futher testing of output done.
732          */
733         /* sanity check - failure with invalid option */
734         const char *argv0[] = {prgname, prefix, mp_flag, "-c", "1", "--invalid-opt"};
735
736         /* With --no-pci */
737         const char *argv1[] = {prgname, prefix, mp_flag, "-c", "1", "--no-pci"};
738         /* With -v */
739         const char *argv2[] = {prgname, prefix, mp_flag, "-c", "1", "-v"};
740         /* With valid --syslog */
741         const char *argv3[] = {prgname, prefix, mp_flag, "-c", "1",
742                         "--syslog", "syslog"};
743         /* With empty --syslog (should fail) */
744         const char *argv4[] = {prgname, prefix, mp_flag, "-c", "1", "--syslog"};
745         /* With invalid --syslog */
746         const char *argv5[] = {prgname, prefix, mp_flag, "-c", "1", "--syslog", "error"};
747         /* With no-sh-conf */
748         const char *argv6[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
749                         no_shconf, nosh_prefix };
750
751 #ifdef RTE_EXEC_ENV_BSDAPP
752         return 0;
753 #endif
754         /* With --huge-dir */
755         const char *argv7[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
756                         "--file-prefix=hugedir", "--huge-dir", hugepath};
757         /* With empty --huge-dir (should fail) */
758         const char *argv8[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
759                         "--file-prefix=hugedir", "--huge-dir"};
760         /* With invalid --huge-dir */
761         const char *argv9[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
762                         "--file-prefix=hugedir", "--huge-dir", "invalid"};
763         /* Secondary process with invalid --huge-dir (should run as flag has no
764          * effect on secondary processes) */
765         const char *argv10[] = {prgname, prefix, mp_flag, "-c", "1", "--huge-dir", "invalid"};
766
767         /* try running with base-virtaddr param */
768         const char *argv11[] = {prgname, "--file-prefix=virtaddr",
769                         "-c", "1", "-n", "2", "--base-virtaddr=0x12345678"};
770
771         /* try running with --vfio-intr INTx flag */
772         const char *argv12[] = {prgname, "--file-prefix=intr",
773                         "-c", "1", "-n", "2", "--vfio-intr=legacy"};
774
775         /* try running with --vfio-intr MSI flag */
776         const char *argv13[] = {prgname, "--file-prefix=intr",
777                         "-c", "1", "-n", "2", "--vfio-intr=msi"};
778
779         /* try running with --vfio-intr MSI-X flag */
780         const char *argv14[] = {prgname, "--file-prefix=intr",
781                         "-c", "1", "-n", "2", "--vfio-intr=msix"};
782
783         /* try running with --vfio-intr invalid flag */
784         const char *argv15[] = {prgname, "--file-prefix=intr",
785                         "-c", "1", "-n", "2", "--vfio-intr=invalid"};
786
787
788         if (launch_proc(argv0) == 0) {
789                 printf("Error - process ran ok with invalid flag\n");
790                 return -1;
791         }
792         if (launch_proc(argv1) != 0) {
793                 printf("Error - process did not run ok with --no-pci flag\n");
794                 return -1;
795         }
796         if (launch_proc(argv2) != 0) {
797                 printf("Error - process did not run ok with -v flag\n");
798                 return -1;
799         }
800         if (launch_proc(argv3) != 0) {
801                 printf("Error - process did not run ok with --syslog flag\n");
802                 return -1;
803         }
804         if (launch_proc(argv4) == 0) {
805                 printf("Error - process run ok with empty --syslog flag\n");
806                 return -1;
807         }
808         if (launch_proc(argv5) == 0) {
809                 printf("Error - process run ok with invalid --syslog flag\n");
810                 return -1;
811         }
812         if (launch_proc(argv6) != 0) {
813                 printf("Error - process did not run ok with --no-shconf flag\n");
814                 return -1;
815         }
816 #ifdef RTE_EXEC_ENV_BSDAPP
817         return 0;
818 #endif
819         if (launch_proc(argv7) != 0) {
820                 printf("Error - process did not run ok with --huge-dir flag\n");
821                 return -1;
822         }
823         if (launch_proc(argv8) == 0) {
824                 printf("Error - process run ok with empty --huge-dir flag\n");
825                 return -1;
826         }
827         if (launch_proc(argv9) == 0) {
828                 printf("Error - process run ok with invalid --huge-dir flag\n");
829                 return -1;
830         }
831         if (launch_proc(argv10) != 0) {
832                 printf("Error - secondary process did not run ok with invalid --huge-dir flag\n");
833                 return -1;
834         }
835         if (launch_proc(argv11) != 0) {
836                 printf("Error - process did not run ok with --base-virtaddr parameter\n");
837                 return -1;
838         }
839         if (launch_proc(argv12) != 0) {
840                 printf("Error - process did not run ok with "
841                                 "--vfio-intr INTx parameter\n");
842                 return -1;
843         }
844         if (launch_proc(argv13) != 0) {
845                 printf("Error - process did not run ok with "
846                                 "--vfio-intr MSI parameter\n");
847                 return -1;
848         }
849         if (launch_proc(argv14) != 0) {
850                 printf("Error - process did not run ok with "
851                                 "--vfio-intr MSI-X parameter\n");
852                 return -1;
853         }
854         if (launch_proc(argv15) == 0) {
855                 printf("Error - process run ok with "
856                                 "--vfio-intr invalid parameter\n");
857                 return -1;
858         }
859         return 0;
860 }
861 #endif
862
863 static int
864 test_file_prefix(void)
865 {
866         /*
867          * 1. check if current process hugefiles are locked
868          * 2. try to run secondary process without a corresponding primary process
869          * (while failing to run, it will also remove any unused hugepage files)
870          * 3. check if current process hugefiles are still in place and are locked
871          * 4. run a primary process with memtest1 prefix
872          * 5. check if memtest1 hugefiles are created
873          * 6. run a primary process with memtest2 prefix
874          * 7. check that only memtest2 hugefiles are present in the hugedir
875          */
876
877 #ifdef RTE_EXEC_ENV_BSDAPP
878         return 0;
879 #endif
880
881         /* this should fail unless the test itself is run with "memtest" prefix */
882         const char *argv0[] = {prgname, mp_flag, "-c", "1", "-n", "2", "-m", "2",
883                         "--file-prefix=" memtest };
884
885         /* primary process with memtest1 */
886         const char *argv1[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
887                                 "--file-prefix=" memtest1 };
888
889         /* primary process with memtest2 */
890         const char *argv2[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
891                                 "--file-prefix=" memtest2 };
892
893         char prefix[32];
894         if (get_current_prefix(prefix, sizeof(prefix)) == NULL) {
895                 printf("Error - unable to get current prefix!\n");
896                 return -1;
897         }
898 #ifdef RTE_LIBRTE_XEN_DOM0
899         return 0;
900 #endif
901
902         /* check if files for current prefix are present */
903         if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
904                 printf("Error - hugepage files for %s were not created!\n", prefix);
905                 return -1;
906         }
907
908         /* checks if files for current prefix are locked */
909         if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
910                 printf("Error - hugepages for current process aren't locked!\n");
911                 return -1;
912         }
913
914         /* check if files for secondary process are present */
915         if (process_hugefiles(memtest, HUGEPAGE_CHECK_EXISTS) == 1) {
916                 /* check if they are not locked */
917                 if (process_hugefiles(memtest, HUGEPAGE_CHECK_LOCKED) == 1) {
918                         printf("Error - hugepages for current process are locked!\n");
919                         return -1;
920                 }
921                 /* they aren't locked, delete them */
922                 else {
923                         if (process_hugefiles(memtest, HUGEPAGE_DELETE) != 1) {
924                                 printf("Error - deleting hugepages failed!\n");
925                                 return -1;
926                         }
927                 }
928         }
929
930         if (launch_proc(argv0) == 0) {
931                 printf("Error - secondary process ran ok without primary process\n");
932                 return -1;
933         }
934
935         /* check if files for current prefix are present */
936         if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
937                 printf("Error - hugepage files for %s were not created!\n", prefix);
938                 return -1;
939         }
940
941         /* checks if files for current prefix are locked */
942         if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
943                 printf("Error - hugepages for current process aren't locked!\n");
944                 return -1;
945         }
946
947         if (launch_proc(argv1) != 0) {
948                 printf("Error - failed to run with --file-prefix=%s\n", memtest);
949                 return -1;
950         }
951
952         /* check if memtest1_map0 is present */
953         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 1) {
954                 printf("Error - hugepage files for %s were not created!\n", memtest1);
955                 return -1;
956         }
957
958         if (launch_proc(argv2) != 0) {
959                 printf("Error - failed to run with --file-prefix=%s\n", memtest2);
960                 return -1;
961         }
962
963         /* check if hugefiles for memtest2 are present */
964         if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 1) {
965                 printf("Error - hugepage files for %s were not created!\n", memtest2);
966                 return -1;
967         }
968
969         /* check if hugefiles for memtest1 are present */
970         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
971                 printf("Error - hugepage files for %s were not deleted!\n", memtest1);
972                 return -1;
973         }
974
975         return 0;
976 }
977
978 /*
979  * Tests for correct handling of -m and --socket-mem flags
980  */
981 static int
982 test_memory_flags(void)
983 {
984         const char* mem_size = NULL;
985 #ifdef RTE_EXEC_ENV_BSDAPP
986         /* BSD target doesn't support prefixes at this point */
987         const char * prefix = "";
988 #else
989         char prefix[PATH_MAX], tmp[PATH_MAX];
990         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
991                 printf("Error - unable to get current prefix!\n");
992                 return -1;
993         }
994         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
995 #endif
996 #ifdef RTE_LIBRTE_XEN_DOM0
997         mem_size = "30";
998 #else
999         mem_size = "2";
1000 #endif
1001
1002
1003         /* valid -m flag and mp flag */
1004         const char *argv0[] = {prgname, prefix, mp_flag, "-c", "10",
1005                         "-n", "2", "-m", mem_size};
1006
1007         /* valid -m flag */
1008         const char *argv1[] = {prgname, "-c", "10", "-n", "2",
1009                         "--file-prefix=" memtest, "-m", mem_size};
1010
1011         /* invalid (zero) --socket-mem flag */
1012         const char *argv2[] = {prgname, "-c", "10", "-n", "2",
1013                         "--file-prefix=" memtest, "--socket-mem=0,0,0,0"};
1014
1015         /* invalid (incomplete) --socket-mem flag */
1016         const char *argv3[] = {prgname, "-c", "10", "-n", "2",
1017                         "--file-prefix=" memtest, "--socket-mem=2,2,"};
1018
1019         /* invalid (mixed with invalid data) --socket-mem flag */
1020         const char *argv4[] = {prgname, "-c", "10", "-n", "2",
1021                         "--file-prefix=" memtest, "--socket-mem=2,2,Fred"};
1022
1023         /* invalid (with numeric value as last character) --socket-mem flag */
1024         const char *argv5[] = {prgname, "-c", "10", "-n", "2",
1025                         "--file-prefix=" memtest, "--socket-mem=2,2,Fred0"};
1026
1027         /* invalid (with empty socket) --socket-mem flag */
1028         const char *argv6[] = {prgname, "-c", "10", "-n", "2",
1029                         "--file-prefix=" memtest, "--socket-mem=2,,2"};
1030
1031         /* invalid (null) --socket-mem flag */
1032         const char *argv7[] = {prgname, "-c", "10", "-n", "2",
1033                         "--file-prefix=" memtest, "--socket-mem="};
1034
1035         /* valid --socket-mem specified together with -m flag */
1036         const char *argv8[] = {prgname, "-c", "10", "-n", "2",
1037                         "--file-prefix=" memtest, "-m", "2", "--socket-mem=2,2"};
1038
1039         /* construct an invalid socket mask with 2 megs on each socket plus
1040          * extra 2 megs on socket that doesn't exist on current system */
1041         char invalid_socket_mem[SOCKET_MEM_STRLEN];
1042         char buf[SOCKET_MEM_STRLEN];    /* to avoid copying string onto itself */
1043
1044 #ifdef RTE_EXEC_ENV_BSDAPP
1045         int i, num_sockets = 1;
1046 #else
1047         int i, num_sockets = get_number_of_sockets();
1048 #endif
1049
1050         if (num_sockets <= 0 || num_sockets > RTE_MAX_NUMA_NODES) {
1051                 printf("Error - cannot get number of sockets!\n");
1052                 return -1;
1053         }
1054
1055         rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "--socket-mem=");
1056
1057         /* add one extra socket */
1058         for (i = 0; i < num_sockets + 1; i++) {
1059                 rte_snprintf(buf, sizeof(buf), "%s2", invalid_socket_mem);
1060                 rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "%s", buf);
1061
1062                 if (num_sockets + 1 - i > 1) {
1063                         rte_snprintf(buf, sizeof(buf), "%s,", invalid_socket_mem);
1064                         rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "%s", buf);
1065                 }
1066         }
1067
1068         /* construct a valid socket mask with 2 megs on each existing socket */
1069         char valid_socket_mem[SOCKET_MEM_STRLEN];
1070
1071         rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "--socket-mem=");
1072
1073         /* add one extra socket */
1074         for (i = 0; i < num_sockets; i++) {
1075                 rte_snprintf(buf, sizeof(buf), "%s2", valid_socket_mem);
1076                 rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "%s", buf);
1077
1078                 if (num_sockets - i > 1) {
1079                         rte_snprintf(buf, sizeof(buf), "%s,", valid_socket_mem);
1080                         rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "%s", buf);
1081                 }
1082         }
1083
1084         /* invalid --socket-mem flag (with extra socket) */
1085         const char *argv9[] = {prgname, "-c", "10", "-n", "2",
1086                         "--file-prefix=" memtest, invalid_socket_mem};
1087
1088         /* valid --socket-mem flag */
1089         const char *argv10[] = {prgname, "-c", "10", "-n", "2",
1090                         "--file-prefix=" memtest, valid_socket_mem};
1091
1092         if (launch_proc(argv0) != 0) {
1093                 printf("Error - secondary process failed with valid -m flag !\n");
1094                 return -1;
1095         }
1096
1097 #ifdef RTE_EXEC_ENV_BSDAPP
1098         /* no other tests are applicable to BSD */
1099         return 0;
1100 #endif
1101
1102         if (launch_proc(argv1) != 0) {
1103                 printf("Error - process failed with valid -m flag!\n");
1104                 return -1;
1105         }
1106 #ifdef RTE_LIBRTE_XEN_DOM0
1107         return 0;
1108 #endif
1109         if (launch_proc(argv2) == 0) {
1110                 printf("Error - process run ok with invalid (zero) --socket-mem!\n");
1111                 return -1;
1112         }
1113
1114         if (launch_proc(argv3) == 0) {
1115                 printf("Error - process run ok with invalid "
1116                                 "(incomplete) --socket-mem!\n");
1117                 return -1;
1118         }
1119
1120         if (launch_proc(argv4) == 0) {
1121                 printf("Error - process run ok with invalid "
1122                                 "(mixed with invalid input) --socket-mem!\n");
1123                 return -1;
1124         }
1125
1126         if (launch_proc(argv5) == 0) {
1127                 printf("Error - process run ok with invalid "
1128                                 "(mixed with invalid input with a numeric value as "
1129                                 "last character) --socket-mem!\n");
1130                 return -1;
1131         }
1132
1133         if (launch_proc(argv6) == 0) {
1134                 printf("Error - process run ok with invalid "
1135                                 "(with empty socket) --socket-mem!\n");
1136                 return -1;
1137         }
1138
1139         if (launch_proc(argv7) == 0) {
1140                 printf("Error - process run ok with invalid (null) --socket-mem!\n");
1141                 return -1;
1142         }
1143
1144         if (launch_proc(argv8) == 0) {
1145                 printf("Error - process run ok with --socket-mem and -m specified!\n");
1146                 return -1;
1147         }
1148
1149         if (launch_proc(argv9) == 0) {
1150                 printf("Error - process run ok with extra socket in --socket-mem!\n");
1151                 return -1;
1152         }
1153
1154         if (launch_proc(argv10) != 0) {
1155                 printf("Error - process failed with valid --socket-mem!\n");
1156                 return -1;
1157         }
1158
1159         return 0;
1160 }
1161
1162 int
1163 test_eal_flags(void)
1164 {
1165         int ret = 0;
1166
1167         ret = test_missing_c_flag();
1168         if (ret < 0) {
1169                 printf("Error in test_missing_c_flag()\n");
1170                 return ret;
1171         }
1172
1173         ret = test_missing_n_flag();
1174         if (ret < 0) {
1175                 printf("Error in test_missing_n_flag()\n");
1176                 return ret;
1177         }
1178
1179         ret = test_no_hpet_flag();
1180         if (ret < 0) {
1181                 printf("Error in test_no_hpet_flag()\n");
1182                 return ret;
1183         }
1184
1185         ret = test_no_huge_flag();
1186         if (ret < 0) {
1187                 printf("Error in test_no_huge_flag()\n");
1188                 return ret;
1189         }
1190
1191         ret = test_whitelist_flag();
1192         if (ret < 0) {
1193                 printf("Error in test_invalid_whitelist_flag()\n");
1194                 return ret;
1195         }
1196
1197         ret = test_invalid_b_flag();
1198         if (ret < 0) {
1199                 printf("Error in test_invalid_b_flag()\n");
1200                 return ret;
1201         }
1202
1203         ret = test_invalid_r_flag();
1204         if (ret < 0) {
1205                 printf("Error in test_invalid_r_flag()\n");
1206                 return ret;
1207         }
1208
1209         ret = test_memory_flags();
1210         if (ret < 0) {
1211                 printf("Error in test_memory_flags()\n");
1212                 return ret;
1213         }
1214
1215         ret = test_file_prefix();
1216         if (ret < 0) {
1217                 printf("Error in test_file_prefix()\n");
1218                 return ret;
1219         }
1220
1221 #ifdef RTE_LIBRTE_XEN_DOM0
1222         ret = test_dom0_misc_flags();
1223 #else
1224         ret = test_misc_flags();
1225 #endif
1226         if (ret < 0) {
1227                 printf("Error in test_misc_flags()");
1228                 return ret;
1229         }
1230
1231         return ret;
1232 }
1233
1234 #else
1235 /* Baremetal version
1236  * Multiprocess not applicable, so just return 0 always
1237  */
1238 int
1239 test_eal_flags(void)
1240 {
1241         printf("Multi-process not possible for baremetal, cannot test EAL flags\n");
1242         return 0;
1243 }
1244
1245 #endif