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