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