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