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