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