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