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