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