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