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