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