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