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