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