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