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