eal: get relative core index
[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
43 #include <rte_eal.h>
44 #include <rte_log.h>
45 #include <rte_lcore.h>
46 #include <rte_version.h>
47 #include <rte_devargs.h>
48
49 #include "eal_internal_cfg.h"
50 #include "eal_options.h"
51 #include "eal_filesystem.h"
52
53 #define BITS_PER_HEX 4
54
55 const char
56 eal_short_options[] =
57         "b:" /* pci-blacklist */
58         "w:" /* pci-whitelist */
59         "c:" /* coremask */
60         "d:"
61         "l:" /* corelist */
62         "m:"
63         "n:"
64         "r:"
65         "v";
66
67 const struct option
68 eal_long_options[] = {
69         {OPT_HUGE_DIR, 1, 0, OPT_HUGE_DIR_NUM},
70         {OPT_PROC_TYPE, 1, 0, OPT_PROC_TYPE_NUM},
71         {OPT_NO_SHCONF, 0, 0, OPT_NO_SHCONF_NUM},
72         {OPT_NO_HPET, 0, 0, OPT_NO_HPET_NUM},
73         {OPT_VMWARE_TSC_MAP, 0, 0, OPT_VMWARE_TSC_MAP_NUM},
74         {OPT_NO_PCI, 0, 0, OPT_NO_PCI_NUM},
75         {OPT_NO_HUGE, 0, 0, OPT_NO_HUGE_NUM},
76         {OPT_FILE_PREFIX, 1, 0, OPT_FILE_PREFIX_NUM},
77         {OPT_SOCKET_MEM, 1, 0, OPT_SOCKET_MEM_NUM},
78         {OPT_PCI_WHITELIST, 1, 0, OPT_PCI_WHITELIST_NUM},
79         {OPT_PCI_BLACKLIST, 1, 0, OPT_PCI_BLACKLIST_NUM},
80         {OPT_VDEV, 1, 0, OPT_VDEV_NUM},
81         {OPT_SYSLOG, 1, NULL, OPT_SYSLOG_NUM},
82         {OPT_LOG_LEVEL, 1, NULL, OPT_LOG_LEVEL_NUM},
83         {OPT_BASE_VIRTADDR, 1, 0, OPT_BASE_VIRTADDR_NUM},
84         {OPT_XEN_DOM0, 0, 0, OPT_XEN_DOM0_NUM},
85         {OPT_CREATE_UIO_DEV, 1, NULL, OPT_CREATE_UIO_DEV_NUM},
86         {OPT_VFIO_INTR, 1, NULL, OPT_VFIO_INTR_NUM},
87         {0, 0, 0, 0}
88 };
89
90 static int lcores_parsed;
91 static int mem_parsed;
92
93 void
94 eal_reset_internal_config(struct internal_config *internal_cfg)
95 {
96         int i;
97
98         internal_cfg->memory = 0;
99         internal_cfg->force_nrank = 0;
100         internal_cfg->force_nchannel = 0;
101         internal_cfg->hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
102         internal_cfg->hugepage_dir = NULL;
103         internal_cfg->force_sockets = 0;
104         /* zero out the NUMA config */
105         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
106                 internal_cfg->socket_mem[i] = 0;
107         /* zero out hugedir descriptors */
108         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
109                 internal_cfg->hugepage_info[i].lock_descriptor = -1;
110         internal_cfg->base_virtaddr = 0;
111
112         internal_cfg->syslog_facility = LOG_DAEMON;
113         /* default value from build option */
114         internal_cfg->log_level = RTE_LOG_LEVEL;
115
116         internal_cfg->xen_dom0_support = 0;
117
118         /* if set to NONE, interrupt mode is determined automatically */
119         internal_cfg->vfio_intr_mode = RTE_INTR_MODE_NONE;
120
121 #ifdef RTE_LIBEAL_USE_HPET
122         internal_cfg->no_hpet = 0;
123 #else
124         internal_cfg->no_hpet = 1;
125 #endif
126         internal_cfg->vmware_tsc_map = 0;
127 }
128
129 /*
130  * Parse the coremask given as argument (hexadecimal string) and fill
131  * the global configuration (core role and core count) with the parsed
132  * value.
133  */
134 static int xdigit2val(unsigned char c)
135 {
136         int val;
137
138         if (isdigit(c))
139                 val = c - '0';
140         else if (isupper(c))
141                 val = c - 'A' + 10;
142         else
143                 val = c - 'a' + 10;
144         return val;
145 }
146
147 static int
148 eal_parse_coremask(const char *coremask)
149 {
150         struct rte_config *cfg = rte_eal_get_configuration();
151         int i, j, idx = 0;
152         unsigned count = 0;
153         char c;
154         int val;
155
156         if (coremask == NULL)
157                 return -1;
158         /* Remove all blank characters ahead and after .
159          * Remove 0x/0X if exists.
160          */
161         while (isblank(*coremask))
162                 coremask++;
163         if (coremask[0] == '0' && ((coremask[1] == 'x')
164                 || (coremask[1] == 'X')))
165                 coremask += 2;
166         i = strnlen(coremask, PATH_MAX);
167         while ((i > 0) && isblank(coremask[i - 1]))
168                 i--;
169         if (i == 0)
170                 return -1;
171
172         for (i = i - 1; i >= 0 && idx < RTE_MAX_LCORE; i--) {
173                 c = coremask[i];
174                 if (isxdigit(c) == 0) {
175                         /* invalid characters */
176                         return -1;
177                 }
178                 val = xdigit2val(c);
179                 for (j = 0; j < BITS_PER_HEX && idx < RTE_MAX_LCORE; j++, idx++)
180                 {
181                         if ((1 << j) & val) {
182                                 if (!lcore_config[idx].detected) {
183                                         RTE_LOG(ERR, EAL, "lcore %u "
184                                                 "unavailable\n", idx);
185                                         return -1;
186                                 }
187                                 cfg->lcore_role[idx] = ROLE_RTE;
188                                 lcore_config[idx].core_index = count;
189                                 if (count == 0)
190                                         cfg->master_lcore = idx;
191                                 count++;
192                         } else {
193                                 cfg->lcore_role[idx] = ROLE_OFF;
194                                 lcore_config[idx].core_index = -1;
195                         }
196                 }
197         }
198         for (; i >= 0; i--)
199                 if (coremask[i] != '0')
200                         return -1;
201         for (; idx < RTE_MAX_LCORE; idx++) {
202                 cfg->lcore_role[idx] = ROLE_OFF;
203                 lcore_config[idx].core_index = -1;
204         }
205         if (count == 0)
206                 return -1;
207         /* Update the count of enabled logical cores of the EAL configuration */
208         cfg->lcore_count = count;
209         lcores_parsed = 1;
210         return 0;
211 }
212
213 static int
214 eal_parse_corelist(const char *corelist)
215 {
216         struct rte_config *cfg = rte_eal_get_configuration();
217         int i, idx = 0;
218         unsigned count = 0;
219         char *end = NULL;
220         int min, max;
221
222         if (corelist == NULL)
223                 return -1;
224
225         /* Remove all blank characters ahead and after */
226         while (isblank(*corelist))
227                 corelist++;
228         i = strnlen(corelist, sysconf(_SC_ARG_MAX));
229         while ((i > 0) && isblank(corelist[i - 1]))
230                 i--;
231
232         /* Reset config */
233         for (idx = 0; idx < RTE_MAX_LCORE; idx++) {
234                 cfg->lcore_role[idx] = ROLE_OFF;
235                 lcore_config[idx].core_index = -1;
236         }
237
238         /* Get list of cores */
239         min = RTE_MAX_LCORE;
240         do {
241                 while (isblank(*corelist))
242                         corelist++;
243                 if (*corelist == '\0')
244                         return -1;
245                 errno = 0;
246                 idx = strtoul(corelist, &end, 10);
247                 if (errno || end == NULL)
248                         return -1;
249                 while (isblank(*end))
250                         end++;
251                 if (*end == '-') {
252                         min = idx;
253                 } else if ((*end == ',') || (*end == '\0')) {
254                         max = idx;
255                         if (min == RTE_MAX_LCORE)
256                                 min = idx;
257                         for (idx = min; idx <= max; idx++) {
258                                 cfg->lcore_role[idx] = ROLE_RTE;
259                                 lcore_config[idx].core_index = count;
260                                 if (count == 0)
261                                         cfg->master_lcore = idx;
262                                 count++;
263                         }
264                         min = RTE_MAX_LCORE;
265                 } else
266                         return -1;
267                 corelist = end + 1;
268         } while (*end != '\0');
269
270         if (count == 0)
271                 return -1;
272
273         lcores_parsed = 1;
274         return 0;
275 }
276
277 static int
278 eal_parse_syslog(const char *facility, struct internal_config *conf)
279 {
280         int i;
281         static struct {
282                 const char *name;
283                 int value;
284         } map[] = {
285                 { "auth", LOG_AUTH },
286                 { "cron", LOG_CRON },
287                 { "daemon", LOG_DAEMON },
288                 { "ftp", LOG_FTP },
289                 { "kern", LOG_KERN },
290                 { "lpr", LOG_LPR },
291                 { "mail", LOG_MAIL },
292                 { "news", LOG_NEWS },
293                 { "syslog", LOG_SYSLOG },
294                 { "user", LOG_USER },
295                 { "uucp", LOG_UUCP },
296                 { "local0", LOG_LOCAL0 },
297                 { "local1", LOG_LOCAL1 },
298                 { "local2", LOG_LOCAL2 },
299                 { "local3", LOG_LOCAL3 },
300                 { "local4", LOG_LOCAL4 },
301                 { "local5", LOG_LOCAL5 },
302                 { "local6", LOG_LOCAL6 },
303                 { "local7", LOG_LOCAL7 },
304                 { NULL, 0 }
305         };
306
307         for (i = 0; map[i].name; i++) {
308                 if (!strcmp(facility, map[i].name)) {
309                         conf->syslog_facility = map[i].value;
310                         return 0;
311                 }
312         }
313         return -1;
314 }
315
316 static int
317 eal_parse_log_level(const char *level, uint32_t *log_level)
318 {
319         char *end;
320         unsigned long tmp;
321
322         errno = 0;
323         tmp = strtoul(level, &end, 0);
324
325         /* check for errors */
326         if ((errno != 0) || (level[0] == '\0') ||
327             end == NULL || (*end != '\0'))
328                 return -1;
329
330         /* log_level is a uint32_t */
331         if (tmp >= UINT32_MAX)
332                 return -1;
333
334         *log_level = tmp;
335         return 0;
336 }
337
338 static enum rte_proc_type_t
339 eal_parse_proc_type(const char *arg)
340 {
341         if (strncasecmp(arg, "primary", sizeof("primary")) == 0)
342                 return RTE_PROC_PRIMARY;
343         if (strncasecmp(arg, "secondary", sizeof("secondary")) == 0)
344                 return RTE_PROC_SECONDARY;
345         if (strncasecmp(arg, "auto", sizeof("auto")) == 0)
346                 return RTE_PROC_AUTO;
347
348         return RTE_PROC_INVALID;
349 }
350
351 int
352 eal_parse_common_option(int opt, const char *optarg,
353                         struct internal_config *conf)
354 {
355         switch (opt) {
356         /* blacklist */
357         case 'b':
358                 if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
359                                 optarg) < 0) {
360                         return -1;
361                 }
362                 break;
363         /* whitelist */
364         case 'w':
365                 if (rte_eal_devargs_add(RTE_DEVTYPE_WHITELISTED_PCI,
366                                 optarg) < 0) {
367                         return -1;
368                 }
369                 break;
370         /* coremask */
371         case 'c':
372                 if (eal_parse_coremask(optarg) < 0) {
373                         RTE_LOG(ERR, EAL, "invalid coremask\n");
374                         return -1;
375                 }
376                 break;
377         /* corelist */
378         case 'l':
379                 if (eal_parse_corelist(optarg) < 0) {
380                         RTE_LOG(ERR, EAL, "invalid core list\n");
381                         return -1;
382                 }
383                 break;
384         /* size of memory */
385         case 'm':
386                 conf->memory = atoi(optarg);
387                 conf->memory *= 1024ULL;
388                 conf->memory *= 1024ULL;
389                 mem_parsed = 1;
390                 break;
391         /* force number of channels */
392         case 'n':
393                 conf->force_nchannel = atoi(optarg);
394                 if (conf->force_nchannel == 0 ||
395                     conf->force_nchannel > 4) {
396                         RTE_LOG(ERR, EAL, "invalid channel number\n");
397                         return -1;
398                 }
399                 break;
400         /* force number of ranks */
401         case 'r':
402                 conf->force_nrank = atoi(optarg);
403                 if (conf->force_nrank == 0 ||
404                     conf->force_nrank > 16) {
405                         RTE_LOG(ERR, EAL, "invalid rank number\n");
406                         return -1;
407                 }
408                 break;
409         case 'v':
410                 /* since message is explicitly requested by user, we
411                  * write message at highest log level so it can always
412                  * be seen
413                  * even if info or warning messages are disabled */
414                 RTE_LOG(CRIT, EAL, "RTE Version: '%s'\n", rte_version());
415                 break;
416
417         /* long options */
418         case OPT_NO_HUGE_NUM:
419                 conf->no_hugetlbfs = 1;
420                 break;
421
422         case OPT_NO_PCI_NUM:
423                 conf->no_pci = 1;
424                 break;
425
426         case OPT_NO_HPET_NUM:
427                 conf->no_hpet = 1;
428                 break;
429
430         case OPT_VMWARE_TSC_MAP_NUM:
431                 conf->vmware_tsc_map = 1;
432                 break;
433
434         case OPT_NO_SHCONF_NUM:
435                 conf->no_shconf = 1;
436                 break;
437
438         case OPT_PROC_TYPE_NUM:
439                 conf->process_type = eal_parse_proc_type(optarg);
440                 break;
441
442         case OPT_VDEV_NUM:
443                 if (rte_eal_devargs_add(RTE_DEVTYPE_VIRTUAL,
444                                 optarg) < 0) {
445                         return -1;
446                 }
447                 break;
448
449         case OPT_SYSLOG_NUM:
450                 if (eal_parse_syslog(optarg, conf) < 0) {
451                         RTE_LOG(ERR, EAL, "invalid parameters for --"
452                                         OPT_SYSLOG "\n");
453                         return -1;
454                 }
455                 break;
456
457         case OPT_LOG_LEVEL_NUM: {
458                 uint32_t log;
459
460                 if (eal_parse_log_level(optarg, &log) < 0) {
461                         RTE_LOG(ERR, EAL,
462                                 "invalid parameters for --"
463                                 OPT_LOG_LEVEL "\n");
464                         return -1;
465                 }
466                 conf->log_level = log;
467                 break;
468         }
469
470         /* don't know what to do, leave this to caller */
471         default:
472                 return 1;
473
474         }
475
476         return 0;
477 }
478
479 int
480 eal_adjust_config(struct internal_config *internal_cfg)
481 {
482         int i;
483
484         if (internal_config.process_type == RTE_PROC_AUTO)
485                 internal_config.process_type = eal_proc_type_detect();
486
487         /* if no memory amounts were requested, this will result in 0 and
488          * will be overridden later, right after eal_hugepage_info_init() */
489         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
490                 internal_cfg->memory += internal_cfg->socket_mem[i];
491
492         return 0;
493 }
494
495 int
496 eal_check_common_options(struct internal_config *internal_cfg)
497 {
498         struct rte_config *cfg = rte_eal_get_configuration();
499
500         if (!lcores_parsed) {
501                 RTE_LOG(ERR, EAL, "CPU cores must be enabled with options "
502                         "-c or -l\n");
503                 return -1;
504         }
505
506         if (internal_cfg->process_type == RTE_PROC_INVALID) {
507                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
508                 return -1;
509         }
510         if (internal_cfg->process_type == RTE_PROC_PRIMARY &&
511                         internal_cfg->force_nchannel == 0) {
512                 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not "
513                         "specified\n");
514                 return -1;
515         }
516         if (index(internal_cfg->hugefile_prefix, '%') != NULL) {
517                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in --"OPT_FILE_PREFIX" "
518                         "option\n");
519                 return -1;
520         }
521         if (mem_parsed && internal_cfg->force_sockets == 1) {
522                 RTE_LOG(ERR, EAL, "Options -m and --"OPT_SOCKET_MEM" cannot "
523                         "be specified at the same time\n");
524                 return -1;
525         }
526         if (internal_cfg->no_hugetlbfs &&
527                         (mem_parsed || internal_cfg->force_sockets == 1)) {
528                 RTE_LOG(ERR, EAL, "Options -m or --"OPT_SOCKET_MEM" cannot "
529                         "be specified together with --"OPT_NO_HUGE"\n");
530                 return -1;
531         }
532
533         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) != 0 &&
534                 rte_eal_devargs_type_count(RTE_DEVTYPE_BLACKLISTED_PCI) != 0) {
535                 RTE_LOG(ERR, EAL, "Options blacklist (-b) and whitelist (-w) "
536                         "cannot be used at the same time\n");
537                 return -1;
538         }
539
540         return 0;
541 }
542
543 void
544 eal_common_usage(void)
545 {
546         printf("-c COREMASK -n NUM [-m NB] [-r NUM] [-b <domain:bus:devid.func>]"
547                "[--proc-type primary|secondary|auto]\n\n"
548                "EAL common options:\n"
549                "  -c COREMASK  : A hexadecimal bitmask of cores to run on\n"
550                "  -l CORELIST  : List of cores to run on\n"
551                "                 The argument format is <c1>[-c2][,c3[-c4],...]\n"
552                "                 where c1, c2, etc are core indexes between 0 and %d\n"
553                "  -n NUM       : Number of memory channels\n"
554                "  -v           : Display version information on startup\n"
555                "  -m MB        : memory to allocate (see also --"OPT_SOCKET_MEM")\n"
556                "  -r NUM       : force number of memory ranks (don't detect)\n"
557                "  --"OPT_SYSLOG"     : set syslog facility\n"
558                "  --"OPT_LOG_LEVEL"  : set default log level\n"
559                "  --"OPT_PROC_TYPE"  : type of this process\n"
560                "  --"OPT_PCI_BLACKLIST", -b: add a PCI device in black list.\n"
561                "               Prevent EAL from using this PCI device. The argument\n"
562                "               format is <domain:bus:devid.func>.\n"
563                "  --"OPT_PCI_WHITELIST", -w: add a PCI device in white list.\n"
564                "               Only use the specified PCI devices. The argument format\n"
565                "               is <[domain:]bus:devid.func>. This option can be present\n"
566                "               several times (once per device).\n"
567                "               [NOTE: PCI whitelist cannot be used with -b option]\n"
568                "  --"OPT_VDEV": add a virtual device.\n"
569                "               The argument format is <driver><id>[,key=val,...]\n"
570                "               (ex: --vdev=eth_pcap0,iface=eth2).\n"
571                "  --"OPT_VMWARE_TSC_MAP": use VMware TSC map instead of native RDTSC\n"
572                "\nEAL options for DEBUG use only:\n"
573                "  --"OPT_NO_HUGE"  : use malloc instead of hugetlbfs\n"
574                "  --"OPT_NO_PCI"   : disable pci\n"
575                "  --"OPT_NO_HPET"  : disable hpet\n"
576                "  --"OPT_NO_SHCONF": no shared config (mmap'd files)\n"
577                "\n", RTE_MAX_LCORE);
578 }