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