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