eal: fix strnlen return value with icc
[dpdk.git] / lib / librte_eal / common / eal_common_options.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2014 6WIND S.A.
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
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <syslog.h>
38 #include <ctype.h>
39 #include <limits.h>
40 #include <errno.h>
41 #include <getopt.h>
42
43 #include <rte_eal.h>
44 #include <rte_log.h>
45 #include <rte_lcore.h>
46 #include <rte_version.h>
47 #include <rte_devargs.h>
48 #include <rte_memcpy.h>
49
50 #include "eal_internal_cfg.h"
51 #include "eal_options.h"
52 #include "eal_filesystem.h"
53
54 #define BITS_PER_HEX 4
55
56 const char
57 eal_short_options[] =
58         "b:" /* pci-blacklist */
59         "c:" /* coremask */
60         "d:" /* driver */
61         "h"  /* help */
62         "l:" /* corelist */
63         "m:" /* memory size */
64         "n:" /* memory channels */
65         "r:" /* memory ranks */
66         "v"  /* version */
67         "w:" /* pci-whitelist */
68         ;
69
70 const struct option
71 eal_long_options[] = {
72         {OPT_BASE_VIRTADDR,     1, NULL, OPT_BASE_VIRTADDR_NUM    },
73         {OPT_CREATE_UIO_DEV,    1, NULL, OPT_CREATE_UIO_DEV_NUM   },
74         {OPT_FILE_PREFIX,       1, NULL, OPT_FILE_PREFIX_NUM      },
75         {OPT_HELP,              0, NULL, OPT_HELP_NUM             },
76         {OPT_HUGE_DIR,          1, NULL, OPT_HUGE_DIR_NUM         },
77         {OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
78         {OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
79         {OPT_MASTER_LCORE,      1, NULL, OPT_MASTER_LCORE_NUM     },
80         {OPT_NO_HPET,           0, NULL, OPT_NO_HPET_NUM          },
81         {OPT_NO_HUGE,           0, NULL, OPT_NO_HUGE_NUM          },
82         {OPT_NO_PCI,            0, NULL, OPT_NO_PCI_NUM           },
83         {OPT_NO_SHCONF,         0, NULL, OPT_NO_SHCONF_NUM        },
84         {OPT_PCI_BLACKLIST,     1, NULL, OPT_PCI_BLACKLIST_NUM    },
85         {OPT_PCI_WHITELIST,     1, NULL, OPT_PCI_WHITELIST_NUM    },
86         {OPT_PROC_TYPE,         1, NULL, OPT_PROC_TYPE_NUM        },
87         {OPT_SOCKET_MEM,        1, NULL, OPT_SOCKET_MEM_NUM       },
88         {OPT_SYSLOG,            1, NULL, OPT_SYSLOG_NUM           },
89         {OPT_VDEV,              1, NULL, OPT_VDEV_NUM             },
90         {OPT_VFIO_INTR,         1, NULL, OPT_VFIO_INTR_NUM        },
91         {OPT_VMWARE_TSC_MAP,    0, NULL, OPT_VMWARE_TSC_MAP_NUM   },
92         {OPT_XEN_DOM0,          0, NULL, OPT_XEN_DOM0_NUM         },
93         {0,                     0, NULL, 0                        }
94 };
95
96 static int lcores_parsed;
97 static int master_lcore_parsed;
98 static int mem_parsed;
99
100 void
101 eal_reset_internal_config(struct internal_config *internal_cfg)
102 {
103         int i;
104
105         internal_cfg->memory = 0;
106         internal_cfg->force_nrank = 0;
107         internal_cfg->force_nchannel = 0;
108         internal_cfg->hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
109         internal_cfg->hugepage_dir = NULL;
110         internal_cfg->force_sockets = 0;
111         /* zero out the NUMA config */
112         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
113                 internal_cfg->socket_mem[i] = 0;
114         /* zero out hugedir descriptors */
115         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
116                 internal_cfg->hugepage_info[i].lock_descriptor = -1;
117         internal_cfg->base_virtaddr = 0;
118
119         internal_cfg->syslog_facility = LOG_DAEMON;
120         /* default value from build option */
121         internal_cfg->log_level = RTE_LOG_LEVEL;
122
123         internal_cfg->xen_dom0_support = 0;
124
125         /* if set to NONE, interrupt mode is determined automatically */
126         internal_cfg->vfio_intr_mode = RTE_INTR_MODE_NONE;
127
128 #ifdef RTE_LIBEAL_USE_HPET
129         internal_cfg->no_hpet = 0;
130 #else
131         internal_cfg->no_hpet = 1;
132 #endif
133         internal_cfg->vmware_tsc_map = 0;
134 }
135
136 /*
137  * Parse the coremask given as argument (hexadecimal string) and fill
138  * the global configuration (core role and core count) with the parsed
139  * value.
140  */
141 static int xdigit2val(unsigned char c)
142 {
143         int val;
144
145         if (isdigit(c))
146                 val = c - '0';
147         else if (isupper(c))
148                 val = c - 'A' + 10;
149         else
150                 val = c - 'a' + 10;
151         return val;
152 }
153
154 static int
155 eal_parse_coremask(const char *coremask)
156 {
157         struct rte_config *cfg = rte_eal_get_configuration();
158         int i, j, idx = 0;
159         unsigned count = 0;
160         char c;
161         int val;
162
163         if (coremask == NULL)
164                 return -1;
165         /* Remove all blank characters ahead and after .
166          * Remove 0x/0X if exists.
167          */
168         while (isblank(*coremask))
169                 coremask++;
170         if (coremask[0] == '0' && ((coremask[1] == 'x')
171                 || (coremask[1] == 'X')))
172                 coremask += 2;
173         i = strlen(coremask);
174         while ((i > 0) && isblank(coremask[i - 1]))
175                 i--;
176         if (i == 0)
177                 return -1;
178
179         for (i = i - 1; i >= 0 && idx < RTE_MAX_LCORE; i--) {
180                 c = coremask[i];
181                 if (isxdigit(c) == 0) {
182                         /* invalid characters */
183                         return -1;
184                 }
185                 val = xdigit2val(c);
186                 for (j = 0; j < BITS_PER_HEX && idx < RTE_MAX_LCORE; j++, idx++)
187                 {
188                         if ((1 << j) & val) {
189                                 if (!lcore_config[idx].detected) {
190                                         RTE_LOG(ERR, EAL, "lcore %u "
191                                                 "unavailable\n", idx);
192                                         return -1;
193                                 }
194                                 cfg->lcore_role[idx] = ROLE_RTE;
195                                 lcore_config[idx].core_index = count;
196                                 count++;
197                         } else {
198                                 cfg->lcore_role[idx] = ROLE_OFF;
199                                 lcore_config[idx].core_index = -1;
200                         }
201                 }
202         }
203         for (; i >= 0; i--)
204                 if (coremask[i] != '0')
205                         return -1;
206         for (; idx < RTE_MAX_LCORE; idx++) {
207                 cfg->lcore_role[idx] = ROLE_OFF;
208                 lcore_config[idx].core_index = -1;
209         }
210         if (count == 0)
211                 return -1;
212         /* Update the count of enabled logical cores of the EAL configuration */
213         cfg->lcore_count = count;
214         lcores_parsed = 1;
215         return 0;
216 }
217
218 static int
219 eal_parse_corelist(const char *corelist)
220 {
221         struct rte_config *cfg = rte_eal_get_configuration();
222         int i, idx = 0;
223         unsigned count = 0;
224         char *end = NULL;
225         int min, max;
226
227         if (corelist == NULL)
228                 return -1;
229
230         /* Remove all blank characters ahead and after */
231         while (isblank(*corelist))
232                 corelist++;
233         i = strlen(corelist);
234         while ((i > 0) && isblank(corelist[i - 1]))
235                 i--;
236
237         /* Reset config */
238         for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
239                 cfg->lcore_role[idx] = ROLE_OFF;
240                 lcore_config[idx].core_index = -1;
241         }
242
243         /* Get list of cores */
244         min = RTE_MAX_LCORE;
245         do {
246                 while (isblank(*corelist))
247                         corelist++;
248                 if (*corelist == '\0')
249                         return -1;
250                 errno = 0;
251                 idx = strtoul(corelist, &end, 10);
252                 if (errno || end == NULL)
253                         return -1;
254                 while (isblank(*end))
255                         end++;
256                 if (*end == '-') {
257                         min = idx;
258                 } else if ((*end == ',') || (*end == '\0')) {
259                         max = idx;
260                         if (min == RTE_MAX_LCORE)
261                                 min = idx;
262                         for (idx = min; idx <= max; idx++) {
263                                 if (cfg->lcore_role[idx] != ROLE_RTE) {
264                                         cfg->lcore_role[idx] = ROLE_RTE;
265                                         lcore_config[idx].core_index = count;
266                                         count++;
267                                 }
268                         }
269                         min = RTE_MAX_LCORE;
270                 } else
271                         return -1;
272                 corelist = end + 1;
273         } while (*end != '\0');
274
275         if (count == 0)
276                 return -1;
277
278         /* Update the count of enabled logical cores of the EAL configuration */
279         cfg->lcore_count = count;
280
281         lcores_parsed = 1;
282         return 0;
283 }
284
285 /* Changes the lcore id of the master thread */
286 static int
287 eal_parse_master_lcore(const char *arg)
288 {
289         char *parsing_end;
290         struct rte_config *cfg = rte_eal_get_configuration();
291
292         errno = 0;
293         cfg->master_lcore = (uint32_t) strtol(arg, &parsing_end, 0);
294         if (errno || parsing_end[0] != 0)
295                 return -1;
296         if (cfg->master_lcore >= RTE_MAX_LCORE)
297                 return -1;
298         master_lcore_parsed = 1;
299         return 0;
300 }
301
302 /*
303  * Parse elem, the elem could be single number/range or '(' ')' group
304  * 1) A single number elem, it's just a simple digit. e.g. 9
305  * 2) A single range elem, two digits with a '-' between. e.g. 2-6
306  * 3) A group elem, combines multiple 1) or 2) with '( )'. e.g (0,2-4,6)
307  *    Within group elem, '-' used for a range separator;
308  *                       ',' used for a single number.
309  */
310 static int
311 eal_parse_set(const char *input, uint16_t set[], unsigned num)
312 {
313         unsigned idx;
314         const char *str = input;
315         char *end = NULL;
316         unsigned min, max;
317
318         memset(set, 0, num * sizeof(uint16_t));
319
320         while (isblank(*str))
321                 str++;
322
323         /* only digit or left bracket is qualify for start point */
324         if ((!isdigit(*str) && *str != '(') || *str == '\0')
325                 return -1;
326
327         /* process single number or single range of number */
328         if (*str != '(') {
329                 errno = 0;
330                 idx = strtoul(str, &end, 10);
331                 if (errno || end == NULL || idx >= num)
332                         return -1;
333                 else {
334                         while (isblank(*end))
335                                 end++;
336
337                         min = idx;
338                         max = idx;
339                         if (*end == '-') {
340                                 /* process single <number>-<number> */
341                                 end++;
342                                 while (isblank(*end))
343                                         end++;
344                                 if (!isdigit(*end))
345                                         return -1;
346
347                                 errno = 0;
348                                 idx = strtoul(end, &end, 10);
349                                 if (errno || end == NULL || idx >= num)
350                                         return -1;
351                                 max = idx;
352                                 while (isblank(*end))
353                                         end++;
354                                 if (*end != ',' && *end != '\0')
355                                         return -1;
356                         }
357
358                         if (*end != ',' && *end != '\0' &&
359                             *end != '@')
360                                 return -1;
361
362                         for (idx = RTE_MIN(min, max);
363                              idx <= RTE_MAX(min, max); idx++)
364                                 set[idx] = 1;
365
366                         return end - input;
367                 }
368         }
369
370         /* process set within bracket */
371         str++;
372         while (isblank(*str))
373                 str++;
374         if (*str == '\0')
375                 return -1;
376
377         min = RTE_MAX_LCORE;
378         do {
379
380                 /* go ahead to the first digit */
381                 while (isblank(*str))
382                         str++;
383                 if (!isdigit(*str))
384                         return -1;
385
386                 /* get the digit value */
387                 errno = 0;
388                 idx = strtoul(str, &end, 10);
389                 if (errno || end == NULL || idx >= num)
390                         return -1;
391
392                 /* go ahead to separator '-',',' and ')' */
393                 while (isblank(*end))
394                         end++;
395                 if (*end == '-') {
396                         if (min == RTE_MAX_LCORE)
397                                 min = idx;
398                         else /* avoid continuous '-' */
399                                 return -1;
400                 } else if ((*end == ',') || (*end == ')')) {
401                         max = idx;
402                         if (min == RTE_MAX_LCORE)
403                                 min = idx;
404                         for (idx = RTE_MIN(min, max);
405                              idx <= RTE_MAX(min, max); idx++)
406                                 set[idx] = 1;
407
408                         min = RTE_MAX_LCORE;
409                 } else
410                         return -1;
411
412                 str = end + 1;
413         } while (*end != '\0' && *end != ')');
414
415         return str - input;
416 }
417
418 /* convert from set array to cpuset bitmap */
419 static int
420 convert_to_cpuset(rte_cpuset_t *cpusetp,
421               uint16_t *set, unsigned num)
422 {
423         unsigned idx;
424
425         CPU_ZERO(cpusetp);
426
427         for (idx = 0; idx < num; idx++) {
428                 if (!set[idx])
429                         continue;
430
431                 if (!lcore_config[idx].detected) {
432                         RTE_LOG(ERR, EAL, "core %u "
433                                 "unavailable\n", idx);
434                         return -1;
435                 }
436
437                 CPU_SET(idx, cpusetp);
438         }
439
440         return 0;
441 }
442
443 /*
444  * The format pattern: --lcores='<lcores[@cpus]>[<,lcores[@cpus]>...]'
445  * lcores, cpus could be a single digit/range or a group.
446  * '(' and ')' are necessary if it's a group.
447  * If not supply '@cpus', the value of cpus uses the same as lcores.
448  * e.g. '1,2@(5-7),(3-5)@(0,2),(0,6),7-8' means start 9 EAL thread as below
449  *   lcore 0 runs on cpuset 0x41 (cpu 0,6)
450  *   lcore 1 runs on cpuset 0x2 (cpu 1)
451  *   lcore 2 runs on cpuset 0xe0 (cpu 5,6,7)
452  *   lcore 3,4,5 runs on cpuset 0x5 (cpu 0,2)
453  *   lcore 6 runs on cpuset 0x41 (cpu 0,6)
454  *   lcore 7 runs on cpuset 0x80 (cpu 7)
455  *   lcore 8 runs on cpuset 0x100 (cpu 8)
456  */
457 static int
458 eal_parse_lcores(const char *lcores)
459 {
460         struct rte_config *cfg = rte_eal_get_configuration();
461         static uint16_t set[RTE_MAX_LCORE];
462         unsigned idx = 0;
463         int i;
464         unsigned count = 0;
465         const char *lcore_start = NULL;
466         const char *end = NULL;
467         int offset;
468         rte_cpuset_t cpuset;
469         int lflags = 0;
470         int ret = -1;
471
472         if (lcores == NULL)
473                 return -1;
474
475         /* Remove all blank characters ahead and after */
476         while (isblank(*lcores))
477                 lcores++;
478         i = strlen(lcores);
479         while ((i > 0) && isblank(lcores[i - 1]))
480                 i--;
481
482         CPU_ZERO(&cpuset);
483
484         /* Reset lcore config */
485         for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
486                 cfg->lcore_role[idx] = ROLE_OFF;
487                 lcore_config[idx].core_index = -1;
488                 CPU_ZERO(&lcore_config[idx].cpuset);
489         }
490
491         /* Get list of cores */
492         do {
493                 while (isblank(*lcores))
494                         lcores++;
495                 if (*lcores == '\0')
496                         goto err;
497
498                 /* record lcore_set start point */
499                 lcore_start = lcores;
500
501                 /* go across a complete bracket */
502                 if (*lcore_start == '(') {
503                         lcores += strcspn(lcores, ")");
504                         if (*lcores++ == '\0')
505                                 goto err;
506                 }
507
508                 /* scan the separator '@', ','(next) or '\0'(finish) */
509                 lcores += strcspn(lcores, "@,");
510
511                 if (*lcores == '@') {
512                         /* explicit assign cpu_set */
513                         offset = eal_parse_set(lcores + 1, set, RTE_DIM(set));
514                         if (offset < 0)
515                                 goto err;
516
517                         /* prepare cpu_set and update the end cursor */
518                         if (0 > convert_to_cpuset(&cpuset,
519                                                   set, RTE_DIM(set)))
520                                 goto err;
521                         end = lcores + 1 + offset;
522                 } else { /* ',' or '\0' */
523                         /* haven't given cpu_set, current loop done */
524                         end = lcores;
525
526                         /* go back to check <number>-<number> */
527                         offset = strcspn(lcore_start, "(-");
528                         if (offset < (end - lcore_start) &&
529                             *(lcore_start + offset) != '(')
530                                 lflags = 1;
531                 }
532
533                 if (*end != ',' && *end != '\0')
534                         goto err;
535
536                 /* parse lcore_set from start point */
537                 if (0 > eal_parse_set(lcore_start, set, RTE_DIM(set)))
538                         goto err;
539
540                 /* without '@', by default using lcore_set as cpu_set */
541                 if (*lcores != '@' &&
542                     0 > convert_to_cpuset(&cpuset, set, RTE_DIM(set)))
543                         goto err;
544
545                 /* start to update lcore_set */
546                 for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
547                         if (!set[idx])
548                                 continue;
549
550                         if (cfg->lcore_role[idx] != ROLE_RTE) {
551                                 lcore_config[idx].core_index = count;
552                                 cfg->lcore_role[idx] = ROLE_RTE;
553                                 count++;
554                         }
555
556                         if (lflags) {
557                                 CPU_ZERO(&cpuset);
558                                 CPU_SET(idx, &cpuset);
559                         }
560                         rte_memcpy(&lcore_config[idx].cpuset, &cpuset,
561                                    sizeof(rte_cpuset_t));
562                 }
563
564                 lcores = end + 1;
565         } while (*end != '\0');
566
567         if (count == 0)
568                 goto err;
569
570         cfg->lcore_count = count;
571         lcores_parsed = 1;
572         ret = 0;
573
574 err:
575
576         return ret;
577 }
578
579 static int
580 eal_parse_syslog(const char *facility, struct internal_config *conf)
581 {
582         int i;
583         static struct {
584                 const char *name;
585                 int value;
586         } map[] = {
587                 { "auth", LOG_AUTH },
588                 { "cron", LOG_CRON },
589                 { "daemon", LOG_DAEMON },
590                 { "ftp", LOG_FTP },
591                 { "kern", LOG_KERN },
592                 { "lpr", LOG_LPR },
593                 { "mail", LOG_MAIL },
594                 { "news", LOG_NEWS },
595                 { "syslog", LOG_SYSLOG },
596                 { "user", LOG_USER },
597                 { "uucp", LOG_UUCP },
598                 { "local0", LOG_LOCAL0 },
599                 { "local1", LOG_LOCAL1 },
600                 { "local2", LOG_LOCAL2 },
601                 { "local3", LOG_LOCAL3 },
602                 { "local4", LOG_LOCAL4 },
603                 { "local5", LOG_LOCAL5 },
604                 { "local6", LOG_LOCAL6 },
605                 { "local7", LOG_LOCAL7 },
606                 { NULL, 0 }
607         };
608
609         for (i = 0; map[i].name; i++) {
610                 if (!strcmp(facility, map[i].name)) {
611                         conf->syslog_facility = map[i].value;
612                         return 0;
613                 }
614         }
615         return -1;
616 }
617
618 static int
619 eal_parse_log_level(const char *level, uint32_t *log_level)
620 {
621         char *end;
622         unsigned long tmp;
623
624         errno = 0;
625         tmp = strtoul(level, &end, 0);
626
627         /* check for errors */
628         if ((errno != 0) || (level[0] == '\0') ||
629             end == NULL || (*end != '\0'))
630                 return -1;
631
632         /* log_level is a uint32_t */
633         if (tmp >= UINT32_MAX)
634                 return -1;
635
636         *log_level = tmp;
637         return 0;
638 }
639
640 static enum rte_proc_type_t
641 eal_parse_proc_type(const char *arg)
642 {
643         if (strncasecmp(arg, "primary", sizeof("primary")) == 0)
644                 return RTE_PROC_PRIMARY;
645         if (strncasecmp(arg, "secondary", sizeof("secondary")) == 0)
646                 return RTE_PROC_SECONDARY;
647         if (strncasecmp(arg, "auto", sizeof("auto")) == 0)
648                 return RTE_PROC_AUTO;
649
650         return RTE_PROC_INVALID;
651 }
652
653 int
654 eal_parse_common_option(int opt, const char *optarg,
655                         struct internal_config *conf)
656 {
657         switch (opt) {
658         /* blacklist */
659         case 'b':
660                 if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
661                                 optarg) < 0) {
662                         return -1;
663                 }
664                 break;
665         /* whitelist */
666         case 'w':
667                 if (rte_eal_devargs_add(RTE_DEVTYPE_WHITELISTED_PCI,
668                                 optarg) < 0) {
669                         return -1;
670                 }
671                 break;
672         /* coremask */
673         case 'c':
674                 if (eal_parse_coremask(optarg) < 0) {
675                         RTE_LOG(ERR, EAL, "invalid coremask\n");
676                         return -1;
677                 }
678                 break;
679         /* corelist */
680         case 'l':
681                 if (eal_parse_corelist(optarg) < 0) {
682                         RTE_LOG(ERR, EAL, "invalid core list\n");
683                         return -1;
684                 }
685                 break;
686         /* size of memory */
687         case 'm':
688                 conf->memory = atoi(optarg);
689                 conf->memory *= 1024ULL;
690                 conf->memory *= 1024ULL;
691                 mem_parsed = 1;
692                 break;
693         /* force number of channels */
694         case 'n':
695                 conf->force_nchannel = atoi(optarg);
696                 if (conf->force_nchannel == 0 ||
697                     conf->force_nchannel > 4) {
698                         RTE_LOG(ERR, EAL, "invalid channel number\n");
699                         return -1;
700                 }
701                 break;
702         /* force number of ranks */
703         case 'r':
704                 conf->force_nrank = atoi(optarg);
705                 if (conf->force_nrank == 0 ||
706                     conf->force_nrank > 16) {
707                         RTE_LOG(ERR, EAL, "invalid rank number\n");
708                         return -1;
709                 }
710                 break;
711         case 'v':
712                 /* since message is explicitly requested by user, we
713                  * write message at highest log level so it can always
714                  * be seen
715                  * even if info or warning messages are disabled */
716                 RTE_LOG(CRIT, EAL, "RTE Version: '%s'\n", rte_version());
717                 break;
718
719         /* long options */
720         case OPT_NO_HUGE_NUM:
721                 conf->no_hugetlbfs = 1;
722                 break;
723
724         case OPT_NO_PCI_NUM:
725                 conf->no_pci = 1;
726                 break;
727
728         case OPT_NO_HPET_NUM:
729                 conf->no_hpet = 1;
730                 break;
731
732         case OPT_VMWARE_TSC_MAP_NUM:
733                 conf->vmware_tsc_map = 1;
734                 break;
735
736         case OPT_NO_SHCONF_NUM:
737                 conf->no_shconf = 1;
738                 break;
739
740         case OPT_PROC_TYPE_NUM:
741                 conf->process_type = eal_parse_proc_type(optarg);
742                 break;
743
744         case OPT_MASTER_LCORE_NUM:
745                 if (eal_parse_master_lcore(optarg) < 0) {
746                         RTE_LOG(ERR, EAL, "invalid parameter for --"
747                                         OPT_MASTER_LCORE "\n");
748                         return -1;
749                 }
750                 break;
751
752         case OPT_VDEV_NUM:
753                 if (rte_eal_devargs_add(RTE_DEVTYPE_VIRTUAL,
754                                 optarg) < 0) {
755                         return -1;
756                 }
757                 break;
758
759         case OPT_SYSLOG_NUM:
760                 if (eal_parse_syslog(optarg, conf) < 0) {
761                         RTE_LOG(ERR, EAL, "invalid parameters for --"
762                                         OPT_SYSLOG "\n");
763                         return -1;
764                 }
765                 break;
766
767         case OPT_LOG_LEVEL_NUM: {
768                 uint32_t log;
769
770                 if (eal_parse_log_level(optarg, &log) < 0) {
771                         RTE_LOG(ERR, EAL,
772                                 "invalid parameters for --"
773                                 OPT_LOG_LEVEL "\n");
774                         return -1;
775                 }
776                 conf->log_level = log;
777                 break;
778         }
779         case OPT_LCORES_NUM:
780                 if (eal_parse_lcores(optarg) < 0) {
781                         RTE_LOG(ERR, EAL, "invalid parameter for --"
782                                 OPT_LCORES "\n");
783                         return -1;
784                 }
785                 break;
786
787         /* don't know what to do, leave this to caller */
788         default:
789                 return 1;
790
791         }
792
793         return 0;
794 }
795
796 int
797 eal_adjust_config(struct internal_config *internal_cfg)
798 {
799         int i;
800         struct rte_config *cfg = rte_eal_get_configuration();
801
802         if (internal_config.process_type == RTE_PROC_AUTO)
803                 internal_config.process_type = eal_proc_type_detect();
804
805         /* default master lcore is the first one */
806         if (!master_lcore_parsed)
807                 cfg->master_lcore = rte_get_next_lcore(-1, 0, 0);
808
809         /* if no memory amounts were requested, this will result in 0 and
810          * will be overridden later, right after eal_hugepage_info_init() */
811         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
812                 internal_cfg->memory += internal_cfg->socket_mem[i];
813
814         return 0;
815 }
816
817 int
818 eal_check_common_options(struct internal_config *internal_cfg)
819 {
820         struct rte_config *cfg = rte_eal_get_configuration();
821
822         if (!lcores_parsed) {
823                 RTE_LOG(ERR, EAL, "CPU cores must be enabled with options "
824                         "-c, -l or --lcores\n");
825                 return -1;
826         }
827         if (cfg->lcore_role[cfg->master_lcore] != ROLE_RTE) {
828                 RTE_LOG(ERR, EAL, "Master lcore is not enabled for DPDK\n");
829                 return -1;
830         }
831
832         if (internal_cfg->process_type == RTE_PROC_INVALID) {
833                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
834                 return -1;
835         }
836         if (internal_cfg->process_type == RTE_PROC_PRIMARY &&
837                         internal_cfg->force_nchannel == 0) {
838                 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not "
839                         "specified\n");
840                 return -1;
841         }
842         if (index(internal_cfg->hugefile_prefix, '%') != NULL) {
843                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in --"OPT_FILE_PREFIX" "
844                         "option\n");
845                 return -1;
846         }
847         if (mem_parsed && internal_cfg->force_sockets == 1) {
848                 RTE_LOG(ERR, EAL, "Options -m and --"OPT_SOCKET_MEM" cannot "
849                         "be specified at the same time\n");
850                 return -1;
851         }
852         if (internal_cfg->no_hugetlbfs &&
853                         (mem_parsed || internal_cfg->force_sockets == 1)) {
854                 RTE_LOG(ERR, EAL, "Options -m or --"OPT_SOCKET_MEM" cannot "
855                         "be specified together with --"OPT_NO_HUGE"\n");
856                 return -1;
857         }
858
859         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) != 0 &&
860                 rte_eal_devargs_type_count(RTE_DEVTYPE_BLACKLISTED_PCI) != 0) {
861                 RTE_LOG(ERR, EAL, "Options blacklist (-b) and whitelist (-w) "
862                         "cannot be used at the same time\n");
863                 return -1;
864         }
865
866         return 0;
867 }
868
869 void
870 eal_common_usage(void)
871 {
872         printf("-c COREMASK|-l CORELIST -n CHANNELS [options]\n\n"
873                "EAL common options:\n"
874                "  -c COREMASK         Hexadecimal bitmask of cores to run on\n"
875                "  -l CORELIST         List of cores to run on\n"
876                "                      The argument format is <c1>[-c2][,c3[-c4],...]\n"
877                "                      where c1, c2, etc are core indexes between 0 and %d\n"
878                "  --"OPT_LCORES" COREMAP    Map lcore set to physical cpu set\n"
879                "                      The argument format is\n"
880                "                            '<lcores[@cpus]>[<,lcores[@cpus]>...]'\n"
881                "                      lcores and cpus list are grouped by '(' and ')'\n"
882                "                      Within the group, '-' is used for range separator,\n"
883                "                      ',' is used for single number separator.\n"
884                "                      '( )' can be omitted for single element group,\n"
885                "                      '@' can be omitted if cpus and lcores have the same value\n"
886                "  --"OPT_MASTER_LCORE" ID   Core ID that is used as master\n"
887                "  -n CHANNELS         Number of memory channels\n"
888                "  -m MB               Memory to allocate (see also --"OPT_SOCKET_MEM")\n"
889                "  -r RANKS            Force number of memory ranks (don't detect)\n"
890                "  -b, --"OPT_PCI_BLACKLIST" Add a PCI device in black list.\n"
891                "                      Prevent EAL from using this PCI device. The argument\n"
892                "                      format is <domain:bus:devid.func>.\n"
893                "  -w, --"OPT_PCI_WHITELIST" Add a PCI device in white list.\n"
894                "                      Only use the specified PCI devices. The argument format\n"
895                "                      is <[domain:]bus:devid.func>. This option can be present\n"
896                "                      several times (once per device).\n"
897                "                      [NOTE: PCI whitelist cannot be used with -b option]\n"
898                "  --"OPT_VDEV"              Add a virtual device.\n"
899                "                      The argument format is <driver><id>[,key=val,...]\n"
900                "                      (ex: --vdev=eth_pcap0,iface=eth2).\n"
901                "  --"OPT_VMWARE_TSC_MAP"    Use VMware TSC map instead of native RDTSC\n"
902                "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
903                "  --"OPT_SYSLOG"            Set syslog facility\n"
904                "  --"OPT_LOG_LEVEL"         Set default log level\n"
905                "  -v                  Display version information on startup\n"
906                "  -h, --help          This help\n"
907                "\nEAL options for DEBUG use only:\n"
908                "  --"OPT_NO_HUGE"           Use malloc instead of hugetlbfs\n"
909                "  --"OPT_NO_PCI"            Disable PCI\n"
910                "  --"OPT_NO_HPET"           Disable HPET\n"
911                "  --"OPT_NO_SHCONF"         No shared config (mmap'd files)\n"
912                "\n", RTE_MAX_LCORE);
913 }