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