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