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