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