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