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