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