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