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