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