eal/linux: change log severity levels
[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 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
54 #include <sys/io.h>
55 #endif
56
57 #include <rte_common.h>
58 #include <rte_debug.h>
59 #include <rte_memory.h>
60 #include <rte_memzone.h>
61 #include <rte_launch.h>
62 #include <rte_eal.h>
63 #include <rte_eal_memconfig.h>
64 #include <rte_per_lcore.h>
65 #include <rte_lcore.h>
66 #include <rte_log.h>
67 #include <rte_random.h>
68 #include <rte_cycles.h>
69 #include <rte_string_fns.h>
70 #include <rte_cpuflags.h>
71 #include <rte_interrupts.h>
72 #include <rte_pci.h>
73 #include <rte_devargs.h>
74 #include <rte_common.h>
75 #include <rte_version.h>
76 #include <rte_atomic.h>
77 #include <malloc_heap.h>
78 #include <rte_eth_ring.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 #include "eal_options.h"
86
87 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
88
89 #define SOCKET_MEM_STRLEN (RTE_MAX_NUMA_NODES * 10)
90
91 /* Allow the application to print its usage message too if set */
92 static rte_usage_hook_t rte_application_usage_hook = NULL;
93
94 TAILQ_HEAD(shared_driver_list, shared_driver);
95
96 /* Definition for shared object drivers. */
97 struct shared_driver {
98         TAILQ_ENTRY(shared_driver) next;
99
100         char    name[PATH_MAX];
101         void*   lib_handle;
102 };
103
104 /* List of external loadable drivers */
105 static struct shared_driver_list solib_list =
106 TAILQ_HEAD_INITIALIZER(solib_list);
107
108 /* early configuration structure, when memory config is not mmapped */
109 static struct rte_mem_config early_mem_config;
110
111 /* define fd variable here, because file needs to be kept open for the
112  * duration of the program, as we hold a write lock on it in the primary proc */
113 static int mem_cfg_fd = -1;
114
115 static struct flock wr_lock = {
116                 .l_type = F_WRLCK,
117                 .l_whence = SEEK_SET,
118                 .l_start = offsetof(struct rte_mem_config, memseg),
119                 .l_len = sizeof(early_mem_config.memseg),
120 };
121
122 /* Address of global and public configuration */
123 static struct rte_config rte_config = {
124                 .mem_config = &early_mem_config,
125 };
126
127 /* internal configuration (per-core) */
128 struct lcore_config lcore_config[RTE_MAX_LCORE];
129
130 /* internal configuration */
131 struct internal_config internal_config;
132
133 /* used by rte_rdtsc() */
134 int rte_cycles_vmware_tsc_map;
135
136 /* Return a pointer to the configuration structure */
137 struct rte_config *
138 rte_eal_get_configuration(void)
139 {
140         return &rte_config;
141 }
142
143 /* parse a sysfs (or other) file containing one integer value */
144 int
145 eal_parse_sysfs_value(const char *filename, unsigned long *val)
146 {
147         FILE *f;
148         char buf[BUFSIZ];
149         char *end = NULL;
150
151         if ((f = fopen(filename, "r")) == NULL) {
152                 RTE_LOG(ERR, EAL, "%s(): cannot open sysfs value %s\n",
153                         __func__, filename);
154                 return -1;
155         }
156
157         if (fgets(buf, sizeof(buf), f) == NULL) {
158                 RTE_LOG(ERR, EAL, "%s(): cannot read sysfs value %s\n",
159                         __func__, filename);
160                 fclose(f);
161                 return -1;
162         }
163         *val = strtoul(buf, &end, 0);
164         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
165                 RTE_LOG(ERR, EAL, "%s(): cannot parse sysfs value %s\n",
166                                 __func__, filename);
167                 fclose(f);
168                 return -1;
169         }
170         fclose(f);
171         return 0;
172 }
173
174
175 /* create memory configuration in shared/mmap memory. Take out
176  * a write lock on the memsegs, so we can auto-detect primary/secondary.
177  * This means we never close the file while running (auto-close on exit).
178  * We also don't lock the whole file, so that in future we can use read-locks
179  * on other parts, e.g. memzones, to detect if there are running secondary
180  * processes. */
181 static void
182 rte_eal_config_create(void)
183 {
184         void *rte_mem_cfg_addr;
185         int retval;
186
187         const char *pathname = eal_runtime_config_path();
188
189         if (internal_config.no_shconf)
190                 return;
191
192         /* map the config before hugepage address so that we don't waste a page */
193         if (internal_config.base_virtaddr != 0)
194                 rte_mem_cfg_addr = (void *)
195                         RTE_ALIGN_FLOOR(internal_config.base_virtaddr -
196                         sizeof(struct rte_mem_config), sysconf(_SC_PAGE_SIZE));
197         else
198                 rte_mem_cfg_addr = NULL;
199
200         if (mem_cfg_fd < 0){
201                 mem_cfg_fd = open(pathname, O_RDWR | O_CREAT, 0660);
202                 if (mem_cfg_fd < 0)
203                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
204         }
205
206         retval = ftruncate(mem_cfg_fd, sizeof(*rte_config.mem_config));
207         if (retval < 0){
208                 close(mem_cfg_fd);
209                 rte_panic("Cannot resize '%s' for rte_mem_config\n", pathname);
210         }
211
212         retval = fcntl(mem_cfg_fd, F_SETLK, &wr_lock);
213         if (retval < 0){
214                 close(mem_cfg_fd);
215                 rte_exit(EXIT_FAILURE, "Cannot create lock on '%s'. Is another primary "
216                                 "process running?\n", pathname);
217         }
218
219         rte_mem_cfg_addr = mmap(rte_mem_cfg_addr, sizeof(*rte_config.mem_config),
220                                 PROT_READ | PROT_WRITE, MAP_SHARED, mem_cfg_fd, 0);
221
222         if (rte_mem_cfg_addr == MAP_FAILED){
223                 rte_panic("Cannot mmap memory for rte_config\n");
224         }
225         memcpy(rte_mem_cfg_addr, &early_mem_config, sizeof(early_mem_config));
226         rte_config.mem_config = (struct rte_mem_config *) rte_mem_cfg_addr;
227
228         /* store address of the config in the config itself so that secondary
229          * processes could later map the config into this exact location */
230         rte_config.mem_config->mem_cfg_addr = (uintptr_t) rte_mem_cfg_addr;
231
232 }
233
234 /* attach to an existing shared memory config */
235 static void
236 rte_eal_config_attach(void)
237 {
238         struct rte_mem_config *mem_config;
239
240         const char *pathname = eal_runtime_config_path();
241
242         if (internal_config.no_shconf)
243                 return;
244
245         if (mem_cfg_fd < 0){
246                 mem_cfg_fd = open(pathname, O_RDWR);
247                 if (mem_cfg_fd < 0)
248                         rte_panic("Cannot open '%s' for rte_mem_config\n", pathname);
249         }
250
251         /* map it as read-only first */
252         mem_config = (struct rte_mem_config *) mmap(NULL, sizeof(*mem_config),
253                         PROT_READ, MAP_SHARED, mem_cfg_fd, 0);
254         if (mem_config == MAP_FAILED)
255                 rte_panic("Cannot mmap memory for rte_config\n");
256
257         rte_config.mem_config = mem_config;
258 }
259
260 /* reattach the shared config at exact memory location primary process has it */
261 static void
262 rte_eal_config_reattach(void)
263 {
264         struct rte_mem_config *mem_config;
265         void *rte_mem_cfg_addr;
266
267         if (internal_config.no_shconf)
268                 return;
269
270         /* save the address primary process has mapped shared config to */
271         rte_mem_cfg_addr = (void *) (uintptr_t) rte_config.mem_config->mem_cfg_addr;
272
273         /* unmap original config */
274         munmap(rte_config.mem_config, sizeof(struct rte_mem_config));
275
276         /* remap the config at proper address */
277         mem_config = (struct rte_mem_config *) mmap(rte_mem_cfg_addr,
278                         sizeof(*mem_config), PROT_READ | PROT_WRITE, MAP_SHARED,
279                         mem_cfg_fd, 0);
280         close(mem_cfg_fd);
281         if (mem_config == MAP_FAILED || mem_config != rte_mem_cfg_addr)
282                 rte_panic("Cannot mmap memory for rte_config\n");
283
284         rte_config.mem_config = mem_config;
285 }
286
287 /* Detect if we are a primary or a secondary process */
288 enum rte_proc_type_t
289 eal_proc_type_detect(void)
290 {
291         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
292         const char *pathname = eal_runtime_config_path();
293
294         /* if we can open the file but not get a write-lock we are a secondary
295          * process. NOTE: if we get a file handle back, we keep that open
296          * and don't close it to prevent a race condition between multiple opens */
297         if (((mem_cfg_fd = open(pathname, O_RDWR)) >= 0) &&
298                         (fcntl(mem_cfg_fd, F_SETLK, &wr_lock) < 0))
299                 ptype = RTE_PROC_SECONDARY;
300
301         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
302                         ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
303
304         return ptype;
305 }
306
307 /* Sets up rte_config structure with the pointer to shared memory config.*/
308 static void
309 rte_config_init(void)
310 {
311         rte_config.process_type = internal_config.process_type;
312
313         switch (rte_config.process_type){
314         case RTE_PROC_PRIMARY:
315                 rte_eal_config_create();
316                 break;
317         case RTE_PROC_SECONDARY:
318                 rte_eal_config_attach();
319                 rte_eal_mcfg_wait_complete(rte_config.mem_config);
320                 rte_eal_config_reattach();
321                 break;
322         case RTE_PROC_AUTO:
323         case RTE_PROC_INVALID:
324                 rte_panic("Invalid process type\n");
325         }
326 }
327
328 /* Unlocks hugepage directories that were locked by eal_hugepage_info_init */
329 static void
330 eal_hugedirs_unlock(void)
331 {
332         int i;
333
334         for (i = 0; i < MAX_HUGEPAGE_SIZES; i++)
335         {
336                 /* skip uninitialized */
337                 if (internal_config.hugepage_info[i].lock_descriptor < 0)
338                         continue;
339                 /* unlock hugepage file */
340                 flock(internal_config.hugepage_info[i].lock_descriptor, LOCK_UN);
341                 close(internal_config.hugepage_info[i].lock_descriptor);
342                 /* reset the field */
343                 internal_config.hugepage_info[i].lock_descriptor = -1;
344         }
345 }
346
347 /* display usage */
348 static void
349 eal_usage(const char *prgname)
350 {
351         printf("\nUsage: %s ", prgname);
352         eal_common_usage();
353         printf("EAL Linux options:\n"
354                "  -d LIB.so           Add driver (can be used multiple times)\n"
355                "  --"OPT_SOCKET_MEM"        Memory to allocate on sockets (comma separated values)\n"
356                "  --"OPT_HUGE_DIR"          Directory where hugetlbfs is mounted\n"
357                "  --"OPT_FILE_PREFIX"       Prefix for hugepage filenames\n"
358                "  --"OPT_BASE_VIRTADDR"     Base virtual address\n"
359                "  --"OPT_CREATE_UIO_DEV"    Create /dev/uioX (usually done by hotplug)\n"
360                "  --"OPT_VFIO_INTR"         Interrupt mode for VFIO (legacy|msi|msix)\n"
361                "  --"OPT_XEN_DOM0"          Support running on Xen dom0 without hugetlbfs\n"
362                "\n");
363         /* Allow the application to print its usage message too if hook is set */
364         if ( rte_application_usage_hook ) {
365                 printf("===== Application Usage =====\n\n");
366                 rte_application_usage_hook(prgname);
367         }
368 }
369
370 /* Set a per-application usage message */
371 rte_usage_hook_t
372 rte_set_application_usage_hook( rte_usage_hook_t usage_func )
373 {
374         rte_usage_hook_t        old_func;
375
376         /* Will be NULL on the first call to denote the last usage routine. */
377         old_func                                        = rte_application_usage_hook;
378         rte_application_usage_hook      = usage_func;
379
380         return old_func;
381 }
382
383 static int
384 eal_parse_socket_mem(char *socket_mem)
385 {
386         char * arg[RTE_MAX_NUMA_NODES];
387         char *end;
388         int arg_num, i, len;
389         uint64_t total_mem = 0;
390
391         len = strnlen(socket_mem, SOCKET_MEM_STRLEN);
392         if (len == SOCKET_MEM_STRLEN) {
393                 RTE_LOG(ERR, EAL, "--socket-mem is too long\n");
394                 return -1;
395         }
396
397         /* all other error cases will be caught later */
398         if (!isdigit(socket_mem[len-1]))
399                 return -1;
400
401         /* split the optarg into separate socket values */
402         arg_num = rte_strsplit(socket_mem, len,
403                         arg, RTE_MAX_NUMA_NODES, ',');
404
405         /* if split failed, or 0 arguments */
406         if (arg_num <= 0)
407                 return -1;
408
409         internal_config.force_sockets = 1;
410
411         /* parse each defined socket option */
412         errno = 0;
413         for (i = 0; i < arg_num; i++) {
414                 end = NULL;
415                 internal_config.socket_mem[i] = strtoull(arg[i], &end, 10);
416
417                 /* check for invalid input */
418                 if ((errno != 0)  ||
419                                 (arg[i][0] == '\0') || (end == NULL) || (*end != '\0'))
420                         return -1;
421                 internal_config.socket_mem[i] *= 1024ULL;
422                 internal_config.socket_mem[i] *= 1024ULL;
423                 total_mem += internal_config.socket_mem[i];
424         }
425
426         /* check if we have a positive amount of total memory */
427         if (total_mem == 0)
428                 return -1;
429
430         return 0;
431 }
432
433 static int
434 eal_parse_base_virtaddr(const char *arg)
435 {
436         char *end;
437         uint64_t addr;
438
439         errno = 0;
440         addr = strtoull(arg, &end, 16);
441
442         /* check for errors */
443         if ((errno != 0) || (arg[0] == '\0') || end == NULL || (*end != '\0'))
444                 return -1;
445
446         /* make sure we don't exceed 32-bit boundary on 32-bit target */
447 #ifndef RTE_ARCH_64
448         if (addr >= UINTPTR_MAX)
449                 return -1;
450 #endif
451
452         /* align the addr on 16M boundary, 16MB is the minimum huge page
453          * size on IBM Power architecture. If the addr is aligned to 16MB,
454          * it can align to 2MB for x86. So this alignment can also be used
455          * on x86 */
456         internal_config.base_virtaddr =
457                 RTE_PTR_ALIGN_CEIL((uintptr_t)addr, (size_t)RTE_PGSIZE_16M);
458
459         return 0;
460 }
461
462 static int
463 eal_parse_vfio_intr(const char *mode)
464 {
465         unsigned i;
466         static struct {
467                 const char *name;
468                 enum rte_intr_mode value;
469         } map[] = {
470                 { "legacy", RTE_INTR_MODE_LEGACY },
471                 { "msi", RTE_INTR_MODE_MSI },
472                 { "msix", RTE_INTR_MODE_MSIX },
473         };
474
475         for (i = 0; i < RTE_DIM(map); i++) {
476                 if (!strcmp(mode, map[i].name)) {
477                         internal_config.vfio_intr_mode = map[i].value;
478                         return 0;
479                 }
480         }
481         return -1;
482 }
483
484 static inline size_t
485 eal_get_hugepage_mem_size(void)
486 {
487         uint64_t size = 0;
488         unsigned i, j;
489
490         for (i = 0; i < internal_config.num_hugepage_sizes; i++) {
491                 struct hugepage_info *hpi = &internal_config.hugepage_info[i];
492                 if (hpi->hugedir != NULL) {
493                         for (j = 0; j < RTE_MAX_NUMA_NODES; j++) {
494                                 size += hpi->hugepage_sz * hpi->num_pages[j];
495                         }
496                 }
497         }
498
499         return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX;
500 }
501
502 /* Parse the arguments for --log-level only */
503 static void
504 eal_log_level_parse(int argc, char **argv)
505 {
506         int opt;
507         char **argvopt;
508         int option_index;
509
510         argvopt = argv;
511
512         eal_reset_internal_config(&internal_config);
513
514         while ((opt = getopt_long(argc, argvopt, eal_short_options,
515                                   eal_long_options, &option_index)) != EOF) {
516
517                 int ret;
518
519                 /* getopt is not happy, stop right now */
520                 if (opt == '?')
521                         break;
522
523                 ret = (opt == OPT_LOG_LEVEL_NUM) ?
524                         eal_parse_common_option(opt, optarg, &internal_config) : 0;
525
526                 /* common parser is not happy */
527                 if (ret < 0)
528                         break;
529         }
530
531         optind = 0; /* reset getopt lib */
532 }
533
534 /* Parse the argument given in the command line of the application */
535 static int
536 eal_parse_args(int argc, char **argv)
537 {
538         int opt, ret;
539         char **argvopt;
540         int option_index;
541         char *prgname = argv[0];
542         struct shared_driver *solib;
543
544         argvopt = argv;
545
546         while ((opt = getopt_long(argc, argvopt, eal_short_options,
547                                   eal_long_options, &option_index)) != EOF) {
548
549                 int ret;
550
551                 /* getopt is not happy, stop right now */
552                 if (opt == '?') {
553                         eal_usage(prgname);
554                         return -1;
555                 }
556
557                 ret = eal_parse_common_option(opt, optarg, &internal_config);
558                 /* common parser is not happy */
559                 if (ret < 0) {
560                         eal_usage(prgname);
561                         return -1;
562                 }
563                 /* common parser handled this option */
564                 if (ret == 0)
565                         continue;
566
567                 switch (opt) {
568                 case 'h':
569                         eal_usage(prgname);
570                         exit(EXIT_SUCCESS);
571
572                 /* force loading of external driver */
573                 case 'd':
574                         solib = malloc(sizeof(*solib));
575                         if (solib == NULL) {
576                                 RTE_LOG(ERR, EAL, "malloc(solib) failed\n");
577                                 return -1;
578                         }
579                         memset(solib, 0, sizeof(*solib));
580                         strncpy(solib->name, optarg, PATH_MAX-1);
581                         solib->name[PATH_MAX-1] = 0;
582                         TAILQ_INSERT_TAIL(&solib_list, solib, next);
583                         break;
584
585                 /* long options */
586                 case OPT_XEN_DOM0_NUM:
587 #ifdef RTE_LIBRTE_XEN_DOM0
588                         internal_config.xen_dom0_support = 1;
589 #else
590                         RTE_LOG(ERR, EAL, "Can't support DPDK app "
591                                 "running on Dom0, please configure"
592                                 " RTE_LIBRTE_XEN_DOM0=y\n");
593                         return -1;
594 #endif
595                         break;
596
597                 case OPT_HUGE_DIR_NUM:
598                         internal_config.hugepage_dir = optarg;
599                         break;
600
601                 case OPT_FILE_PREFIX_NUM:
602                         internal_config.hugefile_prefix = optarg;
603                         break;
604
605                 case OPT_SOCKET_MEM_NUM:
606                         if (eal_parse_socket_mem(optarg) < 0) {
607                                 RTE_LOG(ERR, EAL, "invalid parameters for --"
608                                                 OPT_SOCKET_MEM "\n");
609                                 eal_usage(prgname);
610                                 return -1;
611                         }
612                         break;
613
614                 case OPT_BASE_VIRTADDR_NUM:
615                         if (eal_parse_base_virtaddr(optarg) < 0) {
616                                 RTE_LOG(ERR, EAL, "invalid parameter for --"
617                                                 OPT_BASE_VIRTADDR "\n");
618                                 eal_usage(prgname);
619                                 return -1;
620                         }
621                         break;
622
623                 case OPT_VFIO_INTR_NUM:
624                         if (eal_parse_vfio_intr(optarg) < 0) {
625                                 RTE_LOG(ERR, EAL, "invalid parameters for --"
626                                                 OPT_VFIO_INTR "\n");
627                                 eal_usage(prgname);
628                                 return -1;
629                         }
630                         break;
631
632                 case OPT_CREATE_UIO_DEV_NUM:
633                         internal_config.create_uio_dev = 1;
634                         break;
635
636                 default:
637                         if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
638                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
639                                         "on Linux\n", opt);
640                         } else if (opt >= OPT_LONG_MIN_NUM &&
641                                    opt < OPT_LONG_MAX_NUM) {
642                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
643                                         "on Linux\n",
644                                         eal_long_options[option_index].name);
645                         } else {
646                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
647                                         "on Linux\n", opt);
648                         }
649                         eal_usage(prgname);
650                         return -1;
651                 }
652         }
653
654         if (eal_adjust_config(&internal_config) != 0)
655                 return -1;
656
657         /* sanity checks */
658         if (eal_check_common_options(&internal_config) != 0) {
659                 eal_usage(prgname);
660                 return -1;
661         }
662
663         /* --xen-dom0 doesn't make sense with --socket-mem */
664         if (internal_config.xen_dom0_support && internal_config.force_sockets == 1) {
665                 RTE_LOG(ERR, EAL, "Options --"OPT_SOCKET_MEM" cannot be specified "
666                         "together with --"OPT_XEN_DOM0"\n");
667                 eal_usage(prgname);
668                 return -1;
669         }
670
671         if (optind >= 0)
672                 argv[optind-1] = prgname;
673         ret = optind-1;
674         optind = 0; /* reset getopt lib */
675         return ret;
676 }
677
678 static void
679 eal_check_mem_on_local_socket(void)
680 {
681         const struct rte_memseg *ms;
682         int i, socket_id;
683
684         socket_id = rte_lcore_to_socket_id(rte_config.master_lcore);
685
686         ms = rte_eal_get_physmem_layout();
687
688         for (i = 0; i < RTE_MAX_MEMSEG; i++)
689                 if (ms[i].socket_id == socket_id &&
690                                 ms[i].len > 0)
691                         return;
692
693         RTE_LOG(WARNING, EAL, "WARNING: Master core has no "
694                         "memory on local socket!\n");
695 }
696
697 static int
698 sync_func(__attribute__((unused)) void *arg)
699 {
700         return 0;
701 }
702
703 inline static void
704 rte_eal_mcfg_complete(void)
705 {
706         /* ALL shared mem_config related INIT DONE */
707         if (rte_config.process_type == RTE_PROC_PRIMARY)
708                 rte_config.mem_config->magic = RTE_MAGIC;
709 }
710
711 /*
712  * Request iopl privilege for all RPL, returns 0 on success
713  * iopl() call is mostly for the i386 architecture. For other architectures,
714  * return -1 to indicate IO privilege can't be changed in this way.
715  */
716 int
717 rte_eal_iopl_init(void)
718 {
719 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686)
720         if (iopl(3) != 0)
721                 return -1;
722         return 0;
723 #else
724         return -1;
725 #endif
726 }
727
728 /* Launch threads, called at application init(). */
729 int
730 rte_eal_init(int argc, char **argv)
731 {
732         int i, fctret, ret;
733         pthread_t thread_id;
734         static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
735         struct shared_driver *solib = NULL;
736         const char *logid;
737         char cpuset[RTE_CPU_AFFINITY_STR_LEN];
738
739         if (!rte_atomic32_test_and_set(&run_once))
740                 return -1;
741
742         logid = strrchr(argv[0], '/');
743         logid = strdup(logid ? logid + 1: argv[0]);
744
745         thread_id = pthread_self();
746
747         if (rte_eal_log_early_init() < 0)
748                 rte_panic("Cannot init early logs\n");
749
750         eal_log_level_parse(argc, argv);
751
752         /* set log level as early as possible */
753         rte_set_log_level(internal_config.log_level);
754
755         if (rte_eal_cpu_init() < 0)
756                 rte_panic("Cannot detect lcores\n");
757
758         fctret = eal_parse_args(argc, argv);
759         if (fctret < 0)
760                 exit(1);
761
762         if (internal_config.no_hugetlbfs == 0 &&
763                         internal_config.process_type != RTE_PROC_SECONDARY &&
764                         internal_config.xen_dom0_support == 0 &&
765                         eal_hugepage_info_init() < 0)
766                 rte_panic("Cannot get hugepage information\n");
767
768         if (internal_config.memory == 0 && internal_config.force_sockets == 0) {
769                 if (internal_config.no_hugetlbfs)
770                         internal_config.memory = MEMSIZE_IF_NO_HUGE_PAGE;
771                 else
772                         internal_config.memory = eal_get_hugepage_mem_size();
773         }
774
775         if (internal_config.vmware_tsc_map == 1) {
776 #ifdef RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT
777                 rte_cycles_vmware_tsc_map = 1;
778                 RTE_LOG (DEBUG, EAL, "Using VMWARE TSC MAP, "
779                                 "you must have monitor_control.pseudo_perfctr = TRUE\n");
780 #else
781                 RTE_LOG (WARNING, EAL, "Ignoring --vmware-tsc-map because "
782                                 "RTE_LIBRTE_EAL_VMWARE_TSC_MAP_SUPPORT is not set\n");
783 #endif
784         }
785
786         rte_srand(rte_rdtsc());
787
788         rte_config_init();
789
790         if (rte_eal_pci_init() < 0)
791                 rte_panic("Cannot init PCI\n");
792
793 #ifdef RTE_LIBRTE_IVSHMEM
794         if (rte_eal_ivshmem_init() < 0)
795                 rte_panic("Cannot init IVSHMEM\n");
796 #endif
797
798         if (rte_eal_memory_init() < 0)
799                 rte_panic("Cannot init memory\n");
800
801         /* the directories are locked during eal_hugepage_info_init */
802         eal_hugedirs_unlock();
803
804         if (rte_eal_memzone_init() < 0)
805                 rte_panic("Cannot init memzone\n");
806
807         if (rte_eal_tailqs_init() < 0)
808                 rte_panic("Cannot init tail queues for objects\n");
809
810 #ifdef RTE_LIBRTE_IVSHMEM
811         if (rte_eal_ivshmem_obj_init() < 0)
812                 rte_panic("Cannot init IVSHMEM objects\n");
813 #endif
814
815         if (rte_eal_log_init(logid, internal_config.syslog_facility) < 0)
816                 rte_panic("Cannot init logs\n");
817
818         if (rte_eal_alarm_init() < 0)
819                 rte_panic("Cannot init interrupt-handling thread\n");
820
821         if (rte_eal_intr_init() < 0)
822                 rte_panic("Cannot init interrupt-handling thread\n");
823
824         if (rte_eal_timer_init() < 0)
825                 rte_panic("Cannot init HPET or TSC timers\n");
826
827         eal_check_mem_on_local_socket();
828
829         rte_eal_mcfg_complete();
830
831         TAILQ_FOREACH(solib, &solib_list, next) {
832                 RTE_LOG(DEBUG, EAL, "open shared lib %s\n", solib->name);
833                 solib->lib_handle = dlopen(solib->name, RTLD_NOW);
834                 if (solib->lib_handle == NULL)
835                         RTE_LOG(WARNING, EAL, "%s\n", dlerror());
836         }
837
838         eal_thread_init_master(rte_config.master_lcore);
839
840         ret = eal_thread_dump_affinity(cpuset, RTE_CPU_AFFINITY_STR_LEN);
841
842         RTE_LOG(DEBUG, EAL, "Master lcore %u is ready (tid=%x;cpuset=[%s%s])\n",
843                 rte_config.master_lcore, (int)thread_id, cpuset,
844                 ret == 0 ? "" : "...");
845
846         if (rte_eal_dev_init() < 0)
847                 rte_panic("Cannot init pmd devices\n");
848
849         RTE_LCORE_FOREACH_SLAVE(i) {
850
851                 /*
852                  * create communication pipes between master thread
853                  * and children
854                  */
855                 if (pipe(lcore_config[i].pipe_master2slave) < 0)
856                         rte_panic("Cannot create pipe\n");
857                 if (pipe(lcore_config[i].pipe_slave2master) < 0)
858                         rte_panic("Cannot create pipe\n");
859
860                 lcore_config[i].state = WAIT;
861
862                 /* create a thread for each lcore */
863                 ret = pthread_create(&lcore_config[i].thread_id, NULL,
864                                      eal_thread_loop, NULL);
865                 if (ret != 0)
866                         rte_panic("Cannot create thread\n");
867         }
868
869         /*
870          * Launch a dummy function on all slave lcores, so that master lcore
871          * knows they are all ready when this function returns.
872          */
873         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
874         rte_eal_mp_wait_lcore();
875
876         /* Probe & Initialize PCI devices */
877         if (rte_eal_pci_probe())
878                 rte_panic("Cannot probe PCI\n");
879
880         return fctret;
881 }
882
883 /* get core role */
884 enum rte_lcore_role_t
885 rte_eal_lcore_role(unsigned lcore_id)
886 {
887         return rte_config.lcore_role[lcore_id];
888 }
889
890 enum rte_proc_type_t
891 rte_eal_process_type(void)
892 {
893         return rte_config.process_type;
894 }
895
896 int rte_eal_has_hugepages(void)
897 {
898         return ! internal_config.no_hugetlbfs;
899 }
900
901 int
902 rte_eal_check_module(const char *module_name)
903 {
904         char mod_name[30]; /* Any module names can be longer than 30 bytes? */
905         int ret = 0;
906         int n;
907
908         if (NULL == module_name)
909                 return -1;
910
911         FILE *fd = fopen("/proc/modules", "r");
912         if (NULL == fd) {
913                 RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
914                         " error %i (%s)\n", errno, strerror(errno));
915                 return -1;
916         }
917         while (!feof(fd)) {
918                 n = fscanf(fd, "%29s %*[^\n]", mod_name);
919                 if ((n == 1) && !strcmp(mod_name, module_name)) {
920                         ret = 1;
921                         break;
922                 }
923         }
924         fclose(fd);
925
926         return ret;
927 }