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