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