remove trailing whitespaces
[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
772         if (launch_proc(argv0) == 0) {
773                 printf("Error - process ran ok with invalid flag\n");
774                 return -1;
775         }
776         if (launch_proc(argv1) != 0) {
777                 printf("Error - process did not run ok with --no-pci flag\n");
778                 return -1;
779         }
780         if (launch_proc(argv2) != 0) {
781                 printf("Error - process did not run ok with -v flag\n");
782                 return -1;
783         }
784         if (launch_proc(argv3) != 0) {
785                 printf("Error - process did not run ok with --syslog flag\n");
786                 return -1;
787         }
788         if (launch_proc(argv4) == 0) {
789                 printf("Error - process run ok with empty --syslog flag\n");
790                 return -1;
791         }
792         if (launch_proc(argv5) == 0) {
793                 printf("Error - process run ok with invalid --syslog flag\n");
794                 return -1;
795         }
796         if (launch_proc(argv6) != 0) {
797                 printf("Error - process did not run ok with --no-shconf flag\n");
798                 return -1;
799         }
800 #ifdef RTE_EXEC_ENV_BSDAPP
801         return 0;
802 #endif
803         if (launch_proc(argv7) != 0) {
804                 printf("Error - process did not run ok with --huge-dir flag\n");
805                 return -1;
806         }
807         if (launch_proc(argv8) == 0) {
808                 printf("Error - process run ok with empty --huge-dir flag\n");
809                 return -1;
810         }
811         if (launch_proc(argv9) == 0) {
812                 printf("Error - process run ok with invalid --huge-dir flag\n");
813                 return -1;
814         }
815         if (launch_proc(argv10) != 0) {
816                 printf("Error - secondary process did not run ok with invalid --huge-dir flag\n");
817                 return -1;
818         }
819         if (launch_proc(argv11) != 0) {
820                 printf("Error - process did not run ok with --base-virtaddr parameter\n");
821                 return -1;
822         }
823         return 0;
824 }
825 #endif
826
827 static int
828 test_file_prefix(void)
829 {
830         /*
831          * 1. check if current process hugefiles are locked
832          * 2. try to run secondary process without a corresponding primary process
833          * (while failing to run, it will also remove any unused hugepage files)
834          * 3. check if current process hugefiles are still in place and are locked
835          * 4. run a primary process with memtest1 prefix
836          * 5. check if memtest1 hugefiles are created
837          * 6. run a primary process with memtest2 prefix
838          * 7. check that only memtest2 hugefiles are present in the hugedir
839          */
840
841 #ifdef RTE_EXEC_ENV_BSDAPP
842         return 0;
843 #endif
844
845         /* this should fail unless the test itself is run with "memtest" prefix */
846         const char *argv0[] = {prgname, mp_flag, "-c", "1", "-n", "2", "-m", "2",
847                         "--file-prefix=" memtest };
848
849         /* primary process with memtest1 */
850         const char *argv1[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
851                                 "--file-prefix=" memtest1 };
852
853         /* primary process with memtest2 */
854         const char *argv2[] = {prgname, "-c", "1", "-n", "2", "-m", "2",
855                                 "--file-prefix=" memtest2 };
856
857         char prefix[32];
858         if (get_current_prefix(prefix, sizeof(prefix)) == NULL) {
859                 printf("Error - unable to get current prefix!\n");
860                 return -1;
861         }
862 #ifdef RTE_LIBRTE_XEN_DOM0
863         return 0;
864 #endif
865
866         /* check if files for current prefix are present */
867         if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
868                 printf("Error - hugepage files for %s were not created!\n", prefix);
869                 return -1;
870         }
871
872         /* checks if files for current prefix are locked */
873         if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
874                 printf("Error - hugepages for current process aren't locked!\n");
875                 return -1;
876         }
877
878         /* check if files for secondary process are present */
879         if (process_hugefiles(memtest, HUGEPAGE_CHECK_EXISTS) == 1) {
880                 /* check if they are not locked */
881                 if (process_hugefiles(memtest, HUGEPAGE_CHECK_LOCKED) == 1) {
882                         printf("Error - hugepages for current process are locked!\n");
883                         return -1;
884                 }
885                 /* they aren't locked, delete them */
886                 else {
887                         if (process_hugefiles(memtest, HUGEPAGE_DELETE) != 1) {
888                                 printf("Error - deleting hugepages failed!\n");
889                                 return -1;
890                         }
891                 }
892         }
893
894         if (launch_proc(argv0) == 0) {
895                 printf("Error - secondary process ran ok without primary process\n");
896                 return -1;
897         }
898
899         /* check if files for current prefix are present */
900         if (process_hugefiles(prefix, HUGEPAGE_CHECK_EXISTS) != 1) {
901                 printf("Error - hugepage files for %s were not created!\n", prefix);
902                 return -1;
903         }
904
905         /* checks if files for current prefix are locked */
906         if (process_hugefiles(prefix, HUGEPAGE_CHECK_LOCKED) != 1) {
907                 printf("Error - hugepages for current process aren't locked!\n");
908                 return -1;
909         }
910
911         if (launch_proc(argv1) != 0) {
912                 printf("Error - failed to run with --file-prefix=%s\n", memtest);
913                 return -1;
914         }
915
916         /* check if memtest1_map0 is present */
917         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 1) {
918                 printf("Error - hugepage files for %s were not created!\n", memtest1);
919                 return -1;
920         }
921
922         if (launch_proc(argv2) != 0) {
923                 printf("Error - failed to run with --file-prefix=%s\n", memtest2);
924                 return -1;
925         }
926
927         /* check if hugefiles for memtest2 are present */
928         if (process_hugefiles(memtest2, HUGEPAGE_CHECK_EXISTS) != 1) {
929                 printf("Error - hugepage files for %s were not created!\n", memtest2);
930                 return -1;
931         }
932
933         /* check if hugefiles for memtest1 are present */
934         if (process_hugefiles(memtest1, HUGEPAGE_CHECK_EXISTS) != 0) {
935                 printf("Error - hugepage files for %s were not deleted!\n", memtest1);
936                 return -1;
937         }
938
939         return 0;
940 }
941
942 /*
943  * Tests for correct handling of -m and --socket-mem flags
944  */
945 static int
946 test_memory_flags(void)
947 {
948         const char* mem_size = NULL;
949 #ifdef RTE_EXEC_ENV_BSDAPP
950         /* BSD target doesn't support prefixes at this point */
951         const char * prefix = "";
952 #else
953         char prefix[PATH_MAX], tmp[PATH_MAX];
954         if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
955                 printf("Error - unable to get current prefix!\n");
956                 return -1;
957         }
958         rte_snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
959 #endif
960 #ifdef RTE_LIBRTE_XEN_DOM0
961         mem_size = "30";
962 #else
963         mem_size = "2";
964 #endif
965
966
967         /* valid -m flag and mp flag */
968         const char *argv0[] = {prgname, prefix, mp_flag, "-c", "10",
969                         "-n", "2", "-m", mem_size};
970
971         /* valid -m flag */
972         const char *argv1[] = {prgname, "-c", "10", "-n", "2",
973                         "--file-prefix=" memtest, "-m", mem_size};
974
975         /* invalid (zero) --socket-mem flag */
976         const char *argv2[] = {prgname, "-c", "10", "-n", "2",
977                         "--file-prefix=" memtest, "--socket-mem=0,0,0,0"};
978
979         /* invalid (incomplete) --socket-mem flag */
980         const char *argv3[] = {prgname, "-c", "10", "-n", "2",
981                         "--file-prefix=" memtest, "--socket-mem=2,2,"};
982
983         /* invalid (mixed with invalid data) --socket-mem flag */
984         const char *argv4[] = {prgname, "-c", "10", "-n", "2",
985                         "--file-prefix=" memtest, "--socket-mem=2,2,Fred"};
986
987         /* invalid (with numeric value as last character) --socket-mem flag */
988         const char *argv5[] = {prgname, "-c", "10", "-n", "2",
989                         "--file-prefix=" memtest, "--socket-mem=2,2,Fred0"};
990
991         /* invalid (with empty socket) --socket-mem flag */
992         const char *argv6[] = {prgname, "-c", "10", "-n", "2",
993                         "--file-prefix=" memtest, "--socket-mem=2,,2"};
994
995         /* invalid (null) --socket-mem flag */
996         const char *argv7[] = {prgname, "-c", "10", "-n", "2",
997                         "--file-prefix=" memtest, "--socket-mem="};
998
999         /* valid --socket-mem specified together with -m flag */
1000         const char *argv8[] = {prgname, "-c", "10", "-n", "2",
1001                         "--file-prefix=" memtest, "-m", "2", "--socket-mem=2,2"};
1002
1003         /* construct an invalid socket mask with 2 megs on each socket plus
1004          * extra 2 megs on socket that doesn't exist on current system */
1005         char invalid_socket_mem[SOCKET_MEM_STRLEN];
1006         char buf[SOCKET_MEM_STRLEN];    /* to avoid copying string onto itself */
1007
1008 #ifdef RTE_EXEC_ENV_BSDAPP
1009         int i, num_sockets = 1;
1010 #else
1011         int i, num_sockets = get_number_of_sockets();
1012 #endif
1013
1014         if (num_sockets <= 0 || num_sockets > RTE_MAX_NUMA_NODES) {
1015                 printf("Error - cannot get number of sockets!\n");
1016                 return -1;
1017         }
1018
1019         rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "--socket-mem=");
1020
1021         /* add one extra socket */
1022         for (i = 0; i < num_sockets + 1; i++) {
1023                 rte_snprintf(buf, sizeof(buf), "%s2", invalid_socket_mem);
1024                 rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "%s", buf);
1025
1026                 if (num_sockets + 1 - i > 1) {
1027                         rte_snprintf(buf, sizeof(buf), "%s,", invalid_socket_mem);
1028                         rte_snprintf(invalid_socket_mem, sizeof(invalid_socket_mem), "%s", buf);
1029                 }
1030         }
1031
1032         /* construct a valid socket mask with 2 megs on each existing socket */
1033         char valid_socket_mem[SOCKET_MEM_STRLEN];
1034
1035         rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "--socket-mem=");
1036
1037         /* add one extra socket */
1038         for (i = 0; i < num_sockets; i++) {
1039                 rte_snprintf(buf, sizeof(buf), "%s2", valid_socket_mem);
1040                 rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "%s", buf);
1041
1042                 if (num_sockets - i > 1) {
1043                         rte_snprintf(buf, sizeof(buf), "%s,", valid_socket_mem);
1044                         rte_snprintf(valid_socket_mem, sizeof(valid_socket_mem), "%s", buf);
1045                 }
1046         }
1047
1048         /* invalid --socket-mem flag (with extra socket) */
1049         const char *argv9[] = {prgname, "-c", "10", "-n", "2",
1050                         "--file-prefix=" memtest, invalid_socket_mem};
1051
1052         /* valid --socket-mem flag */
1053         const char *argv10[] = {prgname, "-c", "10", "-n", "2",
1054                         "--file-prefix=" memtest, valid_socket_mem};
1055
1056         if (launch_proc(argv0) != 0) {
1057                 printf("Error - secondary process failed with valid -m flag !\n");
1058                 return -1;
1059         }
1060
1061 #ifdef RTE_EXEC_ENV_BSDAPP
1062         /* no other tests are applicable to BSD */
1063         return 0;
1064 #endif
1065
1066         if (launch_proc(argv1) != 0) {
1067                 printf("Error - process failed with valid -m flag!\n");
1068                 return -1;
1069         }
1070 #ifdef RTE_LIBRTE_XEN_DOM0
1071         return 0;
1072 #endif
1073         if (launch_proc(argv2) == 0) {
1074                 printf("Error - process run ok with invalid (zero) --socket-mem!\n");
1075                 return -1;
1076         }
1077
1078         if (launch_proc(argv3) == 0) {
1079                 printf("Error - process run ok with invalid "
1080                                 "(incomplete) --socket-mem!\n");
1081                 return -1;
1082         }
1083
1084         if (launch_proc(argv4) == 0) {
1085                 printf("Error - process run ok with invalid "
1086                                 "(mixed with invalid input) --socket-mem!\n");
1087                 return -1;
1088         }
1089
1090         if (launch_proc(argv5) == 0) {
1091                 printf("Error - process run ok with invalid "
1092                                 "(mixed with invalid input with a numeric value as "
1093                                 "last character) --socket-mem!\n");
1094                 return -1;
1095         }
1096
1097         if (launch_proc(argv6) == 0) {
1098                 printf("Error - process run ok with invalid "
1099                                 "(with empty socket) --socket-mem!\n");
1100                 return -1;
1101         }
1102
1103         if (launch_proc(argv7) == 0) {
1104                 printf("Error - process run ok with invalid (null) --socket-mem!\n");
1105                 return -1;
1106         }
1107
1108         if (launch_proc(argv8) == 0) {
1109                 printf("Error - process run ok with --socket-mem and -m specified!\n");
1110                 return -1;
1111         }
1112
1113         if (launch_proc(argv9) == 0) {
1114                 printf("Error - process run ok with extra socket in --socket-mem!\n");
1115                 return -1;
1116         }
1117
1118         if (launch_proc(argv10) != 0) {
1119                 printf("Error - process failed with valid --socket-mem!\n");
1120                 return -1;
1121         }
1122
1123         return 0;
1124 }
1125
1126 int
1127 test_eal_flags(void)
1128 {
1129         int ret = 0;
1130
1131         ret = test_missing_c_flag();
1132         if (ret < 0) {
1133                 printf("Error in test_missing_c_flag()\n");
1134                 return ret;
1135         }
1136
1137         ret = test_missing_n_flag();
1138         if (ret < 0) {
1139                 printf("Error in test_missing_n_flag()\n");
1140                 return ret;
1141         }
1142
1143         ret = test_no_hpet_flag();
1144         if (ret < 0) {
1145                 printf("Error in test_no_hpet_flag()\n");
1146                 return ret;
1147         }
1148
1149         ret = test_no_huge_flag();
1150         if (ret < 0) {
1151                 printf("Error in test_no_huge_flag()\n");
1152                 return ret;
1153         }
1154
1155         ret = test_whitelist_flag();
1156         if (ret < 0) {
1157                 printf("Error in test_invalid_whitelist_flag()\n");
1158                 return ret;
1159         }
1160
1161         ret = test_invalid_b_flag();
1162         if (ret < 0) {
1163                 printf("Error in test_invalid_b_flag()\n");
1164                 return ret;
1165         }
1166
1167         ret = test_invalid_r_flag();
1168         if (ret < 0) {
1169                 printf("Error in test_invalid_r_flag()\n");
1170                 return ret;
1171         }
1172
1173         ret = test_memory_flags();
1174         if (ret < 0) {
1175                 printf("Error in test_memory_flags()\n");
1176                 return ret;
1177         }
1178
1179         ret = test_file_prefix();
1180         if (ret < 0) {
1181                 printf("Error in test_file_prefix()\n");
1182                 return ret;
1183         }
1184
1185 #ifdef RTE_LIBRTE_XEN_DOM0
1186         ret = test_dom0_misc_flags();
1187 #else
1188         ret = test_misc_flags();
1189 #endif
1190         if (ret < 0) {
1191                 printf("Error in test_misc_flags()");
1192                 return ret;
1193         }
1194
1195         return ret;
1196 }
1197
1198 #else
1199 /* Baremetal version
1200  * Multiprocess not applicable, so just return 0 always
1201  */
1202 int
1203 test_eal_flags(void)
1204 {
1205         printf("Multi-process not possible for baremetal, cannot test EAL flags\n");
1206         return 0;
1207 }
1208
1209 #endif