trace: add trace directory configuration parameter
[dpdk.git] / lib / librte_eal / common / eal_common_options.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright(c) 2014 6WIND S.A.
4  */
5
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #ifndef RTE_EXEC_ENV_WINDOWS
10 #include <syslog.h>
11 #endif
12 #include <ctype.h>
13 #include <limits.h>
14 #include <errno.h>
15 #include <getopt.h>
16 #ifndef RTE_EXEC_ENV_WINDOWS
17 #include <dlfcn.h>
18 #endif
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <dirent.h>
22
23 #include <rte_string_fns.h>
24 #include <rte_eal.h>
25 #include <rte_log.h>
26 #include <rte_lcore.h>
27 #include <rte_memory.h>
28 #include <rte_tailq.h>
29 #include <rte_version.h>
30 #include <rte_devargs.h>
31 #include <rte_memcpy.h>
32
33 #include "eal_internal_cfg.h"
34 #include "eal_options.h"
35 #include "eal_filesystem.h"
36 #include "eal_private.h"
37 #include "eal_trace.h"
38
39 #define BITS_PER_HEX 4
40 #define LCORE_OPT_LST 1
41 #define LCORE_OPT_MSK 2
42 #define LCORE_OPT_MAP 3
43
44 const char
45 eal_short_options[] =
46         "b:" /* pci-blacklist */
47         "c:" /* coremask */
48         "s:" /* service coremask */
49         "d:" /* driver */
50         "h"  /* help */
51         "l:" /* corelist */
52         "S:" /* service corelist */
53         "m:" /* memory size */
54         "n:" /* memory channels */
55         "r:" /* memory ranks */
56         "v"  /* version */
57         "w:" /* pci-whitelist */
58         ;
59
60 const struct option
61 eal_long_options[] = {
62         {OPT_BASE_VIRTADDR,     1, NULL, OPT_BASE_VIRTADDR_NUM    },
63         {OPT_CREATE_UIO_DEV,    0, NULL, OPT_CREATE_UIO_DEV_NUM   },
64         {OPT_FILE_PREFIX,       1, NULL, OPT_FILE_PREFIX_NUM      },
65         {OPT_HELP,              0, NULL, OPT_HELP_NUM             },
66         {OPT_HUGE_DIR,          1, NULL, OPT_HUGE_DIR_NUM         },
67         {OPT_HUGE_UNLINK,       0, NULL, OPT_HUGE_UNLINK_NUM      },
68         {OPT_IOVA_MODE,         1, NULL, OPT_IOVA_MODE_NUM        },
69         {OPT_LCORES,            1, NULL, OPT_LCORES_NUM           },
70         {OPT_LOG_LEVEL,         1, NULL, OPT_LOG_LEVEL_NUM        },
71         {OPT_TRACE,             1, NULL, OPT_TRACE_NUM            },
72         {OPT_TRACE_DIR,         1, NULL, OPT_TRACE_DIR_NUM        },
73         {OPT_MASTER_LCORE,      1, NULL, OPT_MASTER_LCORE_NUM     },
74         {OPT_MBUF_POOL_OPS_NAME, 1, NULL, OPT_MBUF_POOL_OPS_NAME_NUM},
75         {OPT_NO_HPET,           0, NULL, OPT_NO_HPET_NUM          },
76         {OPT_NO_HUGE,           0, NULL, OPT_NO_HUGE_NUM          },
77         {OPT_NO_PCI,            0, NULL, OPT_NO_PCI_NUM           },
78         {OPT_NO_SHCONF,         0, NULL, OPT_NO_SHCONF_NUM        },
79         {OPT_IN_MEMORY,         0, NULL, OPT_IN_MEMORY_NUM        },
80         {OPT_PCI_BLACKLIST,     1, NULL, OPT_PCI_BLACKLIST_NUM    },
81         {OPT_PCI_WHITELIST,     1, NULL, OPT_PCI_WHITELIST_NUM    },
82         {OPT_PROC_TYPE,         1, NULL, OPT_PROC_TYPE_NUM        },
83         {OPT_SOCKET_MEM,        1, NULL, OPT_SOCKET_MEM_NUM       },
84         {OPT_SOCKET_LIMIT,      1, NULL, OPT_SOCKET_LIMIT_NUM     },
85         {OPT_SYSLOG,            1, NULL, OPT_SYSLOG_NUM           },
86         {OPT_VDEV,              1, NULL, OPT_VDEV_NUM             },
87         {OPT_VFIO_INTR,         1, NULL, OPT_VFIO_INTR_NUM        },
88         {OPT_VMWARE_TSC_MAP,    0, NULL, OPT_VMWARE_TSC_MAP_NUM   },
89         {OPT_LEGACY_MEM,        0, NULL, OPT_LEGACY_MEM_NUM       },
90         {OPT_SINGLE_FILE_SEGMENTS, 0, NULL, OPT_SINGLE_FILE_SEGMENTS_NUM},
91         {OPT_MATCH_ALLOCATIONS, 0, NULL, OPT_MATCH_ALLOCATIONS_NUM},
92         {0,                     0, NULL, 0                        }
93 };
94
95 TAILQ_HEAD(shared_driver_list, shared_driver);
96
97 /* Definition for shared object drivers. */
98 struct shared_driver {
99         TAILQ_ENTRY(shared_driver) next;
100
101         char    name[PATH_MAX];
102         void*   lib_handle;
103 };
104
105 /* List of external loadable drivers */
106 static struct shared_driver_list solib_list =
107 TAILQ_HEAD_INITIALIZER(solib_list);
108
109 /* Default path of external loadable drivers */
110 static const char *default_solib_dir = RTE_EAL_PMD_PATH;
111
112 /*
113  * Stringified version of solib path used by dpdk-pmdinfo.py
114  * Note: PLEASE DO NOT ALTER THIS without making a corresponding
115  * change to usertools/dpdk-pmdinfo.py
116  */
117 static const char dpdk_solib_path[] __rte_used =
118 "DPDK_PLUGIN_PATH=" RTE_EAL_PMD_PATH;
119
120 TAILQ_HEAD(device_option_list, device_option);
121
122 struct device_option {
123         TAILQ_ENTRY(device_option) next;
124
125         enum rte_devtype type;
126         char arg[];
127 };
128
129 static struct device_option_list devopt_list =
130 TAILQ_HEAD_INITIALIZER(devopt_list);
131
132 static int master_lcore_parsed;
133 static int mem_parsed;
134 static int core_parsed;
135
136 static int
137 eal_option_device_add(enum rte_devtype type, const char *optarg)
138 {
139         struct device_option *devopt;
140         size_t optlen;
141         int ret;
142
143         optlen = strlen(optarg) + 1;
144         devopt = calloc(1, sizeof(*devopt) + optlen);
145         if (devopt == NULL) {
146                 RTE_LOG(ERR, EAL, "Unable to allocate device option\n");
147                 return -ENOMEM;
148         }
149
150         devopt->type = type;
151         ret = strlcpy(devopt->arg, optarg, optlen);
152         if (ret < 0) {
153                 RTE_LOG(ERR, EAL, "Unable to copy device option\n");
154                 free(devopt);
155                 return -EINVAL;
156         }
157         TAILQ_INSERT_TAIL(&devopt_list, devopt, next);
158         return 0;
159 }
160
161 int
162 eal_option_device_parse(void)
163 {
164         struct device_option *devopt;
165         void *tmp;
166         int ret = 0;
167
168         TAILQ_FOREACH_SAFE(devopt, &devopt_list, next, tmp) {
169                 if (ret == 0) {
170                         ret = rte_devargs_add(devopt->type, devopt->arg);
171                         if (ret)
172                                 RTE_LOG(ERR, EAL, "Unable to parse device '%s'\n",
173                                         devopt->arg);
174                 }
175                 TAILQ_REMOVE(&devopt_list, devopt, next);
176                 free(devopt);
177         }
178         return ret;
179 }
180
181 const char *
182 eal_get_hugefile_prefix(void)
183 {
184         if (internal_config.hugefile_prefix != NULL)
185                 return internal_config.hugefile_prefix;
186         return HUGEFILE_PREFIX_DEFAULT;
187 }
188
189 void
190 eal_reset_internal_config(struct internal_config *internal_cfg)
191 {
192         int i;
193
194         internal_cfg->memory = 0;
195         internal_cfg->force_nrank = 0;
196         internal_cfg->force_nchannel = 0;
197         internal_cfg->hugefile_prefix = NULL;
198         internal_cfg->hugepage_dir = NULL;
199         internal_cfg->force_sockets = 0;
200         /* zero out the NUMA config */
201         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
202                 internal_cfg->socket_mem[i] = 0;
203         internal_cfg->force_socket_limits = 0;
204         /* zero out the NUMA limits config */
205         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
206                 internal_cfg->socket_limit[i] = 0;
207         /* zero out hugedir descriptors */
208         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++) {
209                 memset(&internal_cfg->hugepage_info[i], 0,
210                                 sizeof(internal_cfg->hugepage_info[0]));
211                 internal_cfg->hugepage_info[i].lock_descriptor = -1;
212         }
213         internal_cfg->base_virtaddr = 0;
214
215 #ifdef LOG_DAEMON
216         internal_cfg->syslog_facility = LOG_DAEMON;
217 #endif
218
219         /* if set to NONE, interrupt mode is determined automatically */
220         internal_cfg->vfio_intr_mode = RTE_INTR_MODE_NONE;
221
222 #ifdef RTE_LIBEAL_USE_HPET
223         internal_cfg->no_hpet = 0;
224 #else
225         internal_cfg->no_hpet = 1;
226 #endif
227         internal_cfg->vmware_tsc_map = 0;
228         internal_cfg->create_uio_dev = 0;
229         internal_cfg->iova_mode = RTE_IOVA_DC;
230         internal_cfg->user_mbuf_pool_ops_name = NULL;
231         CPU_ZERO(&internal_cfg->ctrl_cpuset);
232         internal_cfg->init_complete = 0;
233 }
234
235 static int
236 eal_plugin_add(const char *path)
237 {
238         struct shared_driver *solib;
239
240         solib = malloc(sizeof(*solib));
241         if (solib == NULL) {
242                 RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
243                 return -1;
244         }
245         memset(solib, 0, sizeof(*solib));
246         strlcpy(solib->name, path, PATH_MAX-1);
247         solib->name[PATH_MAX-1] = 0;
248         TAILQ_INSERT_TAIL(&solib_list, solib, next);
249
250         return 0;
251 }
252
253 static int
254 eal_plugindir_init(const char *path)
255 {
256         DIR *d = NULL;
257         struct dirent *dent = NULL;
258         char sopath[PATH_MAX];
259
260         if (path == NULL || *path == '\0')
261                 return 0;
262
263         d = opendir(path);
264         if (d == NULL) {
265                 RTE_LOG(ERR, EAL, "failed to open directory %s: %s\n",
266                         path, strerror(errno));
267                 return -1;
268         }
269
270         while ((dent = readdir(d)) != NULL) {
271                 struct stat sb;
272
273                 snprintf(sopath, sizeof(sopath), "%s/%s", path, dent->d_name);
274
275                 if (!(stat(sopath, &sb) == 0 && S_ISREG(sb.st_mode)))
276                         continue;
277
278                 if (eal_plugin_add(sopath) == -1)
279                         break;
280         }
281
282         closedir(d);
283         /* XXX this ignores failures from readdir() itself */
284         return (dent == NULL) ? 0 : -1;
285 }
286
287 int
288 eal_plugins_init(void)
289 {
290 #ifndef RTE_EXEC_ENV_WINDOWS
291         struct shared_driver *solib = NULL;
292         struct stat sb;
293
294         if (*default_solib_dir != '\0' && stat(default_solib_dir, &sb) == 0 &&
295                                 S_ISDIR(sb.st_mode))
296                 eal_plugin_add(default_solib_dir);
297
298         TAILQ_FOREACH(solib, &solib_list, next) {
299
300                 if (stat(solib->name, &sb) == 0 && S_ISDIR(sb.st_mode)) {
301                         if (eal_plugindir_init(solib->name) == -1) {
302                                 RTE_LOG(ERR, EAL,
303                                         "Cannot init plugin directory %s\n",
304                                         solib->name);
305                                 return -1;
306                         }
307                 } else {
308                         RTE_LOG(DEBUG, EAL, "open shared lib %s\n",
309                                 solib->name);
310                         solib->lib_handle = dlopen(solib->name, RTLD_NOW);
311                         if (solib->lib_handle == NULL) {
312                                 RTE_LOG(ERR, EAL, "%s\n", dlerror());
313                                 return -1;
314                         }
315                 }
316
317         }
318         return 0;
319 #endif
320 }
321
322 /*
323  * Parse the coremask given as argument (hexadecimal string) and fill
324  * the global configuration (core role and core count) with the parsed
325  * value.
326  */
327 static int xdigit2val(unsigned char c)
328 {
329         int val;
330
331         if (isdigit(c))
332                 val = c - '0';
333         else if (isupper(c))
334                 val = c - 'A' + 10;
335         else
336                 val = c - 'a' + 10;
337         return val;
338 }
339
340 static int
341 eal_parse_service_coremask(const char *coremask)
342 {
343         struct rte_config *cfg = rte_eal_get_configuration();
344         int i, j, idx = 0;
345         unsigned int count = 0;
346         char c;
347         int val;
348         uint32_t taken_lcore_count = 0;
349
350         if (coremask == NULL)
351                 return -1;
352         /* Remove all blank characters ahead and after .
353          * Remove 0x/0X if exists.
354          */
355         while (isblank(*coremask))
356                 coremask++;
357         if (coremask[0] == '0' && ((coremask[1] == 'x')
358                 || (coremask[1] == 'X')))
359                 coremask += 2;
360         i = strlen(coremask);
361         while ((i > 0) && isblank(coremask[i - 1]))
362                 i--;
363
364         if (i == 0)
365                 return -1;
366
367         for (i = i - 1; i >= 0 && idx < RTE_MAX_LCORE; i--) {
368                 c = coremask[i];
369                 if (isxdigit(c) == 0) {
370                         /* invalid characters */
371                         return -1;
372                 }
373                 val = xdigit2val(c);
374                 for (j = 0; j < BITS_PER_HEX && idx < RTE_MAX_LCORE;
375                                 j++, idx++) {
376                         if ((1 << j) & val) {
377                                 /* handle master lcore already parsed */
378                                 uint32_t lcore = idx;
379                                 if (master_lcore_parsed &&
380                                                 cfg->master_lcore == lcore) {
381                                         RTE_LOG(ERR, EAL,
382                                                 "lcore %u is master lcore, cannot use as service core\n",
383                                                 idx);
384                                         return -1;
385                                 }
386
387                                 if (eal_cpu_detected(idx) == 0) {
388                                         RTE_LOG(ERR, EAL,
389                                                 "lcore %u unavailable\n", idx);
390                                         return -1;
391                                 }
392
393                                 if (cfg->lcore_role[idx] == ROLE_RTE)
394                                         taken_lcore_count++;
395
396                                 lcore_config[idx].core_role = ROLE_SERVICE;
397                                 count++;
398                         }
399                 }
400         }
401
402         for (; i >= 0; i--)
403                 if (coremask[i] != '0')
404                         return -1;
405
406         for (; idx < RTE_MAX_LCORE; idx++)
407                 lcore_config[idx].core_index = -1;
408
409         if (count == 0)
410                 return -1;
411
412         if (core_parsed && taken_lcore_count != count) {
413                 RTE_LOG(WARNING, EAL,
414                         "Not all service cores are in the coremask. "
415                         "Please ensure -c or -l includes service cores\n");
416         }
417
418         cfg->service_lcore_count = count;
419         return 0;
420 }
421
422 static int
423 eal_service_cores_parsed(void)
424 {
425         int idx;
426         for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
427                 if (lcore_config[idx].core_role == ROLE_SERVICE)
428                         return 1;
429         }
430         return 0;
431 }
432
433 static int
434 update_lcore_config(int *cores)
435 {
436         struct rte_config *cfg = rte_eal_get_configuration();
437         unsigned int count = 0;
438         unsigned int i;
439         int ret = 0;
440
441         for (i = 0; i < RTE_MAX_LCORE; i++) {
442                 if (cores[i] != -1) {
443                         if (eal_cpu_detected(i) == 0) {
444                                 RTE_LOG(ERR, EAL, "lcore %u unavailable\n", i);
445                                 ret = -1;
446                                 continue;
447                         }
448                         cfg->lcore_role[i] = ROLE_RTE;
449                         count++;
450                 } else {
451                         cfg->lcore_role[i] = ROLE_OFF;
452                 }
453                 lcore_config[i].core_index = cores[i];
454         }
455         if (!ret)
456                 cfg->lcore_count = count;
457         return ret;
458 }
459
460 static int
461 eal_parse_coremask(const char *coremask, int *cores)
462 {
463         unsigned count = 0;
464         int i, j, idx;
465         int val;
466         char c;
467
468         for (idx = 0; idx < RTE_MAX_LCORE; idx++)
469                 cores[idx] = -1;
470         idx = 0;
471
472         /* Remove all blank characters ahead and after .
473          * Remove 0x/0X if exists.
474          */
475         while (isblank(*coremask))
476                 coremask++;
477         if (coremask[0] == '0' && ((coremask[1] == 'x')
478                 || (coremask[1] == 'X')))
479                 coremask += 2;
480         i = strlen(coremask);
481         while ((i > 0) && isblank(coremask[i - 1]))
482                 i--;
483         if (i == 0)
484                 return -1;
485
486         for (i = i - 1; i >= 0 && idx < RTE_MAX_LCORE; i--) {
487                 c = coremask[i];
488                 if (isxdigit(c) == 0) {
489                         /* invalid characters */
490                         return -1;
491                 }
492                 val = xdigit2val(c);
493                 for (j = 0; j < BITS_PER_HEX && idx < RTE_MAX_LCORE; j++, idx++)
494                 {
495                         if ((1 << j) & val) {
496                                 cores[idx] = count;
497                                 count++;
498                         }
499                 }
500         }
501         for (; i >= 0; i--)
502                 if (coremask[i] != '0')
503                         return -1;
504         if (count == 0)
505                 return -1;
506         return 0;
507 }
508
509 static int
510 eal_parse_service_corelist(const char *corelist)
511 {
512         struct rte_config *cfg = rte_eal_get_configuration();
513         int i, idx = 0;
514         unsigned count = 0;
515         char *end = NULL;
516         int min, max;
517         uint32_t taken_lcore_count = 0;
518
519         if (corelist == NULL)
520                 return -1;
521
522         /* Remove all blank characters ahead and after */
523         while (isblank(*corelist))
524                 corelist++;
525         i = strlen(corelist);
526         while ((i > 0) && isblank(corelist[i - 1]))
527                 i--;
528
529         /* Get list of cores */
530         min = RTE_MAX_LCORE;
531         do {
532                 while (isblank(*corelist))
533                         corelist++;
534                 if (*corelist == '\0')
535                         return -1;
536                 errno = 0;
537                 idx = strtoul(corelist, &end, 10);
538                 if (errno || end == NULL)
539                         return -1;
540                 while (isblank(*end))
541                         end++;
542                 if (*end == '-') {
543                         min = idx;
544                 } else if ((*end == ',') || (*end == '\0')) {
545                         max = idx;
546                         if (min == RTE_MAX_LCORE)
547                                 min = idx;
548                         for (idx = min; idx <= max; idx++) {
549                                 if (cfg->lcore_role[idx] != ROLE_SERVICE) {
550                                         /* handle master lcore already parsed */
551                                         uint32_t lcore = idx;
552                                         if (cfg->master_lcore == lcore &&
553                                                         master_lcore_parsed) {
554                                                 RTE_LOG(ERR, EAL,
555                                                         "Error: lcore %u is master lcore, cannot use as service core\n",
556                                                         idx);
557                                                 return -1;
558                                         }
559                                         if (cfg->lcore_role[idx] == ROLE_RTE)
560                                                 taken_lcore_count++;
561
562                                         lcore_config[idx].core_role =
563                                                         ROLE_SERVICE;
564                                         count++;
565                                 }
566                         }
567                         min = RTE_MAX_LCORE;
568                 } else
569                         return -1;
570                 corelist = end + 1;
571         } while (*end != '\0');
572
573         if (count == 0)
574                 return -1;
575
576         if (core_parsed && taken_lcore_count != count) {
577                 RTE_LOG(WARNING, EAL,
578                         "Not all service cores were in the coremask. "
579                         "Please ensure -c or -l includes service cores\n");
580         }
581
582         return 0;
583 }
584
585 static int
586 eal_parse_corelist(const char *corelist, int *cores)
587 {
588         unsigned count = 0;
589         char *end = NULL;
590         int min, max;
591         int idx;
592
593         for (idx = 0; idx < RTE_MAX_LCORE; idx++)
594                 cores[idx] = -1;
595
596         /* Remove all blank characters ahead */
597         while (isblank(*corelist))
598                 corelist++;
599
600         /* Get list of cores */
601         min = RTE_MAX_LCORE;
602         do {
603                 while (isblank(*corelist))
604                         corelist++;
605                 if (*corelist == '\0')
606                         return -1;
607                 errno = 0;
608                 idx = strtol(corelist, &end, 10);
609                 if (errno || end == NULL)
610                         return -1;
611                 if (idx < 0 || idx >= RTE_MAX_LCORE)
612                         return -1;
613                 while (isblank(*end))
614                         end++;
615                 if (*end == '-') {
616                         min = idx;
617                 } else if ((*end == ',') || (*end == '\0')) {
618                         max = idx;
619                         if (min == RTE_MAX_LCORE)
620                                 min = idx;
621                         for (idx = min; idx <= max; idx++) {
622                                 if (cores[idx] == -1) {
623                                         cores[idx] = count;
624                                         count++;
625                                 }
626                         }
627                         min = RTE_MAX_LCORE;
628                 } else
629                         return -1;
630                 corelist = end + 1;
631         } while (*end != '\0');
632
633         if (count == 0)
634                 return -1;
635         return 0;
636 }
637
638 /* Changes the lcore id of the master thread */
639 static int
640 eal_parse_master_lcore(const char *arg)
641 {
642         char *parsing_end;
643         struct rte_config *cfg = rte_eal_get_configuration();
644
645         errno = 0;
646         cfg->master_lcore = (uint32_t) strtol(arg, &parsing_end, 0);
647         if (errno || parsing_end[0] != 0)
648                 return -1;
649         if (cfg->master_lcore >= RTE_MAX_LCORE)
650                 return -1;
651         master_lcore_parsed = 1;
652
653         /* ensure master core is not used as service core */
654         if (lcore_config[cfg->master_lcore].core_role == ROLE_SERVICE) {
655                 RTE_LOG(ERR, EAL,
656                         "Error: Master lcore is used as a service core\n");
657                 return -1;
658         }
659
660         return 0;
661 }
662
663 /*
664  * Parse elem, the elem could be single number/range or '(' ')' group
665  * 1) A single number elem, it's just a simple digit. e.g. 9
666  * 2) A single range elem, two digits with a '-' between. e.g. 2-6
667  * 3) A group elem, combines multiple 1) or 2) with '( )'. e.g (0,2-4,6)
668  *    Within group elem, '-' used for a range separator;
669  *                       ',' used for a single number.
670  */
671 static int
672 eal_parse_set(const char *input, rte_cpuset_t *set)
673 {
674         unsigned idx;
675         const char *str = input;
676         char *end = NULL;
677         unsigned min, max;
678
679         CPU_ZERO(set);
680
681         while (isblank(*str))
682                 str++;
683
684         /* only digit or left bracket is qualify for start point */
685         if ((!isdigit(*str) && *str != '(') || *str == '\0')
686                 return -1;
687
688         /* process single number or single range of number */
689         if (*str != '(') {
690                 errno = 0;
691                 idx = strtoul(str, &end, 10);
692                 if (errno || end == NULL || idx >= CPU_SETSIZE)
693                         return -1;
694                 else {
695                         while (isblank(*end))
696                                 end++;
697
698                         min = idx;
699                         max = idx;
700                         if (*end == '-') {
701                                 /* process single <number>-<number> */
702                                 end++;
703                                 while (isblank(*end))
704                                         end++;
705                                 if (!isdigit(*end))
706                                         return -1;
707
708                                 errno = 0;
709                                 idx = strtoul(end, &end, 10);
710                                 if (errno || end == NULL || idx >= CPU_SETSIZE)
711                                         return -1;
712                                 max = idx;
713                                 while (isblank(*end))
714                                         end++;
715                                 if (*end != ',' && *end != '\0')
716                                         return -1;
717                         }
718
719                         if (*end != ',' && *end != '\0' &&
720                             *end != '@')
721                                 return -1;
722
723                         for (idx = RTE_MIN(min, max);
724                              idx <= RTE_MAX(min, max); idx++)
725                                 CPU_SET(idx, set);
726
727                         return end - input;
728                 }
729         }
730
731         /* process set within bracket */
732         str++;
733         while (isblank(*str))
734                 str++;
735         if (*str == '\0')
736                 return -1;
737
738         min = RTE_MAX_LCORE;
739         do {
740
741                 /* go ahead to the first digit */
742                 while (isblank(*str))
743                         str++;
744                 if (!isdigit(*str))
745                         return -1;
746
747                 /* get the digit value */
748                 errno = 0;
749                 idx = strtoul(str, &end, 10);
750                 if (errno || end == NULL || idx >= CPU_SETSIZE)
751                         return -1;
752
753                 /* go ahead to separator '-',',' and ')' */
754                 while (isblank(*end))
755                         end++;
756                 if (*end == '-') {
757                         if (min == RTE_MAX_LCORE)
758                                 min = idx;
759                         else /* avoid continuous '-' */
760                                 return -1;
761                 } else if ((*end == ',') || (*end == ')')) {
762                         max = idx;
763                         if (min == RTE_MAX_LCORE)
764                                 min = idx;
765                         for (idx = RTE_MIN(min, max);
766                              idx <= RTE_MAX(min, max); idx++)
767                                 CPU_SET(idx, set);
768
769                         min = RTE_MAX_LCORE;
770                 } else
771                         return -1;
772
773                 str = end + 1;
774         } while (*end != '\0' && *end != ')');
775
776         /*
777          * to avoid failure that tail blank makes end character check fail
778          * in eal_parse_lcores( )
779          */
780         while (isblank(*str))
781                 str++;
782
783         return str - input;
784 }
785
786 static int
787 check_cpuset(rte_cpuset_t *set)
788 {
789         unsigned int idx;
790
791         for (idx = 0; idx < CPU_SETSIZE; idx++) {
792                 if (!CPU_ISSET(idx, set))
793                         continue;
794
795                 if (eal_cpu_detected(idx) == 0) {
796                         RTE_LOG(ERR, EAL, "core %u "
797                                 "unavailable\n", idx);
798                         return -1;
799                 }
800         }
801         return 0;
802 }
803
804 /*
805  * The format pattern: --lcores='<lcores[@cpus]>[<,lcores[@cpus]>...]'
806  * lcores, cpus could be a single digit/range or a group.
807  * '(' and ')' are necessary if it's a group.
808  * If not supply '@cpus', the value of cpus uses the same as lcores.
809  * e.g. '1,2@(5-7),(3-5)@(0,2),(0,6),7-8' means start 9 EAL thread as below
810  *   lcore 0 runs on cpuset 0x41 (cpu 0,6)
811  *   lcore 1 runs on cpuset 0x2 (cpu 1)
812  *   lcore 2 runs on cpuset 0xe0 (cpu 5,6,7)
813  *   lcore 3,4,5 runs on cpuset 0x5 (cpu 0,2)
814  *   lcore 6 runs on cpuset 0x41 (cpu 0,6)
815  *   lcore 7 runs on cpuset 0x80 (cpu 7)
816  *   lcore 8 runs on cpuset 0x100 (cpu 8)
817  */
818 static int
819 eal_parse_lcores(const char *lcores)
820 {
821         struct rte_config *cfg = rte_eal_get_configuration();
822         rte_cpuset_t lcore_set;
823         unsigned int set_count;
824         unsigned idx = 0;
825         unsigned count = 0;
826         const char *lcore_start = NULL;
827         const char *end = NULL;
828         int offset;
829         rte_cpuset_t cpuset;
830         int lflags;
831         int ret = -1;
832
833         if (lcores == NULL)
834                 return -1;
835
836         /* Remove all blank characters ahead and after */
837         while (isblank(*lcores))
838                 lcores++;
839
840         CPU_ZERO(&cpuset);
841
842         /* Reset lcore config */
843         for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
844                 cfg->lcore_role[idx] = ROLE_OFF;
845                 lcore_config[idx].core_index = -1;
846                 CPU_ZERO(&lcore_config[idx].cpuset);
847         }
848
849         /* Get list of cores */
850         do {
851                 while (isblank(*lcores))
852                         lcores++;
853                 if (*lcores == '\0')
854                         goto err;
855
856                 lflags = 0;
857
858                 /* record lcore_set start point */
859                 lcore_start = lcores;
860
861                 /* go across a complete bracket */
862                 if (*lcore_start == '(') {
863                         lcores += strcspn(lcores, ")");
864                         if (*lcores++ == '\0')
865                                 goto err;
866                 }
867
868                 /* scan the separator '@', ','(next) or '\0'(finish) */
869                 lcores += strcspn(lcores, "@,");
870
871                 if (*lcores == '@') {
872                         /* explicit assign cpuset and update the end cursor */
873                         offset = eal_parse_set(lcores + 1, &cpuset);
874                         if (offset < 0)
875                                 goto err;
876                         end = lcores + 1 + offset;
877                 } else { /* ',' or '\0' */
878                         /* haven't given cpuset, current loop done */
879                         end = lcores;
880
881                         /* go back to check <number>-<number> */
882                         offset = strcspn(lcore_start, "(-");
883                         if (offset < (end - lcore_start) &&
884                             *(lcore_start + offset) != '(')
885                                 lflags = 1;
886                 }
887
888                 if (*end != ',' && *end != '\0')
889                         goto err;
890
891                 /* parse lcore_set from start point */
892                 if (eal_parse_set(lcore_start, &lcore_set) < 0)
893                         goto err;
894
895                 /* without '@', by default using lcore_set as cpuset */
896                 if (*lcores != '@')
897                         rte_memcpy(&cpuset, &lcore_set, sizeof(cpuset));
898
899                 set_count = CPU_COUNT(&lcore_set);
900                 /* start to update lcore_set */
901                 for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
902                         if (!CPU_ISSET(idx, &lcore_set))
903                                 continue;
904                         set_count--;
905
906                         if (cfg->lcore_role[idx] != ROLE_RTE) {
907                                 lcore_config[idx].core_index = count;
908                                 cfg->lcore_role[idx] = ROLE_RTE;
909                                 count++;
910                         }
911
912                         if (lflags) {
913                                 CPU_ZERO(&cpuset);
914                                 CPU_SET(idx, &cpuset);
915                         }
916
917                         if (check_cpuset(&cpuset) < 0)
918                                 goto err;
919                         rte_memcpy(&lcore_config[idx].cpuset, &cpuset,
920                                    sizeof(rte_cpuset_t));
921                 }
922
923                 /* some cores from the lcore_set can't be handled by EAL */
924                 if (set_count != 0)
925                         goto err;
926
927                 lcores = end + 1;
928         } while (*end != '\0');
929
930         if (count == 0)
931                 goto err;
932
933         cfg->lcore_count = count;
934         ret = 0;
935
936 err:
937
938         return ret;
939 }
940
941 #ifndef RTE_EXEC_ENV_WINDOWS
942 static int
943 eal_parse_syslog(const char *facility, struct internal_config *conf)
944 {
945         int i;
946         static const struct {
947                 const char *name;
948                 int value;
949         } map[] = {
950                 { "auth", LOG_AUTH },
951                 { "cron", LOG_CRON },
952                 { "daemon", LOG_DAEMON },
953                 { "ftp", LOG_FTP },
954                 { "kern", LOG_KERN },
955                 { "lpr", LOG_LPR },
956                 { "mail", LOG_MAIL },
957                 { "news", LOG_NEWS },
958                 { "syslog", LOG_SYSLOG },
959                 { "user", LOG_USER },
960                 { "uucp", LOG_UUCP },
961                 { "local0", LOG_LOCAL0 },
962                 { "local1", LOG_LOCAL1 },
963                 { "local2", LOG_LOCAL2 },
964                 { "local3", LOG_LOCAL3 },
965                 { "local4", LOG_LOCAL4 },
966                 { "local5", LOG_LOCAL5 },
967                 { "local6", LOG_LOCAL6 },
968                 { "local7", LOG_LOCAL7 },
969                 { NULL, 0 }
970         };
971
972         for (i = 0; map[i].name; i++) {
973                 if (!strcmp(facility, map[i].name)) {
974                         conf->syslog_facility = map[i].value;
975                         return 0;
976                 }
977         }
978         return -1;
979 }
980 #endif
981
982 static int
983 eal_parse_log_priority(const char *level)
984 {
985         static const char * const levels[] = {
986                 [RTE_LOG_EMERG]   = "emergency",
987                 [RTE_LOG_ALERT]   = "alert",
988                 [RTE_LOG_CRIT]    = "critical",
989                 [RTE_LOG_ERR]     = "error",
990                 [RTE_LOG_WARNING] = "warning",
991                 [RTE_LOG_NOTICE]  = "notice",
992                 [RTE_LOG_INFO]    = "info",
993                 [RTE_LOG_DEBUG]   = "debug",
994         };
995         size_t len = strlen(level);
996         unsigned long tmp;
997         char *end;
998         unsigned int i;
999
1000         if (len == 0)
1001                 return -1;
1002
1003         /* look for named values, skip 0 which is not a valid level */
1004         for (i = 1; i < RTE_DIM(levels); i++) {
1005                 if (strncmp(levels[i], level, len) == 0)
1006                         return i;
1007         }
1008
1009         /* not a string, maybe it is numeric */
1010         errno = 0;
1011         tmp = strtoul(level, &end, 0);
1012
1013         /* check for errors */
1014         if (errno != 0 || end == NULL || *end != '\0' ||
1015             tmp >= UINT32_MAX)
1016                 return -1;
1017
1018         return tmp;
1019 }
1020
1021 static int
1022 eal_parse_log_level(const char *arg)
1023 {
1024         const char *pattern = NULL;
1025         const char *regex = NULL;
1026         char *str, *level;
1027         int priority;
1028
1029         str = strdup(arg);
1030         if (str == NULL)
1031                 return -1;
1032
1033         if ((level = strchr(str, ','))) {
1034                 regex = str;
1035                 *level++ = '\0';
1036         } else if ((level = strchr(str, ':'))) {
1037                 pattern = str;
1038                 *level++ = '\0';
1039         } else {
1040                 level = str;
1041         }
1042
1043         priority = eal_parse_log_priority(level);
1044         if (priority < 0) {
1045                 fprintf(stderr, "invalid log priority: %s\n", level);
1046                 goto fail;
1047         }
1048
1049         if (regex) {
1050                 if (rte_log_set_level_regexp(regex, priority) < 0) {
1051                         fprintf(stderr, "cannot set log level %s,%d\n",
1052                                 regex, priority);
1053                         goto fail;
1054                 }
1055                 if (rte_log_save_regexp(regex, priority) < 0)
1056                         goto fail;
1057         } else if (pattern) {
1058                 if (rte_log_set_level_pattern(pattern, priority) < 0) {
1059                         fprintf(stderr, "cannot set log level %s:%d\n",
1060                                 pattern, priority);
1061                         goto fail;
1062                 }
1063                 if (rte_log_save_pattern(pattern, priority) < 0)
1064                         goto fail;
1065         } else {
1066                 rte_log_set_global_level(priority);
1067         }
1068
1069         free(str);
1070         return 0;
1071
1072 fail:
1073         free(str);
1074         return -1;
1075 }
1076
1077 static enum rte_proc_type_t
1078 eal_parse_proc_type(const char *arg)
1079 {
1080         if (strncasecmp(arg, "primary", sizeof("primary")) == 0)
1081                 return RTE_PROC_PRIMARY;
1082         if (strncasecmp(arg, "secondary", sizeof("secondary")) == 0)
1083                 return RTE_PROC_SECONDARY;
1084         if (strncasecmp(arg, "auto", sizeof("auto")) == 0)
1085                 return RTE_PROC_AUTO;
1086
1087         return RTE_PROC_INVALID;
1088 }
1089
1090 static int
1091 eal_parse_iova_mode(const char *name)
1092 {
1093         int mode;
1094
1095         if (name == NULL)
1096                 return -1;
1097
1098         if (!strcmp("pa", name))
1099                 mode = RTE_IOVA_PA;
1100         else if (!strcmp("va", name))
1101                 mode = RTE_IOVA_VA;
1102         else
1103                 return -1;
1104
1105         internal_config.iova_mode = mode;
1106         return 0;
1107 }
1108
1109 static int
1110 eal_parse_base_virtaddr(const char *arg)
1111 {
1112         char *end;
1113         uint64_t addr;
1114
1115         errno = 0;
1116         addr = strtoull(arg, &end, 16);
1117
1118         /* check for errors */
1119         if ((errno != 0) || (arg[0] == '\0') || end == NULL || (*end != '\0'))
1120                 return -1;
1121
1122         /* make sure we don't exceed 32-bit boundary on 32-bit target */
1123 #ifndef RTE_ARCH_64
1124         if (addr >= UINTPTR_MAX)
1125                 return -1;
1126 #endif
1127
1128         /* align the addr on 16M boundary, 16MB is the minimum huge page
1129          * size on IBM Power architecture. If the addr is aligned to 16MB,
1130          * it can align to 2MB for x86. So this alignment can also be used
1131          * on x86 and other architectures.
1132          */
1133         internal_config.base_virtaddr =
1134                 RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
1135
1136         return 0;
1137 }
1138
1139 /* caller is responsible for freeing the returned string */
1140 static char *
1141 available_cores(void)
1142 {
1143         char *str = NULL;
1144         int previous;
1145         int sequence;
1146         char *tmp;
1147         int idx;
1148
1149         /* find the first available cpu */
1150         for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
1151                 if (eal_cpu_detected(idx) == 0)
1152                         continue;
1153                 break;
1154         }
1155         if (idx >= RTE_MAX_LCORE)
1156                 return NULL;
1157
1158         /* first sequence */
1159         if (asprintf(&str, "%d", idx) < 0)
1160                 return NULL;
1161         previous = idx;
1162         sequence = 0;
1163
1164         for (idx++ ; idx < RTE_MAX_LCORE; idx++) {
1165                 if (eal_cpu_detected(idx) == 0)
1166                         continue;
1167
1168                 if (idx == previous + 1) {
1169                         previous = idx;
1170                         sequence = 1;
1171                         continue;
1172                 }
1173
1174                 /* finish current sequence */
1175                 if (sequence) {
1176                         if (asprintf(&tmp, "%s-%d", str, previous) < 0) {
1177                                 free(str);
1178                                 return NULL;
1179                         }
1180                         free(str);
1181                         str = tmp;
1182                 }
1183
1184                 /* new sequence */
1185                 if (asprintf(&tmp, "%s,%d", str, idx) < 0) {
1186                         free(str);
1187                         return NULL;
1188                 }
1189                 free(str);
1190                 str = tmp;
1191                 previous = idx;
1192                 sequence = 0;
1193         }
1194
1195         /* finish last sequence */
1196         if (sequence) {
1197                 if (asprintf(&tmp, "%s-%d", str, previous) < 0) {
1198                         free(str);
1199                         return NULL;
1200                 }
1201                 free(str);
1202                 str = tmp;
1203         }
1204
1205         return str;
1206 }
1207
1208 int
1209 eal_parse_common_option(int opt, const char *optarg,
1210                         struct internal_config *conf)
1211 {
1212         static int b_used;
1213         static int w_used;
1214
1215         switch (opt) {
1216         /* blacklist */
1217         case 'b':
1218                 if (w_used)
1219                         goto bw_used;
1220                 if (eal_option_device_add(RTE_DEVTYPE_BLACKLISTED_PCI,
1221                                 optarg) < 0) {
1222                         return -1;
1223                 }
1224                 b_used = 1;
1225                 break;
1226         /* whitelist */
1227         case 'w':
1228                 if (b_used)
1229                         goto bw_used;
1230                 if (eal_option_device_add(RTE_DEVTYPE_WHITELISTED_PCI,
1231                                 optarg) < 0) {
1232                         return -1;
1233                 }
1234                 w_used = 1;
1235                 break;
1236         /* coremask */
1237         case 'c': {
1238                 int lcore_indexes[RTE_MAX_LCORE];
1239
1240                 if (eal_service_cores_parsed())
1241                         RTE_LOG(WARNING, EAL,
1242                                 "Service cores parsed before dataplane cores. Please ensure -c is before -s or -S\n");
1243                 if (eal_parse_coremask(optarg, lcore_indexes) < 0) {
1244                         RTE_LOG(ERR, EAL, "invalid coremask syntax\n");
1245                         return -1;
1246                 }
1247                 if (update_lcore_config(lcore_indexes) < 0) {
1248                         char *available = available_cores();
1249
1250                         RTE_LOG(ERR, EAL,
1251                                 "invalid coremask, please check specified cores are part of %s\n",
1252                                 available);
1253                         free(available);
1254                         return -1;
1255                 }
1256
1257                 if (core_parsed) {
1258                         RTE_LOG(ERR, EAL, "Option -c is ignored, because (%s) is set!\n",
1259                                 (core_parsed == LCORE_OPT_LST) ? "-l" :
1260                                 (core_parsed == LCORE_OPT_MAP) ? "--lcore" :
1261                                 "-c");
1262                         return -1;
1263                 }
1264
1265                 core_parsed = LCORE_OPT_MSK;
1266                 break;
1267         }
1268         /* corelist */
1269         case 'l': {
1270                 int lcore_indexes[RTE_MAX_LCORE];
1271
1272                 if (eal_service_cores_parsed())
1273                         RTE_LOG(WARNING, EAL,
1274                                 "Service cores parsed before dataplane cores. Please ensure -l is before -s or -S\n");
1275
1276                 if (eal_parse_corelist(optarg, lcore_indexes) < 0) {
1277                         RTE_LOG(ERR, EAL, "invalid core list syntax\n");
1278                         return -1;
1279                 }
1280                 if (update_lcore_config(lcore_indexes) < 0) {
1281                         char *available = available_cores();
1282
1283                         RTE_LOG(ERR, EAL,
1284                                 "invalid core list, please check specified cores are part of %s\n",
1285                                 available);
1286                         free(available);
1287                         return -1;
1288                 }
1289
1290                 if (core_parsed) {
1291                         RTE_LOG(ERR, EAL, "Option -l is ignored, because (%s) is set!\n",
1292                                 (core_parsed == LCORE_OPT_MSK) ? "-c" :
1293                                 (core_parsed == LCORE_OPT_MAP) ? "--lcore" :
1294                                 "-l");
1295                         return -1;
1296                 }
1297
1298                 core_parsed = LCORE_OPT_LST;
1299                 break;
1300         }
1301         /* service coremask */
1302         case 's':
1303                 if (eal_parse_service_coremask(optarg) < 0) {
1304                         RTE_LOG(ERR, EAL, "invalid service coremask\n");
1305                         return -1;
1306                 }
1307                 break;
1308         /* service corelist */
1309         case 'S':
1310                 if (eal_parse_service_corelist(optarg) < 0) {
1311                         RTE_LOG(ERR, EAL, "invalid service core list\n");
1312                         return -1;
1313                 }
1314                 break;
1315         /* size of memory */
1316         case 'm':
1317                 conf->memory = atoi(optarg);
1318                 conf->memory *= 1024ULL;
1319                 conf->memory *= 1024ULL;
1320                 mem_parsed = 1;
1321                 break;
1322         /* force number of channels */
1323         case 'n':
1324                 conf->force_nchannel = atoi(optarg);
1325                 if (conf->force_nchannel == 0) {
1326                         RTE_LOG(ERR, EAL, "invalid channel number\n");
1327                         return -1;
1328                 }
1329                 break;
1330         /* force number of ranks */
1331         case 'r':
1332                 conf->force_nrank = atoi(optarg);
1333                 if (conf->force_nrank == 0 ||
1334                     conf->force_nrank > 16) {
1335                         RTE_LOG(ERR, EAL, "invalid rank number\n");
1336                         return -1;
1337                 }
1338                 break;
1339         /* force loading of external driver */
1340         case 'd':
1341                 if (eal_plugin_add(optarg) == -1)
1342                         return -1;
1343                 break;
1344         case 'v':
1345                 /* since message is explicitly requested by user, we
1346                  * write message at highest log level so it can always
1347                  * be seen
1348                  * even if info or warning messages are disabled */
1349                 RTE_LOG(CRIT, EAL, "RTE Version: '%s'\n", rte_version());
1350                 break;
1351
1352         /* long options */
1353         case OPT_HUGE_UNLINK_NUM:
1354                 conf->hugepage_unlink = 1;
1355                 break;
1356
1357         case OPT_NO_HUGE_NUM:
1358                 conf->no_hugetlbfs = 1;
1359                 /* no-huge is legacy mem */
1360                 conf->legacy_mem = 1;
1361                 break;
1362
1363         case OPT_NO_PCI_NUM:
1364                 conf->no_pci = 1;
1365                 break;
1366
1367         case OPT_NO_HPET_NUM:
1368                 conf->no_hpet = 1;
1369                 break;
1370
1371         case OPT_VMWARE_TSC_MAP_NUM:
1372                 conf->vmware_tsc_map = 1;
1373                 break;
1374
1375         case OPT_NO_SHCONF_NUM:
1376                 conf->no_shconf = 1;
1377                 break;
1378
1379         case OPT_IN_MEMORY_NUM:
1380                 conf->in_memory = 1;
1381                 /* in-memory is a superset of noshconf and huge-unlink */
1382                 conf->no_shconf = 1;
1383                 conf->hugepage_unlink = 1;
1384                 break;
1385
1386         case OPT_PROC_TYPE_NUM:
1387                 conf->process_type = eal_parse_proc_type(optarg);
1388                 break;
1389
1390         case OPT_MASTER_LCORE_NUM:
1391                 if (eal_parse_master_lcore(optarg) < 0) {
1392                         RTE_LOG(ERR, EAL, "invalid parameter for --"
1393                                         OPT_MASTER_LCORE "\n");
1394                         return -1;
1395                 }
1396                 break;
1397
1398         case OPT_VDEV_NUM:
1399                 if (eal_option_device_add(RTE_DEVTYPE_VIRTUAL,
1400                                 optarg) < 0) {
1401                         return -1;
1402                 }
1403                 break;
1404
1405 #ifndef RTE_EXEC_ENV_WINDOWS
1406         case OPT_SYSLOG_NUM:
1407                 if (eal_parse_syslog(optarg, conf) < 0) {
1408                         RTE_LOG(ERR, EAL, "invalid parameters for --"
1409                                         OPT_SYSLOG "\n");
1410                         return -1;
1411                 }
1412                 break;
1413 #endif
1414
1415         case OPT_LOG_LEVEL_NUM: {
1416                 if (eal_parse_log_level(optarg) < 0) {
1417                         RTE_LOG(ERR, EAL,
1418                                 "invalid parameters for --"
1419                                 OPT_LOG_LEVEL "\n");
1420                         return -1;
1421                 }
1422                 break;
1423         }
1424
1425         case OPT_TRACE_NUM: {
1426                 if (eal_trace_args_save(optarg) < 0) {
1427                         RTE_LOG(ERR, EAL, "invalid parameters for --"
1428                                 OPT_TRACE "\n");
1429                         return -1;
1430                 }
1431                 break;
1432         }
1433
1434         case OPT_TRACE_DIR_NUM: {
1435                 if (eal_trace_dir_args_save(optarg) < 0) {
1436                         RTE_LOG(ERR, EAL, "invalid parameters for --"
1437                                 OPT_TRACE_DIR "\n");
1438                         return -1;
1439                 }
1440                 break;
1441         }
1442
1443         case OPT_LCORES_NUM:
1444                 if (eal_parse_lcores(optarg) < 0) {
1445                         RTE_LOG(ERR, EAL, "invalid parameter for --"
1446                                 OPT_LCORES "\n");
1447                         return -1;
1448                 }
1449
1450                 if (core_parsed) {
1451                         RTE_LOG(ERR, EAL, "Option --lcore is ignored, because (%s) is set!\n",
1452                                 (core_parsed == LCORE_OPT_LST) ? "-l" :
1453                                 (core_parsed == LCORE_OPT_MSK) ? "-c" :
1454                                 "--lcore");
1455                         return -1;
1456                 }
1457
1458                 core_parsed = LCORE_OPT_MAP;
1459                 break;
1460         case OPT_LEGACY_MEM_NUM:
1461                 conf->legacy_mem = 1;
1462                 break;
1463         case OPT_SINGLE_FILE_SEGMENTS_NUM:
1464                 conf->single_file_segments = 1;
1465                 break;
1466         case OPT_IOVA_MODE_NUM:
1467                 if (eal_parse_iova_mode(optarg) < 0) {
1468                         RTE_LOG(ERR, EAL, "invalid parameters for --"
1469                                 OPT_IOVA_MODE "\n");
1470                         return -1;
1471                 }
1472                 break;
1473         case OPT_BASE_VIRTADDR_NUM:
1474                 if (eal_parse_base_virtaddr(optarg) < 0) {
1475                         RTE_LOG(ERR, EAL, "invalid parameter for --"
1476                                         OPT_BASE_VIRTADDR "\n");
1477                         return -1;
1478                 }
1479                 break;
1480
1481         /* don't know what to do, leave this to caller */
1482         default:
1483                 return 1;
1484
1485         }
1486
1487         return 0;
1488 bw_used:
1489         RTE_LOG(ERR, EAL, "Options blacklist (-b) and whitelist (-w) "
1490                 "cannot be used at the same time\n");
1491         return -1;
1492 }
1493
1494 static void
1495 eal_auto_detect_cores(struct rte_config *cfg)
1496 {
1497         unsigned int lcore_id;
1498         unsigned int removed = 0;
1499         rte_cpuset_t affinity_set;
1500
1501         if (pthread_getaffinity_np(pthread_self(), sizeof(rte_cpuset_t),
1502                                 &affinity_set))
1503                 CPU_ZERO(&affinity_set);
1504
1505         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1506                 if (cfg->lcore_role[lcore_id] == ROLE_RTE &&
1507                     !CPU_ISSET(lcore_id, &affinity_set)) {
1508                         cfg->lcore_role[lcore_id] = ROLE_OFF;
1509                         removed++;
1510                 }
1511         }
1512
1513         cfg->lcore_count -= removed;
1514 }
1515
1516 static void
1517 compute_ctrl_threads_cpuset(struct internal_config *internal_cfg)
1518 {
1519         rte_cpuset_t *cpuset = &internal_cfg->ctrl_cpuset;
1520         rte_cpuset_t default_set;
1521         unsigned int lcore_id;
1522
1523         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1524                 if (rte_lcore_has_role(lcore_id, ROLE_OFF))
1525                         continue;
1526                 RTE_CPU_OR(cpuset, cpuset, &lcore_config[lcore_id].cpuset);
1527         }
1528         RTE_CPU_NOT(cpuset, cpuset);
1529
1530         if (pthread_getaffinity_np(pthread_self(), sizeof(rte_cpuset_t),
1531                                 &default_set))
1532                 CPU_ZERO(&default_set);
1533
1534         RTE_CPU_AND(cpuset, cpuset, &default_set);
1535
1536         /* if no remaining cpu, use master lcore cpu affinity */
1537         if (!CPU_COUNT(cpuset)) {
1538                 memcpy(cpuset, &lcore_config[rte_get_master_lcore()].cpuset,
1539                         sizeof(*cpuset));
1540         }
1541 }
1542
1543 int
1544 eal_cleanup_config(struct internal_config *internal_cfg)
1545 {
1546         if (internal_cfg->hugefile_prefix != NULL)
1547                 free(internal_cfg->hugefile_prefix);
1548         if (internal_cfg->hugepage_dir != NULL)
1549                 free(internal_cfg->hugepage_dir);
1550         if (internal_cfg->user_mbuf_pool_ops_name != NULL)
1551                 free(internal_cfg->user_mbuf_pool_ops_name);
1552
1553         return 0;
1554 }
1555
1556 int
1557 eal_adjust_config(struct internal_config *internal_cfg)
1558 {
1559         int i;
1560         struct rte_config *cfg = rte_eal_get_configuration();
1561
1562         if (!core_parsed)
1563                 eal_auto_detect_cores(cfg);
1564
1565         if (internal_config.process_type == RTE_PROC_AUTO)
1566                 internal_config.process_type = eal_proc_type_detect();
1567
1568         /* default master lcore is the first one */
1569         if (!master_lcore_parsed) {
1570                 cfg->master_lcore = rte_get_next_lcore(-1, 0, 0);
1571                 if (cfg->master_lcore >= RTE_MAX_LCORE)
1572                         return -1;
1573                 lcore_config[cfg->master_lcore].core_role = ROLE_RTE;
1574         }
1575
1576         compute_ctrl_threads_cpuset(internal_cfg);
1577
1578         /* if no memory amounts were requested, this will result in 0 and
1579          * will be overridden later, right after eal_hugepage_info_init() */
1580         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1581                 internal_cfg->memory += internal_cfg->socket_mem[i];
1582
1583         return 0;
1584 }
1585
1586 int
1587 eal_check_common_options(struct internal_config *internal_cfg)
1588 {
1589         struct rte_config *cfg = rte_eal_get_configuration();
1590
1591         if (cfg->lcore_role[cfg->master_lcore] != ROLE_RTE) {
1592                 RTE_LOG(ERR, EAL, "Master lcore is not enabled for DPDK\n");
1593                 return -1;
1594         }
1595
1596         if (internal_cfg->process_type == RTE_PROC_INVALID) {
1597                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
1598                 return -1;
1599         }
1600         if (internal_cfg->hugefile_prefix != NULL &&
1601                         strlen(internal_cfg->hugefile_prefix) < 1) {
1602                 RTE_LOG(ERR, EAL, "Invalid length of --" OPT_FILE_PREFIX " option\n");
1603                 return -1;
1604         }
1605         if (internal_cfg->hugepage_dir != NULL &&
1606                         strlen(internal_cfg->hugepage_dir) < 1) {
1607                 RTE_LOG(ERR, EAL, "Invalid length of --" OPT_HUGE_DIR" option\n");
1608                 return -1;
1609         }
1610         if (internal_cfg->user_mbuf_pool_ops_name != NULL &&
1611                         strlen(internal_cfg->user_mbuf_pool_ops_name) < 1) {
1612                 RTE_LOG(ERR, EAL, "Invalid length of --" OPT_MBUF_POOL_OPS_NAME" option\n");
1613                 return -1;
1614         }
1615         if (index(eal_get_hugefile_prefix(), '%') != NULL) {
1616                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in --"OPT_FILE_PREFIX" "
1617                         "option\n");
1618                 return -1;
1619         }
1620         if (mem_parsed && internal_cfg->force_sockets == 1) {
1621                 RTE_LOG(ERR, EAL, "Options -m and --"OPT_SOCKET_MEM" cannot "
1622                         "be specified at the same time\n");
1623                 return -1;
1624         }
1625         if (internal_cfg->no_hugetlbfs && internal_cfg->force_sockets == 1) {
1626                 RTE_LOG(ERR, EAL, "Option --"OPT_SOCKET_MEM" cannot "
1627                         "be specified together with --"OPT_NO_HUGE"\n");
1628                 return -1;
1629         }
1630         if (internal_cfg->no_hugetlbfs && internal_cfg->hugepage_unlink &&
1631                         !internal_cfg->in_memory) {
1632                 RTE_LOG(ERR, EAL, "Option --"OPT_HUGE_UNLINK" cannot "
1633                         "be specified together with --"OPT_NO_HUGE"\n");
1634                 return -1;
1635         }
1636         if (internal_config.force_socket_limits && internal_config.legacy_mem) {
1637                 RTE_LOG(ERR, EAL, "Option --"OPT_SOCKET_LIMIT
1638                         " is only supported in non-legacy memory mode\n");
1639         }
1640         if (internal_cfg->single_file_segments &&
1641                         internal_cfg->hugepage_unlink &&
1642                         !internal_cfg->in_memory) {
1643                 RTE_LOG(ERR, EAL, "Option --"OPT_SINGLE_FILE_SEGMENTS" is "
1644                         "not compatible with --"OPT_HUGE_UNLINK"\n");
1645                 return -1;
1646         }
1647         if (internal_cfg->legacy_mem &&
1648                         internal_cfg->in_memory) {
1649                 RTE_LOG(ERR, EAL, "Option --"OPT_LEGACY_MEM" is not compatible "
1650                                 "with --"OPT_IN_MEMORY"\n");
1651                 return -1;
1652         }
1653         if (internal_cfg->legacy_mem && internal_cfg->match_allocations) {
1654                 RTE_LOG(ERR, EAL, "Option --"OPT_LEGACY_MEM" is not compatible "
1655                                 "with --"OPT_MATCH_ALLOCATIONS"\n");
1656                 return -1;
1657         }
1658         if (internal_cfg->no_hugetlbfs && internal_cfg->match_allocations) {
1659                 RTE_LOG(ERR, EAL, "Option --"OPT_NO_HUGE" is not compatible "
1660                                 "with --"OPT_MATCH_ALLOCATIONS"\n");
1661                 return -1;
1662         }
1663         if (internal_cfg->legacy_mem && internal_cfg->memory == 0) {
1664                 RTE_LOG(NOTICE, EAL, "Static memory layout is selected, "
1665                         "amount of reserved memory can be adjusted with "
1666                         "-m or --"OPT_SOCKET_MEM"\n");
1667         }
1668
1669         return 0;
1670 }
1671
1672 void
1673 eal_common_usage(void)
1674 {
1675         printf("[options]\n\n"
1676                "EAL common options:\n"
1677                "  -c COREMASK         Hexadecimal bitmask of cores to run on\n"
1678                "  -l CORELIST         List of cores to run on\n"
1679                "                      The argument format is <c1>[-c2][,c3[-c4],...]\n"
1680                "                      where c1, c2, etc are core indexes between 0 and %d\n"
1681                "  --"OPT_LCORES" COREMAP    Map lcore set to physical cpu set\n"
1682                "                      The argument format is\n"
1683                "                            '<lcores[@cpus]>[<,lcores[@cpus]>...]'\n"
1684                "                      lcores and cpus list are grouped by '(' and ')'\n"
1685                "                      Within the group, '-' is used for range separator,\n"
1686                "                      ',' is used for single number separator.\n"
1687                "                      '( )' can be omitted for single element group,\n"
1688                "                      '@' can be omitted if cpus and lcores have the same value\n"
1689                "  -s SERVICE COREMASK Hexadecimal bitmask of cores to be used as service cores\n"
1690                "  --"OPT_MASTER_LCORE" ID   Core ID that is used as master\n"
1691                "  --"OPT_MBUF_POOL_OPS_NAME" Pool ops name for mbuf to use\n"
1692                "  -n CHANNELS         Number of memory channels\n"
1693                "  -m MB               Memory to allocate (see also --"OPT_SOCKET_MEM")\n"
1694                "  -r RANKS            Force number of memory ranks (don't detect)\n"
1695                "  -b, --"OPT_PCI_BLACKLIST" Add a PCI device in black list.\n"
1696                "                      Prevent EAL from using this PCI device. The argument\n"
1697                "                      format is <domain:bus:devid.func>.\n"
1698                "  -w, --"OPT_PCI_WHITELIST" Add a PCI device in white list.\n"
1699                "                      Only use the specified PCI devices. The argument format\n"
1700                "                      is <[domain:]bus:devid.func>. This option can be present\n"
1701                "                      several times (once per device).\n"
1702                "                      [NOTE: PCI whitelist cannot be used with -b option]\n"
1703                "  --"OPT_VDEV"              Add a virtual device.\n"
1704                "                      The argument format is <driver><id>[,key=val,...]\n"
1705                "                      (ex: --vdev=net_pcap0,iface=eth2).\n"
1706                "  --"OPT_IOVA_MODE"   Set IOVA mode. 'pa' for IOVA_PA\n"
1707                "                      'va' for IOVA_VA\n"
1708                "  -d LIB.so|DIR       Add a driver or driver directory\n"
1709                "                      (can be used multiple times)\n"
1710                "  --"OPT_VMWARE_TSC_MAP"    Use VMware TSC map instead of native RDTSC\n"
1711                "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
1712 #ifndef RTE_EXEC_ENV_WINDOWS
1713                "  --"OPT_SYSLOG"            Set syslog facility\n"
1714 #endif
1715                "  --"OPT_LOG_LEVEL"=<int>   Set global log level\n"
1716                "  --"OPT_LOG_LEVEL"=<type-match>:<int>\n"
1717                "                      Set specific log level\n"
1718                "  --"OPT_TRACE"=<regex-match>\n"
1719                "                      Enable trace based on regular expression trace name.\n"
1720                "                      By default, the trace is disabled.\n"
1721                "                      User must specify this option to enable trace.\n"
1722                "  --"OPT_TRACE_DIR"=<directory path>\n"
1723                "                      Specify trace directory for trace output.\n"
1724                "                      By default, trace output will created at\n"
1725                "                      $HOME directory and parameter must be\n"
1726                "                      specified once only.\n"
1727                "  -v                  Display version information on startup\n"
1728                "  -h, --help          This help\n"
1729                "  --"OPT_IN_MEMORY"   Operate entirely in memory. This will\n"
1730                "                      disable secondary process support\n"
1731                "  --"OPT_BASE_VIRTADDR"     Base virtual address\n"
1732                "\nEAL options for DEBUG use only:\n"
1733                "  --"OPT_HUGE_UNLINK"       Unlink hugepage files after init\n"
1734                "  --"OPT_NO_HUGE"           Use malloc instead of hugetlbfs\n"
1735                "  --"OPT_NO_PCI"            Disable PCI\n"
1736                "  --"OPT_NO_HPET"           Disable HPET\n"
1737                "  --"OPT_NO_SHCONF"         No shared config (mmap'd files)\n"
1738                "\n", RTE_MAX_LCORE);
1739         rte_option_usage();
1740 }