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