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