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