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