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