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