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