eal: set log level from command line
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2012-2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <unistd.h>
41 #include <pthread.h>
42 #include <syslog.h>
43 #include <getopt.h>
44 #include <sys/file.h>
45 #include <fcntl.h>
46 #include <dlfcn.h>
47 #include <stddef.h>
48 #include <errno.h>
49 #include <limits.h>
50 #include <errno.h>
51 #include <sys/mman.h>
52 #include <sys/queue.h>
53 #include <sys/io.h>
54
55 #include <rte_common.h>
56 #include <rte_debug.h>
57 #include <rte_memory.h>
58 #include <rte_memzone.h>
59 #include <rte_launch.h>
60 #include <rte_tailq.h>
61 #include <rte_eal.h>
62 #include <rte_eal_memconfig.h>
63 #include <rte_per_lcore.h>
64 #include <rte_lcore.h>
65 #include <rte_log.h>
66 #include <rte_random.h>
67 #include <rte_cycles.h>
68 #include <rte_string_fns.h>
69 #include <rte_cpuflags.h>
70 #include <rte_interrupts.h>
71 #include <rte_pci.h>
72 #include <rte_devargs.h>
73 #include <rte_common.h>
74 #include <rte_version.h>
75 #include <rte_atomic.h>
76 #include <malloc_heap.h>
77 #include <rte_eth_ring.h>
78 #include <rte_dev.h>
79
80 #include "eal_private.h"
81 #include "eal_thread.h"
82 #include "eal_internal_cfg.h"
83 #include "eal_filesystem.h"
84 #include "eal_hugepages.h"
85
86 #define OPT_HUGE_DIR    "huge-dir"
87 #define OPT_PROC_TYPE   "proc-type"
88 #define OPT_NO_SHCONF   "no-shconf"
89 #define OPT_NO_HPET     "no-hpet"
90 #define OPT_VMWARE_TSC_MAP   "vmware-tsc-map"
91 #define OPT_NO_PCI      "no-pci"
92 #define OPT_NO_HUGE     "no-huge"
93 #define OPT_FILE_PREFIX "file-prefix"
94 #define OPT_SOCKET_MEM  "socket-mem"
95 #define OPT_USE_DEVICE  "use-device"
96 #define OPT_PCI_WHITELIST "pci-whitelist"
97 #define OPT_PCI_BLACKLIST "pci-blacklist"
98 #define OPT_VDEV        "vdev"
99 #define OPT_SYSLOG      "syslog"
100 #define OPT_LOG_LEVEL   "log-level"
101 #define OPT_BASE_VIRTADDR   "base-virtaddr"
102 #define OPT_XEN_DOM0    "xen-dom0"
103 #define OPT_CREATE_UIO_DEV "create-uio-dev"
104 #define OPT_VFIO_INTR    "vfio-intr"
105
106 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
107
108 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
109
110 #define HIGHEST_RPL 3
111
112 #define BITS_PER_HEX 4
113
114 /* Allow the application to print its usage message too if set */
115 static rte_usage_hook_t rte_application_usage_hook = NULL;
116
117 TAILQ_HEAD(shared_driver_list, shared_driver);
118
119 /* Definition for shared object drivers. */
120 struct shared_driver {
121         TAILQ_ENTRY(shared_driver) next;
122
123         char    name[PATH_MAX];
124         void*   lib_handle;
125 };
126
127 /* List of external loadable drivers */
128 static struct shared_driver_list solib_list =
129 TAILQ_HEAD_INITIALIZER(solib_list);
130
131 /* early configuration structure, when memory config is not mmapped */
132 static struct rte_mem_config early_mem_config;
133
134 /* define fd variable here, because file needs to be kept open for the
135  * duration of the program, as we hold a write lock on it in the primary proc */
136 static int mem_cfg_fd = -1;
137
138 static struct flock wr_lock = {
139                 .l_type = F_WRLCK,
140                 .l_whence = SEEK_SET,
141                 .l_start = offsetof(struct rte_mem_config, memseg),
142                 .l_len = sizeof(early_mem_config.memseg),
143 };
144
145 /* Address of global and public configuration */
146 static struct rte_config rte_config = {
147                 .mem_config = &early_mem_config,
148 };
149
150 /* internal configuration (per-core) */
151 struct lcore_config lcore_config[RTE_MAX_LCORE];
152
153 /* internal configuration */
154 struct internal_config internal_config;
155
156 /* used by rte_rdtsc() */
157 int rte_cycles_vmware_tsc_map;
158
159 /* Return a pointer to the configuration structure */
160 struct rte_config *
161 rte_eal_get_configuration(void)
162 {
163         return &rte_config;
164 }
165
166 /* parse a sysfs (or other) file containing one integer value */
167 int
168 eal_parse_sysfs_value(const char *filename, unsigned long *val)
169 {
170         FILE *f;
171         char buf[BUFSIZ];
172         char *end = NULL;
173
174         if ((f = fopen(filename, "r")) == NULL) {
175                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
176                         __func__, filename);
177                 return -1;
178         }
179
180         if (fgets(buf, sizeof(buf), f) == NULL) {
181                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
182                         __func__, filename);
183                 fclose(f);
184                 return -1;
185         }
186         *val = strtoul(buf, &end, 0);
187         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
188                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
189                                 __func__, filename);
190                 fclose(f);
191                 return -1;
192         }
193         fclose(f);
194         return 0;
195 }
196
197
198 /* create memory configuration in shared/mmap memory. Take out
199  * a write lock on the memsegs, so we can auto-detect primary/secondary.
200  * This means we never close the file while running (auto-close on exit).
201  * We also don't lock the whole file, so that in future we can use read-locks
202  * on other parts, e.g. memzones, to detect if there are running secondary
203  * processes. */
204 static void
205 rte_eal_config_create(void)
206 {
207         void *rte_mem_cfg_addr;
208         int retval;
209
210         const char *pathname = eal_runtime_config_path();
211
212         if (internal_config.no_shconf)
213                 return;
214
215         /* map the config before hugepage address so that we don't waste a page */
216         if (internal_config.base_virtaddr != 0)
217                 rte_mem_cfg_addr = (void *)
218                         RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
219                         sizeof(struct rte_mem_config), sysconf(_SC_PAGE_SIZE));
220         else
221                 rte_mem_cfg_addr = NULL;
222
223         if (mem_cfg_fd < 0){
224                 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
225                 if (mem_cfg_fd < 0)
226                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
227         }
228
229         retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
230         if (retval < 0){
231                 close(mem_cfg_fd);
232                 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
233         }
234
235         retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
236         if (retval < 0){
237                 close(mem_cfg_fd);
238                 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
239                                 "process running?\n", pathname);
240         }
241
242         rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
243                                 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
244
245         if (rte_mem_cfg_addr == MAP_FAILED){
246                 rte_panic("Cannot mmap memory for rte_config\n");
247         }
248         memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
249         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
250
251         /* store address of the config in the config itself so that secondary
252          * processes could later map the config into this exact location */
253         rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
254
255 }
256
257 /* attach to an existing shared memory config */
258 static void
259 rte_eal_config_attach(void)
260 {
261         struct rte_mem_config *mem_config;
262
263         const char *pathname = eal_runtime_config_path();
264
265         if (internal_config.no_shconf)
266                 return;
267
268         if (mem_cfg_fd < 0){
269                 mem_cfg_fd = open(pathname, O_RDWR);
270                 if (mem_cfg_fd < 0)
271                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
272         }
273
274         /* map it as read-only first */
275         mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
276                         PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
277         if (mem_config == MAP_FAILED)
278                 rte_panic("Cannot mmap memory for rte_config\n");
279
280         rte_config.mem_config = mem_config;
281 }
282
283 /* reattach the shared config at exact memory location primary process has it */
284 static void
285 rte_eal_config_reattach(void)
286 {
287         struct rte_mem_config *mem_config;
288         void *rte_mem_cfg_addr;
289
290         if (internal_config.no_shconf)
291                 return;
292
293         /* save the address primary process has mapped shared config to */
294         rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
295
296         /* unmap original config */
297         munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
298
299         /* remap the config at proper address */
300         mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
301                         sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
302                         mem_cfg_fd, 0);
303         close(mem_cfg_fd);
304         if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr)
305                 rte_panic("Cannot mmap memory for rte_config\n");
306
307         rte_config.mem_config = mem_config;
308 }
309
310 /* Detect if we are a primary or a secondary process */
311 static enum rte_proc_type_t
312 eal_proc_type_detect(void)
313 {
314         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
315         const char *pathname = eal_runtime_config_path();
316
317         /* if we can open the file but not get a write-lock we are a secondary
318          * process. NOTE: if we get a file handle back, we keep that open
319          * and don't close it to prevent a race condition between multiple opens */
320         if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
321                         (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
322                 ptype = RTE_PROC_SECONDARY;
323
324         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
325                         ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
326
327         return ptype;
328 }
329
330 /* Sets up rte_config structure with the pointer to shared memory config.*/
331 static void
332 rte_config_init(void)
333 {
334         rte_config.process_type = (internal_config.process_type == RTE_PROC_AUTO) ?
335                         eal_proc_type_detect() : /* for auto, detect the type */
336                         internal_config.process_type; /* otherwise use what's already set */
337
338         switch (rte_config.process_type){
339         case RTE_PROC_PRIMARY:
340                 rte_eal_config_create();
341                 break;
342         case RTE_PROC_SECONDARY:
343                 rte_eal_config_attach();
344                 rte_eal_mcfg_wait_complete(rte_config.mem_config);
345                 rte_eal_config_reattach();
346                 break;
347         case RTE_PROC_AUTO:
348         case RTE_PROC_INVALID:
349                 rte_panic("Invalid process type\n");
350         }
351 }
352
353 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
354 static void
355 eal_hugedirs_unlock(void)
356 {
357         int i;
358
359         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
360         {
361                 /* skip uninitialized */
362                 if (internal_config.hugepage_info[i].lock_descriptor < 0)
363                         continue;
364                 /* unlock hugepage file */
365                 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
366                 close(internal_config.hugepage_info[i].lock_descriptor);
367                 /* reset the field */
368                 internal_config.hugepage_info[i].lock_descriptor = -1;
369         }
370 }
371
372 /* display usage */
373 static void
374 eal_usage(const char *prgname)
375 {
376         printf("\nUsage: %s -c COREMASK -n NUM [-m NB] [-r NUM] [-b <domain:bus:devid.func>]"
377                "[--proc-type primary|secondary|auto] \n\n"
378                "EAL options:\n"
379                "  -c COREMASK  : A hexadecimal bitmask of cores to run on\n"
380                "  -n NUM       : Number of memory channels\n"
381                    "  -v           : Display version information on startup\n"
382                "  -d LIB.so    : add driver (can be used multiple times)\n"
383                "  -m MB        : memory to allocate (see also --"OPT_SOCKET_MEM")\n"
384                "  -r NUM       : force number of memory ranks (don't detect)\n"
385                "  --"OPT_XEN_DOM0" : support application running on Xen Domain0 "
386                            "without hugetlbfs\n"
387                "  --"OPT_SYSLOG"     : set syslog facility\n"
388                "  --"OPT_LOG_LEVEL"  : set default log level\n"
389                "  --"OPT_SOCKET_MEM" : memory to allocate on specific\n"
390                    "                 sockets (use comma separated values)\n"
391                "  --"OPT_HUGE_DIR"   : directory where hugetlbfs is mounted\n"
392                "  --"OPT_PROC_TYPE"  : type of this process\n"
393                "  --"OPT_FILE_PREFIX": prefix for hugepage filenames\n"
394                "  --"OPT_PCI_BLACKLIST", -b: add a PCI device in black list.\n"
395                "               Prevent EAL from using this PCI device. The argument\n"
396                "               format is <domain:bus:devid.func>.\n"
397                "  --"OPT_PCI_WHITELIST", -w: add a PCI device in white list.\n"
398                "               Only use the specified PCI devices. The argument format\n"
399                "               is <[domain:]bus:devid.func>. This option can be present\n"
400                "               several times (once per device).\n"
401                "               [NOTE: PCI whitelist cannot be used with -b option]\n"
402                "  --"OPT_VDEV": add a virtual device.\n"
403                "               The argument format is <driver><id>[,key=val,...]\n"
404                "               (ex: --vdev=eth_pcap0,iface=eth2).\n"
405                "  --"OPT_VMWARE_TSC_MAP": use VMware TSC map instead of native RDTSC\n"
406                "  --"OPT_BASE_VIRTADDR": specify base virtual address\n"
407                "  --"OPT_VFIO_INTR": specify desired interrupt mode for VFIO "
408                            "(legacy|msi|msix)\n"
409                "  --"OPT_CREATE_UIO_DEV": create /dev/uioX (usually done by hotplug)\n"
410                "\nEAL options for DEBUG use only:\n"
411                "  --"OPT_NO_HUGE"  : use malloc instead of hugetlbfs\n"
412                "  --"OPT_NO_PCI"   : disable pci\n"
413                "  --"OPT_NO_HPET"  : disable hpet\n"
414                "  --"OPT_NO_SHCONF": no shared config (mmap'd files)\n"
415                "\n",
416                prgname);
417         /* Allow the application to print its usage message too if hook is set */
418         if ( rte_application_usage_hook ) {
419                 printf("===== Application Usage =====\n\n");
420                 rte_application_usage_hook(prgname);
421         }
422 }
423
424 /* Set a per-application usage message */
425 rte_usage_hook_t
426 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
427 {
428         rte_usage_hook_t        old_func;
429
430         /* Will be NULL on the first call to denote the last usage routine. */
431         old_func                                        = rte_application_usage_hook;
432         rte_application_usage_hook      = usage_func;
433
434         return old_func;
435 }
436
437 /*
438  * Parse the coremask given as argument (hexadecimal string) and fill
439  * the global configuration (core role and core count) with the parsed
440  * value.
441  */
442 static int xdigit2val(unsigned char c)
443 {
444         int val;
445         if(isdigit(c))
446                 val = c - '0';
447         else if(isupper(c))
448                 val = c - 'A' + 10;
449         else
450                 val = c - 'a' + 10;
451         return val;
452 }
453 static int
454 eal_parse_coremask(const char *coremask)
455 {
456         struct rte_config *cfg = rte_eal_get_configuration();
457         int i, j, idx = 0 ;
458         unsigned count = 0;
459         char c;
460         int val;
461
462         if (coremask == NULL)
463                 return -1;
464         /* Remove all blank characters ahead and after .
465          * Remove 0x/0X if exists.
466          */
467         while (isblank(*coremask))
468                 coremask++;
469         if (coremask[0] == '0' && ((coremask[1] == 'x')
470                 ||  (coremask[1] == 'X')) )
471                 coremask += 2;
472         i = strnlen(coremask, PATH_MAX);
473         while ((i > 0) && isblank(coremask[i - 1]))
474                 i--;
475         if (i == 0)
476                 return -1;
477
478         for (i = i - 1; i >= 0 && idx < RTE_MAX_LCORE; i--) {
479                 c = coremask[i];
480                 if (isxdigit(c) == 0) {
481                         /* invalid characters */
482                         return (-1);
483                 }
484                 val = xdigit2val(c);
485                 for(j = 0; j < BITS_PER_HEX && idx < RTE_MAX_LCORE; j++, idx++) {
486                         if((1 << j) & val) {
487                                 if (!lcore_config[idx].detected) {
488                                         RTE_LOG(ERR, EAL, "lcore %u "
489                                                 "unavailable\n", idx);
490                                         return -1;
491                                 }
492                                 cfg->lcore_role[idx] = ROLE_RTE;
493                                 if(count == 0)
494                                         cfg->master_lcore = idx;
495                                 count++;
496                         } else  {
497                                 cfg->lcore_role[idx] = ROLE_OFF;
498                         }
499                 }
500         }
501         for(; i >= 0; i--)
502                 if(coremask[i] != '0')
503                         return -1;
504         for(; idx < RTE_MAX_LCORE; idx++)
505                 cfg->lcore_role[idx] = ROLE_OFF;
506         if(count == 0)
507                 return -1;
508         /* Update the count of enabled logical cores of the EAL configuration */
509         cfg->lcore_count = count;
510         return 0;
511 }
512
513 static int
514 eal_parse_syslog(const char *facility)
515 {
516         int i;
517         static struct {
518                 const char *name;
519                 int value;
520         } map[] = {
521                 { "auth", LOG_AUTH },
522                 { "cron", LOG_CRON },
523                 { "daemon", LOG_DAEMON },
524                 { "ftp", LOG_FTP },
525                 { "kern", LOG_KERN },
526                 { "lpr", LOG_LPR },
527                 { "mail", LOG_MAIL },
528                 { "news", LOG_NEWS },
529                 { "syslog", LOG_SYSLOG },
530                 { "user", LOG_USER },
531                 { "uucp", LOG_UUCP },
532                 { "local0", LOG_LOCAL0 },
533                 { "local1", LOG_LOCAL1 },
534                 { "local2", LOG_LOCAL2 },
535                 { "local3", LOG_LOCAL3 },
536                 { "local4", LOG_LOCAL4 },
537                 { "local5", LOG_LOCAL5 },
538                 { "local6", LOG_LOCAL6 },
539                 { "local7", LOG_LOCAL7 },
540                 { NULL, 0 }
541         };
542
543         for (i = 0; map[i].name; i++) {
544                 if (!strcmp(facility, map[i].name)) {
545                         internal_config.syslog_facility = map[i].value;
546                         return 0;
547                 }
548         }
549         return -1;
550 }
551
552 static int
553 eal_parse_log_level(const char *level, uint32_t *log_level)
554 {
555         char *end;
556         unsigned long tmp;
557
558         errno = 0;
559         tmp = strtoul(level, &end, 0);
560
561         /* check for errors */
562         if ((errno != 0) || (level[0] == '\0') ||
563             end == NULL || (*end != '\0'))
564                 return -1;
565
566         /* log_level is a uint32_t */
567         if (tmp >= UINT32_MAX)
568                 return -1;
569
570         *log_level = tmp;
571         return 0;
572 }
573
574 static int
575 eal_parse_socket_mem(char *socket_mem)
576 {
577         char * arg[RTE_MAX_NUMA_NODES];
578         char *end;
579         int arg_num, i, len;
580         uint64_t total_mem = 0;
581
582         len = strnlen(socket_mem, SOCKET_MEM_STRLEN);
583         if (len == SOCKET_MEM_STRLEN) {
584                 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
585                 return -1;
586         }
587
588         /* all other error cases will be caught later */
589         if (!isdigit(socket_mem[len-1]))
590                 return -1;
591
592         /* split the optarg into separate socket values */
593         arg_num = rte_strsplit(socket_mem, len,
594                         arg, RTE_MAX_NUMA_NODES, ',');
595
596         /* if split failed, or 0 arguments */
597         if (arg_num <= 0)
598                 return -1;
599
600         internal_config.force_sockets = 1;
601
602         /* parse each defined socket option */
603         errno = 0;
604         for (i = 0; i < arg_num; i++) {
605                 end = NULL;
606                 internal_config.socket_mem[i] = strtoull(arg[i], &end, 10);
607
608                 /* check for invalid input */
609                 if ((errno != 0)  ||
610                                 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
611                         return -1;
612                 internal_config.socket_mem[i] *= 1024ULL;
613                 internal_config.socket_mem[i] *= 1024ULL;
614                 total_mem += internal_config.socket_mem[i];
615         }
616
617         /* check if we have a positive amount of total memory */
618         if (total_mem == 0)
619                 return -1;
620
621         return 0;
622 }
623
624 static int
625 eal_parse_base_virtaddr(const char *arg)
626 {
627         char *end;
628         uint64_t addr;
629
630         errno = 0;
631         addr = strtoull(arg, &end, 16);
632
633         /* check for errors */
634         if ((errno != 0) || (arg[0] == '\0') || end == NULL || (*end != '\0'))
635                 return -1;
636
637         /* make sure we don't exceed 32-bit boundary on 32-bit target */
638 #ifndef RTE_ARCH_X86_64
639         if (addr >= UINTPTR_MAX)
640                 return -1;
641 #endif
642
643         /* align the addr on 2M boundary */
644         internal_config.base_virtaddr = RTE_PTR_ALIGN_CEIL((uintptr_t)addr,
645                                                            RTE_PGSIZE_2M);
646
647         return 0;
648 }
649
650 static int
651 eal_parse_vfio_intr(const char *mode)
652 {
653         unsigned i;
654         static struct {
655                 const char *name;
656                 enum rte_intr_mode value;
657         } map[] = {
658                 { "legacy", RTE_INTR_MODE_LEGACY },
659                 { "msi", RTE_INTR_MODE_MSI },
660                 { "msix", RTE_INTR_MODE_MSIX },
661         };
662
663         for (i = 0; i < RTE_DIM(map); i++) {
664                 if (!strcmp(mode, map[i].name)) {
665                         internal_config.vfio_intr_mode = map[i].value;
666                         return 0;
667                 }
668         }
669         return -1;
670 }
671
672 static inline size_t
673 eal_get_hugepage_mem_size(void)
674 {
675         uint64_t size = 0;
676         unsigned i, j;
677
678         for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
679                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
680                 if (hpi->hugedir != NULL) {
681                         for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
682                                 size += hpi->hugepage_sz * hpi->num_pages[j];
683                         }
684                 }
685         }
686
687         return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
688 }
689
690 static enum rte_proc_type_t
691 eal_parse_proc_type(const char *arg)
692 {
693         if (strncasecmp(arg, "primary", sizeof("primary")) == 0)
694                 return RTE_PROC_PRIMARY;
695         if (strncasecmp(arg, "secondary", sizeof("secondary")) == 0)
696                 return RTE_PROC_SECONDARY;
697         if (strncasecmp(arg, "auto", sizeof("auto")) == 0)
698                 return RTE_PROC_AUTO;
699
700         return RTE_PROC_INVALID;
701 }
702
703 /* Parse the argument given in the command line of the application */
704 static int
705 eal_parse_args(int argc, char **argv)
706 {
707         int opt, ret, i;
708         char **argvopt;
709         int option_index;
710         int coremask_ok = 0;
711         char *prgname = argv[0];
712         static struct option lgopts[] = {
713                 {OPT_NO_HUGE, 0, 0, 0},
714                 {OPT_NO_PCI, 0, 0, 0},
715                 {OPT_NO_HPET, 0, 0, 0},
716                 {OPT_VMWARE_TSC_MAP, 0, 0, 0},
717                 {OPT_HUGE_DIR, 1, 0, 0},
718                 {OPT_NO_SHCONF, 0, 0, 0},
719                 {OPT_PROC_TYPE, 1, 0, 0},
720                 {OPT_FILE_PREFIX, 1, 0, 0},
721                 {OPT_SOCKET_MEM, 1, 0, 0},
722                 {OPT_PCI_WHITELIST, 1, 0, 0},
723                 {OPT_PCI_BLACKLIST, 1, 0, 0},
724                 {OPT_VDEV, 1, 0, 0},
725                 {OPT_SYSLOG, 1, NULL, 0},
726                 {OPT_LOG_LEVEL, 1, NULL, 0},
727                 {OPT_VFIO_INTR, 1, NULL, 0},
728                 {OPT_BASE_VIRTADDR, 1, 0, 0},
729                 {OPT_XEN_DOM0, 0, 0, 0},
730                 {OPT_CREATE_UIO_DEV, 1, NULL, 0},
731                 {0, 0, 0, 0}
732         };
733         struct shared_driver *solib;
734
735         argvopt = argv;
736
737         internal_config.memory = 0;
738         internal_config.force_nrank = 0;
739         internal_config.force_nchannel = 0;
740         internal_config.hugefile_prefix = HUGEFILE_PREFIX_DEFAULT;
741         internal_config.hugepage_dir = NULL;
742         internal_config.force_sockets = 0;
743         internal_config.syslog_facility = LOG_DAEMON;
744         /* default value from build option */
745         internal_config.log_level = RTE_LOG_LEVEL;
746         internal_config.xen_dom0_support = 0;
747         /* if set to NONE, interrupt mode is determined automatically */
748         internal_config.vfio_intr_mode = RTE_INTR_MODE_NONE;
749 #ifdef RTE_LIBEAL_USE_HPET
750         internal_config.no_hpet = 0;
751 #else
752         internal_config.no_hpet = 1;
753 #endif
754         /* zero out the NUMA config */
755         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
756                 internal_config.socket_mem[i] = 0;
757
758         /* zero out hugedir descriptors */
759         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
760                 internal_config.hugepage_info[i].lock_descriptor = -1;
761
762         internal_config.vmware_tsc_map = 0;
763         internal_config.base_virtaddr = 0;
764
765         while ((opt = getopt_long(argc, argvopt, "b:w:c:d:m:n:r:v",
766                                   lgopts, &option_index)) != EOF) {
767
768                 switch (opt) {
769                 /* blacklist */
770                 case 'b':
771                         if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
772                                         optarg) < 0) {
773                                 eal_usage(prgname);
774                                 return (-1);
775                         }
776                         break;
777                 /* whitelist */
778                 case 'w':
779                         if (rte_eal_devargs_add(RTE_DEVTYPE_WHITELISTED_PCI,
780                                         optarg) < 0) {
781                                 eal_usage(prgname);
782                                 return -1;
783                         }
784                         break;
785                 /* coremask */
786                 case 'c':
787                         if (eal_parse_coremask(optarg) < 0) {
788                                 RTE_LOG(ERR, EAL, "invalid coremask\n");
789                                 eal_usage(prgname);
790                                 return -1;
791                         }
792                         coremask_ok = 1;
793                         break;
794                 /* force loading of external driver */
795                 case 'd':
796                         solib = malloc(sizeof(*solib));
797                         if (solib == NULL) {
798                                 RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
799                                 return -1;
800                         }
801                         memset(solib, 0, sizeof(*solib));
802                         strncpy(solib->name, optarg, PATH_MAX-1);
803                         solib->name[PATH_MAX-1] = 0;
804                         TAILQ_INSERT_TAIL(&solib_list, solib, next);
805                         break;
806                 /* size of memory */
807                 case 'm':
808                         internal_config.memory = atoi(optarg);
809                         internal_config.memory *= 1024ULL;
810                         internal_config.memory *= 1024ULL;
811                         break;
812                 /* force number of channels */
813                 case 'n':
814                         internal_config.force_nchannel = atoi(optarg);
815                         if (internal_config.force_nchannel == 0 ||
816                             internal_config.force_nchannel > 4) {
817                                 RTE_LOG(ERR, EAL, "invalid channel number\n");
818                                 eal_usage(prgname);
819                                 return -1;
820                         }
821                         break;
822                 /* force number of ranks */
823                 case 'r':
824                         internal_config.force_nrank = atoi(optarg);
825                         if (internal_config.force_nrank == 0 ||
826                             internal_config.force_nrank > 16) {
827                                 RTE_LOG(ERR, EAL, "invalid rank number\n");
828                                 eal_usage(prgname);
829                                 return -1;
830                         }
831                         break;
832                 case 'v':
833                         /* since message is explicitly requested by user, we
834                          * write message at highest log level so it can always be seen
835                          * even if info or warning messages are disabled */
836                         RTE_LOG(CRIT, EAL, "RTE Version: '%s'\n", rte_version());
837                         break;
838
839                 /* long options */
840                 case 0:
841                         if (!strcmp(lgopts[option_index].name, OPT_NO_HUGE)) {
842                                 internal_config.no_hugetlbfs = 1;
843                         }
844                         if (!strcmp(lgopts[option_index].name, OPT_XEN_DOM0)) {
845                 #ifdef RTE_LIBRTE_XEN_DOM0
846                                 internal_config.xen_dom0_support = 1;
847                 #else
848                                 RTE_LOG(ERR, EAL, "Can't support DPDK app "
849                                         "running on Dom0, please configure"
850                                         " RTE_LIBRTE_XEN_DOM0=y\n");
851                                 return -1;
852                 #endif
853                         }
854                         else if (!strcmp(lgopts[option_index].name, OPT_NO_PCI)) {
855                                 internal_config.no_pci = 1;
856                         }
857                         else if (!strcmp(lgopts[option_index].name, OPT_NO_HPET)) {
858                                 internal_config.no_hpet = 1;
859                         }
860                         else if (!strcmp(lgopts[option_index].name, OPT_VMWARE_TSC_MAP)) {
861                                 internal_config.vmware_tsc_map = 1;
862                         }
863                         else if (!strcmp(lgopts[option_index].name, OPT_NO_SHCONF)) {
864                                 internal_config.no_shconf = 1;
865                         }
866                         else if (!strcmp(lgopts[option_index].name, OPT_HUGE_DIR)) {
867                                 internal_config.hugepage_dir = optarg;
868                         }
869                         else if (!strcmp(lgopts[option_index].name, OPT_PROC_TYPE)) {
870                                 internal_config.process_type = eal_parse_proc_type(optarg);
871                         }
872                         else if (!strcmp(lgopts[option_index].name, OPT_FILE_PREFIX)) {
873                                 internal_config.hugefile_prefix = optarg;
874                         }
875                         else if (!strcmp(lgopts[option_index].name, OPT_SOCKET_MEM)) {
876                                 if (eal_parse_socket_mem(optarg) < 0) {
877                                         RTE_LOG(ERR, EAL, "invalid parameters for --"
878                                                         OPT_SOCKET_MEM "\n");
879                                         eal_usage(prgname);
880                                         return -1;
881                                 }
882                         }
883                         else if (!strcmp(lgopts[option_index].name, OPT_USE_DEVICE)) {
884                                 printf("The --use-device option is deprecated, please use\n"
885                                         "--whitelist or --vdev instead.\n");
886                                 eal_usage(prgname);
887                                 return -1;
888                         }
889                         else if (!strcmp(lgopts[option_index].name, OPT_PCI_BLACKLIST)) {
890                                 if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
891                                                 optarg) < 0) {
892                                         eal_usage(prgname);
893                                         return -1;
894                                 }
895                         }
896                         else if (!strcmp(lgopts[option_index].name, OPT_PCI_WHITELIST)) {
897                                 if (rte_eal_devargs_add(RTE_DEVTYPE_WHITELISTED_PCI,
898                                                 optarg) < 0) {
899                                         eal_usage(prgname);
900                                         return -1;
901                                 }
902                         }
903                         else if (!strcmp(lgopts[option_index].name, OPT_VDEV)) {
904                                 if (rte_eal_devargs_add(RTE_DEVTYPE_VIRTUAL,
905                                                 optarg) < 0) {
906                                         eal_usage(prgname);
907                                         return -1;
908                                 }
909                         }
910                         else if (!strcmp(lgopts[option_index].name, OPT_SYSLOG)) {
911                                 if (eal_parse_syslog(optarg) < 0) {
912                                         RTE_LOG(ERR, EAL, "invalid parameters for --"
913                                                         OPT_SYSLOG "\n");
914                                         eal_usage(prgname);
915                                         return -1;
916                                 }
917                         } else if (!strcmp(lgopts[option_index].name,
918                                          OPT_LOG_LEVEL)) {
919                                 uint32_t log;
920
921                                 if (eal_parse_log_level(optarg, &log) < 0) {
922                                         RTE_LOG(ERR, EAL,
923                                                 "invalid parameters for --"
924                                                 OPT_LOG_LEVEL "\n");
925                                         eal_usage(prgname);
926                                         return -1;
927                                 }
928                                 internal_config.log_level = log;
929                         }
930                         else if (!strcmp(lgopts[option_index].name, OPT_BASE_VIRTADDR)) {
931                                 if (eal_parse_base_virtaddr(optarg) < 0) {
932                                         RTE_LOG(ERR, EAL, "invalid parameter for --"
933                                                         OPT_BASE_VIRTADDR "\n");
934                                         eal_usage(prgname);
935                                         return -1;
936                                 }
937                         }
938                         else if (!strcmp(lgopts[option_index].name, OPT_VFIO_INTR)) {
939                                 if (eal_parse_vfio_intr(optarg) < 0) {
940                                         RTE_LOG(ERR, EAL, "invalid parameters for --"
941                                                         OPT_VFIO_INTR "\n");
942                                         eal_usage(prgname);
943                                         return -1;
944                                 }
945                         }
946                         else if (!strcmp(lgopts[option_index].name, OPT_CREATE_UIO_DEV)) {
947                                 internal_config.create_uio_dev = 1;
948                         }
949                         break;
950
951                 default:
952                         eal_usage(prgname);
953                         return -1;
954                 }
955         }
956
957         /* sanity checks */
958         if (!coremask_ok) {
959                 RTE_LOG(ERR, EAL, "coremask not specified\n");
960                 eal_usage(prgname);
961                 return -1;
962         }
963         if (internal_config.process_type == RTE_PROC_AUTO){
964                 internal_config.process_type = eal_proc_type_detect();
965         }
966         if (internal_config.process_type == RTE_PROC_INVALID){
967                 RTE_LOG(ERR, EAL, "Invalid process type specified\n");
968                 eal_usage(prgname);
969                 return -1;
970         }
971         if (internal_config.process_type == RTE_PROC_PRIMARY &&
972                         internal_config.force_nchannel == 0) {
973                 RTE_LOG(ERR, EAL, "Number of memory channels (-n) not specified\n");
974                 eal_usage(prgname);
975                 return -1;
976         }
977         if (index(internal_config.hugefile_prefix,'%') != NULL){
978                 RTE_LOG(ERR, EAL, "Invalid char, '%%', in '"OPT_FILE_PREFIX"' option\n");
979                 eal_usage(prgname);
980                 return -1;
981         }
982         if (internal_config.memory > 0 && internal_config.force_sockets == 1) {
983                 RTE_LOG(ERR, EAL, "Options -m and --socket-mem cannot be specified "
984                                 "at the same time\n");
985                 eal_usage(prgname);
986                 return -1;
987         }
988         /* --no-huge doesn't make sense with either -m or --socket-mem */
989         if (internal_config.no_hugetlbfs &&
990                         (internal_config.memory > 0 ||
991                                         internal_config.force_sockets == 1)) {
992                 RTE_LOG(ERR, EAL, "Options -m or --socket-mem cannot be specified "
993                                 "together with --no-huge!\n");
994                 eal_usage(prgname);
995                 return -1;
996         }
997         /* --xen-dom0 doesn't make sense with --socket-mem */
998         if (internal_config.xen_dom0_support && internal_config.force_sockets == 1) {
999                 RTE_LOG(ERR, EAL, "Options --socket-mem cannot be specified "
1000                                         "together with --xen_dom0!\n");
1001                 eal_usage(prgname);
1002                 return -1;
1003         }
1004
1005         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) != 0 &&
1006                 rte_eal_devargs_type_count(RTE_DEVTYPE_BLACKLISTED_PCI) != 0) {
1007                 RTE_LOG(ERR, EAL, "Error: blacklist [-b] and whitelist "
1008                         "[-w] options cannot be used at the same time\n");
1009                 eal_usage(prgname);
1010                 return -1;
1011         }
1012
1013         if (optind >= 0)
1014                 argv[optind-1] = prgname;
1015
1016         /* if no memory amounts were requested, this will result in 0 and
1017          * will be overriden later, right after eal_hugepage_info_init() */
1018         for (i = 0; i < RTE_MAX_NUMA_NODES; i++)
1019                 internal_config.memory += internal_config.socket_mem[i];
1020
1021         ret = optind-1;
1022         optind = 0; /* reset getopt lib */
1023         return ret;
1024 }
1025
1026 static void
1027 eal_check_mem_on_local_socket(void)
1028 {
1029         const struct rte_memseg *ms;
1030         int i, socket_id;
1031
1032         socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
1033
1034         ms = rte_eal_get_physmem_layout();
1035
1036         for (i = 0; i < RTE_MAX_MEMSEG; i++)
1037                 if (ms[i].socket_id == socket_id &&
1038                                 ms[i].len > 0)
1039                         return;
1040
1041         RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
1042                         "memory on local socket!\n");
1043 }
1044
1045 static int
1046 sync_func(__attribute__((unused)) void *arg)
1047 {
1048         return 0;
1049 }
1050
1051 inline static void
1052 rte_eal_mcfg_complete(void)
1053 {
1054         /* ALL shared mem_config related INIT DONE */
1055         if (rte_config.process_type == RTE_PROC_PRIMARY)
1056                 rte_config.mem_config->magic = RTE_MAGIC;
1057 }
1058
1059 /*
1060  * Request iopl privilege for all RPL, returns 0 on success
1061  */
1062 static int
1063 rte_eal_iopl_init(void)
1064 {
1065         return iopl(HIGHEST_RPL);
1066 }
1067
1068 /* Launch threads, called at application init(). */
1069 int
1070 rte_eal_init(int argc, char **argv)
1071 {
1072         int i, fctret, ret;
1073         pthread_t thread_id;
1074         static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
1075         struct shared_driver *solib = NULL;
1076         const char *logid;
1077
1078         if (!rte_atomic32_test_and_set(&run_once))
1079                 return -1;
1080
1081         logid = strrchr(argv[0], '/');
1082         logid = strdup(logid ? logid + 1: argv[0]);
1083
1084         thread_id = pthread_self();
1085
1086         if (rte_eal_log_early_init() < 0)
1087                 rte_panic("Cannot init early logs\n");
1088
1089         if (rte_eal_cpu_init() < 0)
1090                 rte_panic("Cannot detect lcores\n");
1091
1092         fctret = eal_parse_args(argc, argv);
1093         if (fctret < 0)
1094                 exit(1);
1095
1096         /* set log level as early as possible */
1097         rte_set_log_level(internal_config.log_level);
1098
1099         if (internal_config.no_hugetlbfs == 0 &&
1100                         internal_config.process_type != RTE_PROC_SECONDARY &&
1101                         internal_config.xen_dom0_support == 0 &&
1102                         eal_hugepage_info_init() < 0)
1103                 rte_panic("Cannot get hugepage information\n");
1104
1105         if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
1106                 if (internal_config.no_hugetlbfs)
1107                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
1108                 else
1109                         internal_config.memory = eal_get_hugepage_mem_size();
1110         }
1111
1112         if (internal_config.vmware_tsc_map == 1) {
1113 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
1114                 rte_cycles_vmware_tsc_map = 1;
1115                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
1116                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
1117 #else
1118                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
1119                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
1120 #endif
1121         }
1122
1123         rte_srand(rte_rdtsc());
1124
1125         rte_config_init();
1126
1127         if (rte_eal_iopl_init() == 0)
1128                 rte_config.flags |= EAL_FLG_HIGH_IOPL;
1129
1130         if (rte_eal_pci_init() < 0)
1131                 rte_panic("Cannot init PCI\n");
1132
1133 #ifdef RTE_LIBRTE_IVSHMEM
1134         if (rte_eal_ivshmem_init() < 0)
1135                 rte_panic("Cannot init IVSHMEM\n");
1136 #endif
1137
1138         if (rte_eal_memory_init() < 0)
1139                 rte_panic("Cannot init memory\n");
1140
1141         /* the directories are locked during eal_hugepage_info_init */
1142         eal_hugedirs_unlock();
1143
1144         if (rte_eal_memzone_init() < 0)
1145                 rte_panic("Cannot init memzone\n");
1146
1147         if (rte_eal_tailqs_init() < 0)
1148                 rte_panic("Cannot init tail queues for objects\n");
1149
1150 #ifdef RTE_LIBRTE_IVSHMEM
1151         if (rte_eal_ivshmem_obj_init() < 0)
1152                 rte_panic("Cannot init IVSHMEM objects\n");
1153 #endif
1154
1155         if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0)
1156                 rte_panic("Cannot init logs\n");
1157
1158         if (rte_eal_alarm_init() < 0)
1159                 rte_panic("Cannot init interrupt-handling thread\n");
1160
1161         if (rte_eal_intr_init() < 0)
1162                 rte_panic("Cannot init interrupt-handling thread\n");
1163
1164         if (rte_eal_timer_init() < 0)
1165                 rte_panic("Cannot init HPET or TSC timers\n");
1166
1167         eal_check_mem_on_local_socket();
1168
1169         rte_eal_mcfg_complete();
1170
1171         TAILQ_FOREACH(solib, &solib_list, next) {
1172                 RTE_LOG(INFO, EAL, "open shared lib %s\n", solib->name);
1173                 solib->lib_handle = dlopen(solib->name, RTLD_NOW);
1174                 if (solib->lib_handle == NULL)
1175                         RTE_LOG(WARNING, EAL, "%s\n", dlerror());
1176         }
1177
1178         eal_thread_init_master(rte_config.master_lcore);
1179
1180         RTE_LOG(DEBUG, EAL, "Master core %u is ready (tid=%x)\n",
1181                 rte_config.master_lcore, (int)thread_id);
1182
1183         if (rte_eal_dev_init(PMD_INIT_PRE_PCI_PROBE) < 0)
1184                 rte_panic("Cannot init pmd devices\n");
1185
1186         RTE_LCORE_FOREACH_SLAVE(i) {
1187
1188                 /*
1189                  * create communication pipes between master thread
1190                  * and children
1191                  */
1192                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
1193                         rte_panic("Cannot create pipe\n");
1194                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
1195                         rte_panic("Cannot create pipe\n");
1196
1197                 lcore_config[i].state = WAIT;
1198
1199                 /* create a thread for each lcore */
1200                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
1201                                      eal_thread_loop, NULL);
1202                 if (ret != 0)
1203                         rte_panic("Cannot create thread\n");
1204         }
1205
1206         /*
1207          * Launch a dummy function on all slave lcores, so that master lcore
1208          * knows they are all ready when this function returns.
1209          */
1210         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
1211         rte_eal_mp_wait_lcore();
1212
1213         /* Probe & Initialize PCI devices */
1214         if (rte_eal_pci_probe())
1215                         rte_panic("Cannot probe PCI\n");
1216
1217         /* Initialize any outstanding devices */
1218         if (rte_eal_dev_init(PMD_INIT_POST_PCI_PROBE) < 0)
1219                 rte_panic("Cannot init pmd devices\n");
1220
1221         return fctret;
1222 }
1223
1224 /* get core role */
1225 enum rte_lcore_role_t
1226 rte_eal_lcore_role(unsigned lcore_id)
1227 {
1228         return (rte_config.lcore_role[lcore_id]);
1229 }
1230
1231 enum rte_proc_type_t
1232 rte_eal_process_type(void)
1233 {
1234         return (rte_config.process_type);
1235 }
1236
1237 int rte_eal_has_hugepages(void)
1238 {
1239         return ! internal_config.no_hugetlbfs;
1240 }