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