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